Yoast\WP\SEO\Generators

Breadcrumbs_Generator::generate()publicYoast 1.0

Generates the breadcrumbs.

Метод класса: Breadcrumbs_Generator{}

Возвращает

Массив<Массив>. An array of associative arrays that each have a 'text' and a 'url'.

Использование

$Breadcrumbs_Generator = new Breadcrumbs_Generator();
$Breadcrumbs_Generator->generate( $context );
$context(Meta_Tags_Context) (обязательный)
The meta tags context.

Код Breadcrumbs_Generator::generate() Yoast 22.3

public function generate( Meta_Tags_Context $context ) {
	$static_ancestors = [];
	$breadcrumbs_home = $this->options->get( 'breadcrumbs-home' );
	if ( $breadcrumbs_home !== '' && ! \in_array( $this->current_page_helper->get_page_type(), [ 'Home_Page', 'Static_Home_Page' ], true ) ) {
		$front_page_id = $this->current_page_helper->get_front_page_id();
		if ( $front_page_id === 0 ) {
			$home_page_ancestor = $this->repository->find_for_home_page();
			if ( \is_a( $home_page_ancestor, Indexable::class ) ) {
				$static_ancestors[] = $home_page_ancestor;
			}
		}
		else {
			$static_ancestor = $this->repository->find_by_id_and_type( $front_page_id, 'post' );
			if ( \is_a( $static_ancestor, Indexable::class ) && $static_ancestor->post_status !== 'unindexed' ) {
				$static_ancestors[] = $static_ancestor;
			}
		}
	}
	$page_for_posts = \get_option( 'page_for_posts' );
	if ( $this->should_have_blog_crumb( $page_for_posts, $context ) ) {
		$static_ancestor = $this->repository->find_by_id_and_type( $page_for_posts, 'post' );
		if ( \is_a( $static_ancestor, Indexable::class ) && $static_ancestor->post_status !== 'unindexed' ) {
			$static_ancestors[] = $static_ancestor;
		}
	}
	if (
		$context->indexable->object_type === 'post'
		&& $context->indexable->object_sub_type !== 'post'
		&& $context->indexable->object_sub_type !== 'page'
		&& $this->post_type_helper->has_archive( $context->indexable->object_sub_type )
	) {
		$static_ancestor = $this->repository->find_for_post_type_archive( $context->indexable->object_sub_type );
		if ( \is_a( $static_ancestor, Indexable::class ) ) {
			$static_ancestors[] = $static_ancestor;
		}
	}
	if ( $context->indexable->object_type === 'term' ) {
		$parent = $this->get_taxonomy_post_type_parent( $context->indexable->object_sub_type );
		if ( $parent && $parent !== 'post' && $this->post_type_helper->has_archive( $parent ) ) {
			$static_ancestor = $this->repository->find_for_post_type_archive( $parent );
			if ( \is_a( $static_ancestor, Indexable::class ) ) {
				$static_ancestors[] = $static_ancestor;
			}
		}
	}
	$indexables = [];
	if ( ! \in_array( $this->current_page_helper->get_page_type(), [ 'Home_Page', 'Static_Home_Page' ], true ) ) {
		// Get all ancestors of the indexable and append itself to get all indexables in the full crumb.
		$indexables = $this->repository->get_ancestors( $context->indexable );
	}
	$indexables[] = $context->indexable;

	if ( ! empty( $static_ancestors ) ) {
		\array_unshift( $indexables, ...$static_ancestors );
	}

	$indexables = \apply_filters( 'wpseo_breadcrumb_indexables', $indexables, $context );
	$indexables = \is_array( $indexables ) ? $indexables : [];
	$indexables = \array_filter(
		$indexables,
		static function ( $indexable ) {
			return \is_a( $indexable, Indexable::class );
		}
	);

	$crumbs = \array_map( [ $this,'get_post_type_crumb' ], $indexables );

	if ( $breadcrumbs_home !== '' ) {
		$crumbs[0]['text'] = $breadcrumbs_home;
	}

	$crumbs = $this->add_paged_crumb( $crumbs, $context->indexable );

	/**
	 * Filter: 'wpseo_breadcrumb_links' - Allow the developer to filter the Yoast SEO breadcrumb links, add to them, change order, etc.
	 *
	 * @param array $crumbs The crumbs array.
	 */
	$filtered_crumbs = \apply_filters( 'wpseo_breadcrumb_links', $crumbs );

	// Basic check to make sure the filtered crumbs are in an array.
	if ( ! \is_array( $filtered_crumbs ) ) {
		\_doing_it_wrong(
			'Filter: \'wpseo_breadcrumb_links\'',
			'The `wpseo_breadcrumb_links` filter should return a multi-dimensional array.',
			'YoastSEO v20.0'
		);
	}
	else {
		$crumbs = $filtered_crumbs;
	}

	$filter_callback = static function ( $link_info, $index ) use ( $crumbs ) {
		/**
		 * Filter: 'wpseo_breadcrumb_single_link_info' - Allow developers to filter the Yoast SEO Breadcrumb link information.
		 *
		 * @param array $link_info The breadcrumb link information.
		 * @param int   $index     The index of the breadcrumb in the list.
		 * @param array $crumbs    The complete list of breadcrumbs.
		 */
		return \apply_filters( 'wpseo_breadcrumb_single_link_info', $link_info, $index, $crumbs );
	};
	return \array_map( $filter_callback, $crumbs, \array_keys( $crumbs ) );
}