2014-12-05 13:42:19 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WC_Breadcrumb class.
|
|
|
|
*
|
2018-03-15 21:30:08 +00:00
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @version 2.3.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Breadcrumb class.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
class WC_Breadcrumb {
|
|
|
|
|
2015-01-05 16:19:00 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Breadcrumb trail.
|
2015-01-05 16:19:00 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-12-05 13:42:19 +00:00
|
|
|
private $crumbs = array();
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Add a crumb so we don't get lost.
|
2015-01-05 16:19:00 +00:00
|
|
|
*
|
2018-03-15 21:30:08 +00:00
|
|
|
* @param string $name Name.
|
|
|
|
* @param string $link Link.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
public function add_crumb( $name, $link = '' ) {
|
|
|
|
$this->crumbs[] = array(
|
2016-01-15 09:59:04 +00:00
|
|
|
strip_tags( $name ),
|
2016-08-27 02:08:49 +00:00
|
|
|
$link,
|
2014-12-05 13:42:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Reset crumbs.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
public function reset() {
|
|
|
|
$this->crumbs = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get the breadcrumb.
|
2015-01-05 16:19:00 +00:00
|
|
|
*
|
2014-12-05 13:42:19 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_breadcrumb() {
|
2015-03-09 10:17:31 +00:00
|
|
|
return apply_filters( 'woocommerce_get_breadcrumb', $this->crumbs, $this );
|
2014-12-05 13:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Generate breadcrumb trail.
|
2015-01-05 16:19:00 +00:00
|
|
|
*
|
2014-12-05 13:42:19 +00:00
|
|
|
* @return array of breadcrumbs
|
|
|
|
*/
|
|
|
|
public function generate() {
|
|
|
|
$conditionals = array(
|
|
|
|
'is_home',
|
|
|
|
'is_404',
|
|
|
|
'is_attachment',
|
|
|
|
'is_single',
|
|
|
|
'is_product_category',
|
|
|
|
'is_product_tag',
|
|
|
|
'is_shop',
|
2015-02-18 12:53:39 +00:00
|
|
|
'is_page',
|
2014-12-05 13:42:19 +00:00
|
|
|
'is_post_type_archive',
|
|
|
|
'is_category',
|
|
|
|
'is_tag',
|
|
|
|
'is_author',
|
2015-08-13 19:02:57 +00:00
|
|
|
'is_date',
|
2016-08-27 02:08:49 +00:00
|
|
|
'is_tax',
|
2014-12-05 13:42:19 +00:00
|
|
|
);
|
|
|
|
|
2016-08-02 03:27:30 +00:00
|
|
|
if ( ( ! is_front_page() && ! ( is_post_type_archive() && intval( get_option( 'page_on_front' ) ) === wc_get_page_id( 'shop' ) ) ) || is_paged() ) {
|
2014-12-05 13:42:19 +00:00
|
|
|
foreach ( $conditionals as $conditional ) {
|
|
|
|
if ( call_user_func( $conditional ) ) {
|
|
|
|
call_user_func( array( $this, 'add_crumbs_' . substr( $conditional, 3 ) ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->search_trail();
|
|
|
|
$this->paged_trail();
|
|
|
|
|
|
|
|
return $this->get_breadcrumb();
|
|
|
|
}
|
|
|
|
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Prepend the shop page to shop breadcrumbs.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function prepend_shop_page() {
|
2017-02-07 16:16:45 +00:00
|
|
|
$permalinks = wc_get_permalink_structure();
|
2014-12-05 13:42:19 +00:00
|
|
|
$shop_page_id = wc_get_page_id( 'shop' );
|
|
|
|
$shop_page = get_post( $shop_page_id );
|
|
|
|
|
2018-03-15 21:30:08 +00:00
|
|
|
// If permalinks contain the shop page in the URI prepend the breadcrumb with shop.
|
|
|
|
if ( $shop_page_id && $shop_page && isset( $permalinks['product_base'] ) && strstr( $permalinks['product_base'], '/' . $shop_page->post_name ) && intval( get_option( 'page_on_front' ) ) !== $shop_page_id ) {
|
2015-03-06 20:11:23 +00:00
|
|
|
$this->add_crumb( get_the_title( $shop_page ), get_permalink( $shop_page ) );
|
2014-12-05 13:42:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-15 21:30:08 +00:00
|
|
|
* Is home trail..
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function add_crumbs_home() {
|
|
|
|
$this->add_crumb( single_post_title( '', false ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* 404 trail.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function add_crumbs_404() {
|
|
|
|
$this->add_crumb( __( 'Error 404', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-15 21:30:08 +00:00
|
|
|
* Attachment trail.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function add_crumbs_attachment() {
|
|
|
|
global $post;
|
2015-01-05 16:19:00 +00:00
|
|
|
|
|
|
|
$this->add_crumbs_single( $post->post_parent, get_permalink( $post->post_parent ) );
|
2014-12-05 13:42:19 +00:00
|
|
|
$this->add_crumb( get_the_title(), get_permalink() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Single post trail.
|
2015-01-05 16:19:00 +00:00
|
|
|
*
|
2018-03-15 21:30:08 +00:00
|
|
|
* @param int $post_id Post ID.
|
|
|
|
* @param string $permalink Post permalink.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
2015-01-05 16:19:00 +00:00
|
|
|
private function add_crumbs_single( $post_id = 0, $permalink = '' ) {
|
|
|
|
if ( ! $post_id ) {
|
|
|
|
global $post;
|
|
|
|
} else {
|
2018-03-15 21:30:08 +00:00
|
|
|
$post = get_post( $post_id ); // WPCS: override ok.
|
2015-01-05 16:19:00 +00:00
|
|
|
}
|
2014-12-05 13:42:19 +00:00
|
|
|
|
2015-01-05 16:19:00 +00:00
|
|
|
if ( 'product' === get_post_type( $post ) ) {
|
2014-12-05 13:42:19 +00:00
|
|
|
$this->prepend_shop_page();
|
2018-02-20 11:49:06 +00:00
|
|
|
|
2018-03-15 21:30:08 +00:00
|
|
|
$terms = wc_get_product_terms(
|
|
|
|
$post->ID, 'product_cat', apply_filters(
|
|
|
|
'woocommerce_breadcrumb_product_terms_args', array(
|
|
|
|
'orderby' => 'parent',
|
|
|
|
'order' => 'DESC',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2018-02-20 11:49:06 +00:00
|
|
|
|
|
|
|
if ( $terms ) {
|
2015-03-09 10:17:31 +00:00
|
|
|
$main_term = apply_filters( 'woocommerce_breadcrumb_main_term', $terms[0], $terms );
|
2014-12-05 13:42:19 +00:00
|
|
|
$this->term_ancestors( $main_term->term_id, 'product_cat' );
|
|
|
|
$this->add_crumb( $main_term->name, get_term_link( $main_term ) );
|
|
|
|
}
|
2018-01-04 12:09:11 +00:00
|
|
|
} elseif ( 'post' !== get_post_type( $post ) ) {
|
2015-01-05 16:19:00 +00:00
|
|
|
$post_type = get_post_type_object( get_post_type( $post ) );
|
2018-01-04 12:09:11 +00:00
|
|
|
|
|
|
|
if ( ! empty( $post_type->has_archive ) ) {
|
|
|
|
$this->add_crumb( $post_type->labels->singular_name, get_post_type_archive_link( get_post_type( $post ) ) );
|
|
|
|
}
|
2014-12-05 13:42:19 +00:00
|
|
|
} else {
|
2015-01-05 16:19:00 +00:00
|
|
|
$cat = current( get_the_category( $post ) );
|
2014-12-05 13:42:19 +00:00
|
|
|
if ( $cat ) {
|
2017-08-16 10:22:22 +00:00
|
|
|
$this->term_ancestors( $cat->term_id, 'category' );
|
2014-12-05 13:42:19 +00:00
|
|
|
$this->add_crumb( $cat->name, get_term_link( $cat ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 16:19:00 +00:00
|
|
|
$this->add_crumb( get_the_title( $post ), $permalink );
|
2014-12-05 13:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Page trail.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function add_crumbs_page() {
|
|
|
|
global $post;
|
|
|
|
|
|
|
|
if ( $post->post_parent ) {
|
|
|
|
$parent_crumbs = array();
|
|
|
|
$parent_id = $post->post_parent;
|
|
|
|
|
|
|
|
while ( $parent_id ) {
|
2018-03-15 21:30:08 +00:00
|
|
|
$page = get_post( $parent_id );
|
|
|
|
$parent_id = $page->post_parent;
|
2014-12-21 05:21:19 +00:00
|
|
|
$parent_crumbs[] = array( get_the_title( $page->ID ), get_permalink( $page->ID ) );
|
2014-12-05 13:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$parent_crumbs = array_reverse( $parent_crumbs );
|
|
|
|
|
|
|
|
foreach ( $parent_crumbs as $crumb ) {
|
|
|
|
$this->add_crumb( $crumb[0], $crumb[1] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->add_crumb( get_the_title(), get_permalink() );
|
|
|
|
$this->endpoint_trail();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Product category trail.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function add_crumbs_product_category() {
|
|
|
|
$current_term = $GLOBALS['wp_query']->get_queried_object();
|
|
|
|
|
|
|
|
$this->prepend_shop_page();
|
|
|
|
$this->term_ancestors( $current_term->term_id, 'product_cat' );
|
|
|
|
$this->add_crumb( $current_term->name );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Product tag trail.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function add_crumbs_product_tag() {
|
2015-01-05 16:19:00 +00:00
|
|
|
$current_term = $GLOBALS['wp_query']->get_queried_object();
|
|
|
|
|
2014-12-05 13:42:19 +00:00
|
|
|
$this->prepend_shop_page();
|
2018-03-15 21:30:08 +00:00
|
|
|
|
|
|
|
/* translators: %s: product tag */
|
2015-01-05 16:19:00 +00:00
|
|
|
$this->add_crumb( sprintf( __( 'Products tagged “%s”', 'woocommerce' ), $current_term->name ) );
|
2014-12-05 13:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Shop breadcrumb.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function add_crumbs_shop() {
|
2018-03-15 21:30:08 +00:00
|
|
|
if ( intval( get_option( 'page_on_front' ) ) === wc_get_page_id( 'shop' ) ) {
|
2015-02-18 12:53:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-05 13:42:19 +00:00
|
|
|
$_name = wc_get_page_id( 'shop' ) ? get_the_title( wc_get_page_id( 'shop' ) ) : '';
|
|
|
|
|
|
|
|
if ( ! $_name ) {
|
|
|
|
$product_post_type = get_post_type_object( 'product' );
|
2018-03-15 21:30:08 +00:00
|
|
|
$_name = $product_post_type->labels->singular_name;
|
2014-12-05 13:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->add_crumb( $_name, get_post_type_archive_link( 'product' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Post type archive trail.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function add_crumbs_post_type_archive() {
|
|
|
|
$post_type = get_post_type_object( get_post_type() );
|
|
|
|
|
|
|
|
if ( $post_type ) {
|
|
|
|
$this->add_crumb( $post_type->labels->singular_name, get_post_type_archive_link( get_post_type() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Category trail.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function add_crumbs_category() {
|
|
|
|
$this_category = get_category( $GLOBALS['wp_query']->get_queried_object() );
|
|
|
|
|
2018-03-15 21:30:08 +00:00
|
|
|
if ( 0 !== intval( $this_category->parent ) ) {
|
2017-08-16 10:22:22 +00:00
|
|
|
$this->term_ancestors( $this_category->term_id, 'category' );
|
2014-12-05 13:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->add_crumb( single_cat_title( '', false ), get_category_link( $this_category->term_id ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Tag trail.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function add_crumbs_tag() {
|
|
|
|
$queried_object = $GLOBALS['wp_query']->get_queried_object();
|
2018-03-15 21:30:08 +00:00
|
|
|
|
|
|
|
/* translators: %s: tag name */
|
2014-12-05 13:42:19 +00:00
|
|
|
$this->add_crumb( sprintf( __( 'Posts tagged “%s”', 'woocommerce' ), single_tag_title( '', false ) ), get_tag_link( $queried_object->term_id ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Add crumbs for date based archives.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function add_crumbs_date() {
|
|
|
|
if ( is_year() || is_month() || is_day() ) {
|
|
|
|
$this->add_crumb( get_the_time( 'Y' ), get_year_link( get_the_time( 'Y' ) ) );
|
|
|
|
}
|
|
|
|
if ( is_month() || is_day() ) {
|
|
|
|
$this->add_crumb( get_the_time( 'F' ), get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) );
|
|
|
|
}
|
|
|
|
if ( is_day() ) {
|
|
|
|
$this->add_crumb( get_the_time( 'd' ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 19:02:57 +00:00
|
|
|
/**
|
2015-12-01 07:31:11 +00:00
|
|
|
* Add crumbs for taxonomies
|
2015-08-13 19:02:57 +00:00
|
|
|
*/
|
|
|
|
private function add_crumbs_tax() {
|
|
|
|
$this_term = $GLOBALS['wp_query']->get_queried_object();
|
|
|
|
$taxonomy = get_taxonomy( $this_term->taxonomy );
|
|
|
|
|
|
|
|
$this->add_crumb( $taxonomy->labels->name );
|
|
|
|
|
2018-03-15 21:30:08 +00:00
|
|
|
if ( 0 !== intval( $this_term->parent ) ) {
|
2015-12-01 07:31:11 +00:00
|
|
|
$this->term_ancestors( $this_term->term_id, $this_term->taxonomy );
|
2015-08-13 19:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->add_crumb( single_term_title( '', false ), get_term_link( $this_term->term_id, $this_term->taxonomy ) );
|
|
|
|
}
|
|
|
|
|
2014-12-05 13:42:19 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Add a breadcrumb for author archives.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function add_crumbs_author() {
|
|
|
|
global $author;
|
|
|
|
|
|
|
|
$userdata = get_userdata( $author );
|
2018-03-15 21:30:08 +00:00
|
|
|
|
|
|
|
/* translators: %s: author name */
|
2014-12-05 13:42:19 +00:00
|
|
|
$this->add_crumb( sprintf( __( 'Author: %s', 'woocommerce' ), $userdata->display_name ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Add crumbs for a term.
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2018-03-15 21:30:08 +00:00
|
|
|
* @param int $term_id Term ID.
|
|
|
|
* @param string $taxonomy Taxonomy.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function term_ancestors( $term_id, $taxonomy ) {
|
|
|
|
$ancestors = get_ancestors( $term_id, $taxonomy );
|
|
|
|
$ancestors = array_reverse( $ancestors );
|
|
|
|
|
|
|
|
foreach ( $ancestors as $ancestor ) {
|
|
|
|
$ancestor = get_term( $ancestor, $taxonomy );
|
|
|
|
|
|
|
|
if ( ! is_wp_error( $ancestor ) && $ancestor ) {
|
|
|
|
$this->add_crumb( $ancestor->name, get_term_link( $ancestor ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Endpoints.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function endpoint_trail() {
|
2018-03-15 21:30:08 +00:00
|
|
|
$endpoint = is_wc_endpoint_url() ? WC()->query->get_current_endpoint() : '';
|
|
|
|
$endpoint_title = $endpoint ? WC()->query->get_endpoint_title( $endpoint ) : '';
|
|
|
|
|
|
|
|
if ( $endpoint_title ) {
|
2014-12-05 13:42:19 +00:00
|
|
|
$this->add_crumb( $endpoint_title );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Add a breadcrumb for search results.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function search_trail() {
|
|
|
|
if ( is_search() ) {
|
2018-03-15 21:30:08 +00:00
|
|
|
/* translators: %s: search term */
|
2014-12-05 13:42:19 +00:00
|
|
|
$this->add_crumb( sprintf( __( 'Search results for “%s”', 'woocommerce' ), get_search_query() ), remove_query_arg( 'paged' ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Add a breadcrumb for pagination.
|
2014-12-05 13:42:19 +00:00
|
|
|
*/
|
|
|
|
private function paged_trail() {
|
|
|
|
if ( get_query_var( 'paged' ) ) {
|
2018-03-15 21:30:08 +00:00
|
|
|
/* translators: %d: page number */
|
2014-12-05 13:42:19 +00:00
|
|
|
$this->add_crumb( sprintf( __( 'Page %d', 'woocommerce' ), get_query_var( 'paged' ) ) );
|
|
|
|
}
|
|
|
|
}
|
2015-01-05 16:19:00 +00:00
|
|
|
}
|