h ( $this->crumbs as $index => $crumb ) {
$link_info = $crumb; // Keep pre-set url/text combis.
if ( isset( $crumb['id'] ) ) {
$link_info = $this->get_link_info_for_id( $crumb['id'] );
} elseif ( isset( $crumb['term'] ) ) {
$link_info = $this->get_link_info_for_term( $crumb['term'] );
} elseif ( isset( $crumb['ptarchive'] ) ) {
$link_info = $this->get_link_info_for_ptarchive( $crumb['ptarchive'] );
}
$this->crumb_to_link( $link_info, $index );
}
}
/**
* Retrieves the link url and text based on the post ID.
*
* @param int $id Post ID.
* @return array Array of link text and url
*/
private function get_link_info_for_id( $id ) {
$link = array();
$link['url'] = get_permalink( $id );
$link['text'] = esc_html( get_the_title( $id ) );
return $link;
}
/**
* Retrieves link url and text based on term object
*
* @param object $term Term object.
*
* @return array Array of link text and url
*/
private function get_link_info_for_term( $term ) {
$link = array();
$link['url'] = get_term_link( $term );
$link['text'] = $term->name;
return $link;
}
/**
* Retrieves link url and text based on post type
*
* @param string $post_type Post type.
*
* @return array Array of link text and url
*/
private function get_link_info_for_ptarchive( $post_type ) {
$link = array();
$archive_title = '';
$post_type_obj = get_post_type_object( $post_type );
if ( is_object( $post_type_obj ) ) {
if ( isset( $post_type_obj->labels->name ) && ! empty( $post_type_obj->labels->name ) ) {
$archive_title = $post_type_obj->labels->name;
} elseif ( isset( $post_type_obj->label ) && ! empty( $post_type_obj->label ) ) {
$archive_title = $post_type_obj->label;
} elseif ( isset( $post_type_obj->labels->menu_name ) && ! empty( $post_type_obj->labels->menu_name ) ) {
$archive_title = $post_type_obj->labels->menu_name;
} else {
$archive_title = $post_type_obj->name;
}
}
$link['url'] = get_post_type_archive_link( $post_type );
$link['text'] = $archive_title;
/**
* Filters the post type archive text and URL.
*
* @param array $link The link object of the post type archive.
* @param string $post_type Post type.
*/
$link = apply_filters( 'tripp_breadcrumb_post_type_archive_link', $link, $post_type );
return $link;
}
/**
* Creates a breadcrumb element string.
*
* @param array $link {
* An array of the link info.
*
* @type string $text Link text.
* @type string $url Link url.
* @type string $title Optional. Link title attribute text.
* }
* @param int $i Index for the current breadcrumb.
*/
private function crumb_to_link( $link, $i ) {
$link_output = '';
if ( isset( $link['text'] ) && ! empty( $link['text'] ) ) {
$link['text'] = trim( $link['text'] );
if ( isset( $link['url'] ) && ! empty( $link['url'] ) && ( $i < ( $this->crumb_count - 1 ) ) ) {
$this->links[] = sprintf(
'%3$s',
esc_url( $link['url'] ),
isset( $link['title'] ) ? ' title="' . esc_attr( $link['title'] ) . '"' : '',
esc_html( $link['text'] )
);
$this->json_data[] = array(
'@type' => 'ListItem',
'position' => absint( $i + 1 ),
'name' => esc_html( $link['text'] ),
'item' => esc_url( $link['url'] ),
);
} else {
$this->links[] = sprintf(
'%1$s',
esc_html( $link['text'] )
);
$this->json_data[] = array(
'@type' => 'ListItem',
'position' => absint( $i + 1 ),
'name' => esc_html( $link['text'] ),
);
}
}
}
/**
* Create a complete breadcrumb string from an array of breadcrumb element strings
*/
private function links_to_string() {
if ( is_array( $this->links ) && ! empty( $this->links ) ) {
// Remove any effectively empty links.
$links = array_map( 'trim', $this->links );
$links = array_filter( $links );
$this->output = implode( $this->separator, $links );
}
}
/**
* Wrap a complete breadcrumb string in a Breadcrumb RDFA wrapper
*/
private function generate_breadcrumb() {
if ( ! empty( $this->output ) ) {
$json_ld_content = '';
if ( true === $this->options['rich_snippet'] ) {
$json_ld_content = '';
}
$output = sprintf(
'<%1$s class="%2$s">%3$s%1$s>',
$this->wrapper,
esc_attr( $this->options['class'] ),
$this->output . $json_ld_content
);
$output = apply_filters( 'tripp_breadcrumb_output', $output );
$this->output = $output;
}
}
}