Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../src/generato...
File: breadcrumbs-generator.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Generators;
[2] Fix | Delete
[3] Fix | Delete
use Yoast\WP\SEO\Context\Meta_Tags_Context;
[4] Fix | Delete
use Yoast\WP\SEO\Helpers\Current_Page_Helper;
[5] Fix | Delete
use Yoast\WP\SEO\Helpers\Options_Helper;
[6] Fix | Delete
use Yoast\WP\SEO\Helpers\Pagination_Helper;
[7] Fix | Delete
use Yoast\WP\SEO\Helpers\Post_Type_Helper;
[8] Fix | Delete
use Yoast\WP\SEO\Helpers\Url_Helper;
[9] Fix | Delete
use Yoast\WP\SEO\Models\Indexable;
[10] Fix | Delete
use Yoast\WP\SEO\Repositories\Indexable_Repository;
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Represents the generator class for the breadcrumbs.
[14] Fix | Delete
*/
[15] Fix | Delete
class Breadcrumbs_Generator implements Generator_Interface {
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* The indexable repository.
[19] Fix | Delete
*
[20] Fix | Delete
* @var Indexable_Repository
[21] Fix | Delete
*/
[22] Fix | Delete
private $repository;
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* The options helper.
[26] Fix | Delete
*
[27] Fix | Delete
* @var Options_Helper
[28] Fix | Delete
*/
[29] Fix | Delete
private $options;
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* The current page helper.
[33] Fix | Delete
*
[34] Fix | Delete
* @var Current_Page_Helper
[35] Fix | Delete
*/
[36] Fix | Delete
private $current_page_helper;
[37] Fix | Delete
[38] Fix | Delete
/**
[39] Fix | Delete
* The post type helper.
[40] Fix | Delete
*
[41] Fix | Delete
* @var Post_Type_Helper
[42] Fix | Delete
*/
[43] Fix | Delete
private $post_type_helper;
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* The URL helper.
[47] Fix | Delete
*
[48] Fix | Delete
* @var Url_Helper
[49] Fix | Delete
*/
[50] Fix | Delete
private $url_helper;
[51] Fix | Delete
[52] Fix | Delete
/**
[53] Fix | Delete
* The pagination helper.
[54] Fix | Delete
*
[55] Fix | Delete
* @var Pagination_Helper
[56] Fix | Delete
*/
[57] Fix | Delete
private $pagination_helper;
[58] Fix | Delete
[59] Fix | Delete
/**
[60] Fix | Delete
* Breadcrumbs_Generator constructor.
[61] Fix | Delete
*
[62] Fix | Delete
* @param Indexable_Repository $repository The repository.
[63] Fix | Delete
* @param Options_Helper $options The options helper.
[64] Fix | Delete
* @param Current_Page_Helper $current_page_helper The current page helper.
[65] Fix | Delete
* @param Post_Type_Helper $post_type_helper The post type helper.
[66] Fix | Delete
* @param Url_Helper $url_helper The URL helper.
[67] Fix | Delete
* @param Pagination_Helper $pagination_helper The pagination helper.
[68] Fix | Delete
*/
[69] Fix | Delete
public function __construct(
[70] Fix | Delete
Indexable_Repository $repository,
[71] Fix | Delete
Options_Helper $options,
[72] Fix | Delete
Current_Page_Helper $current_page_helper,
[73] Fix | Delete
Post_Type_Helper $post_type_helper,
[74] Fix | Delete
Url_Helper $url_helper,
[75] Fix | Delete
Pagination_Helper $pagination_helper
[76] Fix | Delete
) {
[77] Fix | Delete
$this->repository = $repository;
[78] Fix | Delete
$this->options = $options;
[79] Fix | Delete
$this->current_page_helper = $current_page_helper;
[80] Fix | Delete
$this->post_type_helper = $post_type_helper;
[81] Fix | Delete
$this->url_helper = $url_helper;
[82] Fix | Delete
$this->pagination_helper = $pagination_helper;
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
/**
[86] Fix | Delete
* Generates the breadcrumbs.
[87] Fix | Delete
*
[88] Fix | Delete
* @param Meta_Tags_Context $context The meta tags context.
[89] Fix | Delete
*
[90] Fix | Delete
* @return array An array of associative arrays that each have a 'text' and a 'url'.
[91] Fix | Delete
*/
[92] Fix | Delete
public function generate( Meta_Tags_Context $context ) {
[93] Fix | Delete
$static_ancestors = [];
[94] Fix | Delete
$breadcrumbs_home = $this->options->get( 'breadcrumbs-home' );
[95] Fix | Delete
if ( $breadcrumbs_home !== '' && ! \in_array( $this->current_page_helper->get_page_type(), [ 'Home_Page', 'Static_Home_Page' ], true ) ) {
[96] Fix | Delete
$front_page_id = $this->current_page_helper->get_front_page_id();
[97] Fix | Delete
if ( $front_page_id === 0 ) {
[98] Fix | Delete
$static_ancestors[] = $this->repository->find_for_home_page();
[99] Fix | Delete
}
[100] Fix | Delete
else {
[101] Fix | Delete
$static_ancestor = $this->repository->find_by_id_and_type( $front_page_id, 'post' );
[102] Fix | Delete
if ( $static_ancestor->post_status !== 'unindexed' ) {
[103] Fix | Delete
$static_ancestors[] = $static_ancestor;
[104] Fix | Delete
}
[105] Fix | Delete
}
[106] Fix | Delete
}
[107] Fix | Delete
$page_for_posts = \get_option( 'page_for_posts' );
[108] Fix | Delete
if ( $this->should_have_blog_crumb( $page_for_posts, $context ) ) {
[109] Fix | Delete
$static_ancestor = $this->repository->find_by_id_and_type( $page_for_posts, 'post' );
[110] Fix | Delete
if ( $static_ancestor->post_status !== 'unindexed' ) {
[111] Fix | Delete
$static_ancestors[] = $static_ancestor;
[112] Fix | Delete
}
[113] Fix | Delete
}
[114] Fix | Delete
if (
[115] Fix | Delete
$context->indexable->object_type === 'post'
[116] Fix | Delete
&& $context->indexable->object_sub_type !== 'post'
[117] Fix | Delete
&& $context->indexable->object_sub_type !== 'page'
[118] Fix | Delete
&& $this->post_type_helper->has_archive( $context->indexable->object_sub_type )
[119] Fix | Delete
) {
[120] Fix | Delete
$static_ancestors[] = $this->repository->find_for_post_type_archive( $context->indexable->object_sub_type );
[121] Fix | Delete
}
[122] Fix | Delete
if ( $context->indexable->object_type === 'term' ) {
[123] Fix | Delete
$parent = $this->get_taxonomy_post_type_parent( $context->indexable->object_sub_type );
[124] Fix | Delete
if ( $parent && $parent !== 'post' && $this->post_type_helper->has_archive( $parent ) ) {
[125] Fix | Delete
$static_ancestors[] = $this->repository->find_for_post_type_archive( $parent );
[126] Fix | Delete
}
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
// Get all ancestors of the indexable and append itself to get all indexables in the full crumb.
[130] Fix | Delete
$indexables = $this->repository->get_ancestors( $context->indexable );
[131] Fix | Delete
$indexables[] = $context->indexable;
[132] Fix | Delete
[133] Fix | Delete
if ( ! empty( $static_ancestors ) ) {
[134] Fix | Delete
\array_unshift( $indexables, ...$static_ancestors );
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
$indexables = \apply_filters( 'wpseo_breadcrumb_indexables', $indexables, $context );
[138] Fix | Delete
[139] Fix | Delete
$callback = function ( Indexable $ancestor ) {
[140] Fix | Delete
$crumb = [
[141] Fix | Delete
'url' => $ancestor->permalink,
[142] Fix | Delete
'text' => $ancestor->breadcrumb_title,
[143] Fix | Delete
];
[144] Fix | Delete
switch ( $ancestor->object_type ) {
[145] Fix | Delete
case 'post':
[146] Fix | Delete
$crumb = $this->get_post_crumb( $crumb, $ancestor );
[147] Fix | Delete
break;
[148] Fix | Delete
case 'post-type-archive':
[149] Fix | Delete
$crumb = $this->get_post_type_archive_crumb( $crumb, $ancestor );
[150] Fix | Delete
break;
[151] Fix | Delete
case 'term':
[152] Fix | Delete
$crumb = $this->get_term_crumb( $crumb, $ancestor );
[153] Fix | Delete
break;
[154] Fix | Delete
case 'system-page':
[155] Fix | Delete
$crumb = $this->get_system_page_crumb( $crumb, $ancestor );
[156] Fix | Delete
break;
[157] Fix | Delete
case 'user':
[158] Fix | Delete
$crumb = $this->get_user_crumb( $crumb, $ancestor );
[159] Fix | Delete
break;
[160] Fix | Delete
case 'date-archive':
[161] Fix | Delete
$crumb = $this->get_date_archive_crumb( $crumb );
[162] Fix | Delete
break;
[163] Fix | Delete
}
[164] Fix | Delete
return $crumb;
[165] Fix | Delete
};
[166] Fix | Delete
$crumbs = \array_map( $callback, $indexables );
[167] Fix | Delete
[168] Fix | Delete
if ( $breadcrumbs_home !== '' ) {
[169] Fix | Delete
$crumbs[0]['text'] = $breadcrumbs_home;
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
$crumbs = $this->add_paged_crumb( $crumbs, $context->indexable );
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* Filter: 'wpseo_breadcrumb_links' - Allow the developer to filter the Yoast SEO breadcrumb links, add to them, change order, etc.
[176] Fix | Delete
*
[177] Fix | Delete
* @api array $crumbs The crumbs array.
[178] Fix | Delete
*/
[179] Fix | Delete
$crumbs = \apply_filters( 'wpseo_breadcrumb_links', $crumbs );
[180] Fix | Delete
[181] Fix | Delete
$filter_callback = static function( $link_info, $index ) use ( $crumbs ) {
[182] Fix | Delete
/**
[183] Fix | Delete
* Filter: 'wpseo_breadcrumb_single_link_info' - Allow developers to filter the Yoast SEO Breadcrumb link information.
[184] Fix | Delete
*
[185] Fix | Delete
* @api array $link_info The breadcrumb link information.
[186] Fix | Delete
*
[187] Fix | Delete
* @param int $index The index of the breadcrumb in the list.
[188] Fix | Delete
* @param array $crumbs The complete list of breadcrumbs.
[189] Fix | Delete
*/
[190] Fix | Delete
return \apply_filters( 'wpseo_breadcrumb_single_link_info', $link_info, $index, $crumbs );
[191] Fix | Delete
};
[192] Fix | Delete
return \array_map( $filter_callback, $crumbs, \array_keys( $crumbs ) );
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
/**
[196] Fix | Delete
* Returns the modified post crumb.
[197] Fix | Delete
*
[198] Fix | Delete
* @param array $crumb The crumb.
[199] Fix | Delete
* @param Indexable $ancestor The indexable.
[200] Fix | Delete
*
[201] Fix | Delete
* @return array The crumb.
[202] Fix | Delete
*/
[203] Fix | Delete
private function get_post_crumb( $crumb, $ancestor ) {
[204] Fix | Delete
$crumb['id'] = $ancestor->object_id;
[205] Fix | Delete
[206] Fix | Delete
return $crumb;
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
/**
[210] Fix | Delete
* Returns the modified post type crumb.
[211] Fix | Delete
*
[212] Fix | Delete
* @param array $crumb The crumb.
[213] Fix | Delete
* @param Indexable $ancestor The indexable.
[214] Fix | Delete
*
[215] Fix | Delete
* @return array The crumb.
[216] Fix | Delete
*/
[217] Fix | Delete
private function get_post_type_archive_crumb( $crumb, $ancestor ) {
[218] Fix | Delete
$crumb['ptarchive'] = $ancestor->object_sub_type;
[219] Fix | Delete
[220] Fix | Delete
return $crumb;
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
/**
[224] Fix | Delete
* Returns the modified term crumb.
[225] Fix | Delete
*
[226] Fix | Delete
* @param array $crumb The crumb.
[227] Fix | Delete
* @param Indexable $ancestor The indexable.
[228] Fix | Delete
*
[229] Fix | Delete
* @return array The crumb.
[230] Fix | Delete
*/
[231] Fix | Delete
private function get_term_crumb( $crumb, $ancestor ) {
[232] Fix | Delete
$crumb['term_id'] = $ancestor->object_id;
[233] Fix | Delete
[234] Fix | Delete
return $crumb;
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
/**
[238] Fix | Delete
* Returns the modified system page crumb.
[239] Fix | Delete
*
[240] Fix | Delete
* @param array $crumb The crumb.
[241] Fix | Delete
* @param Indexable $ancestor The indexable.
[242] Fix | Delete
*
[243] Fix | Delete
* @return array The crumb.
[244] Fix | Delete
*/
[245] Fix | Delete
private function get_system_page_crumb( $crumb, $ancestor ) {
[246] Fix | Delete
if ( $ancestor->object_sub_type === 'search-result' ) {
[247] Fix | Delete
$crumb['text'] = $this->options->get( 'breadcrumbs-searchprefix' ) . ' ' . \esc_html( \get_search_query() );
[248] Fix | Delete
$crumb['url'] = \get_search_link();
[249] Fix | Delete
}
[250] Fix | Delete
elseif ( $ancestor->object_sub_type === '404' ) {
[251] Fix | Delete
$crumb['text'] = $this->options->get( 'breadcrumbs-404crumb' );
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
return $crumb;
[255] Fix | Delete
}
[256] Fix | Delete
[257] Fix | Delete
/**
[258] Fix | Delete
* Returns the modified user crumb.
[259] Fix | Delete
*
[260] Fix | Delete
* @param array $crumb The crumb.
[261] Fix | Delete
* @param Indexable $ancestor The indexable.
[262] Fix | Delete
*
[263] Fix | Delete
* @return array The crumb.
[264] Fix | Delete
*/
[265] Fix | Delete
private function get_user_crumb( $crumb, $ancestor ) {
[266] Fix | Delete
$display_name = \get_the_author_meta( 'display_name', $ancestor->object_id );
[267] Fix | Delete
$crumb['text'] = $this->options->get( 'breadcrumbs-archiveprefix' ) . ' ' . $display_name;
[268] Fix | Delete
[269] Fix | Delete
return $crumb;
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
/**
[273] Fix | Delete
* Returns the modified date archive crumb.
[274] Fix | Delete
*
[275] Fix | Delete
* @param array $crumb The crumb.
[276] Fix | Delete
*
[277] Fix | Delete
* @return array The crumb.
[278] Fix | Delete
*/
[279] Fix | Delete
protected function get_date_archive_crumb( $crumb ) {
[280] Fix | Delete
$home_url = $this->url_helper->home();
[281] Fix | Delete
$prefix = $this->options->get( 'breadcrumbs-archiveprefix' );
[282] Fix | Delete
[283] Fix | Delete
if ( \is_day() ) {
[284] Fix | Delete
$day = \esc_html( \get_the_date() );
[285] Fix | Delete
$crumb['url'] = $home_url . \get_the_date( 'Y/m/d' ) . '/';
[286] Fix | Delete
$crumb['text'] = $prefix . ' ' . $day;
[287] Fix | Delete
}
[288] Fix | Delete
elseif ( \is_month() ) {
[289] Fix | Delete
$month = \esc_html( \trim( \single_month_title( ' ', false ) ) );
[290] Fix | Delete
$crumb['url'] = $home_url . \get_the_date( 'Y/m' ) . '/';
[291] Fix | Delete
$crumb['text'] = $prefix . ' ' . $month;
[292] Fix | Delete
}
[293] Fix | Delete
elseif ( \is_year() ) {
[294] Fix | Delete
$year = \get_the_date( 'Y' );
[295] Fix | Delete
$crumb['url'] = $home_url . $year . '/';
[296] Fix | Delete
$crumb['text'] = $prefix . ' ' . $year;
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
return $crumb;
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
/**
[303] Fix | Delete
* Returns whether or not a blog crumb should be added.
[304] Fix | Delete
*
[305] Fix | Delete
* @param int $page_for_posts The page for posts ID.
[306] Fix | Delete
* @param Meta_Tags_Context $context The meta tags context.
[307] Fix | Delete
*
[308] Fix | Delete
* @return bool Whether or not a blog crumb should be added.
[309] Fix | Delete
*/
[310] Fix | Delete
protected function should_have_blog_crumb( $page_for_posts, $context ) {
[311] Fix | Delete
// When there is no page configured as blog page.
[312] Fix | Delete
if ( \get_option( 'show_on_front' ) !== 'page' || ! $page_for_posts ) {
[313] Fix | Delete
return false;
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
if ( $context->indexable->object_type === 'term' ) {
[317] Fix | Delete
$parent = $this->get_taxonomy_post_type_parent( $context->indexable->object_sub_type );
[318] Fix | Delete
return $parent === 'post';
[319] Fix | Delete
}
[320] Fix | Delete
[321] Fix | Delete
if ( $this->options->get( 'breadcrumbs-display-blog-page' ) !== true ) {
[322] Fix | Delete
return false;
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
// When the current page is the home page, searchpage or isn't a singular post.
[326] Fix | Delete
if ( \is_home() || \is_search() || ! \is_singular( 'post' ) ) {
[327] Fix | Delete
return false;
[328] Fix | Delete
}
[329] Fix | Delete
[330] Fix | Delete
return true;
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
/**
[334] Fix | Delete
* Returns the post type parent of a given taxonomy.
[335] Fix | Delete
*
[336] Fix | Delete
* @param string $taxonomy The taxonomy.
[337] Fix | Delete
*
[338] Fix | Delete
* @return string|false The parent if it exists, false otherwise.
[339] Fix | Delete
*/
[340] Fix | Delete
protected function get_taxonomy_post_type_parent( $taxonomy ) {
[341] Fix | Delete
$parent = $this->options->get( 'taxonomy-' . $taxonomy . '-ptparent' );
[342] Fix | Delete
[343] Fix | Delete
if ( empty( $parent ) || (string) $parent === '0' ) {
[344] Fix | Delete
return false;
[345] Fix | Delete
}
[346] Fix | Delete
[347] Fix | Delete
return $parent;
[348] Fix | Delete
}
[349] Fix | Delete
[350] Fix | Delete
/**
[351] Fix | Delete
* Adds a crumb for the current page, if we're on an archive page or paginated post.
[352] Fix | Delete
*
[353] Fix | Delete
* @param array $crumbs The array of breadcrumbs.
[354] Fix | Delete
* @param Indexable $current_indexable The current indexable.
[355] Fix | Delete
*
[356] Fix | Delete
* @return array The breadcrumbs.
[357] Fix | Delete
*/
[358] Fix | Delete
protected function add_paged_crumb( array $crumbs, $current_indexable ) {
[359] Fix | Delete
$is_simple_page = $this->current_page_helper->is_simple_page();
[360] Fix | Delete
[361] Fix | Delete
// If we're not on a paged page do nothing.
[362] Fix | Delete
if ( ! $is_simple_page && ! $this->current_page_helper->is_paged() ) {
[363] Fix | Delete
return $crumbs;
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
// If we're not on a paginated post do nothing.
[367] Fix | Delete
if ( $is_simple_page && $current_indexable->number_of_pages === null ) {
[368] Fix | Delete
return $crumbs;
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
$current_page_number = $this->pagination_helper->get_current_page_number();
[372] Fix | Delete
if ( $current_page_number <= 1 ) {
[373] Fix | Delete
return $crumbs;
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
$crumbs[] = [
[377] Fix | Delete
'text' => \sprintf(
[378] Fix | Delete
/* translators: %s expands to the current page number */
[379] Fix | Delete
\__( 'Page %s', 'wordpress-seo' ),
[380] Fix | Delete
$current_page_number
[381] Fix | Delete
),
[382] Fix | Delete
];
[383] Fix | Delete
[384] Fix | Delete
return $crumbs;
[385] Fix | Delete
}
[386] Fix | Delete
}
[387] Fix | Delete
[388] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function