forked from LearnPress/learnpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-lp-page-controller.php
1056 lines (889 loc) · 28.7 KB
/
class-lp-page-controller.php
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Class LP_Page_Controller
*/
class LP_Page_Controller {
protected static $_instance = null;
/**
* Store the object has queried by WP.
*
* @var int
*/
//protected $_queried_object = 0;
/**
* @var int
*/
//protected $_filter_content_priority = 10000;
/**
* Flag for 404 content.
*
* @var bool
*/
protected $_is_404 = false;
/**
* LP_Page_Controller constructor.
*/
protected function __construct() {
add_filter( 'post_type_archive_link', [ $this, 'link_archive_course' ], 10, 2 );
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), -1 );
add_filter( 'template_include', array( $this, 'template_loader' ), 10 );
add_filter( 'template_include', array( $this, 'check_pages' ), 30 );
add_filter( 'template_include', array( $this, 'auto_shortcode' ), 50 );
add_filter( 'the_post', array( $this, 'setup_data_for_item_course' ) );
add_filter( 'request', array( $this, 'remove_course_post_format' ), 1 );
add_shortcode( 'learn_press_archive_course', array( $this, 'archive_content' ) );
add_filter( 'pre_get_document_title', array( $this, 'set_title_pages' ), 20, 1 );
// Yoast seo
add_filter( 'wpseo_opengraph_desc', array( $this, 'lp_desc_item_yoast_seo' ), 11, 1 );
add_filter( 'wpseo_metadesc', array( $this, 'lp_desc_item_yoast_seo' ), 11, 1 );
// edit link item course when form search default wp
add_filter( 'post_type_link', array( $this, 'post_type_link' ), 10, 2 );
// Set link profile to admin menu
//add_action( 'admin_bar_menu', array( $this, 'learn_press_edit_admin_bar' ) );
// Web hook detected PayPal request.
add_action( 'init', [ $this, 'check_webhook_paypal_ipn' ] );
// Set again x-wp-nonce on header when has cache with not login.
add_filter( 'rest_send_nocache_headers', array( $this, 'check_x_wp_nonce_cache' ) );
}
/**
* Set link archive course.
*
* @param string $link
* @param string $post_type
*
* @return string
*/
public function link_archive_course( string $link, string $post_type ): string {
if ( $post_type == LP_COURSE_CPT && learn_press_get_page_id( 'courses' ) ) {
$link = learn_press_get_page_link( 'courses' );
}
return $link;
}
/**
* Set link item course when form search default wp
*
* @param string $post_link
* @param object $post
*/
public function post_type_link( $post_link, $post ) {
// Set item's course permalink
$course_item_types = learn_press_get_course_item_types();
$item_id = $post->ID;
if ( in_array( $post->post_type, $course_item_types ) ) {
$section_id = LP_Section_DB::getInstance()->get_section_id_by_item_id( $item_id );
if ( ! $section_id ) {
return $post_link;
}
$course_id = LP_Section_DB::getInstance()->get_course_id_by_section( $section_id );
if ( ! $course_id ) {
return $post_link;
}
$course = learn_press_get_course( $course_id );
if ( ! $course ) {
return $post_link;
}
$post_link = $course->get_item_link( $item_id );
} elseif ( LP_COURSE_CPT === $post->post_type ) {
// Abort early if the placeholder rewrite tag isn't in the generated URL
if ( false === strpos( $post_link, '%' ) ) {
return $post_link;
}
// Get the custom taxonomy terms in use by this post
$terms = get_the_terms( $post->ID, 'course_category' );
if ( ! empty( $terms ) ) {
$terms = _learn_press_usort_terms_by_ID( $terms ); // order by ID
$category_object = apply_filters(
'learn_press_course_post_type_link_course_category',
$terms[0],
$terms,
$post
);
$category_object = get_term( $category_object, 'course_category' );
$course_category = $category_object->slug;
$parent = $category_object->parent;
if ( $parent ) {
$ancestors = get_ancestors( $category_object->term_id, 'course_category' );
foreach ( $ancestors as $ancestor ) {
$ancestor_object = get_term( $ancestor, 'course_category' );
$course_category = $ancestor_object->slug . '/' . $course_category;
}
}
} else {
// If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
$course_category = _x( 'uncategorized', 'slug', 'learnpress' );
}
$find = array(
'%year%',
'%monthnum%',
'%day%',
'%hour%',
'%minute%',
'%second%',
'%post_id%',
'%category%',
'%course_category%',
);
$replace = array(
date_i18n( 'Y', strtotime( $post->post_date ) ),
date_i18n( 'm', strtotime( $post->post_date ) ),
date_i18n( 'd', strtotime( $post->post_date ) ),
date_i18n( 'H', strtotime( $post->post_date ) ),
date_i18n( 'i', strtotime( $post->post_date ) ),
date_i18n( 's', strtotime( $post->post_date ) ),
$post->ID,
$course_category,
$course_category,
);
$post_link = str_replace( $find, $replace, $post_link );
}
return $post_link;
}
private function has_block_template( $template_name ) {
if ( ! $template_name ) {
return false;
}
$has_template = false;
$template_name = str_replace( 'course', LP_COURSE_CPT, $template_name );
$template_filename = $template_name . '.html';
// Since Gutenberg 12.1.0, the conventions for block templates directories have changed,
// we should check both these possible directories for backwards-compatibility.
$possible_templates_dirs = array( 'templates', 'block-templates' );
// Combine the possible root directory names with either the template directory
// or the stylesheet directory for child themes, getting all possible block templates
// locations combinations.
$filepath = DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $template_filename;
$legacy_filepath = DIRECTORY_SEPARATOR . 'block-templates' . DIRECTORY_SEPARATOR . $template_filename;
$possible_paths = array(
get_stylesheet_directory() . $filepath,
get_stylesheet_directory() . $legacy_filepath,
get_template_directory() . $filepath,
get_template_directory() . $legacy_filepath,
);
// Check the first matching one.
foreach ( $possible_paths as $path ) {
if ( is_readable( $path ) ) {
$has_template = true;
break;
}
}
/**
* Filters the value of the result of the block template check.
*
* @since x.x.x
*
* @param boolean $has_template value to be filtered.
* @param string $template_name The name of the template.
*/
return (bool) apply_filters( 'learnpress_has_block_template', $has_template, $template_name );
}
/**
* Set title of pages
*
* 1. Title course archive page
* 2. Title item of course
* 3. Title page Profile
*
* @param string $title
*
* @return string
* @author tungnx
* @since 3.2.7.7
* @version 1.0.1
*/
public function set_title_pages( $title = '' ): string {
global $wp_query;
$flag_title_course = false;
$course_archive_page_id = LP_Settings::get_option( 'courses_page_id', 0 );
// Set title single course.
if ( learn_press_is_course() ) {
$item = LP_Global::course_item();
if ( $item ) {
$title = apply_filters( 'learn-press/document-course-title-parts', get_the_title() . ' → ' . $item->get_title(), $item );
$flag_title_course = true;
}
} elseif ( learn_press_is_courses() ) { // Set title course archive page.
if ( learn_press_is_search() ) {
$title = __( 'Course Search Results', 'learnpress' );
} else {
$title = $course_archive_page_id ? get_the_title( $course_archive_page_id ) : __( 'Courses', 'learnpress' );
}
$flag_title_course = true;
} elseif ( learn_press_is_profile() ) {
$profile = LP_Profile::instance();
$tab_slug = $profile->get_current_tab();
$tab = $profile->get_tab_at( $tab_slug );
$page_id = learn_press_get_page_id( 'profile' );
if ( $page_id ) {
$page_title = get_the_title( $page_id );
} else {
$page_title = '';
}
if ( $tab instanceof LP_Profile_Tab ) {
$title = join(
' ',
apply_filters(
'learn-press/document-profile-title-parts',
array(
$page_title,
'→',
$tab->get( 'title' ),
)
)
);
}
$flag_title_course = true;
}
if ( $flag_title_course ) {
$title .= ' - ' . get_bloginfo( 'name', 'display' );
}
if ( empty( $title ) ) {
$title = get_bloginfo( 'name', 'display' );
}
return apply_filters( 'learn-press/title-page', $title );
}
/**
* Set description of course's item for yoast seo
*
* @param $desc
*
* @return mixed
* @author tungnx
* @since 3.2.7.9
*/
public function lp_desc_item_yoast_seo( $desc ) {
if ( learn_press_is_course() ) {
$item = LP_Global::course_item();
if ( empty( $item ) ) {
return $desc;
}
$desc = get_post_meta( $item->get_id(), '_yoast_wpseo_metadesc', true );
}
return $desc;
}
public function check_pages( $template ) {
if ( learn_press_is_checkout() ) {
$available_gateways = LP_Gateways::instance()->get_available_payment_gateways();
if ( ! $available_gateways ) {
learn_press_add_message( __( 'No payment method is available.', 'learnpress' ), 'error' );
}
} else {
global $wp_query;
$logout_slug = learn_press_profile_logout_slug();
if ( $logout_slug && ( $wp_query->get( 'view' ) === $logout_slug ) ) {
wp_safe_redirect( str_replace( '&', '&', wp_logout_url( learn_press_get_page_link( 'profile' ) ) ) );
exit;
}
}
return $template;
}
/**
* Auto inserting a registered shortcode to a specific page
* if that page is viewing in single mode.
*
* @param string $template
*
* @return string;
* @since 3.3.0
*/
public function auto_shortcode( $template ) {
global $post;
$the_post = $post;
if ( $the_post && is_page( $the_post->ID ) ) {
// Filter here to insert the shortcode
$auto_shortcodes = apply_filters( 'learn-press/auto-shortcode-pages', array() );
if ( ! empty( $auto_shortcodes[ $the_post->ID ] ) ) {
$shortcode_tag = $auto_shortcodes[ $the_post->ID ];
preg_match( '/\[' . $shortcode_tag . '\s?(.*)\]/', $the_post->post_content, $results );
if ( empty( $results ) ) {
$content = $the_post->post_content . "[$shortcode_tag]";
$the_post->post_content = $content;
}
}
}
return $template;
}
/**
* Load data for item of course
*
* @param $post
*
* @return mixed
* @editor tungnx
* Todo: should remove this function when load true type post's item
*/
public function setup_data_for_item_course( $post ): WP_Post {
/**
* @var WP $wp
* @var WP_Query $wp_query
* @var LP_Course $lp_course
* @var LP_Course_Item|LP_Quiz|LP_Lesson $lp_course_item
*/
global $wp, $wp_query, $lp_course_item;
if ( LP_COURSE_CPT !== $post->post_type ) {
return $post;
}
$course = learn_press_get_course();
if ( ! $course ) {
return $post;
}
/**
* @deprecated v4.1.6.1 LearnPress::instance()->global['course'], $GLOBALS['course']
* Some theme still use: global $course; LearnPress::instance()->global['course']
*/
//LearnPress::instance()->global['course'] = $GLOBALS['course'] = $GLOBALS['lp_course'] = $course;
LearnPress::instance()->global['course'] = $GLOBALS['course'] = $course;
if ( wp_verify_nonce( LP_Request::get( 'preview' ), 'preview-' . $post->ID ) ) {
$GLOBALS['preview_course'] = $post->ID;
}
$vars = $wp->query_vars;
if ( empty( $vars['course-item'] ) ) {
return $post;
}
if ( ! $wp_query->is_main_query() ) {
return $post;
}
try {
$user = learn_press_get_current_user();
// If item name is set in query vars
if ( ! is_numeric( $vars['course-item'] ) ) {
$item_type = $vars['item-type'];
$post_item = learn_press_get_post_by_name( $vars['course-item'], $item_type );
} else {
$post_item = get_post( absint( $vars['course-item'] ) );
}
if ( ! $post_item ) {
return $post;
}
$lp_course_item = apply_filters( 'learn-press/single-course-request-item', $course->get_item( $post_item->ID ) );
if ( ! $lp_course_item ) {
return $post;
}
// Set item viewing
$user->set_viewing_item( $lp_course_item );
} catch ( Exception $ex ) {
learn_press_add_message( $ex->getMessage(), 'error' );
}
return $post;
}
/**
* Set page 404
*
* @return mixed
* @editor tungnx
* @reason not use
* @deprecated 4.0.0
*/
/*
public function set_404( $is_404 ) {
global $wp_query;
$wp_query->is_404 = $this->_is_404 = (bool) $is_404;
}*/
public function is_404() {
return apply_filters( 'learn-press/query/404', $this->_is_404 );
}
/**
* In preview mode, if there is a 'post_format' in query var
* wp check and replace our post-type to post. This make preview
* course item become 404
*
* @param $qv
*
* @return mixed
*/
public function remove_course_post_format( $qv ) {
if ( ! empty( $qv['post_type'] ) && LP_COURSE_CPT === $qv['post_type'] ) {
if ( ! empty( $qv['post_format'] ) ) {
unset( $qv['post_format'] );
}
}
return $qv;
}
/**
* @return bool
*/
protected function _is_archive() {
return learn_press_is_courses() || learn_press_is_course_tag() || learn_press_is_course_category() || learn_press_is_search() || learn_press_is_course_tax();
}
/**
* @return bool
*/
protected function _is_single() {
return learn_press_is_course() && is_single();
}
/**
* Load content of course depending on query.
*
* @param string $template
*
* @return bool|string
*/
public function template_loader( $template ) {
if ( is_embed() ) {
return $template;
}
// $this->_maybe_redirect_courses_page();
$default_template = $this->get_page_template();
if ( $default_template ) {
$templates = $this->get_page_templates( $default_template );
/**
* Disable override templates in theme by default since LP 4.0.0
*/
if ( learn_press_override_templates() ) {
$new_template = locate_template( $templates );
}
if ( ! isset( $new_template ) || ! $new_template ) {
$new_template = LP_TEMPLATE_PATH . $default_template;
}
$template = $new_template;
}
return $template;
}
/**
* Get the default filename for a template.
*
* @return string
* @since 4.0.0
*/
private function get_page_template() {
$page_template = '';
if ( is_singular( LP_COURSE_CPT ) ) {
if ( ! self::has_block_template( 'single-course' ) ) {
$page_template = 'single-course.php';
}
if ( $this->_is_single() ) {
global $post;
setup_postdata( $post );
$course_item = LP_Global::course_item();
if ( $course_item && ! self::has_block_template( 'content-single-course-item' ) ) {
$page_template = 'content-single-item.php';
}
}
} elseif ( learn_press_is_course_taxonomy() ) {
$object = get_queried_object();
if ( is_tax( 'course_category' ) || is_tax( 'course_tag' ) ) {
$page_template = 'taxonomy-' . $object->taxonomy . '.php';
if ( self::has_block_template( 'taxonomy-' . $object->taxonomy ) ) {
$page_template = '';
} elseif ( ! file_exists( learn_press_locate_template( $page_template ) ) && ! self::has_block_template( 'archive-course' ) ) {
$page_template = 'archive-course.php';
}
} elseif ( ! self::has_block_template( 'archive-course' ) ) {
$page_template = 'archive-course.php';
}
} elseif ( ( is_post_type_archive( LP_COURSE_CPT ) || ( ! empty( learn_press_get_page_id( 'courses' ) ) && is_page( learn_press_get_page_id( 'courses' ) ) ) ) && ! self::has_block_template( 'archive-course' ) ) {
$page_template = 'archive-course.php';
} elseif ( learn_press_is_checkout() && ! self::has_block_template( 'page-lp_checkout' ) ) {
$page_template = 'pages/checkout.php';
}
return apply_filters( 'learn-press/page-template', $page_template );
}
private function get_page_templates( $default_template ) {
$templates = apply_filters( 'learn-press/page-templates', array(), $default_template );
if ( is_page_template() ) {
$page_template = get_page_template_slug();
if ( $page_template ) {
$validated_file = validate_file( $page_template );
if ( 0 === $validated_file ) {
$templates[] = $page_template;
} else {
error_log( "LearnPress: Unable to validate template path: \"$page_template\". Error Code: $validated_file." );
}
}
}
if ( is_singular( LP_COURSE_CPT ) ) {
$object = get_queried_object();
$name_decoded = urldecode( $object->post_name );
if ( $name_decoded !== $object->post_name ) {
$templates[] = "single-course-$name_decoded.php";
}
$templates[] = "single-product-$object->post_name.php";
}
if ( learn_press_is_course_taxonomy() ) {
$object = get_queried_object();
$templates[] = 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
$templates[] = learn_press_template_path() . '/taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
$templates[] = 'taxonomy-' . $object->taxonomy . '.php';
$templates[] = learn_press_template_path() . '/taxonomy-' . $object->taxonomy . '.php';
}
$templates[] = $default_template;
$templates[] = learn_press_template_path() . '/' . $default_template;
return array_unique( $templates );
}
/**
* Filter to allow search more templates in theme for wp page template hierarchy.
* Theme twentytwenty used 'singular.php' instead of 'page.php'
*
* @param array $templates
*
* @return array
* @since 3.x.x
* @deprecated 4.1.6.9.2
*/
/*public function page_template_hierarchy( $templates ) {
$templates = array_merge( $templates, array( 'singular.php' ) );
return $templates;
}*/
/**
* Archive course content.
*
* @return false|string
*/
public function archive_content() {
ob_start();
learn_press_get_template( 'content-archive-course.php' );
return ob_get_clean();
}
/**
* @param $title
*
* @return mixed
*/
public function page_title( $title ) {
global $wp_query;
if ( ! empty( $wp_query->queried_object_id ) ) {
$title['title'] = get_the_title( $wp_query->queried_object_id );
}
return $title;
}
/**
* Query courses if page is archive courses
*
* @param $q WP_Query
*
* @return WP_Query
* @editor tungnx
* @since 3.0.0
* @version 4.1.3
* @throws Exception
*/
public function pre_get_posts( WP_Query $q ): WP_Query {
// Affect only the main query and not in admin
if ( ! $q->is_main_query() && ! is_admin() ) {
return $q;
}
try {
if ( LP_Page_Controller::page_current() === LP_PAGE_COURSES ) {
if ( ( LP_Settings_Courses::is_ajax_load_courses() && ! LP_Settings_Courses::is_no_load_ajax_first_courses() ) ) {
LearnPress::instance()->template( 'course' )->remove_callback( 'learn-press/after-courses-loop', 'loop/course/pagination.php', 10 );
/**
* If page is archive course - query set posts_per_page = 1
* For fastest - because when page loaded - call API to load list courses
*
* Current, apply only for LP, not apply for theme Thimpress, because theme override
*/
$q->set( 'posts_per_page', 1 );
$q->set( 'posts_per_archive_page', 1 );
$q->set( 'nopaging', true );
} else {
$filter = new LP_Course_Filter();
$filter->only_fields = [ 'ID' ];
$filter->limit = -1;
$is_need_check_in_arr = false;
$limit = LP_Settings::get_option( 'archive_course_limit', 6 );
$q->set( 'posts_per_page', $limit );
// $q->set( 'cache_results', true ); // it default true
// Search courses by keyword
// $q->set( 's', $_REQUEST['c_search'] ?? '' );
if ( ! empty( $_REQUEST['c_search'] ) ) {
$filter->post_title = LP_Helper::sanitize_params_submitted( $_REQUEST['c_search'] );
$is_need_check_in_arr = true;
}
$author_ids_str = LP_Helper::sanitize_params_submitted( $_REQUEST['c_authors'] ?? 0 );
if ( ! empty( $author_ids_str ) ) {
$q->set( 'author', $author_ids_str );
}
// Meta query
$meta_query = [];
if ( isset( $_REQUEST['sort_by'] ) ) {
$sort_by = LP_Helper::sanitize_params_submitted( $_REQUEST['sort_by'] );
if ( 'on_paid' === $sort_by ) {
$meta_query[] = array(
'key' => '_lp_price',
'value' => 0,
'compare' => '>',
);
}
if ( 'on_free' === $sort_by ) {
$meta_query[] = array(
'key' => '_lp_price',
'value' => 0,
'compare' => '=',
);
}
}
$q->set( 'meta_query', $meta_query );
// End Meta query
// Tax query
$tax_query = [];
$term_ids_str = LP_Helper::sanitize_params_submitted( urldecode( $_REQUEST['term_id'] ?? '' ) );
if ( ! empty( $term_ids_str ) ) {
$term_ids = explode( ',', $term_ids_str );
$tax_query[] = array(
'taxonomy' => 'course_category',
'field' => 'term_id',
'terms' => $term_ids,
'operator' => 'IN',
);
}
$q->set( 'tax_query', $tax_query );
// End Tax query
// Author query
if ( isset( $_REQUEST['c_author'] ) ) {
$author_ids = LP_Helper::sanitize_params_submitted( $_REQUEST['c_author'] );
$q->set( 'author__in', $author_ids );
}
// End Author query
// Order query
if ( isset( $_REQUEST['order_by'] ) ) {
$order_by = LP_Helper::sanitize_params_submitted( $_REQUEST['order_by'] );
$order = 'DESC';
switch ( $order_by ) {
case 'post_title':
$order_by = 'title';
$order = 'ASC';
break;
case 'popular':
$filter->order_by = 'popular';
$order_by = 'post__in';
$is_need_check_in_arr = true;
break;
default:
$order_by = 'date';
break;
}
$q->set( 'orderby', $order_by );
$q->set( 'order', $order );
}
if ( $is_need_check_in_arr ) {
$posts_in = LP_Course::get_courses( $filter );
if ( ! empty( $posts_in ) ) {
$posts_in = LP_Database::get_values_by_key( $posts_in );
$q->set( 'post__in', $posts_in );
} else {
$q->set( 'post__in', 0 );
}
}
$q = apply_filters( 'lp/page-courses/query/legacy', $q );
}
return $q;
}
// Exclude item not assign
if ( $q->is_search() ) {
// Exclude item not assign any course
$course_item_types = learn_press_get_course_item_types();
$list_ids_exclude = array();
foreach ( $course_item_types as $item_type ) {
$filter = new LP_Post_Type_Filter();
$filter->post_type = $item_type;
$exclude_item = LP_Course_DB::getInstance()->get_item_ids_unassigned( $filter );
$exclude_item = LP_Course_DB::get_values_by_key( $exclude_item );
$list_ids_exclude = array_merge( $list_ids_exclude, $exclude_item );
}
// Exclude question not assign any quiz
$question_ids = LP_Question_DB::getInstance()->get_questions_not_assign_quiz();
$question_ids = LP_Course_DB::get_values_by_key( $question_ids );
$list_ids_exclude = array_merge( $list_ids_exclude, $question_ids );
if ( ! empty( $list_ids_exclude ) ) {
$q->set( 'post__not_in', $list_ids_exclude );
}
return $q;
}
// Handle 404 if user are viewing course item directly.
$this->set_link_item_course_default_wp_to_page_404( $q );
} catch ( Throwable $e ) {
error_log( $e->getMessage() );
}
return $q;
}
/**
* Handle 404 if user are viewing course item directly.
* Example: http://example.com/lesson/slug-lesson
* Apply for user not admin, instructor, co-instructor
*
* @param WP_Query $q
* @editor tungnx
* @since 3.2.7.5
*/
public function set_link_item_course_default_wp_to_page_404( $q ) {
$post_type_apply_404 = apply_filters( 'lp/page-controller/', array( LP_LESSON_CPT, LP_QUIZ_CPT, LP_QUESTION_CPT, 'lp_assignment' ) );
if ( ! isset( $q->query_vars['post_type'] ) || ! in_array( $q->query_vars['post_type'], $post_type_apply_404 ) ) {
return $q;
}
try {
$flag_load_404 = true;
$user = wp_get_current_user();
$post_author = 0;
if ( $user ) {
if ( isset( $_GET['preview_id'] ) ) {
$post_id = absint( $_GET['preview_id'] );
$post = get_post( $post_id );
$post_author = $post->post_author;
} elseif ( isset( $_GET['preview'] ) && isset( $_GET['p'] ) ) {
$post_id = absint( $_GET['p'] );
$post = get_post( $post_id );
$post_author = $post->post_author;
} else {
$post_author = LP_Database::getInstance()->getPostAuthorByTypeAndSlug( $q->query_vars['post_type'] ?? '', $q->query_vars['name'] ?? '' );
}
if ( $user->has_cap( 'administrator' ) ) {
$flag_load_404 = false;
} elseif ( $user->has_cap( LP_TEACHER_ROLE ) && $post_author == $user->ID ) {
$flag_load_404 = false;
}
}
$flag_load_404 = apply_filters( 'learnpress/page/set-link-item-course-404', $flag_load_404, $post_author, $user );
if ( $flag_load_404 ) {
learn_press_404_page();
}
} catch ( Throwable $e ) {
error_log( $e->getMessage() );
}
}
/**
* @deprecated 4.1.6.9.2
*/
/*public function the_content_callback( $content ) {
if ( $this->_archive_contents ) {
preg_match( '/\[learn_press_archive_course\s?(.*)\]/', $content, $results );
$this->_shortcode_exists = ! empty( $results );
if ( $this->_shortcode_exists ) {
$this->_shortcode_tag = $results[0];
$content = str_replace( $this->_shortcode_tag, $this->_archive_contents, $content );
} else {
$content .= $this->_archive_contents;
}
}
return $content;
}*/
/**
* Check is page Become a teacher
*
* @return bool|mixed|void
* @since 3.2.8
* @author tungnx
*/
public static function is_page_become_a_teacher() {
$page_id = learn_press_get_page_id( 'become_a_teacher' );
if ( $page_id && is_page( $page_id ) ) {
return true;
}
return apply_filters( 'learnpress/is-page/become-a-teacher', false );
}
/**
* Get page current on frontend
*
* @return string
* @since 3.2.8
* @author tungnx
*/
public static function page_current(): string {
/**
* @var WP_Query $wp_query
*/
global $wp_query;
if ( ! is_object( $wp_query ) || ! $wp_query->get_queried_object() ) {
return '';
}
if ( learn_press_is_checkout() ) {
return LP_PAGE_CHECKOUT;
} elseif ( LP_Global::course_item_quiz() ) {
return LP_PAGE_QUIZ;
} elseif ( learn_press_is_course() && LP_Global::course_item() ) {
return LP_PAGE_SINGLE_COURSE_CURRICULUM;
} elseif ( learn_press_is_courses() ) {
return LP_PAGE_COURSES;
} elseif ( learn_press_is_course() ) {
return LP_PAGE_SINGLE_COURSE;
} elseif ( self::is_page_become_a_teacher() ) {
return LP_PAGE_BECOME_A_TEACHER;
} elseif ( learn_press_is_profile() ) {
return LP_PAGE_PROFILE;
} else {
return apply_filters( 'learnpress/page/current', '' );
}
}
public static function instance() {
if ( is_admin() ) {
return null;
}
if ( ! self::$_instance ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Add user profile link into admin bar
*
* @editor tungnx
* @version 1.0.1
* @since 3.0.0
* @deprecated 4.1.7.3
*/
public function learn_press_edit_admin_bar() {
global $wp_admin_bar;
$current_user = wp_get_current_user();
if ( ! in_array( LP_TEACHER_ROLE, $current_user->roles ) && ! in_array( 'administrator', $current_user->roles ) ) {
return;
}
$page_profile_id = learn_press_get_page_id( 'profile' );
if ( $page_profile_id && get_post_status( $page_profile_id ) != 'trash' ) {
$user_id = $current_user->ID;
$wp_admin_bar->add_menu(
array(
'id' => 'course_profile',
'parent' => 'user-actions',
'title' => get_the_title( $page_profile_id ),
'href' => learn_press_user_profile_link( $user_id, false ),
)
);
}
}
/**