class-breadcrumbs.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Frontend
  6. */
  7. /**
  8. * This class handles the Breadcrumbs generation and display.
  9. */
  10. class WPSEO_Breadcrumbs {
  11. /**
  12. * Instance of this class.
  13. *
  14. * @var object
  15. */
  16. public static $instance;
  17. /**
  18. * Last used 'before' string.
  19. *
  20. * @var string
  21. */
  22. public static $before = '';
  23. /**
  24. * Last used 'after' string.
  25. *
  26. * @var string
  27. */
  28. public static $after = '';
  29. /**
  30. * The date helper.
  31. *
  32. * @var WPSEO_Date_Helper
  33. */
  34. protected $date;
  35. /**
  36. * Blog's show on front setting, 'page' or 'posts'.
  37. *
  38. * @var string
  39. */
  40. private $show_on_front;
  41. /**
  42. * Blog's page for posts setting, page id or false.
  43. *
  44. * @var mixed
  45. */
  46. private $page_for_posts;
  47. /**
  48. * Current post object.
  49. *
  50. * @var mixed
  51. */
  52. private $post;
  53. /**
  54. * HTML wrapper element for a single breadcrumb element.
  55. *
  56. * @var string
  57. */
  58. private $element = 'span';
  59. /**
  60. * Yoast SEO breadcrumb separator.
  61. *
  62. * @var string
  63. */
  64. private $separator = '';
  65. /**
  66. * HTML wrapper element for the Yoast SEO breadcrumbs output.
  67. *
  68. * @var string
  69. */
  70. private $wrapper = 'span';
  71. /**
  72. * Array of crumbs.
  73. *
  74. * Each element of the crumbs array can either have one of these keys:
  75. * "id" for post types;
  76. * "ptarchive" for a post type archive;
  77. * "term" for a taxonomy term.
  78. * OR it consists of a predefined set of 'text', 'url' and 'allow_html'.
  79. *
  80. * @var array
  81. */
  82. private $crumbs = [];
  83. /**
  84. * Count of the elements in the $crumbs property.
  85. *
  86. * @var array
  87. */
  88. private $crumb_count = 0;
  89. /**
  90. * Array of individual (linked) html strings created from crumbs.
  91. *
  92. * @var array
  93. */
  94. private $links = [];
  95. /**
  96. * Breadcrumb html string.
  97. *
  98. * @var string
  99. */
  100. private $output;
  101. /**
  102. * Holds the WooCommerce shop page instance.
  103. *
  104. * @var WPSEO_WooCommerce_Shop_Page
  105. */
  106. private $woocommerce_shop_page;
  107. /**
  108. * Create the breadcrumb.
  109. */
  110. protected function __construct() {
  111. $this->post = ( isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null );
  112. $this->show_on_front = get_option( 'show_on_front' );
  113. $this->page_for_posts = get_option( 'page_for_posts' );
  114. $this->woocommerce_shop_page = new WPSEO_WooCommerce_Shop_Page();
  115. $this->date = new WPSEO_Date_Helper();
  116. $this->filter_element();
  117. $this->filter_separator();
  118. $this->filter_wrapper();
  119. $this->set_crumbs();
  120. $this->prepare_links();
  121. $this->links_to_string();
  122. $this->wrap_breadcrumb();
  123. }
  124. /**
  125. * Get breadcrumb string using the singleton instance of this class.
  126. *
  127. * @param string $before Optional string to prepend.
  128. * @param string $after Optional string to append.
  129. * @param bool $display Echo or return flag.
  130. *
  131. * @return string Returns the breadcrumbs as a string.
  132. */
  133. public static function breadcrumb( $before = '', $after = '', $display = true ) {
  134. // Remember the last used before/after for use in case the object goes __toString().
  135. self::$before = $before;
  136. self::$after = $after;
  137. $instance = self::get_instance();
  138. $output = $before . $instance->output . $after;
  139. if ( $display === true ) {
  140. echo $output;
  141. return '';
  142. }
  143. return $output;
  144. }
  145. /**
  146. * Magic method to use in case the class would be send to string.
  147. *
  148. * @return string
  149. */
  150. public function __toString() {
  151. return self::$before . $this->output . self::$after;
  152. }
  153. /**
  154. * Retrieves an instance of the class.
  155. *
  156. * @return WPSEO_Breadcrumbs The instance.
  157. */
  158. public static function get_instance() {
  159. if ( ! ( self::$instance instanceof self ) ) {
  160. self::$instance = new self();
  161. }
  162. return self::$instance;
  163. }
  164. /**
  165. * Returns the collected links for the breadcrumbs.
  166. *
  167. * @return array The collected links.
  168. */
  169. public function get_links() {
  170. return $this->links;
  171. }
  172. /**
  173. * Returns the link url for a single id.
  174. *
  175. * When the target is private and the user isn't allowed to access it, just return an empty string.
  176. *
  177. * @param int $id The target id.
  178. *
  179. * @return string Empty string when post isn't accessible. An URL if accessible.
  180. */
  181. protected function get_link_url_for_id( $id ) {
  182. $post_status = get_post_status( $id );
  183. $post_type = get_post_type_object( get_post_type( $id ) );
  184. // Don't link if item is private and user does't have capability to read it.
  185. if ( $post_status === 'private' && $post_type !== null && ! current_user_can( $post_type->cap->read_private_posts ) ) {
  186. return '';
  187. }
  188. $url = get_permalink( $id );
  189. if ( $url === false ) {
  190. return '';
  191. }
  192. return $url;
  193. }
  194. /**
  195. * Filter: 'wpseo_breadcrumb_single_link_wrapper' - Allows developer to change or wrap each breadcrumb element.
  196. *
  197. * @api string $element
  198. */
  199. private function filter_element() {
  200. $this->element = esc_attr( apply_filters( 'wpseo_breadcrumb_single_link_wrapper', $this->element ) );
  201. }
  202. /**
  203. * Filter: 'wpseo_breadcrumb_separator' - Allow (theme) developer to change the Yoast SEO breadcrumb separator.
  204. *
  205. * @api string $breadcrumbs_sep Breadcrumbs separator.
  206. */
  207. private function filter_separator() {
  208. $separator = apply_filters( 'wpseo_breadcrumb_separator', WPSEO_Options::get( 'breadcrumbs-sep' ) );
  209. $this->separator = ' ' . $separator . ' ';
  210. }
  211. /**
  212. * Filter: 'wpseo_breadcrumb_output_wrapper' - Allow changing the HTML wrapper element for the Yoast SEO breadcrumbs output.
  213. *
  214. * @api string $wrapper The wrapper element.
  215. */
  216. private function filter_wrapper() {
  217. $wrapper = apply_filters( 'wpseo_breadcrumb_output_wrapper', $this->wrapper );
  218. $wrapper = tag_escape( $wrapper );
  219. if ( is_string( $wrapper ) && '' !== $wrapper ) {
  220. $this->wrapper = $wrapper;
  221. }
  222. }
  223. /**
  224. * Get a term's parents.
  225. *
  226. * @param object $term Term to get the parents for.
  227. *
  228. * @return array
  229. */
  230. private function get_term_parents( $term ) {
  231. $tax = $term->taxonomy;
  232. $parents = [];
  233. while ( $term->parent !== 0 ) {
  234. $term = get_term( $term->parent, $tax );
  235. $parents[] = $term;
  236. }
  237. return array_reverse( $parents );
  238. }
  239. /**
  240. * Find the deepest term in an array of term objects.
  241. *
  242. * @param array $terms Terms set.
  243. *
  244. * @return object
  245. */
  246. private function find_deepest_term( $terms ) {
  247. /*
  248. * Let's find the deepest term in this array, by looping through and then
  249. * unsetting every term that is used as a parent by another one in the array.
  250. */
  251. $terms_by_id = [];
  252. foreach ( $terms as $term ) {
  253. $terms_by_id[ $term->term_id ] = $term;
  254. }
  255. foreach ( $terms as $term ) {
  256. unset( $terms_by_id[ $term->parent ] );
  257. }
  258. unset( $term );
  259. /*
  260. * As we could still have two subcategories, from different parent categories,
  261. * let's pick the one with the lowest ordered ancestor.
  262. */
  263. $parents_count = 0;
  264. $term_order = 9999; // Because ASC.
  265. reset( $terms_by_id );
  266. $deepest_term = current( $terms_by_id );
  267. foreach ( $terms_by_id as $term ) {
  268. $parents = $this->get_term_parents( $term );
  269. if ( count( $parents ) >= $parents_count ) {
  270. $parents_count = count( $parents );
  271. $parent_order = 9999; // Set default order.
  272. foreach ( $parents as $parent ) {
  273. if ( $parent->parent === 0 && isset( $parent->term_order ) ) {
  274. $parent_order = $parent->term_order;
  275. }
  276. }
  277. unset( $parent );
  278. // Check if parent has lowest order.
  279. if ( $parent_order < $term_order ) {
  280. $term_order = $parent_order;
  281. $deepest_term = $term;
  282. }
  283. }
  284. }
  285. return $deepest_term;
  286. }
  287. /**
  288. * Retrieve the hierachical ancestors for the current 'post'.
  289. *
  290. * @return array
  291. */
  292. private function get_post_ancestors() {
  293. $ancestors = [];
  294. if ( isset( $this->post->ancestors ) ) {
  295. if ( is_array( $this->post->ancestors ) ) {
  296. $ancestors = array_values( $this->post->ancestors );
  297. }
  298. else {
  299. $ancestors = [ $this->post->ancestors ];
  300. }
  301. }
  302. elseif ( isset( $this->post->post_parent ) ) {
  303. $ancestors = [ $this->post->post_parent ];
  304. }
  305. /**
  306. * Filter: Allow changing the ancestors for the Yoast SEO breadcrumbs output.
  307. *
  308. * @api array $ancestors Ancestors.
  309. */
  310. $ancestors = apply_filters( 'wp_seo_get_bc_ancestors', $ancestors );
  311. if ( ! is_array( $ancestors ) ) {
  312. trigger_error( 'The return value for the "wp_seo_get_bc_ancestors" filter should be an array.', E_USER_WARNING );
  313. $ancestors = (array) $ancestors;
  314. }
  315. // Reverse the order so it's oldest to newest.
  316. $ancestors = array_reverse( $ancestors );
  317. return $ancestors;
  318. }
  319. /**
  320. * Determine the crumbs which should form the breadcrumb.
  321. */
  322. private function set_crumbs() {
  323. global $wp_query;
  324. $this->maybe_add_home_crumb();
  325. $this->maybe_add_blog_crumb();
  326. // Ignore coding standards for empty if statement.
  327. // @codingStandardsIgnoreStart
  328. if ( $this->is_front_page() ) {
  329. // Do nothing.
  330. // @codingStandardsIgnoreEnd
  331. }
  332. elseif ( $this->show_on_front === 'page' && is_home() ) {
  333. $this->add_blog_crumb();
  334. }
  335. elseif ( is_singular() ) {
  336. $this->maybe_add_pt_archive_crumb_for_post();
  337. if ( isset( $this->post->post_parent ) && 0 === $this->post->post_parent ) {
  338. $this->maybe_add_taxonomy_crumbs_for_post();
  339. }
  340. if ( isset( $this->post->post_parent ) && $this->post->post_parent !== 0 ) {
  341. $this->add_post_ancestor_crumbs();
  342. }
  343. if ( isset( $this->post->ID ) ) {
  344. $this->add_single_post_crumb( $this->post->ID );
  345. }
  346. }
  347. elseif ( is_post_type_archive() ) {
  348. if ( $this->woocommerce_shop_page->is_shop_page() &&
  349. $this->woocommerce_shop_page->get_shop_page_id() !== -1
  350. ) {
  351. $this->add_single_post_crumb( $this->woocommerce_shop_page->get_shop_page_id() );
  352. }
  353. else {
  354. $post_type = $wp_query->get( 'post_type' );
  355. if ( $post_type && is_string( $post_type ) ) {
  356. $this->add_ptarchive_crumb( $post_type );
  357. }
  358. }
  359. }
  360. elseif ( is_tax() || is_tag() || is_category() ) {
  361. $this->add_crumbs_for_taxonomy();
  362. }
  363. elseif ( is_date() ) {
  364. if ( is_day() ) {
  365. $this->add_linked_month_year_crumb();
  366. $this->add_date_crumb();
  367. }
  368. elseif ( is_month() ) {
  369. $this->add_month_crumb();
  370. }
  371. elseif ( is_year() ) {
  372. $this->add_year_crumb();
  373. }
  374. }
  375. elseif ( is_author() ) {
  376. $user = $wp_query->get_queried_object();
  377. $display_name = get_the_author_meta( 'display_name', $user->ID );
  378. $this->add_predefined_crumb(
  379. WPSEO_Options::get( 'breadcrumbs-archiveprefix' ) . ' ' . $display_name,
  380. null,
  381. true
  382. );
  383. }
  384. elseif ( is_search() ) {
  385. $this->add_predefined_crumb(
  386. WPSEO_Options::get( 'breadcrumbs-searchprefix' ) . ' “' . esc_html( get_search_query() ) . '”',
  387. null,
  388. true
  389. );
  390. }
  391. elseif ( is_404() ) {
  392. $this->add_predefined_crumb(
  393. WPSEO_Options::get( 'breadcrumbs-404crumb' ),
  394. null,
  395. true
  396. );
  397. }
  398. $this->maybe_add_page_crumb();
  399. /**
  400. * Filter: 'wpseo_breadcrumb_links' - Allow the developer to filter the Yoast SEO breadcrumb links, add to them, change order, etc.
  401. *
  402. * @api array $crumbs The crumbs array.
  403. */
  404. $this->crumbs = apply_filters( 'wpseo_breadcrumb_links', $this->crumbs );
  405. $this->crumb_count = count( $this->crumbs );
  406. }
  407. /**
  408. * Determine whether we are on the front page of the site.
  409. *
  410. * @return bool
  411. */
  412. private function is_front_page() {
  413. if ( $this->show_on_front === 'page' && is_front_page() ) {
  414. return true;
  415. }
  416. if ( $this->show_on_front === 'posts' && is_home() ) {
  417. return true;
  418. }
  419. return false;
  420. }
  421. /**
  422. * Add a single id based crumb to the crumbs property.
  423. *
  424. * @param int $id Post ID.
  425. */
  426. private function add_single_post_crumb( $id ) {
  427. $this->crumbs[] = [
  428. 'id' => $id,
  429. ];
  430. }
  431. /**
  432. * Add a term based crumb to the crumbs property.
  433. *
  434. * @param object $term Term data object.
  435. */
  436. private function add_term_crumb( $term ) {
  437. $this->crumbs[] = [
  438. 'term' => $term,
  439. ];
  440. }
  441. /**
  442. * Add a ptarchive based crumb to the crumbs property.
  443. *
  444. * @param string $pt Post type.
  445. */
  446. private function add_ptarchive_crumb( $pt ) {
  447. $this->crumbs[] = [
  448. 'ptarchive' => $pt,
  449. ];
  450. }
  451. /**
  452. * Add a predefined crumb to the crumbs property.
  453. *
  454. * @param string $text Text string.
  455. * @param string $url URL string.
  456. * @param bool $allow_html Flag to allow HTML.
  457. */
  458. private function add_predefined_crumb( $text, $url = '', $allow_html = false ) {
  459. $this->crumbs[] = [
  460. 'text' => $text,
  461. 'url' => $url,
  462. 'allow_html' => $allow_html,
  463. ];
  464. }
  465. /**
  466. * Add Homepage crumb to the crumbs property.
  467. */
  468. private function maybe_add_home_crumb() {
  469. if ( WPSEO_Options::get( 'breadcrumbs-home' ) !== '' ) {
  470. $this->add_predefined_crumb(
  471. WPSEO_Options::get( 'breadcrumbs-home' ),
  472. WPSEO_Utils::home_url(),
  473. true
  474. );
  475. }
  476. }
  477. /**
  478. * Add Blog crumb to the crumbs property.
  479. */
  480. private function add_blog_crumb() {
  481. $this->add_single_post_crumb( $this->page_for_posts );
  482. }
  483. /**
  484. * Add Blog crumb to the crumbs property for single posts where Home != blogpage.
  485. *
  486. * @return void
  487. */
  488. private function maybe_add_blog_crumb() {
  489. // When the show blog page is not enabled.
  490. if ( WPSEO_Options::get( 'breadcrumbs-display-blog-page' ) !== true ) {
  491. return;
  492. }
  493. // When there is no page configured as blog page.
  494. if ( 'page' !== $this->show_on_front || ! $this->page_for_posts ) {
  495. return;
  496. }
  497. // When the current page is the home page, searchpage or isn't a singular post.
  498. if ( is_home() || is_search() || ! is_singular( 'post' ) ) {
  499. return;
  500. }
  501. $this->add_blog_crumb();
  502. }
  503. /**
  504. * Add ptarchive crumb to the crumbs property if it can be linked to, for a single post.
  505. */
  506. private function maybe_add_pt_archive_crumb_for_post() {
  507. // Never do this for the Post type archive for posts, as that would break `maybe_add_blog_crumb`.
  508. if ( $this->post->post_type === 'post' ) {
  509. return;
  510. }
  511. if ( isset( $this->post->post_type ) && get_post_type_archive_link( $this->post->post_type ) ) {
  512. $this->add_ptarchive_crumb( $this->post->post_type );
  513. }
  514. }
  515. /**
  516. * Add taxonomy crumbs to the crumbs property for a single post.
  517. */
  518. private function maybe_add_taxonomy_crumbs_for_post() {
  519. if ( WPSEO_Options::get( 'post_types-' . $this->post->post_type . '-maintax' ) && (string) WPSEO_Options::get( 'post_types-' . $this->post->post_type . '-maintax' ) !== '0' ) {
  520. $main_tax = WPSEO_Options::get( 'post_types-' . $this->post->post_type . '-maintax' );
  521. if ( isset( $this->post->ID ) ) {
  522. $terms = get_the_terms( $this->post, $main_tax );
  523. if ( is_array( $terms ) && $terms !== [] ) {
  524. $primary_term = new WPSEO_Primary_Term( $main_tax, $this->post->ID );
  525. if ( $primary_term->get_primary_term() ) {
  526. $breadcrumb_term = get_term( $primary_term->get_primary_term(), $main_tax );
  527. }
  528. else {
  529. $breadcrumb_term = $this->find_deepest_term( $terms );
  530. }
  531. if ( is_taxonomy_hierarchical( $main_tax ) && $breadcrumb_term->parent !== 0 ) {
  532. $parent_terms = $this->get_term_parents( $breadcrumb_term );
  533. foreach ( $parent_terms as $parent_term ) {
  534. $this->add_term_crumb( $parent_term );
  535. }
  536. }
  537. $this->add_term_crumb( $breadcrumb_term );
  538. }
  539. }
  540. }
  541. }
  542. /**
  543. * Add hierarchical ancestor crumbs to the crumbs property for a single post.
  544. */
  545. private function add_post_ancestor_crumbs() {
  546. $ancestors = $this->get_post_ancestors();
  547. if ( is_array( $ancestors ) && $ancestors !== [] ) {
  548. foreach ( $ancestors as $ancestor ) {
  549. $this->add_single_post_crumb( $ancestor );
  550. }
  551. }
  552. }
  553. /**
  554. * Add taxonomy parent crumbs to the crumbs property for a taxonomy.
  555. */
  556. private function add_crumbs_for_taxonomy() {
  557. $term = $GLOBALS['wp_query']->get_queried_object();
  558. // @todo adjust function name.
  559. $this->maybe_add_preferred_term_parent_crumb( $term );
  560. $this->maybe_add_term_parent_crumbs( $term );
  561. $this->add_term_crumb( $term );
  562. }
  563. /**
  564. * Adds a page crumb to the visible breadcrumbs.
  565. *
  566. * @return void
  567. */
  568. private function maybe_add_page_crumb() {
  569. if ( ! is_paged() ) {
  570. return;
  571. }
  572. $current_page = get_query_var( 'paged', 1 );
  573. if ( $current_page <= 1 ) {
  574. return;
  575. }
  576. $this->crumbs[] = [
  577. 'text' => sprintf(
  578. /* translators: %s expands to the current page number */
  579. __( 'Page %s', 'wordpress-seo' ),
  580. $current_page
  581. ),
  582. 'url' => '',
  583. 'hide_in_schema' => true,
  584. ];
  585. }
  586. /**
  587. * Add parent taxonomy crumb based on user defined preference.
  588. *
  589. * @param object $term Term data object.
  590. */
  591. private function maybe_add_preferred_term_parent_crumb( $term ) {
  592. if ( WPSEO_Options::get( 'taxonomy-' . $term->taxonomy . '-ptparent' ) && (string) WPSEO_Options::get( 'taxonomy-' . $term->taxonomy . '-ptparent' ) !== '0' ) {
  593. if ( 'post' === WPSEO_Options::get( 'taxonomy-' . $term->taxonomy . '-ptparent' ) && $this->show_on_front === 'page' ) {
  594. if ( $this->page_for_posts ) {
  595. $this->add_blog_crumb();
  596. }
  597. return;
  598. }
  599. $this->add_ptarchive_crumb( WPSEO_Options::get( 'taxonomy-' . $term->taxonomy . '-ptparent' ) );
  600. }
  601. }
  602. /**
  603. * Add parent taxonomy crumbs to the crumb property for hierachical taxonomy.
  604. *
  605. * @param object $term Term data object.
  606. */
  607. private function maybe_add_term_parent_crumbs( $term ) {
  608. if ( is_taxonomy_hierarchical( $term->taxonomy ) && $term->parent !== 0 ) {
  609. foreach ( $this->get_term_parents( $term ) as $parent_term ) {
  610. $this->add_term_crumb( $parent_term );
  611. }
  612. }
  613. }
  614. /**
  615. * Add month-year crumb to crumbs property.
  616. */
  617. private function add_linked_month_year_crumb() {
  618. $this->add_predefined_crumb(
  619. $GLOBALS['wp_locale']->get_month( get_query_var( 'monthnum' ) ) . ' ' . get_query_var( 'year' ),
  620. get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) )
  621. );
  622. }
  623. /**
  624. * Add (non-link) month crumb to crumbs property.
  625. */
  626. private function add_month_crumb() {
  627. $this->add_predefined_crumb(
  628. WPSEO_Options::get( 'breadcrumbs-archiveprefix' ) . ' ' . esc_html( single_month_title( ' ', false ) ),
  629. null,
  630. true
  631. );
  632. }
  633. /**
  634. * Add (non-link) year crumb to crumbs property.
  635. */
  636. private function add_year_crumb() {
  637. $this->add_predefined_crumb(
  638. WPSEO_Options::get( 'breadcrumbs-archiveprefix' ) . ' ' . esc_html( get_query_var( 'year' ) ),
  639. null,
  640. true
  641. );
  642. }
  643. /**
  644. * Add (non-link) date crumb to crumbs property.
  645. */
  646. private function add_date_crumb() {
  647. $date = get_the_date();
  648. $this->add_predefined_crumb(
  649. WPSEO_Options::get( 'breadcrumbs-archiveprefix' ) . ' ' . esc_html( $date ),
  650. null,
  651. true
  652. );
  653. }
  654. /**
  655. * Take the crumbs array and convert each crumb to a single breadcrumb string.
  656. *
  657. * @link http://support.google.com/webmasters/bin/answer.py?hl=en&answer=185417 Google documentation on RDFA
  658. */
  659. private function prepare_links() {
  660. if ( ! is_array( $this->crumbs ) || $this->crumbs === [] ) {
  661. return;
  662. }
  663. foreach ( $this->crumbs as $index => $crumb ) {
  664. $link_info = $crumb; // Keep pre-set url/text combis.
  665. if ( isset( $crumb['id'] ) ) {
  666. $link_info = $this->get_link_info_for_id( $crumb['id'] );
  667. }
  668. if ( isset( $crumb['term'] ) ) {
  669. $link_info = $this->get_link_info_for_term( $crumb['term'] );
  670. }
  671. if ( isset( $crumb['ptarchive'] ) ) {
  672. $link_info = $this->get_link_info_for_ptarchive( $crumb['ptarchive'] );
  673. }
  674. /**
  675. * Filter: 'wpseo_breadcrumb_single_link_info' - Allow developers to filter the Yoast SEO Breadcrumb link information.
  676. *
  677. * @api array $link_info The breadcrumb link information.
  678. *
  679. * @param int $index The index of the breadcrumb in the list.
  680. * @param array $crumbs The complete list of breadcrumbs.
  681. */
  682. $link_info = apply_filters( 'wpseo_breadcrumb_single_link_info', $link_info, $index, $this->crumbs );
  683. $this->links[ $index ] = $link_info;
  684. }
  685. }
  686. /**
  687. * Retrieve link url and text based on post id.
  688. *
  689. * @param int $id Post ID.
  690. *
  691. * @return array Array of link text and url.
  692. */
  693. private function get_link_info_for_id( $id ) {
  694. $link = [];
  695. $link['url'] = $this->get_link_url_for_id( $id );
  696. $link['text'] = WPSEO_Meta::get_value( 'bctitle', $id );
  697. if ( $link['text'] === '' ) {
  698. $link['text'] = wp_strip_all_tags( get_the_title( $id ), true );
  699. }
  700. return $link;
  701. }
  702. /**
  703. * Retrieve link url and text based on term object.
  704. *
  705. * @param object $term Term object.
  706. *
  707. * @return array Array of link text and url.
  708. */
  709. private function get_link_info_for_term( $term ) {
  710. $link = [];
  711. $bctitle = WPSEO_Taxonomy_Meta::get_term_meta( $term, $term->taxonomy, 'bctitle' );
  712. if ( ! is_string( $bctitle ) || $bctitle === '' ) {
  713. $bctitle = $term->name;
  714. }
  715. $link['url'] = get_term_link( $term );
  716. $link['text'] = $bctitle;
  717. return $link;
  718. }
  719. /**
  720. * Retrieve link url and text based on post type.
  721. *
  722. * @param string $pt Post type.
  723. *
  724. * @return array Array of link text and url.
  725. */
  726. private function get_link_info_for_ptarchive( $pt ) {
  727. $link = [];
  728. $archive_title = $this->get_archive_title( $pt );
  729. $link['url'] = get_post_type_archive_link( $pt );
  730. $link['text'] = $archive_title;
  731. return $link;
  732. }
  733. /**
  734. * Gets the custom set breadcrumb title for the passed post type.
  735. *
  736. * @param string $post_type The post type to check.
  737. *
  738. * @return string the breadcrumb title.
  739. */
  740. private function get_post_type_archive_breadcrumb( $post_type ) {
  741. return WPSEO_Options::get( 'bctitle-ptarchive-' . $post_type, '' );
  742. }
  743. /**
  744. * Gets the breadcrumb for the passed post type if it's a WooCommerce product and has a breadcrumb title set.
  745. *
  746. * @param string $post_type The post type to check.
  747. *
  748. * @return string The breadcrumb title.
  749. */
  750. private function get_woocommerce_breadcrumb( $post_type ) {
  751. if ( $post_type !== 'product' || ! WPSEO_Utils::is_woocommerce_active() ) {
  752. return '';
  753. }
  754. $shop_page_id = $this->woocommerce_shop_page->get_shop_page_id();
  755. if ( $shop_page_id === -1 ) {
  756. return '';
  757. }
  758. return WPSEO_Meta::get_value( 'bctitle', $shop_page_id );
  759. }
  760. /**
  761. * Determines the archive title based on the passed post type.
  762. *
  763. * @param string $post_type The post type to determine the title for.
  764. *
  765. * @return string The archive title.
  766. */
  767. private function get_archive_title( $post_type ) {
  768. $woocommerce_breadcrumb = $this->get_woocommerce_breadcrumb( $post_type );
  769. if ( $woocommerce_breadcrumb !== '' ) {
  770. return $woocommerce_breadcrumb;
  771. }
  772. $post_type_archive_breadcrumb = $this->get_post_type_archive_breadcrumb( $post_type );
  773. if ( $post_type_archive_breadcrumb !== '' ) {
  774. return $post_type_archive_breadcrumb;
  775. }
  776. $post_type_obj = get_post_type_object( $post_type );
  777. if ( ! is_object( $post_type_obj ) ) {
  778. return '';
  779. }
  780. if ( isset( $post_type_obj->label ) && $post_type_obj->label !== '' ) {
  781. return $post_type_obj->label;
  782. }
  783. if ( isset( $post_type_obj->labels->menu_name ) && $post_type_obj->labels->menu_name !== '' ) {
  784. return $post_type_obj->labels->menu_name;
  785. }
  786. return $post_type_obj->name;
  787. }
  788. /**
  789. * Create a breadcrumb element string.
  790. *
  791. * @todo The `$paged` variable only works for archives, not for paged articles, so this does not work
  792. * for paged article at this moment.
  793. *
  794. * @param array $link Link info array containing the keys:
  795. * 'text' => (string) link text.
  796. * 'url' => (string) link url.
  797. * (optional) 'title' => (string) link title attribute text.
  798. * (optional) 'allow_html' => (bool) whether to (not) escape html in the link text.
  799. * This prevents html stripping from the text strings set in the
  800. * WPSEO -> Internal Links options page.
  801. * @param int $i Index for the current breadcrumb.
  802. *
  803. * @return string
  804. */
  805. private function crumb_to_link( $link, $i ) {
  806. $link_output = '';
  807. if ( isset( $link['text'] ) && ( is_string( $link['text'] ) && $link['text'] !== '' ) ) {
  808. $link['text'] = trim( $link['text'] );
  809. if ( ! isset( $link['allow_html'] ) || $link['allow_html'] !== true ) {
  810. $link['text'] = esc_html( $link['text'] );
  811. }
  812. if ( ( $i < ( $this->crumb_count - 1 ) && ( isset( $link['url'] ) && ( is_string( $link['url'] ) && $link['url'] !== '' ) ) )
  813. ) {
  814. $link_output .= '<' . $this->element . '>';
  815. $title_attr = isset( $link['title'] ) ? ' title="' . esc_attr( $link['title'] ) . '"' : '';
  816. $link_output .= '<a href="' . esc_url( $link['url'] ) . '" ' . $title_attr . '>' . $link['text'] . '</a>';
  817. }
  818. else {
  819. $inner_elm = 'span';
  820. if ( $i === ( $this->crumb_count - 1 ) && WPSEO_Options::get( 'breadcrumbs-boldlast' ) === true ) {
  821. $inner_elm = 'strong';
  822. }
  823. $link_output .= '<' . $inner_elm . ' class="breadcrumb_last" aria-current="page">' . $link['text'] . '</' . $inner_elm . '>';
  824. // This is the last element, now close all previous elements.
  825. while ( $i > 0 ) {
  826. $link_output .= '</' . $this->element . '>';
  827. $i--;
  828. }
  829. }
  830. }
  831. /**
  832. * Filter: 'wpseo_breadcrumb_single_link' - Allow changing of each link being put out by the Yoast SEO breadcrumbs class.
  833. *
  834. * @api string $link_output The output string.
  835. *
  836. * @param array $link The link array.
  837. */
  838. return apply_filters( 'wpseo_breadcrumb_single_link', $link_output, $link );
  839. }
  840. /**
  841. * Create a complete breadcrumb string from an array of breadcrumb element strings.
  842. */
  843. private function links_to_string() {
  844. if ( is_array( $this->links ) && $this->links !== [] ) {
  845. // Converts info to an effective link.
  846. $links = $this->links;
  847. foreach ( $links as $key => $link ) {
  848. $links[ $key ] = $this->crumb_to_link( $link, $key );
  849. }
  850. // Removes any effectively empty links.
  851. $links = array_map( 'trim', $links );
  852. $links = array_filter( $links );
  853. $this->output = implode( $this->separator, $links );
  854. }
  855. }
  856. /**
  857. * Wrap a complete breadcrumb string in a wrapper.
  858. */
  859. private function wrap_breadcrumb() {
  860. if ( is_string( $this->output ) && $this->output !== '' ) {
  861. $output = '<' . $this->wrapper . $this->get_output_id() . $this->get_output_class() . '>' . $this->output . '</' . $this->wrapper . '>';
  862. /**
  863. * Filter: 'wpseo_breadcrumb_output' - Allow changing the HTML output of the Yoast SEO breadcrumbs class.
  864. *
  865. * @api string $unsigned HTML output.
  866. */
  867. $output = apply_filters( 'wpseo_breadcrumb_output', $output );
  868. if ( WPSEO_Options::get( 'breadcrumbs-prefix' ) !== '' ) {
  869. $output = "\t" . WPSEO_Options::get( 'breadcrumbs-prefix' ) . "\n" . $output;
  870. }
  871. $this->output = $output;
  872. }
  873. }
  874. /**
  875. * Retrieves HTML ID attribute.
  876. *
  877. * @return string
  878. */
  879. private function get_output_id() {
  880. /**
  881. * Filter: 'wpseo_breadcrumb_output_id' - Allow changing the HTML ID on the Yoast SEO breadcrumbs wrapper element.
  882. *
  883. * @api string $unsigned ID to add to the wrapper element.
  884. */
  885. $id = apply_filters( 'wpseo_breadcrumb_output_id', '' );
  886. if ( is_string( $id ) && '' !== $id ) {
  887. $id = ' id="' . esc_attr( $id ) . '"';
  888. }
  889. return $id;
  890. }
  891. /**
  892. * Retrieves HTML Class attribute.
  893. *
  894. * @return string
  895. */
  896. private function get_output_class() {
  897. /**
  898. * Filter: 'wpseo_breadcrumb_output_class' - Allow changing the HTML class on the Yoast SEO breadcrumbs wrapper element.
  899. *
  900. * @api string $unsigned Class to add to the wrapper element.
  901. */
  902. $class = apply_filters( 'wpseo_breadcrumb_output_class', '' );
  903. if ( is_string( $class ) && '' !== $class ) {
  904. $class = ' class="' . esc_attr( $class ) . '"';
  905. }
  906. return $class;
  907. }
  908. }