PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7
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.1.7, at inc/class-lp-breadcrumb.php

411 lines 10.0 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 $this->paged_trail();
93
94 return $this->get_breadcrumb();
95 }
96
97 return array();
98 }
99
100 /**
101 * Prepend the courses page to courses breadcrumbs
102 */
103 private function prepend_courses_page() {
104 $courses_page_id = learn_press_get_page_id( 'courses' );
105 $courses_page = get_post( $courses_page_id );
106
107 // If permalinks contain the courses page in the URI prepend the breadcrumb with courses
108 if ( $courses_page_id && $courses_page /*&& strstr( $permalinks['lp_course_base'], '/' . $courses_page->post_name )*/ && get_option( 'page_on_front' ) != $courses_page_id ) {
109 $this->add_crumb( get_the_title( $courses_page ), get_permalink( $courses_page ) );
110 }
111 }
112
113 /**
114 * is home trail
115 */
116 private function add_crumbs_home() {
117 $this->add_crumb( single_post_title( '', false ) );
118 }
119
120 /**
121 * 404 trail
122 */
123 private function add_crumbs_404() {
124 $this->add_crumb( __( 'Error 404', 'learnpress' ) );
125 }
126
127 /**
128 * attachment trail
129 */
130 private function add_crumbs_attachment() {
131 global $post;
132
133 $this->add_crumbs_single( $post->post_parent, get_permalink( $post->post_parent ) );
134 $this->add_crumb( get_the_title(), get_permalink() );
135 }
136
137 /**
138 * Single post trail
139 *
140 * @param int $post_id
141 * @param string $permalink
142 */
143 private function add_crumbs_single( $post_id = 0, $permalink = '' ) {
144 if ( ! $post_id ) {
145 global $post;
146 } else {
147 $post = get_post( $post_id );
148 }
149 if ( LP_COURSE_CPT === get_post_type( $post ) ) {
150 $this->prepend_courses_page();
151
152 $terms = learn_press_get_course_terms(
153 $post->ID,
154 'course_category',
155 array(
156 'orderby' => 'parent',
157 'order' => 'DESC',
158 )
159 );
160
161 if ( $terms ) {
162 $main_term = apply_filters( 'learn_press_breadcrumb_main_term', $terms[0], $terms );
163 $this->term_ancestors( $main_term->term_id, 'course_category' );
164 $this->add_crumb( $main_term->name, get_term_link( $main_term ) );
165 }
166 } elseif ( 'post' != get_post_type( $post ) ) {
167 $post_type = get_post_type_object( get_post_type( $post ) );
168 $this->add_crumb( $post_type->labels->singular_name, get_post_type_archive_link( get_post_type( $post ) ) );
169 } else {
170 $cat = current( get_the_category( $post ) );
171 if ( $cat ) {
172 $this->term_ancestors( $cat->term_id, 'post_category' );
173 $this->add_crumb( $cat->name, get_term_link( $cat ) );
174 }
175 }
176
177 $this->add_crumb( get_the_title( $post ), $permalink );
178 }
179
180 /**
181 * Page trail
182 */
183 private function add_crumbs_page() {
184 global $post;
185
186 if ( $post->post_parent ) {
187 $parent_crumbs = array();
188 $parent_id = $post->post_parent;
189
190 while ( $parent_id ) {
191 $page = get_post( $parent_id );
192 $parent_id = $page->post_parent;
193 $parent_crumbs[] = array( get_the_title( $page->ID ), get_permalink( $page->ID ) );
194 }
195
196 $parent_crumbs = array_reverse( $parent_crumbs );
197
198 foreach ( $parent_crumbs as $crumb ) {
199 $this->add_crumb( $crumb[0], $crumb[1] );
200 }
201 }
202
203 $this->add_crumb( get_the_title(), get_permalink() );
204 $this->endpoint_trail();
205 }
206
207 /**
208 * Course category trail
209 */
210 private function add_crumbs_course_category() {
211 global $wp_query;
212 $current_term = $this->get_queried_object();
213
214 if ( isset( $current_term->term_id ) ) {
215 $this->prepend_courses_page();
216 $this->term_ancestors( $current_term->term_id, 'course_category' );
217 $this->add_crumb( $current_term->name );
218 }
219 }
220
221 /**
222 * Course tag trail
223 */
224 private function add_crumbs_course_tag() {
225 global $wp_query;
226 $current_term = $this->get_queried_object();
227
228 $this->prepend_courses_page();
229 $this->add_crumb( sprintf( __( 'Courses tagged &ldquo;%s&rdquo;', 'learnpress' ), $current_term->name ) );
230 }
231
232 /**
233 * Courses archive breadcrumb
234 */
235 private function add_crumbs_courses() {
236 $course_page_id = learn_press_get_page_id( 'courses' );
237 if ( get_option( 'page_on_front' ) == $course_page_id ) {
238 return;
239 }
240
241 $_name = $course_page_id ? get_the_title( $course_page_id ) : '';
242
243 if ( ! $_name ) {
244 $course_post_type = get_post_type_object( LP_COURSE_CPT );
245
246 if ( $course_post_type ) {
247 $_name = $course_post_type->labels->singular_name;
248 }
249 }
250
251 $this->add_crumb( $_name, get_post_type_archive_link( LP_COURSE_CPT ) );
252 }
253
254 /**
255 * Post type archive trail
256 */
257 private function add_crumbs_post_type_archive() {
258 $post_type = get_post_type_object( get_post_type() );
259
260 if ( $post_type ) {
261 $this->add_crumb( $post_type->labels->singular_name, get_post_type_archive_link( get_post_type() ) );
262 }
263 }
264
265 /**
266 * Category trail
267 */
268 private function add_crumbs_category() {
269 global $wp_query;
270 $this_category = get_category( $this->get_queried_object() );
271
272 if ( 0 != $this_category->parent ) {
273 $this->term_ancestors( $this_category->parent, 'post_category' );
274 $this->add_crumb( $this_category->name, get_category_link( $this_category->term_id ) );
275 }
276
277 $this->add_crumb( single_cat_title( '', false ), get_category_link( $this_category->term_id ) );
278 }
279
280 /**
281 * Tag trail
282 */
283 private function add_crumbs_tag() {
284 global $wp_query;
285
286 $queried_object = $this->get_queried_object();
287 $this->add_crumb( sprintf( __( 'Posts tagged &ldquo;%s&rdquo;', 'learnpress' ), single_tag_title( '', false ) ), get_tag_link( $queried_object->term_id ) );
288 }
289
290 /**
291 * Add crumbs for date based archives
292 */
293 private function add_crumbs_date() {
294 if ( is_year() || is_month() || is_day() ) {
295 $this->add_crumb( get_the_time( 'Y' ), get_year_link( get_the_time( 'Y' ) ) );
296 }
297 if ( is_month() || is_day() ) {
298 $this->add_crumb( get_the_time( 'F' ), get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) );
299 }
300 if ( is_day() ) {
301 $this->add_crumb( get_the_time( 'd' ) );
302 }
303 }
304
305 /**
306 * Add crumbs for date based archives
307 */
308 private function add_crumbs_tax() {
309 global $wp_query;
310
311 $this_term = $this->get_queried_object();
312 $taxonomy = get_taxonomy( $this_term->taxonomy );
313
314 $this->add_crumb( $taxonomy->labels->name );
315
316 if ( 0 != $this_term->parent ) {
317 $this->term_ancestors( $this_term->parent, 'post_category' );
318 $this->add_crumb( $this_term->name, get_term_link( $this_term->term_id, $this_term->taxonomy ) );
319 }
320
321 $this->add_crumb( single_term_title( '', false ), get_term_link( $this_term->term_id, $this_term->taxonomy ) );
322 }
323
324 /**
325 * Get default queried object by WP.
326 * Fixed issue when viewing course category the text in breadcrumb is blank.
327 *
328 * @since 3.0.0
329 *
330 * @return object
331 */
332 private function get_queried_object() {
333 static $default_queried_object;
334
335 if ( $default_queried_object ) {
336 return $default_queried_object;
337 }
338
339 global $wp_query;
340
341 // Store the current queried object
342 $queried_object = $wp_query->queried_object;
343 $queried_object_id = $wp_query->queried_object_id;
344
345 // Clear the current queried object to ensure we get the default.
346 $wp_query->queried_object = null;
347 $wp_query->queried_object_id = null;
348
349 $default_queried_object = $wp_query->get_queried_object();
350
351 // Restore current queried object
352 $wp_query->queried_object = $queried_object;
353 $wp_query->queried_object_id = $queried_object_id;
354
355 return $default_queried_object;
356 }
357
358 /**
359 * Add a breadcrumb for author archives
360 */
361 private function add_crumbs_author() {
362 global $author;
363
364 $userdata = get_userdata( $author );
365 $this->add_crumb( sprintf( __( 'Author: %s', 'learnpress' ), $userdata->display_name ) );
366 }
367
368 /**
369 * Add crumbs for a term
370 *
371 * @param string $taxonomy
372 */
373 private function term_ancestors( $term_id, $taxonomy ) {
374 $ancestors = get_ancestors( $term_id, $taxonomy );
375 $ancestors = array_reverse( $ancestors );
376
377 foreach ( $ancestors as $ancestor ) {
378 $ancestor = get_term( $ancestor, $taxonomy );
379
380 if ( ! is_wp_error( $ancestor ) && $ancestor ) {
381 $this->add_crumb( $ancestor->name, get_term_link( $ancestor ) );
382 }
383 }
384 }
385
386 /**
387 * Endpoints
388 */
389 private function endpoint_trail() {
390
391 }
392
393 /**
394 * Add a breadcrumb for search results
395 */
396 private function search_trail() {
397 if ( is_search() ) {
398 $this->add_crumb( sprintf( __( 'Search results for &ldquo;%s&rdquo;', 'learnpress' ), get_search_query() ), esc_url_raw( remove_query_arg( 'paged' ) ) );
399 }
400 }
401
402 /**
403 * Add a breadcrumb for pagination
404 */
405 private function paged_trail() {
406 if ( get_query_var( 'paged' ) ) {
407 $this->add_crumb( sprintf( __( 'Page %d', 'learnpress' ), get_query_var( 'paged' ) ) );
408 }
409 }
410 }
411