/frontend/class-breadcrumbs.php

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