PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.1
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / class-lp-breadcrumb.php

class-lp-breadcrumb.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.1, at inc/class-lp-breadcrumb.php

416 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 /**
8 * LP_Breadcrumb class.
9 * This class is modified from WC_Breadcrumb of WooCommerce. Thanks to WooThemes :)
10 *
11 * @class LP_Breadcrumb
12 *
13 * @author ThimPress
14 * @version 1.0
15 * @package LearnPress/Classes
16 */
17 class LP_Breadcrumb {
18
19 /**
20 * Breadcrumb trail
21 *
22 * @var array
23 */
24 private $crumbs = array();
25
26 /**
27 * Add a crumb so we don't get lost
28 *
29 * @param string $name
30 * @param string $link
31 */
32 public function add_crumb( $name, $link = '' ) {
33 $this->crumbs[] = array(
34 $name,
35 $link,
36 );
37 }
38
39 /**
40 * Reset crumbs
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( 'learn_press_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_single',
63 'learn_press_is_course_category',
64 'learn_press_is_course_tag',
65 'learn_press_is_courses',
66 'is_page',
67 'is_post_type_archive',
68 'is_category',
69 'is_tag',
70 'is_author',
71 'is_date',
72 'is_tax',
73 'is_home',
74 'is_404',
75 'is_attachment',
76 );
77
78 if ( ( ! is_front_page() && ! ( is_post_type_archive() && get_option( 'page_on_front' ) == learn_press_get_page_id( 'courses' ) ) ) || is_paged() ) {
79 foreach ( $conditionals as $conditional ) {
80
81 if ( is_callable( $conditional ) && call_user_func( $conditional ) ) {
82 $conditional = preg_replace( '/^learn_press_/', '', $conditional );
83 $conditional = preg_replace( '/^is_/', '', $conditional );
84 if ( is_callable( array( $this, 'add_crumbs_' . $conditional ) ) ) {
85 call_user_func( array( $this, 'add_crumbs_' . $conditional ) );
86 }
87 break;
88 }
89 }
90
91 $this->search_trail();
92
93 return $this->get_breadcrumb();
94 }
95
96 return array();
97 }
98
99 /**
100 * Prepend the courses page to courses breadcrumbs
101 */
102 private function prepend_courses_page() {
103 $courses_page_id = learn_press_get_page_id( 'courses' );
104 $courses_page = get_post( $courses_page_id );
105
106 // If permalinks contain the courses page in the URI prepend the breadcrumb with courses
107 if ( $courses_page_id && $courses_page /*&& strstr( $permalinks['lp_course_base'], '/' . $courses_page->post_name )*/ && get_option( 'page_on_front' ) != $courses_page_id ) {
108 $this->add_crumb( get_the_title( $courses_page ), get_permalink( $courses_page ) );
109 }
110 }
111
112 /**
113 * is home trail
114 */
115 private function add_crumbs_home() {
116 $this->add_crumb( single_post_title( '', false ) );
117 }
118
119 /**
120 * 404 trail
121 */
122 private function add_crumbs_404() {
123 $this->add_crumb( __( 'Error 404', 'learnpress' ) );
124 }
125
126 /**
127 * attachment trail
128 */
129 private function add_crumbs_attachment() {
130 global $post;
131
132 $this->add_crumbs_single( $post->post_parent, get_permalink( $post->post_parent ) );
133 $this->add_crumb( get_the_title(), get_permalink() );
134 }
135
136 /**
137 * Single post trail
138 *
139 * @param int $post_id
140 * @param string $permalink
141 */
142 private function add_crumbs_single( $post_id = 0, $permalink = '' ) {
143 if ( ! $post_id ) {
144 global $post;
145 } else {
146 $post = get_post( $post_id );
147 }
148 if ( LP_COURSE_CPT === get_post_type( $post ) ) {
149 $this->prepend_courses_page();
150
151 $terms = learn_press_get_course_terms(
152 $post->ID,
153 'course_category',
154 array(
155 'orderby' => 'parent',
156 'order' => 'DESC',
157 )
158 );
159
160 if ( $terms ) {
161 $main_term = apply_filters( 'learn_press_breadcrumb_main_term', $terms[0], $terms );
162 $this->term_ancestors( $main_term->term_id, 'course_category' );
163 $this->add_crumb( $main_term->name, get_term_link( $main_term ) );
164 }
165 } elseif ( 'post' != get_post_type( $post ) ) {
166 $post_type = get_post_type_object( get_post_type( $post ) );
167 $this->add_crumb( $post_type->labels->singular_name, get_post_type_archive_link( get_post_type( $post ) ) );
168 } else {
169 $cat = current( get_the_category( $post ) );
170 if ( $cat ) {
171 $this->term_ancestors( $cat->term_id, 'post_category' );
172 $this->add_crumb( $cat->name, get_term_link( $cat ) );
173 }
174 }
175
176 //$this->add_crumb( get_the_title( $post ), $permalink );
177 }
178
179 /**
180 * Page trail
181 */
182 private function add_crumbs_page() {
183 global $post;
184
185 if ( $post->post_parent ) {
186 $parent_crumbs = array();
187 $parent_id = $post->post_parent;
188
189 while ( $parent_id ) {
190 $page = get_post( $parent_id );
191 $parent_id = $page->post_parent;
192 $parent_crumbs[] = array( get_the_title( $page->ID ), get_permalink( $page->ID ) );
193 }
194
195 $parent_crumbs = array_reverse( $parent_crumbs );
196
197 foreach ( $parent_crumbs as $crumb ) {
198 $this->add_crumb( $crumb[0], $crumb[1] );
199 }
200 }
201
202 $this->add_crumb( get_the_title(), get_permalink() );
203 $this->endpoint_trail();
204 }
205
206 /**
207 * Course category trail
208 */
209 private function add_crumbs_course_category() {
210 global $wp_query;
211 $current_term = $this->get_queried_object();
212
213 if ( isset( $current_term->term_id ) ) {
214 $this->prepend_courses_page();
215 $this->term_ancestors( $current_term->term_id, 'course_category' );
216 $this->add_crumb( $current_term->name );
217 }
218 }
219
220 /**
221 * Course tag trail
222 */
223 private function add_crumbs_course_tag() {
224 global $wp_query;
225 $current_term = $this->get_queried_object();
226
227 $this->prepend_courses_page();
228 $this->add_crumb( sprintf( __( 'Courses tagged &ldquo;%s&rdquo;', 'learnpress' ), $current_term->name ) );
229 }
230
231 /**
232 * Courses archive breadcrumb
233 */
234 private function add_crumbs_courses() {
235 $course_page_id = learn_press_get_page_id( 'courses' );
236 if ( get_option( 'page_on_front' ) == $course_page_id ) {
237 return;
238 }
239
240 $_name = $course_page_id ? get_the_title( $course_page_id ) : '';
241
242 if ( ! $_name ) {
243 $course_post_type = get_post_type_object( LP_COURSE_CPT );
244
245 if ( $course_post_type ) {
246 $_name = $course_post_type->labels->singular_name;
247 }
248 }
249
250 $this->add_crumb( $_name, get_post_type_archive_link( LP_COURSE_CPT ) );
251 }
252
253 /**
254 * Post type archive trail
255 */
256 private function add_crumbs_post_type_archive() {
257 $post_type = get_post_type_object( get_post_type() );
258
259 if ( $post_type ) {
260 $this->add_crumb( $post_type->labels->singular_name, get_post_type_archive_link( get_post_type() ) );
261 }
262 }
263
264 /**
265 * Category trail
266 */
267 private function add_crumbs_category() {
268 global $wp_query;
269 $this_category = get_category( $this->get_queried_object() );
270
271 if ( 0 != $this_category->parent ) {
272 $this->term_ancestors( $this_category->parent, 'post_category' );
273 $this->add_crumb( $this_category->name, get_category_link( $this_category->term_id ) );
274 }
275
276 $this->add_crumb( single_cat_title( '', false ), get_category_link( $this_category->term_id ) );
277 }
278
279 /**
280 * Tag trail
281 */
282 private function add_crumbs_tag() {
283 global $wp_query;
284
285 $queried_object = $this->get_queried_object();
286 $this->add_crumb( sprintf( __( 'Posts tagged &ldquo;%s&rdquo;', 'learnpress' ), single_tag_title( '', false ) ), get_tag_link( $queried_object->term_id ) );
287 }
288
289 /**
290 * Add crumbs for date based archives
291 */
292 private function add_crumbs_date() {
293 if ( is_year() || is_month() || is_day() ) {
294 $this->add_crumb( get_the_time( 'Y' ), get_year_link( get_the_time( 'Y' ) ) );
295 }
296 if ( is_month() || is_day() ) {
297 $this->add_crumb( get_the_time( 'F' ), get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) );
298 }
299 if ( is_day() ) {
300 $this->add_crumb( get_the_time( 'd' ) );
301 }
302 }
303
304 /**
305 * Add crumbs for date based archives
306 */
307 private function add_crumbs_tax() {
308 global $wp_query;
309
310 $this_term = $this->get_queried_object();
311 $taxonomy = get_taxonomy( $this_term->taxonomy );
312
313 $this->add_crumb( $taxonomy->labels->name );
314
315 if ( 0 != $this_term->parent ) {
316 $this->term_ancestors( $this_term->parent, 'post_category' );
317 $this->add_crumb( $this_term->name, get_term_link( $this_term->term_id, $this_term->taxonomy ) );
318 }
319
320 $this->add_crumb( single_term_title( '', false ), get_term_link( $this_term->term_id, $this_term->taxonomy ) );
321 }
322
323 /**
324 * Get default queried object by WP.
325 * Fixed issue when viewing course category the text in breadcrumb is blank.
326 *
327 * @return object
328 * @since 3.0.0
329 *
330 */
331 private function get_queried_object() {
332 static $default_queried_object;
333
334 if ( $default_queried_object ) {
335 return $default_queried_object;
336 }
337
338 global $wp_query;
339
340 // Store the current queried object
341 $queried_object = $wp_query->queried_object;
342 $queried_object_id = $wp_query->queried_object_id;
343
344 // Clear the current queried object to ensure we get the default.
345 $wp_query->queried_object = null;
346 $wp_query->queried_object_id = null;
347
348 $default_queried_object = $wp_query->get_queried_object();
349
350 // Restore current queried object
351 $wp_query->queried_object = $queried_object;
352 $wp_query->queried_object_id = $queried_object_id;
353
354 return $default_queried_object;
355 }
356
357 /**
358 * Add a breadcrumb for author archives
359 */
360 private function add_crumbs_author() {
361 global $author;
362
363 $userdata = get_userdata( $author );
364 $this->add_crumb( sprintf( __( 'Author: %s', 'learnpress' ), $userdata->display_name ) );
365 }
366
367 /**
368 * Add crumbs for a term
369 *
370 * @param string $taxonomy
371 */
372 private function term_ancestors( $term_id, $taxonomy ) {
373 $ancestors = get_ancestors( $term_id, $taxonomy );
374 $ancestors = array_reverse( $ancestors );
375
376 foreach ( $ancestors as $ancestor ) {
377 $ancestor = get_term( $ancestor, $taxonomy );
378
379 if ( ! is_wp_error( $ancestor ) && $ancestor ) {
380 $this->add_crumb( $ancestor->name, get_term_link( $ancestor ) );
381 }
382 }
383 }
384
385 /**
386 * Endpoints
387 */
388 private function endpoint_trail() {
389 }
390
391 /**
392 * Add a breadcrumb for search results
393 */
394 private function search_trail() {
395 if ( is_search() ) {
396 $this->add_crumb( sprintf( __( 'Search results for &ldquo;%s&rdquo;', 'learnpress' ), get_search_query() ), esc_url_raw( remove_query_arg( 'paged' ) ) );
397 } elseif ( ! empty( $_GET['c_search'] ) ) {
398 $this->add_crumb(
399 sprintf(
400 __( 'Search results for &ldquo;%s&rdquo;', 'learnpress' ),
401 esc_html( $_GET['c_search'] )
402 )
403 );
404 }
405 }
406
407 /**
408 * Add a breadcrumb for pagination
409 */
410 /*private function paged_trail() {
411 if ( get_query_var( 'paged' ) ) {
412 $this->add_crumb( sprintf( __( 'Page %d', 'learnpress' ), get_query_var( 'paged' ) ) );
413 }
414 }*/
415 }
416