Addons.php
4 years ago
Admin.php
4 years ago
Ajax.php
4 years ago
Announcements.php
4 years ago
Assets.php
4 years ago
Backend_Page_Trait.php
4 years ago
Course.php
4 years ago
Course_Filter.php
4 years ago
Course_List.php
4 years ago
Course_Settings_Tabs.php
4 years ago
Course_Widget.php
4 years ago
Custom_Validation.php
4 years ago
Dashboard.php
4 years ago
FormHandler.php
4 years ago
Frontend.php
4 years ago
Gutenberg.php
4 years ago
Input.php
4 years ago
Instructor.php
4 years ago
Instructors_List.php
4 years ago
Lesson.php
4 years ago
Options_V2.php
4 years ago
Post_types.php
4 years ago
Private_Course_Access.php
4 years ago
Q_and_A.php
4 years ago
Question_Answers_List.php
4 years ago
Quiz.php
4 years ago
Quiz_Attempts_List.php
4 years ago
RestAPI.php
4 years ago
Reviews.php
4 years ago
Rewrite_Rules.php
4 years ago
Shortcode.php
4 years ago
Student.php
4 years ago
Students_List.php
4 years ago
Taxonomies.php
4 years ago
Template.php
4 years ago
Theme_Compatibility.php
5 years ago
Tools.php
4 years ago
Tools_V2.php
4 years ago
Tutor.php
4 years ago
TutorEDD.php
4 years ago
Tutor_Base.php
5 years ago
Tutor_List_Table.php
4 years ago
Tutor_Setup.php
4 years ago
Upgrader.php
4 years ago
User.php
4 years ago
Utils.php
4 years ago
Video_Stream.php
4 years ago
Withdraw.php
4 years ago
Withdraw_Requests_List.php
4 years ago
WooCommerce.php
4 years ago
Course_Filter.php
185 lines
| 1 | <?php |
| 2 | namespace TUTOR; |
| 3 | |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | |
| 8 | class Course_Filter { |
| 9 | private $category = 'course-category'; |
| 10 | private $tag = 'course-tag'; |
| 11 | private $current_term_id = null; |
| 12 | |
| 13 | function __construct($register_hook=true) { |
| 14 | if(!$register_hook) { |
| 15 | return; |
| 16 | } |
| 17 | add_action( 'wp_ajax_tutor_course_filter_ajax', array( $this, 'load_listing' ), 10, 0 ); |
| 18 | add_action( 'wp_ajax_nopriv_tutor_course_filter_ajax', array( $this, 'load_listing' ), 10, 0 ); |
| 19 | } |
| 20 | |
| 21 | public function load_listing($filters=null, $return_filter=false) { |
| 22 | !$return_filter ? tutils()->checking_nonce() : 0; |
| 23 | $_post = tutor_sanitize_data($filters==null ? $_POST : $filters); |
| 24 | !is_array( $_post ) ? $_post=array() : 0; |
| 25 | |
| 26 | $default_per_page = tutils()->get_option( 'courses_per_page', 12 ); |
| 27 | $courses_per_page = (int) tutils()->array_get( 'course_per_page', $_post, $default_per_page ); |
| 28 | |
| 29 | $page = ( isset( $_post['current_page'] ) && is_numeric( $_post['current_page'] ) && $_post['current_page'] > 0 ) ? $_post['current_page'] : 1; |
| 30 | $_post['current_page'] = $page; |
| 31 | |
| 32 | $args = array( |
| 33 | 'post_status' => 'publish', |
| 34 | 'post_type' => 'courses', |
| 35 | 'posts_per_page' => $courses_per_page, |
| 36 | 'paged' => (int)$page, |
| 37 | 'tax_query' => array( |
| 38 | 'relation' => 'OR', |
| 39 | ), |
| 40 | ); |
| 41 | |
| 42 | // Prepare taxonomy |
| 43 | foreach ( array( 'category', 'tag' ) as $taxonomy ) { |
| 44 | |
| 45 | $term_array = tutils()->array_get( 'tutor-course-filter-' . $taxonomy, $_post, array() ); |
| 46 | ! is_array( $term_array ) ? $term_array = array( $term_array ) : 0; |
| 47 | |
| 48 | $term_array = array_filter( |
| 49 | $term_array, |
| 50 | function( $term_id ) { |
| 51 | return is_numeric( $term_id ); |
| 52 | } |
| 53 | ); |
| 54 | |
| 55 | if ( count( $term_array ) > 0 ) { |
| 56 | $tax_query = array( |
| 57 | 'taxonomy' => $this->$taxonomy, |
| 58 | 'field' => 'term_id', |
| 59 | 'terms' => $term_array, |
| 60 | 'operator' => 'IN', |
| 61 | ); |
| 62 | array_push( $args['tax_query'], $tax_query ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Prepare level and price type |
| 67 | $is_membership = get_tutor_option( 'monetize_by' ) == 'pmpro' && tutils()->has_pmpro(); |
| 68 | $level_price = array(); |
| 69 | foreach ( array( 'level', 'price' ) as $type ) { |
| 70 | |
| 71 | if ( $is_membership && 'price' === $type ) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | $type_array = tutils()->array_get( 'tutor-course-filter-' . $type, $_post, array() ); |
| 76 | $type_array = array_map( 'sanitize_text_field', ( is_array( $type_array ) ? $type_array : array( $type_array ) ) ); |
| 77 | |
| 78 | if($type=='level' && in_array('all_levels', $type_array)) { |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | if ( count( $type_array ) > 0 ) { |
| 83 | $level_price[] = array( |
| 84 | 'key' => 'level' === $type ? '_tutor_course_level' : '_tutor_course_price_type', |
| 85 | 'value' => $type_array, |
| 86 | 'compare' => 'IN', |
| 87 | ); |
| 88 | } |
| 89 | } |
| 90 | count( $level_price ) ? $args['meta_query'] = $level_price : 0; |
| 91 | |
| 92 | $search_key = sanitize_text_field( tutils()->array_get( 'keyword', $_post, null ) ); |
| 93 | $search_key ? $args['s'] = $search_key : 0; |
| 94 | |
| 95 | if ( isset( $_post['tutor_course_filter'] ) ) { |
| 96 | switch ( $_post['tutor_course_filter'] ) { |
| 97 | |
| 98 | case 'newest_first': |
| 99 | $args['orderby'] = 'ID'; |
| 100 | $args['order'] = 'desc'; |
| 101 | break; |
| 102 | |
| 103 | case 'oldest_first': |
| 104 | $args['orderby'] = 'ID'; |
| 105 | $args['order'] = 'asc'; |
| 106 | break; |
| 107 | |
| 108 | case 'course_title_az': |
| 109 | $args['orderby'] = 'post_title'; |
| 110 | $args['order'] = 'asc'; |
| 111 | break; |
| 112 | |
| 113 | case 'course_title_za': |
| 114 | $args['orderby'] = 'post_title'; |
| 115 | $args['order'] = 'desc'; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Return filters |
| 121 | $filters = apply_filters( 'tutor_course_filter_args', $args ); |
| 122 | if($return_filter){ |
| 123 | return $filters; |
| 124 | } |
| 125 | |
| 126 | ob_start(); |
| 127 | |
| 128 | query_posts( $filters ); |
| 129 | tutor_load_template( 'archive-course-init', array_merge( array('loop_content_only' => true), $_post )); |
| 130 | |
| 131 | wp_send_json_success( array('html' => ob_get_clean()) ); |
| 132 | exit; |
| 133 | } |
| 134 | |
| 135 | private function get_current_term_id() { |
| 136 | |
| 137 | if ( $this->current_term_id === null ) { |
| 138 | $queried = get_queried_object(); |
| 139 | $this->current_term_id = ( is_object( $queried ) && property_exists( $queried, 'term_id' ) ) ? $queried->term_id : false; |
| 140 | } |
| 141 | |
| 142 | return $this->current_term_id; |
| 143 | } |
| 144 | |
| 145 | private function sort_terms_hierarchically( $terms, $parent_id = 0 ) { |
| 146 | $term_array = array(); |
| 147 | |
| 148 | foreach ( $terms as $term ) { |
| 149 | if ( $term->parent == $parent_id ) { |
| 150 | $term->children = $this->sort_terms_hierarchically( $terms, $term->term_id ); |
| 151 | $term_array[] = $term; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return $term_array; |
| 156 | } |
| 157 | |
| 158 | private function render_terms_hierarchically( $terms, $taxonomy ) { |
| 159 | |
| 160 | $term_id = $this->get_current_term_id(); |
| 161 | |
| 162 | foreach($terms as $term){ |
| 163 | ?> |
| 164 | <li class="tutor-list-item"> |
| 165 | <label> |
| 166 | <input type="checkbox" class="tutor-form-check-input" name="tutor-course-filter-<?php echo $taxonomy; ?>" value="<?php echo $term->term_id; ?>" <?php echo $term->term_id==$term_id ? 'checked="checked"' : ''; ?>/> |
| 167 | <?php echo $term->name; ?> |
| 168 | </label> |
| 169 | </li> |
| 170 | <?php isset($term->children) ? $this->render_terms_hierarchically($term->children, $taxonomy) : 0; ?> |
| 171 | <?php |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | public function render_terms( $taxonomy ) { |
| 176 | $terms = get_terms( |
| 177 | array( |
| 178 | 'taxonomy' => $this->$taxonomy, |
| 179 | 'hide_empty' => true, |
| 180 | ) |
| 181 | ); |
| 182 | $this->render_terms_hierarchically( $this->sort_terms_hierarchically( $terms ), $taxonomy ); |
| 183 | } |
| 184 | } |
| 185 |