PageRenderTime 39ms CodeModel.GetById 14ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

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