breadcrumbs.php
18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
<?php
namespace uncanny_learndash_toolkit;
/**
* Class learndashBreadcrumbs
* @package uncanny_custom_toolkit
*/
class Breadcrumbs extends Config implements RequiredFunctions {
/**
* Class constructor
*/
public function __construct() {
add_action( 'plugins_loaded', array( __CLASS__, 'run_frontend_hooks' ) );
}
/*
* Initialize frontend actions and filters
*/
public static function run_frontend_hooks() {
if ( true === self::dependants_exist() ) {
/* ADD FILTERS ACTIONS FUNCTION */
add_shortcode( 'uo_breadcrumbs', array( __CLASS__, 'uo_breadcrumbs' ) );
//Disable WP SEO breadcrumbs
add_filter( 'wpseo_breadcrumb_output', array( __CLASS__, 'wpseo_uo_breadcrumbs' ) );
// Enhance LD 3.x breadcrumb
add_filter( 'learndash_breadcrumbs', array( __CLASS__, 'uo_learndash_breadcrumbs' ) );
add_filter( 'learndash_breadcrumbs_keys', array( __CLASS__, 'uo_breadcrumbs_keys' ) );
}
}
/**
* Does the plugin rely on another function or plugin
*
* @return boolean || string Return either true or name of function or plugin
*
*/
public static function dependants_exist() {
/* Checks for LearnDash */
global $learndash_post_types;
if ( ! isset( $learndash_post_types ) ) {
return 'Plugin: LearnDash';
}
return true;
}
/**
* Description of class in Admin View
*
* @return array
*/
public static function get_details() {
$module_id = 'breadcrumbs';
$class_title = esc_html__( 'Breadcrumbs', 'uncanny-learndash-toolkit' );
$kb_link = 'https://www.uncannyowl.com/knowledge-base/learndash-breadcrumb-links/';
/* Sample Simple Description with shortcode */
$class_description = esc_html__( 'Easily add breadcrumb links that work with courses, lessons, topics and quizzes using a shortcode or template change. WooCommerce is also supported.', 'uncanny-learndash-toolkit' );
/* Icon as font awesome icon */
$class_icon = '<i class="uo_icon_fa fa fa-link"></i>';
$category = 'wordpress';
$type = 'free';
return array(
'id' => $module_id,
'title' => $class_title,
'type' => $type,
'category' => $category,
'kb_link' => $kb_link, // OR set as null not to display
'description' => $class_description,
'dependants_exist' => self::dependants_exist(),
'settings' => self::get_class_settings( $class_title ),
//'settings' => false,
'icon' => $class_icon,
);
}
/**
* HTML for modal to create settings
*
* @param String
*
* @return string Return either false or settings html modal
*
*/
public static function get_class_settings( $class_title ) {
$pages[] = array( 'value' => 0, 'text' => '-- Select Page --' );
$get_pages = get_pages(
array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
) );
foreach ( $get_pages as $page ) {
$pages[] = array( 'value' => $page->ID, 'text' => get_the_title( $page->ID ) );
}
// Create options
$options = array(
array(
'type' => 'text',
'label' => esc_html__( 'Dashboard Page Title', 'uncanny-learndash-toolkit' ),
'option_name' => 'uncanny-breadcrumbs-dashboard-text',
),
array(
'type' => 'select',
'label' => esc_html__( 'Dashboard Page', 'uncanny-learndash-toolkit' ),
'select_name' => 'uncanny-breadcrumbs-dashboard-link',
'options' => $pages,
),
array(
'type' => 'text',
'label' => esc_html__( 'Breadcrumbs Separator', 'uncanny-learndash-toolkit' ),
'option_name' => 'uncanny-breadcrumbs-dashboard-separator',
),
array(
'type' => 'checkbox',
'label' => esc_html__( 'Include Current Page', 'uncanny-learndash-toolkit' ),
'option_name' => 'uncanny-breadcrumbs-include-current-page',
),
);
// Build html
$html = self::settings_output(
array(
'class' => __CLASS__,
'title' => $class_title,
'options' => $options,
) );
return $html;
}
/**
* @return mixed
*/
public static function wpseo_uo_breadcrumbs() {
return self::uo_breadcrumbs();
}
/**
* @param bool $echo
*
* @return string
*/
public static function uo_breadcrumbs() {
global $wp_query;
//$wp_query = new WP_Query();
$learn_dash_labels = new \LearnDash_Custom_Label();
$course_label = $learn_dash_labels::get_label( 'courses' );
// Define main variables
$trail = array();
$trail[] = self::uo_build_anchor_links( get_bloginfo( 'url' ), esc_html__( 'Home', 'uncanny-learndash-toolkit' ), 'get-blog-info-url' );
//$dashboard_link = get_post_type_archive_link( 'sfwd-courses' );
$dashboard_link = '';
$dashboard_text = 'Dashboard';
$dashboard_separator = '»';
$get_dashboard_text = self::get_settings_value( 'uncanny-breadcrumbs-dashboard-text', __CLASS__ );
$get_dashboard_link = self::get_settings_value( 'uncanny-breadcrumbs-dashboard-link', __CLASS__ );
$get_dashboard_separator = self::get_settings_value( 'uncanny-breadcrumbs-dashboard-separator', __CLASS__ );
$get_include_current_page = self::get_settings_value( 'uncanny-breadcrumbs-include-current-page', __CLASS__ );
$course_archive_link = self::uo_build_anchor_links( get_post_type_archive_link( 'sfwd-courses' ), esc_html__( $course_label, 'uncanny-learndash-toolkit' ), 'course-archive-link' );
//$course_archive_link = self::uo_build_anchor_links( get_post_type_archive_link( 'sfwd-courses' ), esc_html__( 'Courses', 'uncanny-learndash-toolkit' ) );
if ( strlen( trim( $get_dashboard_text ) ) ) {
$dashboard_text = $get_dashboard_text;
}
if ( strlen( trim( $get_dashboard_link ) ) && '0' !== $get_dashboard_link ) {
$dashboard_link = get_permalink( $get_dashboard_link );
$dashboard_link = self::uo_build_anchor_links( $dashboard_link, $dashboard_text, 'dashboard-link-no-get-dashboard-link' );
}
if ( strlen( trim( $get_dashboard_separator ) ) ) {
$dashboard_separator = $get_dashboard_separator;
}
$lesson_id = false;
// If it's on home page
if ( is_front_page() ) {
$trail = array(); //Removing Single Home link from Homepage.
} elseif ( is_singular() ) {
// Get singular vars (page, post, attachments)
$post = $wp_query->get_queried_object();
$post_id = absint( $wp_query->get_queried_object_id() );
$post_type = $post->post_type;
if ( 'post' === $post_type ) {
$maybe_tax = self::uo_post_taxonomy( $post_id );
if ( false !== $maybe_tax ) {
$trail[] = $maybe_tax;
}
$trail[] = get_the_title( $post_id );
} elseif ( 'page' === $post_type ) {
// If Woocommerce is installed and being viewed, add shop page to cart, checkout pages
if ( class_exists( 'Woocommerce' ) ) {
if ( is_cart() || is_checkout() ) {
// Get shop page
if ( function_exists( 'wc_get_page_id' ) ) {
$shop_id = wc_get_page_id( 'shop' );
$shop_title = get_the_title( $shop_id );
if ( function_exists( 'wpml_object_id' ) ) {
$shop_title = get_the_title( wpml_object_id( $shop_id, 'page' ) );
}
// Shop page
if ( $shop_id && $shop_title ) {
$trail[] = self::uo_build_anchor_links( get_permalink( $shop_id ), $shop_title, 'woo-shop-page' );
}
}
}
if ( $get_include_current_page === 'on' ) {
$trail[] = '<span class="uo-current_page">' . get_the_title( $post_id ) . '</span>';
}
} else {
// Regular pages. See if the page has any ancestors. Add in the trail if ancestors are found
$ancestors = get_ancestors( $post_id, 'page' );
if ( ! empty ( $ancestors ) ) {
$ancestors = array_reverse( $ancestors );
foreach ( $ancestors as $page ) {
$trail[] = self::uo_build_anchor_links( get_permalink( $page ), get_the_title( $page ), 'regular-page-ancestors' );
}
}
if ( $get_include_current_page === 'on' ) {
$trail[] = '<span class="uo-current_page">' . get_the_title( $post_id ) . '</span>';
}
}
} elseif ( 'sfwd-courses' === $post_type ) {
// See if Single Course is being displayed.
if ( strlen( trim( $get_dashboard_link ) ) && '0' !== $get_dashboard_link ) {
$trail[] = $dashboard_link;
} else {
$trail[] = $course_archive_link;
}
if ( $get_include_current_page === 'on' ) {
$trail[] = '<span class="uo-current_page">' . get_the_title( $post_id ) . '</span>';
}
} elseif ( 'sfwd-lessons' === $post_type ) {
// See if Single Lesson is being displayed.
$course_id = learndash_get_course_id( $post_id ); // Getting Parent Course ID
if ( strlen( trim( $get_dashboard_link ) ) && '0' !== $get_dashboard_link ) {
$trail[] = $dashboard_link;
} else {
$trail[] = $course_archive_link;
}
$trail[] = self::uo_build_anchor_links( get_permalink( $course_id ), get_the_title( $course_id ), 'lessons-course-link' ); // Getting Lesson's Course Link
if ( $get_include_current_page === 'on' ) {
$trail[] = '<span class="uo-current_page">' . get_the_title( $post_id ) . '</span>';
}
} elseif ( 'sfwd-topic' === $post_type ) {
// See if single Topic is being displayed
$course_id = learndash_get_course_id( $post_id ); // Getting Parent Course ID
$lesson_id = learndash_get_lesson_id( $post_id, $course_id ); // Getting Parent Lesson ID
if ( strlen( trim( $get_dashboard_link ) ) && '0' !== $get_dashboard_link ) {
$trail[] = $dashboard_link;
} else {
$trail[] = $course_archive_link;
}
$trail[] = self::uo_build_anchor_links( get_permalink( $course_id ), get_the_title( $course_id ), 'topics-course-link' ); // Getting Lesson's Course Link
$trail[] = self::uo_build_anchor_links( get_permalink( $lesson_id ), get_the_title( $lesson_id ), 'topics-lesson-link' ); // Getting Topics's Lesson Link
if ( $get_include_current_page === 'on' ) {
$trail[] = '<span class="uo-current_page">' . get_the_title( $post_id ) . '</span>';
}
} elseif ( 'sfwd-quiz' === $post_type ) {
// See if quiz is being displayed
$course_id = learndash_get_course_id( $post_id ); // Getting Parent Course ID
if ( strlen( trim( $get_dashboard_link ) ) && '0' !== $get_dashboard_link ) {
$trail[] = $dashboard_link;
} else {
$trail[] = $course_archive_link;
}
$topic_id = learndash_get_lesson_id( $post_id, $course_id ); // Getting Parent Topic/Lesson ID
if ( 'sfwd-topic' === get_post_type( $topic_id ) ) {
$lesson_id = learndash_get_lesson_id( $topic_id, $course_id ); // Getting Parent Lesson ID
} else {
// detect topic id & grab lesson id
$parent_ids = learndash_course_get_all_parent_step_ids( $course_id, $post_id );
if ( ! empty( $parent_ids ) ) {
foreach ( $parent_ids as $parent_id ) {
if ( get_post_type( $parent_id ) === learndash_get_post_type_slug( 'topic' ) ) {
$topic_id = $parent_id;
$lesson_id = learndash_get_lesson_id( $topic_id, $course_id ); // Getting Parent Lesson ID
break;
}
}
}
}
$trail[] = self::uo_build_anchor_links( get_permalink( $course_id ), get_the_title( $course_id ), 'quizs-course-link' ); // Getting Lesson's Course Link
//If $lesson_id is false, the quiz is associated with a lesson and course but not a topic.
if ( $lesson_id ) {
$trail[] = self::uo_build_anchor_links( get_permalink( $lesson_id ), get_the_title( $lesson_id ), 'quizs-lesson-link' ); // Getting Topics's Lesson Link
}
//If $topic_id is false, the quiz is associated with a course but not associated with any lessons or topics.
if ( $topic_id ) {
$trail[] = self::uo_build_anchor_links( get_permalink( $topic_id ), get_the_title( $topic_id ), 'quizs-topic-link' );
}
if ( $get_include_current_page === 'on' ) {
$trail[] = '<span class="uo-current_page">' . get_the_title( $post_id ) . '</span>';
}
} else {
// Add shop page to single product
if ( 'product' === $post_type ) {
// Get shop page
if ( class_exists( 'Woocommerce' ) && function_exists( 'wc_get_page_id' ) ) {
$shop_id = wc_get_page_id( 'shop' );
$shop_title = get_the_title( $shop_id );
if ( function_exists( 'wpml_object_id' ) ) {
$shop_title = get_the_title( wpml_object_id( $shop_id, 'page' ) );
}
// Shop page
if ( $shop_id && $shop_title ) {
$trail[] = self::uo_build_anchor_links( get_permalink( $shop_id ), $shop_title, 'products-woo-shop-page' );
}
}
}
// Getting terms of the post.
if ( self::lms_get_taxonomy( $post_id, $post_type ) ) {
$trail[] = self::lms_get_taxonomy( $post_id, $post_type );
}
if ( $get_include_current_page === 'on' ) {
$trail[] = '<span class="uo-current_page">' . get_the_title( $post_id ) . '</span>';
}
}
}
// If it's an Archive
if ( is_archive() ) {
//Ignore if Courses & Products
if ( ! is_post_type_archive( 'sfwd-courses' ) && ! is_post_type_archive( 'product' ) ) {
if ( is_category() || is_tax() ) {
$trail[] = single_cat_title( '', false ); // If its Blog Category
}
if ( is_day() ) {
$trail[] = get_the_date(); // If its Single Day Archive
}
if ( is_month() ) {
$trail[] = get_the_date( __( 'F Y', 'uncanny-learndash-toolkit' ) ) . esc_html__( ' Archives', 'uncanny-learndash-toolkit' ); // If Mothly Archives
}
if ( is_year() ) {
$trail[] = get_the_date( __( 'Y', 'uncanny-learndash-toolkit' ) ) . esc_html__( ' Archives', 'uncanny-learndash-toolkit' ); // If its Yearly Archives
}
if ( is_author() ) {
$trail[] = get_the_author(); // If its Author's Archives
}
} elseif ( is_post_type_archive( 'sfwd-courses' ) ) {
$trail[] = esc_html__( $course_label, 'uncanny-learndash-toolkit' );
} elseif ( is_post_type_archive( 'product' ) ) {
$trail[] = esc_html__( 'Shop', 'uncanny-learndash-toolkit' );
}
}
if ( is_search() ) {
$trail[] = esc_html__( 'Search', 'uncanny-learndash-toolkit' );
$trail[] = get_search_query();
}
// Build breadcrumbs
$classes = 'sfwd-breadcrumbs clr';
if ( array_key_exists( 'the_content', $GLOBALS['wp_filter'] ) ) {
$classes .= ' lms-breadcrumbs ';
}
// Open breadcrumbs
$breadcrumb = '<nav aria-label="' . esc_html__( 'Breadcrumb', 'uncanny-learndash-toolkit' ) . '" class="' . esc_attr( $classes ) . '"><div class="breadcrumb-trail">';
// Separator HTML
$separator = '<span class="sep"> ' . stripslashes( $dashboard_separator ) . ' </span>';
// Join all trail items into a string
$breadcrumb .= implode( $separator, $trail );
// Close breadcrumbs
$breadcrumb .= '</div></nav>';
return $breadcrumb;
}
/**
* @param $permalink
* @param $title
* @param $type
*
* @return mixed
*/
public static function uo_build_anchor_links( $permalink, $title, $type = '' ) {
$link = sprintf(
'<span itemscope="" itemtype="http://schema.org/Breadcrumb"><a href="%1$s" title="%2$s" rel="%3$s" class="trail-begin"><span itemprop="%2$s">%4$s</span></a></span>',
esc_url( $permalink ),
esc_attr( $title ),
sanitize_title( $title ),
esc_html( $title )
);
return apply_filters( 'uo_build_anchor_links', $link, $permalink, $title, $type );
}
/**
* @param $post_id
* @param string $taxonomy
*
* @return bool
*/
public static function uo_post_taxonomy( $post_id, $taxonomy = 'category' ) {
$terms = get_the_terms( $post_id, $taxonomy );
$t = array();
if ( $terms ) {
foreach ( $terms as $term ) {
$t[] = self::uo_build_anchor_links( get_term_link( $term->slug, $taxonomy ), $term->name );
}
return implode( ' / ', $t );
} else {
return false;
}
}
/**
* @param $post_id
* @param $post_type
*
* @return bool
*/
public static function lms_get_taxonomy( $post_id, $post_type ) {
$taxonomies = get_taxonomies( array( 'object_type' => array( $post_type ) ), 'objects' );
$tax = array();
if ( $taxonomies ) {
foreach ( $taxonomies as $taxonomy ) {
// Pass the $taxonomy name to uo_post_taxonomy to return with proper terms and links
$tax[] = self::uo_post_taxonomy( $post_id, $taxonomy->query_var );
}
return implode( ' / ', $tax );
} else {
return false;
}
}
/**
* @return null
*/
public static function uo_get_the_id() {
// If singular get_the_ID
if ( is_singular() ) {
return get_the_ID();
} // Get ID of WooCommerce product archive
elseif ( is_post_type_archive( 'product' ) && class_exists( 'Woocommerce' ) && function_exists( 'wc_get_page_id' ) ) {
$shop_id = wc_get_page_id( 'shop' );
if ( isset( $shop_id ) ) {
return wc_get_page_id( 'shop' );
}
} // Posts page
elseif ( is_home() && $page_for_posts = get_option( 'page_for_posts' ) ) {
return $page_for_posts;
} // Return nothing
else {
return null;
}
return null;
}
/**
* @param $keys
*
* @since 3.2
*
* @return array
*/
public static function uo_breadcrumbs_keys( $keys ){
$dashboard_link = '';
$dashboard_text = 'Dashboard';
$get_dashboard_text = self::get_settings_value( 'uncanny-breadcrumbs-dashboard-text', __CLASS__ );
$get_dashboard_link = self::get_settings_value( 'uncanny-breadcrumbs-dashboard-link', __CLASS__ );
$get_dashboard_separator = self::get_settings_value( 'uncanny-breadcrumbs-dashboard-separator', __CLASS__ );
if ( strlen( trim( $get_dashboard_text ) ) ) {
$dashboard_text = $get_dashboard_text;
}
if ( strlen( trim( $get_dashboard_link ) ) && '0' !== $get_dashboard_link ) {
$dashboard_link = get_permalink( $get_dashboard_link );
}
if ( ! empty( $dashboard_link ) && ! empty( $dashboard_text ) ) {
$keys = array_merge(['dashboard'],$keys);
}
return $keys;
}
/**
* @param $breadcrumbs
*
* @since 3.2
*
* @return array
*/
public static function uo_learndash_breadcrumbs( $breadcrumbs ){
$dashboard_link = '';
$dashboard_text = 'Dashboard';
$get_dashboard_text = self::get_settings_value( 'uncanny-breadcrumbs-dashboard-text', __CLASS__ );
$get_dashboard_link = self::get_settings_value( 'uncanny-breadcrumbs-dashboard-link', __CLASS__ );
$get_dashboard_separator = self::get_settings_value( 'uncanny-breadcrumbs-dashboard-separator', __CLASS__ );
if ( strlen( trim( $get_dashboard_text ) ) ) {
$dashboard_text = $get_dashboard_text;
}
if ( strlen( trim( $get_dashboard_link ) ) && '0' !== $get_dashboard_link ) {
$dashboard_link = get_permalink( $get_dashboard_link );
}
if ( ! empty( $dashboard_link ) && ! empty( $dashboard_text ) ) {
$breadcrumbs['dashboard'] = [
'permalink' => $dashboard_link,
'title' => $dashboard_text,
];
}
return $breadcrumbs;
}
}