WC_Breadcrumb

WC_Breadcrumb class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-breadcrumb.php  
  1. class WC_Breadcrumb { 
  2.  
  3. /** 
  4. * Breadcrumb trail. 
  5. * @var array 
  6. */ 
  7. private $crumbs = array(); 
  8.  
  9. /** 
  10. * Add a crumb so we don't get lost. 
  11. * @param string $name 
  12. * @param string $link 
  13. */ 
  14. public function add_crumb( $name, $link = '' ) { 
  15. $this->crumbs[] = array( 
  16. strip_tags( $name ),  
  17. $link,  
  18. ); 
  19.  
  20. /** 
  21. * Reset crumbs. 
  22. */ 
  23. public function reset() { 
  24. $this->crumbs = array(); 
  25.  
  26. /** 
  27. * Get the breadcrumb. 
  28. * @return array 
  29. */ 
  30. public function get_breadcrumb() { 
  31. return apply_filters( 'woocommerce_get_breadcrumb', $this->crumbs, $this ); 
  32.  
  33. /** 
  34. * Generate breadcrumb trail. 
  35. * @return array of breadcrumbs 
  36. */ 
  37. public function generate() { 
  38. $conditionals = array( 
  39. 'is_home',  
  40. 'is_404',  
  41. 'is_attachment',  
  42. 'is_single',  
  43. 'is_product_category',  
  44. 'is_product_tag',  
  45. 'is_shop',  
  46. 'is_page',  
  47. 'is_post_type_archive',  
  48. 'is_category',  
  49. 'is_tag',  
  50. 'is_author',  
  51. 'is_date',  
  52. 'is_tax',  
  53. ); 
  54.  
  55. if ( ( ! is_front_page() && ! ( is_post_type_archive() && intval( get_option( 'page_on_front' ) ) === wc_get_page_id( 'shop' ) ) ) || is_paged() ) { 
  56. foreach ( $conditionals as $conditional ) { 
  57. if ( call_user_func( $conditional ) ) { 
  58. call_user_func( array( $this, 'add_crumbs_' . substr( $conditional, 3 ) ) ); 
  59. break; 
  60.  
  61. $this->search_trail(); 
  62. $this->paged_trail(); 
  63.  
  64. return $this->get_breadcrumb(); 
  65.  
  66. return array(); 
  67.  
  68. /** 
  69. * Prepend the shop page to shop breadcrumbs. 
  70. */ 
  71. private function prepend_shop_page() { 
  72. $permalinks = wc_get_permalink_structure(); 
  73. $shop_page_id = wc_get_page_id( 'shop' ); 
  74. $shop_page = get_post( $shop_page_id ); 
  75.  
  76. // If permalinks contain the shop page in the URI prepend the breadcrumb with shop 
  77. if ( $shop_page_id && $shop_page && isset( $permalinks['product_base'] ) && strstr( $permalinks['product_base'], '/' . $shop_page->post_name ) && get_option( 'page_on_front' ) != $shop_page_id ) { 
  78. $this->add_crumb( get_the_title( $shop_page ), get_permalink( $shop_page ) ); 
  79.  
  80. /** 
  81. * is home trail. 
  82. */ 
  83. private function add_crumbs_home() { 
  84. $this->add_crumb( single_post_title( '', false ) ); 
  85.  
  86. /** 
  87. * 404 trail. 
  88. */ 
  89. private function add_crumbs_404() { 
  90. $this->add_crumb( __( 'Error 404', 'woocommerce' ) ); 
  91.  
  92. /** 
  93. * attachment trail. 
  94. */ 
  95. private function add_crumbs_attachment() { 
  96. global $post; 
  97.  
  98. $this->add_crumbs_single( $post->post_parent, get_permalink( $post->post_parent ) ); 
  99. $this->add_crumb( get_the_title(), get_permalink() ); 
  100.  
  101. /** 
  102. * Single post trail. 
  103. * @param int $post_id 
  104. * @param string $permalink 
  105. */ 
  106. private function add_crumbs_single( $post_id = 0, $permalink = '' ) { 
  107. if ( ! $post_id ) { 
  108. global $post; 
  109. } else { 
  110. $post = get_post( $post_id ); 
  111.  
  112. if ( 'product' === get_post_type( $post ) ) { 
  113. $this->prepend_shop_page(); 
  114. if ( $terms = wc_get_product_terms( $post->ID, 'product_cat', array( 'orderby' => 'parent', 'order' => 'DESC' ) ) ) { 
  115. $main_term = apply_filters( 'woocommerce_breadcrumb_main_term', $terms[0], $terms ); 
  116. $this->term_ancestors( $main_term->term_id, 'product_cat' ); 
  117. $this->add_crumb( $main_term->name, get_term_link( $main_term ) ); 
  118. } elseif ( 'post' != get_post_type( $post ) ) { 
  119. $post_type = get_post_type_object( get_post_type( $post ) ); 
  120. $this->add_crumb( $post_type->labels->singular_name, get_post_type_archive_link( get_post_type( $post ) ) ); 
  121. } else { 
  122. $cat = current( get_the_category( $post ) ); 
  123. if ( $cat ) { 
  124. $this->term_ancestors( $cat->term_id, 'post_category' ); 
  125. $this->add_crumb( $cat->name, get_term_link( $cat ) ); 
  126.  
  127. $this->add_crumb( get_the_title( $post ), $permalink ); 
  128.  
  129. /** 
  130. * Page trail. 
  131. */ 
  132. private function add_crumbs_page() { 
  133. global $post; 
  134.  
  135. if ( $post->post_parent ) { 
  136. $parent_crumbs = array(); 
  137. $parent_id = $post->post_parent; 
  138.  
  139. while ( $parent_id ) { 
  140. $page = get_post( $parent_id ); 
  141. $parent_id = $page->post_parent; 
  142. $parent_crumbs[] = array( get_the_title( $page->ID ), get_permalink( $page->ID ) ); 
  143.  
  144. $parent_crumbs = array_reverse( $parent_crumbs ); 
  145.  
  146. foreach ( $parent_crumbs as $crumb ) { 
  147. $this->add_crumb( $crumb[0], $crumb[1] ); 
  148.  
  149. $this->add_crumb( get_the_title(), get_permalink() ); 
  150. $this->endpoint_trail(); 
  151.  
  152. /** 
  153. * Product category trail. 
  154. */ 
  155. private function add_crumbs_product_category() { 
  156. $current_term = $GLOBALS['wp_query']->get_queried_object(); 
  157.  
  158. $this->prepend_shop_page(); 
  159. $this->term_ancestors( $current_term->term_id, 'product_cat' ); 
  160. $this->add_crumb( $current_term->name ); 
  161.  
  162. /** 
  163. * Product tag trail. 
  164. */ 
  165. private function add_crumbs_product_tag() { 
  166. $current_term = $GLOBALS['wp_query']->get_queried_object(); 
  167.  
  168. $this->prepend_shop_page(); 
  169. $this->add_crumb( sprintf( __( 'Products tagged “%s”', 'woocommerce' ), $current_term->name ) ); 
  170.  
  171. /** 
  172. * Shop breadcrumb. 
  173. */ 
  174. private function add_crumbs_shop() { 
  175. if ( get_option( 'page_on_front' ) == wc_get_page_id( 'shop' ) ) { 
  176. return; 
  177.  
  178. $_name = wc_get_page_id( 'shop' ) ? get_the_title( wc_get_page_id( 'shop' ) ) : ''; 
  179.  
  180. if ( ! $_name ) { 
  181. $product_post_type = get_post_type_object( 'product' ); 
  182. $_name = $product_post_type->labels->singular_name; 
  183.  
  184. $this->add_crumb( $_name, get_post_type_archive_link( 'product' ) ); 
  185.  
  186. /** 
  187. * Post type archive trail. 
  188. */ 
  189. private function add_crumbs_post_type_archive() { 
  190. $post_type = get_post_type_object( get_post_type() ); 
  191.  
  192. if ( $post_type ) { 
  193. $this->add_crumb( $post_type->labels->singular_name, get_post_type_archive_link( get_post_type() ) ); 
  194.  
  195. /** 
  196. * Category trail. 
  197. */ 
  198. private function add_crumbs_category() { 
  199. $this_category = get_category( $GLOBALS['wp_query']->get_queried_object() ); 
  200.  
  201. if ( 0 != $this_category->parent ) { 
  202. $this->term_ancestors( $this_category->parent, 'post_category' ); 
  203. $this->add_crumb( $this_category->name, get_category_link( $this_category->term_id ) ); 
  204.  
  205. $this->add_crumb( single_cat_title( '', false ), get_category_link( $this_category->term_id ) ); 
  206.  
  207. /** 
  208. * Tag trail. 
  209. */ 
  210. private function add_crumbs_tag() { 
  211. $queried_object = $GLOBALS['wp_query']->get_queried_object(); 
  212. $this->add_crumb( sprintf( __( 'Posts tagged “%s”', 'woocommerce' ), single_tag_title( '', false ) ), get_tag_link( $queried_object->term_id ) ); 
  213.  
  214. /** 
  215. * Add crumbs for date based archives. 
  216. */ 
  217. private function add_crumbs_date() { 
  218. if ( is_year() || is_month() || is_day() ) { 
  219. $this->add_crumb( get_the_time( 'Y' ), get_year_link( get_the_time( 'Y' ) ) ); 
  220. if ( is_month() || is_day() ) { 
  221. $this->add_crumb( get_the_time( 'F' ), get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) ); 
  222. if ( is_day() ) { 
  223. $this->add_crumb( get_the_time( 'd' ) ); 
  224.  
  225. /** 
  226. * Add crumbs for taxonomies 
  227. */ 
  228. private function add_crumbs_tax() { 
  229. $this_term = $GLOBALS['wp_query']->get_queried_object(); 
  230. $taxonomy = get_taxonomy( $this_term->taxonomy ); 
  231.  
  232. $this->add_crumb( $taxonomy->labels->name ); 
  233.  
  234. if ( 0 != $this_term->parent ) { 
  235. $this->term_ancestors( $this_term->term_id, $this_term->taxonomy ); 
  236.  
  237. $this->add_crumb( single_term_title( '', false ), get_term_link( $this_term->term_id, $this_term->taxonomy ) ); 
  238.  
  239. /** 
  240. * Add a breadcrumb for author archives. 
  241. */ 
  242. private function add_crumbs_author() { 
  243. global $author; 
  244.  
  245. $userdata = get_userdata( $author ); 
  246. $this->add_crumb( sprintf( __( 'Author: %s', 'woocommerce' ), $userdata->display_name ) ); 
  247.  
  248. /** 
  249. * Add crumbs for a term. 
  250. * @param string $taxonomy 
  251. */ 
  252. private function term_ancestors( $term_id, $taxonomy ) { 
  253. $ancestors = get_ancestors( $term_id, $taxonomy ); 
  254. $ancestors = array_reverse( $ancestors ); 
  255.  
  256. foreach ( $ancestors as $ancestor ) { 
  257. $ancestor = get_term( $ancestor, $taxonomy ); 
  258.  
  259. if ( ! is_wp_error( $ancestor ) && $ancestor ) { 
  260. $this->add_crumb( $ancestor->name, get_term_link( $ancestor ) ); 
  261.  
  262. /** 
  263. * Endpoints. 
  264. */ 
  265. private function endpoint_trail() { 
  266. // Is an endpoint showing? 
  267. if ( is_wc_endpoint_url() && ( $endpoint = WC()->query->get_current_endpoint() ) && ( $endpoint_title = WC()->query->get_endpoint_title( $endpoint ) ) ) { 
  268. $this->add_crumb( $endpoint_title ); 
  269.  
  270. /** 
  271. * Add a breadcrumb for search results. 
  272. */ 
  273. private function search_trail() { 
  274. if ( is_search() ) { 
  275. $this->add_crumb( sprintf( __( 'Search results for “%s”', 'woocommerce' ), get_search_query() ), remove_query_arg( 'paged' ) ); 
  276.  
  277. /** 
  278. * Add a breadcrumb for pagination. 
  279. */ 
  280. private function paged_trail() { 
  281. if ( get_query_var( 'paged' ) ) { 
  282. $this->add_crumb( sprintf( __( 'Page %d', 'woocommerce' ), get_query_var( 'paged' ) ) );