/wp-content/plugins/wordpress-seo/frontend/class-breadcrumbs.php

https://bitbucket.org/carloskikea/helpet · PHP · 905 lines · 475 code · 124 blank · 306 comment · 102 complexity · 9780b493ba420f44e51a97e8633efad3 MD5 · raw file

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