PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/class-wc-breadcrumb.php

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