class-breadcrumbs.php 26 KB

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