WPSEO_Breadcrumbs

This class handles the Breadcrumbs generation and display.

Defined (1)

The class is defined in the following location(s).

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