Açıklama Yok

class-wc-breadcrumb.php 9.5KB

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