PluginProbe
WooCommerce / 11.1.0-rc.2
WooCommerce v11.1.0-rc.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / includes / class-wc-breadcrumb.php
class-wc-breadcrumb.php
434 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @return void
29 */
30 public function add_crumb( $name, $link = '' ) {
31 $this->crumbs[] = array(
32 wp_strip_all_tags( $name ),
33 $link,
34 );
35 }
36
37 /**
38 * Reset crumbs.
39 *
40 * @return void
41 */
42 public function reset() {
43 $this->crumbs = array();
44 }
45
46 /**
47 * Get the breadcrumb.
48 *
49 * @return array
50 */
51 public function get_breadcrumb() {
52 return apply_filters( 'woocommerce_get_breadcrumb', $this->crumbs, $this );
53 }
54
55 /**
56 * Generate breadcrumb trail.
57 *
58 * @return array of breadcrumbs
59 */
60 public function generate() {
61 $conditionals = array(
62 'is_home',
63 'is_404',
64 'is_attachment',
65 'is_single',
66 'is_product_category',
67 'is_product_tag',
68 'is_shop',
69 'is_page',
70 'is_post_type_archive',
71 'is_category',
72 'is_tag',
73 'is_author',
74 'is_date',
75 'is_tax',
76 );
77
78 if ( ( ! is_front_page() && ! ( is_post_type_archive() && intval( get_option( 'page_on_front' ) ) === wc_get_page_id( 'shop' ) ) ) || is_paged() ) {
79 foreach ( $conditionals as $conditional ) {
80 if ( call_user_func( $conditional ) ) {
81 call_user_func( array( $this, 'add_crumbs_' . substr( $conditional, 3 ) ) );
82 break;
83 }
84 }
85
86 $this->search_trail();
87 $this->paged_trail();
88
89 return $this->get_breadcrumb();
90 }
91
92 return array();
93 }
94
95 /**
96 * Prepend the shop page to shop breadcrumbs.
97 *
98 * @return void
99 */
100 protected function prepend_shop_page() {
101 $permalinks = wc_get_permalink_structure();
102 $shop_page_id = wc_get_page_id( 'shop' );
103 $shop_page = get_post( $shop_page_id );
104
105 // If permalinks contain the shop page in the URI prepend the breadcrumb with shop.
106 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 ) {
107 $this->add_crumb( get_the_title( $shop_page ), get_permalink( $shop_page ) );
108 }
109 }
110
111 /**
112 * Is home trail..
113 *
114 * @return void
115 */
116 protected function add_crumbs_home() {
117 $this->add_crumb( single_post_title( '', false ) );
118 }
119
120 /**
121 * 404 trail.
122 *
123 * @return void
124 */
125 protected function add_crumbs_404() {
126 $this->add_crumb( __( 'Error 404', 'woocommerce' ) );
127 }
128
129 /**
130 * Attachment trail.
131 *
132 * @return void
133 */
134 protected function add_crumbs_attachment() {
135 global $post;
136
137 $this->add_crumbs_single( $post->post_parent, get_permalink( $post->post_parent ) );
138 $this->add_crumb( get_the_title(), get_permalink() );
139 }
140
141 /**
142 * Single post trail.
143 *
144 * @param int $post_id Post ID.
145 * @param string $permalink Post permalink.
146 * @return void
147 */
148 protected function add_crumbs_single( $post_id = 0, $permalink = '' ) {
149 if ( ! $post_id ) {
150 global $post;
151 } else {
152 $post = get_post( $post_id ); // WPCS: override ok.
153 }
154
155 if ( ! $permalink ) {
156 $permalink = get_permalink( $post );
157 }
158
159 if ( 'product' === get_post_type( $post ) ) {
160 $this->prepend_shop_page();
161
162 $terms = wc_get_product_terms(
163 $post->ID,
164 'product_cat',
165 apply_filters(
166 'woocommerce_breadcrumb_product_terms_args',
167 array(
168 'orderby' => 'parent',
169 'order' => 'DESC',
170 )
171 )
172 );
173
174 if ( $terms ) {
175 $main_term = apply_filters( 'woocommerce_breadcrumb_main_term', $terms[0], $terms );
176 $this->term_ancestors( $main_term->term_id, 'product_cat' );
177 $this->add_crumb( $main_term->name, get_term_link( $main_term ) );
178 }
179 } elseif ( 'post' !== get_post_type( $post ) ) {
180 $post_type = get_post_type_object( get_post_type( $post ) );
181
182 if ( ! empty( $post_type->has_archive ) ) {
183 $this->add_crumb( $post_type->labels->singular_name, get_post_type_archive_link( get_post_type( $post ) ) );
184 }
185 } else {
186 $cat = current( get_the_category( $post ) );
187 if ( $cat ) {
188 $this->term_ancestors( $cat->term_id, 'category' );
189 $this->add_crumb( $cat->name, get_term_link( $cat ) );
190 }
191 }
192
193 $this->add_crumb( get_the_title( $post ), $permalink );
194 }
195
196 /**
197 * Page trail.
198 *
199 * @return void
200 */
201 protected function add_crumbs_page() {
202 global $post;
203
204 if ( $post->post_parent ) {
205 $parent_crumbs = array();
206 $parent_id = $post->post_parent;
207
208 while ( $parent_id ) {
209 $page = get_post( $parent_id );
210 $parent_id = $page->post_parent;
211 $parent_crumbs[] = array( get_the_title( $page->ID ), get_permalink( $page->ID ) );
212 }
213
214 $parent_crumbs = array_reverse( $parent_crumbs );
215
216 foreach ( $parent_crumbs as $crumb ) {
217 $this->add_crumb( $crumb[0], $crumb[1] );
218 }
219 }
220
221 // On WC endpoints, get_the_title() returns the endpoint title (via wc_page_endpoint_title filter).
222 // Use post_title directly to avoid duplicates like "Orders / Orders" instead of "My Account / Orders".
223 $permalink = get_permalink();
224 if ( is_wc_endpoint_url() ) {
225 $this->add_crumb( $post->post_title, $permalink ? $permalink : '' );
226 } else {
227 $this->add_crumb( get_the_title(), $permalink ? $permalink : '' );
228 }
229
230 $this->endpoint_trail();
231 }
232
233 /**
234 * Product category trail.
235 *
236 * @return void
237 */
238 protected function add_crumbs_product_category() {
239 $current_term = $GLOBALS['wp_query']->get_queried_object();
240
241 $this->prepend_shop_page();
242 $this->term_ancestors( $current_term->term_id, 'product_cat' );
243 $this->add_crumb( $current_term->name, get_term_link( $current_term, 'product_cat' ) );
244 }
245
246 /**
247 * Product tag trail.
248 *
249 * @return void
250 */
251 protected function add_crumbs_product_tag() {
252 $current_term = $GLOBALS['wp_query']->get_queried_object();
253
254 $this->prepend_shop_page();
255
256 /* translators: %s: product tag */
257 $this->add_crumb( sprintf( __( 'Products tagged &ldquo;%s&rdquo;', 'woocommerce' ), $current_term->name ), get_term_link( $current_term, 'product_tag' ) );
258 }
259
260 /**
261 * Shop breadcrumb.
262 *
263 * @return void
264 */
265 protected function add_crumbs_shop() {
266 if ( intval( get_option( 'page_on_front' ) ) === wc_get_page_id( 'shop' ) ) {
267 return;
268 }
269
270 $_name = wc_get_page_id( 'shop' ) ? get_the_title( wc_get_page_id( 'shop' ) ) : '';
271
272 if ( ! $_name ) {
273 $product_post_type = get_post_type_object( 'product' );
274 $_name = $product_post_type->labels->name;
275 }
276
277 $this->add_crumb( $_name, get_post_type_archive_link( 'product' ) );
278 }
279
280 /**
281 * Post type archive trail.
282 *
283 * @return void
284 */
285 protected function add_crumbs_post_type_archive() {
286 $post_type = get_post_type_object( get_post_type() );
287
288 if ( $post_type ) {
289 $this->add_crumb( $post_type->labels->name, get_post_type_archive_link( get_post_type() ) );
290 }
291 }
292
293 /**
294 * Category trail.
295 *
296 * @return void
297 */
298 protected function add_crumbs_category() {
299 $this_category = get_category( $GLOBALS['wp_query']->get_queried_object() );
300
301 if ( is_wp_error( $this_category ) || ! $this_category ) {
302 return;
303 }
304
305 if ( 0 !== intval( $this_category->parent ) ) {
306 $this->term_ancestors( $this_category->term_id, 'category' );
307 }
308
309 $this->add_crumb( single_cat_title( '', false ), get_category_link( $this_category->term_id ) );
310 }
311
312 /**
313 * Tag trail.
314 *
315 * @return void
316 */
317 protected function add_crumbs_tag() {
318 $queried_object = $GLOBALS['wp_query']->get_queried_object();
319
320 /* translators: %s: tag name */
321 $this->add_crumb( sprintf( __( 'Posts tagged &ldquo;%s&rdquo;', 'woocommerce' ), single_tag_title( '', false ) ), get_tag_link( $queried_object->term_id ) );
322 }
323
324 /**
325 * Add crumbs for date based archives.
326 *
327 * @return void
328 */
329 protected function add_crumbs_date() {
330 if ( is_year() || is_month() || is_day() ) {
331 $this->add_crumb( get_the_time( 'Y' ), get_year_link( get_the_time( 'Y' ) ) );
332 }
333 if ( is_month() || is_day() ) {
334 $this->add_crumb( get_the_time( 'F' ), get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) );
335 }
336 if ( is_day() ) {
337 $this->add_crumb( get_the_time( 'd' ) );
338 }
339 }
340
341 /**
342 * Add crumbs for taxonomies.
343 *
344 * @return void
345 */
346 protected function add_crumbs_tax() {
347 $this_term = $GLOBALS['wp_query']->get_queried_object();
348 $taxonomy = get_taxonomy( $this_term->taxonomy );
349
350 if ( 'product_brand' !== $this_term->taxonomy ) {
351 $this->add_crumb( $taxonomy->labels->name );
352 }
353
354 if ( 0 !== intval( $this_term->parent ) ) {
355 $this->term_ancestors( $this_term->term_id, $this_term->taxonomy );
356 }
357
358 $this->add_crumb( single_term_title( '', false ), get_term_link( $this_term->term_id, $this_term->taxonomy ) );
359 }
360
361 /**
362 * Add a breadcrumb for author archives.
363 *
364 * @return void
365 */
366 protected function add_crumbs_author() {
367 global $author;
368
369 $userdata = get_userdata( $author );
370
371 /* translators: %s: author name */
372 $this->add_crumb( sprintf( __( 'Author: %s', 'woocommerce' ), $userdata->display_name ) );
373 }
374
375 /**
376 * Add crumbs for a term.
377 *
378 * @param int $term_id Term ID.
379 * @param string $taxonomy Taxonomy.
380 * @return void
381 */
382 protected function term_ancestors( $term_id, $taxonomy ) {
383 $ancestors = get_ancestors( $term_id, $taxonomy );
384 $ancestors = array_reverse( $ancestors );
385
386 foreach ( $ancestors as $ancestor ) {
387 $ancestor = get_term( $ancestor, $taxonomy );
388
389 if ( ! is_wp_error( $ancestor ) && $ancestor ) {
390 $this->add_crumb( $ancestor->name, get_term_link( $ancestor ) );
391 }
392 }
393 }
394
395 /**
396 * Endpoints.
397 *
398 * @return void
399 */
400 protected function endpoint_trail() {
401 $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';
402 $endpoint = is_wc_endpoint_url() ? WC()->query->get_current_endpoint() : '';
403 $endpoint_title = $endpoint ? WC()->query->get_endpoint_title( $endpoint, $action ) : '';
404
405 if ( $endpoint_title ) {
406 $this->add_crumb( $endpoint_title );
407 }
408 }
409
410 /**
411 * Add a breadcrumb for search results.
412 *
413 * @return void
414 */
415 protected function search_trail() {
416 if ( is_search() ) {
417 /* translators: %s: search term */
418 $this->add_crumb( sprintf( __( 'Search results for &ldquo;%s&rdquo;', 'woocommerce' ), get_search_query() ), remove_query_arg( 'paged' ) );
419 }
420 }
421
422 /**
423 * Add a breadcrumb for pagination.
424 *
425 * @return void
426 */
427 protected function paged_trail() {
428 if ( get_query_var( 'paged' ) && 'subcategories' !== woocommerce_get_loop_display_mode() ) {
429 /* translators: %d: page number */
430 $this->add_crumb( sprintf( __( 'Page %d', 'woocommerce' ), get_query_var( 'paged' ) ) );
431 }
432 }
433 }
434