abstracts
9 months ago
admin
9 months ago
blocks
10 months ago
cli
11 months ago
customizer
11 months ago
data-stores
9 months ago
emails
9 months ago
export
1 year ago
gateways
9 months ago
import
11 months ago
integrations
2 years ago
interfaces
1 year ago
legacy
1 year ago
libraries
1 year ago
log-handlers
1 year ago
payment-tokens
5 years ago
product-usage
1 year ago
queue
4 years ago
react-admin
9 months ago
rest-api
9 months ago
shipping
1 year ago
shortcodes
10 months ago
theme-support
2 years ago
tracks
10 months ago
traits
5 years ago
walkers
5 years ago
wccom-site
9 months ago
widgets
1 year ago
class-wc-ajax.php
10 months ago
class-wc-auth.php
1 year ago
class-wc-autoloader.php
1 year ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-brands-brand-settings-manager.php
1 year ago
class-wc-brands-coupons.php
1 year ago
class-wc-brands.php
9 months ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
10 months ago
class-wc-cart-fees.php
2 years ago
class-wc-cart-session.php
9 months ago
class-wc-cart-totals.php
10 months ago
class-wc-cart.php
9 months ago
class-wc-checkout.php
1 year ago
class-wc-cli.php
9 months ago
class-wc-comments.php
9 months ago
class-wc-countries.php
1 year ago
class-wc-coupon.php
9 months ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
1 year ago
class-wc-customer.php
9 months ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
3 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
2 years ago
class-wc-deprecated-filter-hooks.php
3 years ago
class-wc-discounts.php
10 months ago
class-wc-download-handler.php
1 year ago
class-wc-emails.php
10 months ago
class-wc-embed.php
1 year ago
class-wc-form-handler.php
9 months ago
class-wc-frontend-scripts.php
9 months ago
class-wc-geo-ip.php
11 months ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
1 year ago
class-wc-https.php
2 years ago
class-wc-install.php
9 months ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
2 years ago
class-wc-logger.php
1 year ago
class-wc-meta-data.php
4 years ago
class-wc-order-factory.php
2 years ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
1 year ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
1 year ago
class-wc-order-item-shipping.php
1 year ago
class-wc-order-item-tax.php
4 years ago
class-wc-order-item.php
1 year ago
class-wc-order-query.php
4 years ago
class-wc-order-refund.php
1 year ago
class-wc-order.php
9 months ago
class-wc-payment-gateways.php
9 months ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
9 months ago
class-wc-post-types.php
1 year ago
class-wc-privacy-background-process.php
1 year ago
class-wc-privacy-erasers.php
9 months ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
11 months ago
class-wc-product-attribute.php
4 years ago
class-wc-product-download.php
2 years ago
class-wc-product-external.php
1 year ago
class-wc-product-factory.php
1 year ago
class-wc-product-grouped.php
10 months ago
class-wc-product-query.php
1 year ago
class-wc-product-simple.php
10 months ago
class-wc-product-variable.php
10 months ago
class-wc-product-variation.php
1 year ago
class-wc-query.php
1 year ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
1 year ago
class-wc-register-wp-admin-settings.php
4 years ago
class-wc-rest-authentication.php
1 year ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
9 months ago
class-wc-shipping-rate.php
11 months ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
1 year ago
class-wc-shortcodes.php
1 year ago
class-wc-structured-data.php
10 months ago
class-wc-tax.php
10 months ago
class-wc-template-loader.php
9 months ago
class-wc-tracker.php
11 months ago
class-wc-validation.php
2 years ago
class-wc-webhook.php
1 year ago
class-woocommerce.php
5 months ago
wc-account-functions.php
9 months ago
wc-attribute-functions.php
9 months ago
wc-brands-functions.php
1 year ago
wc-cart-functions.php
9 months ago
wc-conditional-functions.php
10 months ago
wc-core-functions.php
9 months ago
wc-coupon-functions.php
9 months ago
wc-deprecated-functions.php
9 months ago
wc-formatting-functions.php
8 months ago
wc-notice-functions.php
11 months ago
wc-order-functions.php
9 months ago
wc-order-item-functions.php
3 years ago
wc-order-step-logger-functions.php
1 year ago
wc-page-functions.php
1 year ago
wc-product-functions.php
10 months ago
wc-rest-functions.php
10 months ago
wc-stock-functions.php
1 year ago
wc-template-functions.php
9 months ago
wc-template-hooks.php
9 months ago
wc-term-functions.php
9 months ago
wc-update-functions.php
8 months ago
wc-user-functions.php
9 months ago
wc-webhook-functions.php
1 year ago
wc-widget-functions.php
5 years ago
class-wc-breadcrumb.php
381 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WC_Breadcrumb class. |
| 4 | * |
| 5 | * @package WooCommerce\Classes |
| 6 | * @version 2.3.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Breadcrumb class. |
| 13 | */ |
| 14 | class WC_Breadcrumb { |
| 15 | |
| 16 | /** |
| 17 | * Breadcrumb trail. |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | protected $crumbs = array(); |
| 22 | |
| 23 | /** |
| 24 | * Add a crumb so we don't get lost. |
| 25 | * |
| 26 | * @param string $name Name. |
| 27 | * @param string $link Link. |
| 28 | */ |
| 29 | public function add_crumb( $name, $link = '' ) { |
| 30 | $this->crumbs[] = array( |
| 31 | wp_strip_all_tags( $name ), |
| 32 | $link, |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Reset crumbs. |
| 38 | */ |
| 39 | public function reset() { |
| 40 | $this->crumbs = array(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get the breadcrumb. |
| 45 | * |
| 46 | * @return array |
| 47 | */ |
| 48 | public function get_breadcrumb() { |
| 49 | return apply_filters( 'woocommerce_get_breadcrumb', $this->crumbs, $this ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Generate breadcrumb trail. |
| 54 | * |
| 55 | * @return array of breadcrumbs |
| 56 | */ |
| 57 | public function generate() { |
| 58 | $conditionals = array( |
| 59 | 'is_home', |
| 60 | 'is_404', |
| 61 | 'is_attachment', |
| 62 | 'is_single', |
| 63 | 'is_product_category', |
| 64 | 'is_product_tag', |
| 65 | 'is_shop', |
| 66 | 'is_page', |
| 67 | 'is_post_type_archive', |
| 68 | 'is_category', |
| 69 | 'is_tag', |
| 70 | 'is_author', |
| 71 | 'is_date', |
| 72 | 'is_tax', |
| 73 | ); |
| 74 | |
| 75 | if ( ( ! is_front_page() && ! ( is_post_type_archive() && intval( get_option( 'page_on_front' ) ) === wc_get_page_id( 'shop' ) ) ) || is_paged() ) { |
| 76 | foreach ( $conditionals as $conditional ) { |
| 77 | if ( call_user_func( $conditional ) ) { |
| 78 | call_user_func( array( $this, 'add_crumbs_' . substr( $conditional, 3 ) ) ); |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $this->search_trail(); |
| 84 | $this->paged_trail(); |
| 85 | |
| 86 | return $this->get_breadcrumb(); |
| 87 | } |
| 88 | |
| 89 | return array(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Prepend the shop page to shop breadcrumbs. |
| 94 | */ |
| 95 | protected function prepend_shop_page() { |
| 96 | $permalinks = wc_get_permalink_structure(); |
| 97 | $shop_page_id = wc_get_page_id( 'shop' ); |
| 98 | $shop_page = get_post( $shop_page_id ); |
| 99 | |
| 100 | // If permalinks contain the shop page in the URI prepend the breadcrumb with shop. |
| 101 | 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 ) { |
| 102 | $this->add_crumb( get_the_title( $shop_page ), get_permalink( $shop_page ) ); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Is home trail.. |
| 108 | */ |
| 109 | protected function add_crumbs_home() { |
| 110 | $this->add_crumb( single_post_title( '', false ) ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * 404 trail. |
| 115 | */ |
| 116 | protected function add_crumbs_404() { |
| 117 | $this->add_crumb( __( 'Error 404', 'woocommerce' ) ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Attachment trail. |
| 122 | */ |
| 123 | protected function add_crumbs_attachment() { |
| 124 | global $post; |
| 125 | |
| 126 | $this->add_crumbs_single( $post->post_parent, get_permalink( $post->post_parent ) ); |
| 127 | $this->add_crumb( get_the_title(), get_permalink() ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Single post trail. |
| 132 | * |
| 133 | * @param int $post_id Post ID. |
| 134 | * @param string $permalink Post permalink. |
| 135 | */ |
| 136 | protected function add_crumbs_single( $post_id = 0, $permalink = '' ) { |
| 137 | if ( ! $post_id ) { |
| 138 | global $post; |
| 139 | } else { |
| 140 | $post = get_post( $post_id ); // WPCS: override ok. |
| 141 | } |
| 142 | |
| 143 | if ( ! $permalink ) { |
| 144 | $permalink = get_permalink( $post ); |
| 145 | } |
| 146 | |
| 147 | if ( 'product' === get_post_type( $post ) ) { |
| 148 | $this->prepend_shop_page(); |
| 149 | |
| 150 | $terms = wc_get_product_terms( |
| 151 | $post->ID, |
| 152 | 'product_cat', |
| 153 | apply_filters( |
| 154 | 'woocommerce_breadcrumb_product_terms_args', |
| 155 | array( |
| 156 | 'orderby' => 'parent', |
| 157 | 'order' => 'DESC', |
| 158 | ) |
| 159 | ) |
| 160 | ); |
| 161 | |
| 162 | if ( $terms ) { |
| 163 | $main_term = apply_filters( 'woocommerce_breadcrumb_main_term', $terms[0], $terms ); |
| 164 | $this->term_ancestors( $main_term->term_id, 'product_cat' ); |
| 165 | $this->add_crumb( $main_term->name, get_term_link( $main_term ) ); |
| 166 | } |
| 167 | } elseif ( 'post' !== get_post_type( $post ) ) { |
| 168 | $post_type = get_post_type_object( get_post_type( $post ) ); |
| 169 | |
| 170 | if ( ! empty( $post_type->has_archive ) ) { |
| 171 | $this->add_crumb( $post_type->labels->singular_name, get_post_type_archive_link( get_post_type( $post ) ) ); |
| 172 | } |
| 173 | } else { |
| 174 | $cat = current( get_the_category( $post ) ); |
| 175 | if ( $cat ) { |
| 176 | $this->term_ancestors( $cat->term_id, 'category' ); |
| 177 | $this->add_crumb( $cat->name, get_term_link( $cat ) ); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | $this->add_crumb( get_the_title( $post ), $permalink ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Page trail. |
| 186 | */ |
| 187 | protected function add_crumbs_page() { |
| 188 | global $post; |
| 189 | |
| 190 | if ( $post->post_parent ) { |
| 191 | $parent_crumbs = array(); |
| 192 | $parent_id = $post->post_parent; |
| 193 | |
| 194 | while ( $parent_id ) { |
| 195 | $page = get_post( $parent_id ); |
| 196 | $parent_id = $page->post_parent; |
| 197 | $parent_crumbs[] = array( get_the_title( $page->ID ), get_permalink( $page->ID ) ); |
| 198 | } |
| 199 | |
| 200 | $parent_crumbs = array_reverse( $parent_crumbs ); |
| 201 | |
| 202 | foreach ( $parent_crumbs as $crumb ) { |
| 203 | $this->add_crumb( $crumb[0], $crumb[1] ); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | $this->add_crumb( get_the_title(), get_permalink() ); |
| 208 | $this->endpoint_trail(); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Product category trail. |
| 213 | */ |
| 214 | protected function add_crumbs_product_category() { |
| 215 | $current_term = $GLOBALS['wp_query']->get_queried_object(); |
| 216 | |
| 217 | $this->prepend_shop_page(); |
| 218 | $this->term_ancestors( $current_term->term_id, 'product_cat' ); |
| 219 | $this->add_crumb( $current_term->name, get_term_link( $current_term, 'product_cat' ) ); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Product tag trail. |
| 224 | */ |
| 225 | protected function add_crumbs_product_tag() { |
| 226 | $current_term = $GLOBALS['wp_query']->get_queried_object(); |
| 227 | |
| 228 | $this->prepend_shop_page(); |
| 229 | |
| 230 | /* translators: %s: product tag */ |
| 231 | $this->add_crumb( sprintf( __( 'Products tagged “%s”', 'woocommerce' ), $current_term->name ), get_term_link( $current_term, 'product_tag' ) ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Shop breadcrumb. |
| 236 | */ |
| 237 | protected function add_crumbs_shop() { |
| 238 | if ( intval( get_option( 'page_on_front' ) ) === wc_get_page_id( 'shop' ) ) { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | $_name = wc_get_page_id( 'shop' ) ? get_the_title( wc_get_page_id( 'shop' ) ) : ''; |
| 243 | |
| 244 | if ( ! $_name ) { |
| 245 | $product_post_type = get_post_type_object( 'product' ); |
| 246 | $_name = $product_post_type->labels->name; |
| 247 | } |
| 248 | |
| 249 | $this->add_crumb( $_name, get_post_type_archive_link( 'product' ) ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Post type archive trail. |
| 254 | */ |
| 255 | protected function add_crumbs_post_type_archive() { |
| 256 | $post_type = get_post_type_object( get_post_type() ); |
| 257 | |
| 258 | if ( $post_type ) { |
| 259 | $this->add_crumb( $post_type->labels->name, get_post_type_archive_link( get_post_type() ) ); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Category trail. |
| 265 | */ |
| 266 | protected function add_crumbs_category() { |
| 267 | $this_category = get_category( $GLOBALS['wp_query']->get_queried_object() ); |
| 268 | |
| 269 | if ( 0 !== intval( $this_category->parent ) ) { |
| 270 | $this->term_ancestors( $this_category->term_id, 'category' ); |
| 271 | } |
| 272 | |
| 273 | $this->add_crumb( single_cat_title( '', false ), get_category_link( $this_category->term_id ) ); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Tag trail. |
| 278 | */ |
| 279 | protected function add_crumbs_tag() { |
| 280 | $queried_object = $GLOBALS['wp_query']->get_queried_object(); |
| 281 | |
| 282 | /* translators: %s: tag name */ |
| 283 | $this->add_crumb( sprintf( __( 'Posts tagged “%s”', 'woocommerce' ), single_tag_title( '', false ) ), get_tag_link( $queried_object->term_id ) ); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Add crumbs for date based archives. |
| 288 | */ |
| 289 | protected function add_crumbs_date() { |
| 290 | if ( is_year() || is_month() || is_day() ) { |
| 291 | $this->add_crumb( get_the_time( 'Y' ), get_year_link( get_the_time( 'Y' ) ) ); |
| 292 | } |
| 293 | if ( is_month() || is_day() ) { |
| 294 | $this->add_crumb( get_the_time( 'F' ), get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) ); |
| 295 | } |
| 296 | if ( is_day() ) { |
| 297 | $this->add_crumb( get_the_time( 'd' ) ); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Add crumbs for taxonomies |
| 303 | */ |
| 304 | protected function add_crumbs_tax() { |
| 305 | $this_term = $GLOBALS['wp_query']->get_queried_object(); |
| 306 | $taxonomy = get_taxonomy( $this_term->taxonomy ); |
| 307 | |
| 308 | $this->add_crumb( $taxonomy->labels->name ); |
| 309 | |
| 310 | if ( 0 !== intval( $this_term->parent ) ) { |
| 311 | $this->term_ancestors( $this_term->term_id, $this_term->taxonomy ); |
| 312 | } |
| 313 | |
| 314 | $this->add_crumb( single_term_title( '', false ), get_term_link( $this_term->term_id, $this_term->taxonomy ) ); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Add a breadcrumb for author archives. |
| 319 | */ |
| 320 | protected function add_crumbs_author() { |
| 321 | global $author; |
| 322 | |
| 323 | $userdata = get_userdata( $author ); |
| 324 | |
| 325 | /* translators: %s: author name */ |
| 326 | $this->add_crumb( sprintf( __( 'Author: %s', 'woocommerce' ), $userdata->display_name ) ); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Add crumbs for a term. |
| 331 | * |
| 332 | * @param int $term_id Term ID. |
| 333 | * @param string $taxonomy Taxonomy. |
| 334 | */ |
| 335 | protected function term_ancestors( $term_id, $taxonomy ) { |
| 336 | $ancestors = get_ancestors( $term_id, $taxonomy ); |
| 337 | $ancestors = array_reverse( $ancestors ); |
| 338 | |
| 339 | foreach ( $ancestors as $ancestor ) { |
| 340 | $ancestor = get_term( $ancestor, $taxonomy ); |
| 341 | |
| 342 | if ( ! is_wp_error( $ancestor ) && $ancestor ) { |
| 343 | $this->add_crumb( $ancestor->name, get_term_link( $ancestor ) ); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Endpoints. |
| 350 | */ |
| 351 | protected function endpoint_trail() { |
| 352 | $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; |
| 353 | $endpoint = is_wc_endpoint_url() ? WC()->query->get_current_endpoint() : ''; |
| 354 | $endpoint_title = $endpoint ? WC()->query->get_endpoint_title( $endpoint, $action ) : ''; |
| 355 | |
| 356 | if ( $endpoint_title ) { |
| 357 | $this->add_crumb( $endpoint_title ); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Add a breadcrumb for search results. |
| 363 | */ |
| 364 | protected function search_trail() { |
| 365 | if ( is_search() ) { |
| 366 | /* translators: %s: search term */ |
| 367 | $this->add_crumb( sprintf( __( 'Search results for “%s”', 'woocommerce' ), get_search_query() ), remove_query_arg( 'paged' ) ); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Add a breadcrumb for pagination. |
| 373 | */ |
| 374 | protected function paged_trail() { |
| 375 | if ( get_query_var( 'paged' ) && 'subcategories' !== woocommerce_get_loop_display_mode() ) { |
| 376 | /* translators: %d: page number */ |
| 377 | $this->add_crumb( sprintf( __( 'Page %d', 'woocommerce' ), get_query_var( 'paged' ) ) ); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 |