PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/learnpress/inc/class-lp-breadcrumb.php

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