Astra_Breadcrumb_Trail::trail()

Formats the HTML output for the breadcrumb trail.


Description


Return

(string)


Source

File: inc/addons/breadcrumbs/class-astra-breadcrumb-trail.php

	public function trail() {

		// Set up variables that we'll need.
		$breadcrumb    = '';
		$item_count    = count( $this->items );
		$item_position = 0;
		$meta          = '';

		if ( 2 > $item_count ) {
			$this->args['schema'] = false;
		}

		// Connect the breadcrumb trail if there are items in the trail.
		if ( 0 < $item_count ) {

			// Add 'browse' label if it should be shown.
			if ( true === $this->args['show_browse'] ) {

				$breadcrumb .= sprintf(
					'<%1$s class="trail-browse">%2$s</%1$s>',
					tag_escape( $this->args['browse_tag'] ),
					$this->labels['browse']
				);
			}

			// Open the unordered list.
			$breadcrumb .= sprintf(
				'<%1$s class="trail-items" %2$s>',
				tag_escape( $this->args['list_tag'] ),
				( $this->args['schema'] ? 'itemscope itemtype="http://schema.org/BreadcrumbList"' : '' )
			);

			if ( $this->args['schema'] ) {
				// Add the number of items and item list order schema.
				$breadcrumb .= sprintf( '<meta content="%1$d" %2$s />', absint( $item_count ), astra_attr(
					'breadcrumb-trail-items-num-meta',
					array(
						'name'  => 'numberOfItems',
						'class' => '',
					)
				) );
				$breadcrumb .= '<meta ' . astra_attr(
					'breadcrumb-trail-items-list-meta',
					array(
						'class'   => '',
						'name'    => 'itemListOrder',
						'content' => 'Ascending',
					)
				) . '/>';
			}

			// Loop through the items and add them to the list.
			foreach ( $this->items as $item ) {

				// Iterate the item position.
				++$item_position;

				// Check if the item is linked.
				preg_match( '/(<a.*?>)(.*?)(<\/a>)/i', $item, $matches );

				// Wrap the item text with appropriate itemprop.
				$item = ! empty( $matches ) ? sprintf( '%s<span %s>%s</span>%s', $matches[1], $this->args['schema'] ? 'itemprop="name"' : '', $matches[2], $matches[3] ) : sprintf( '<span>%s</span>', $item );

				// Wrap the item with its itemprop.
				$item = ( ! empty( $matches ) && $this->args['schema'] )
					? preg_replace( '/(<a.*?)([\'"])>/i', '$1$2 itemprop=$2item$2>', $item )
					: sprintf( '<span>%s</span>', $item );

				// Add list item classes.
				$item_class       = 'trail-item';
				$item_schema_attr = 'itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"';

				if ( 1 === $item_position && 1 < $item_count ) {
					$item_class .= ' trail-begin';
				} elseif ( $item_count === $item_position ) {
					$item_class .= ' trail-end';
					$item_schema_attr = '';
				}

				// Create list item attributes.
				$attributes = $this->args['schema'] ? $item_schema_attr : '';

				$attributes .= ' class="' . $item_class . '"';

				if ( $this->args['schema'] ) {
					// Build the meta position HTML.
					$meta = sprintf( '<meta itemprop="position" content="%s" />', absint( $item_position ) );
				}

				if ( $item_count === $item_position ) {
					$meta = '';
				}

				// Build the list item.
				$breadcrumb .= sprintf( '<%1$s %2$s>%3$s%4$s</%1$s>', tag_escape( $this->args['item_tag'] ),$attributes, $item, $meta );
			}

			// Close the unordered list.
			$breadcrumb .= sprintf( '</%s>', tag_escape( $this->args['list_tag'] ) );

			// Wrap the breadcrumb trail.
			$breadcrumb = sprintf(
				'<%1$s role="navigation" aria-label="%2$s" class="breadcrumb-trail breadcrumbs" >%3$s%4$s%5$s</%1$s>',
				tag_escape( $this->args['container'] ),
				esc_attr( $this->labels['aria_label'] ),
				$this->args['before'],
				$breadcrumb,
				$this->args['after']
			);
		}

		// Allow developers to filter the breadcrumb trail HTML.
		$breadcrumb = apply_filters( 'astra_breadcrumb_trail', $breadcrumb, $this->args );

		if ( false === $this->args['echo'] ) {
			return $breadcrumb;
		}

		echo $breadcrumb;
	}

Changelog

Changelog
Version Description
0.6.0 Introduced.


User Contributed Notes

You must log in before being able to contribute a note or feedback.