Addons.php
1 day ago
Admin.php
1 day ago
Ajax.php
1 day ago
Announcements.php
1 day ago
Assets.php
1 day ago
Backend_Page_Trait.php
1 year ago
BaseController.php
1 year ago
Config.php
1 day ago
Container.php
11 months ago
Course.php
1 day ago
Course_Embed.php
3 years ago
Course_Filter.php
1 day ago
Course_List.php
1 day ago
Course_Settings_Tabs.php
1 day ago
Course_Widget.php
1 year ago
Custom_Validation.php
1 day ago
Dashboard.php
1 day ago
Earnings.php
9 months ago
FormHandler.php
1 day ago
Frontend.php
1 day ago
Gutenberg.php
1 year ago
Icon.php
1 day ago
Input.php
1 day ago
Instructor.php
1 day ago
Instructors_List.php
1 day ago
Lesson.php
1 day ago
Options_V2.php
1 day ago
Permalink.php
1 day ago
Post_types.php
2 days ago
Private_Course_Access.php
1 day ago
Q_And_A.php
1 day ago
Question_Answers_List.php
11 months ago
Quiz.php
1 day ago
QuizBuilder.php
1 day ago
Quiz_Attempts_List.php
1 day ago
RestAPI.php
2 years ago
Reviews.php
1 day ago
Rewrite_Rules.php
2 years ago
SampleCourse.php
1 day ago
Shortcode.php
1 day ago
Singleton.php
1 year ago
Student.php
1 day ago
Students_List.php
1 year ago
Taxonomies.php
1 year ago
Template.php
1 day ago
Theme_Compatibility.php
3 years ago
Tools.php
1 year ago
Tools_V2.php
4 weeks ago
Tutor.php
1 day ago
TutorEDD.php
1 day ago
Tutor_Base.php
2 years ago
Tutor_Setup.php
1 day ago
Upgrader.php
1 day ago
User.php
1 day ago
UserPreference.php
1 day ago
Utils.php
1 day ago
Video_Stream.php
3 years ago
WhatsNew.php
10 months ago
Withdraw.php
1 day ago
Withdraw_Requests_List.php
11 months ago
WooCommerce.php
1 day ago
Course_Filter.php
332 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Manage Course Filter |
| 4 | * |
| 5 | * @package Tutor |
| 6 | * @author Themeum <support@themeum.com> |
| 7 | * @link https://themeum.com |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace TUTOR; |
| 12 | |
| 13 | use Tutor\Models\CourseModel; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Course filter class |
| 21 | * |
| 22 | * @since 1.0.0 |
| 23 | */ |
| 24 | class Course_Filter { |
| 25 | /** |
| 26 | * Course Category |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | private $category = 'course-category'; |
| 31 | /** |
| 32 | * Course Tag |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | private $tag = 'course-tag'; |
| 37 | /** |
| 38 | * Course term id |
| 39 | * |
| 40 | * @var null|integer |
| 41 | */ |
| 42 | private $current_term_id = null; |
| 43 | |
| 44 | /** |
| 45 | * Constructor |
| 46 | * |
| 47 | * @since 1.0.0 |
| 48 | * |
| 49 | * @param boolean $register_hook register hook or not. |
| 50 | * |
| 51 | * @return void|null |
| 52 | */ |
| 53 | public function __construct( $register_hook = true ) { |
| 54 | if ( ! $register_hook ) { |
| 55 | return; |
| 56 | } |
| 57 | add_action( 'wp_ajax_tutor_course_filter_ajax', array( $this, 'load_listing' ), 10, 0 ); |
| 58 | add_action( 'wp_ajax_nopriv_tutor_course_filter_ajax', array( $this, 'load_listing' ), 10, 0 ); |
| 59 | add_filter( 'term_link', array( $this, 'filter_course_category_term_link' ), 10, 3 ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Load course listing |
| 64 | * |
| 65 | * @since 1.0.0 |
| 66 | * |
| 67 | * @param mixed $filters filters. |
| 68 | * @param boolean $return_filter return filtered data or not. |
| 69 | * @return mixed |
| 70 | */ |
| 71 | public function load_listing( $filters = null, $return_filter = false ) { |
| 72 | ! $return_filter ? tutils()->checking_nonce() : 0; |
| 73 | |
| 74 | $sanitized_post = tutor_sanitize_data( null == $filters ? $_POST : $filters ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 75 | ! is_array( $sanitized_post ) ? $sanitized_post = array() : 0; |
| 76 | |
| 77 | $default_per_page = tutils()->get_option( 'courses_per_page', 12 ); |
| 78 | $courses_per_page = (int) tutils()->array_get( 'course_per_page', $sanitized_post, $default_per_page ); |
| 79 | |
| 80 | // Pagination arg. |
| 81 | $current_page = (int) Input::sanitize_request_data( 'current_page', 1 ); |
| 82 | $paged = max( 1, $current_page ); |
| 83 | $sanitized_post['current_page'] = $paged; |
| 84 | |
| 85 | // Order arg. |
| 86 | $order_by = 'post_date'; |
| 87 | $order = 'DESC'; |
| 88 | |
| 89 | if ( isset( $sanitized_post['course_order'] ) ) { |
| 90 | switch ( $sanitized_post['course_order'] ) { |
| 91 | case 'newest_first': |
| 92 | $order_by = 'post_date'; |
| 93 | $order = 'DESC'; |
| 94 | break; |
| 95 | |
| 96 | case 'oldest_first': |
| 97 | $order_by = 'post_date'; |
| 98 | $order = 'ASC'; |
| 99 | break; |
| 100 | |
| 101 | case 'course_title_az': |
| 102 | $order_by = 'post_title'; |
| 103 | $order = 'ASC'; |
| 104 | break; |
| 105 | |
| 106 | case 'course_title_za': |
| 107 | $order_by = 'post_title'; |
| 108 | $order = 'DESC'; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | $args = array( |
| 114 | 'post_status' => 'publish', |
| 115 | 'post_type' => tutor()->course_post_type, |
| 116 | 'posts_per_page' => $courses_per_page, |
| 117 | 'paged' => $paged, |
| 118 | 'orderby' => $order_by, |
| 119 | 'order' => $order, |
| 120 | 'tax_query' => array( |
| 121 | 'relation' => 'AND', |
| 122 | ), |
| 123 | ); |
| 124 | |
| 125 | $post_ids_array = tutils()->array_get( 'tutor-course-filter-post-ids', $sanitized_post, array() ); |
| 126 | $post_ids_array = array_map( 'intval', $post_ids_array ); |
| 127 | |
| 128 | if ( count( $post_ids_array ) ) { |
| 129 | $args['post__in'] = $post_ids_array; |
| 130 | } |
| 131 | |
| 132 | $exclude_ids_array = tutils()->array_get( 'tutor-course-filter-exclude-ids', $sanitized_post, array() ); |
| 133 | $exclude_ids_array = array_map( 'intval', $exclude_ids_array ); |
| 134 | |
| 135 | if ( count( $exclude_ids_array ) ) { |
| 136 | $args['post__not_in'] = $exclude_ids_array; |
| 137 | } |
| 138 | // Prepare taxonomy. |
| 139 | foreach ( array( 'category', 'tag' ) as $taxonomy ) { |
| 140 | |
| 141 | $term_array = tutils()->array_get( 'tutor-course-filter-' . $taxonomy, $sanitized_post, array() ); |
| 142 | ! is_array( $term_array ) ? $term_array = array( $term_array ) : 0; |
| 143 | |
| 144 | $term_array = array_filter( $term_array, fn ( $term_id ) => is_numeric( $term_id ) ); |
| 145 | |
| 146 | if ( count( $term_array ) > 0 ) { |
| 147 | $tax_query = array( |
| 148 | 'taxonomy' => $this->$taxonomy, |
| 149 | 'field' => 'term_id', |
| 150 | 'terms' => $term_array, |
| 151 | 'operator' => 'IN', |
| 152 | ); |
| 153 | array_push( $args['tax_query'], $tax_query ); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Prepare level and price type. |
| 158 | $is_membership = get_tutor_option( 'monetize_by' ) == 'pmpro' && tutils()->has_pmpro(); |
| 159 | $level_price = array(); |
| 160 | foreach ( array( 'level', 'price' ) as $type ) { |
| 161 | |
| 162 | if ( $is_membership && 'price' === $type ) { |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | $type_array = tutils()->array_get( 'tutor-course-filter-' . $type, $sanitized_post, array() ); |
| 167 | $type_array = array_map( 'sanitize_text_field', ( is_array( $type_array ) ? $type_array : array( $type_array ) ) ); |
| 168 | |
| 169 | if ( 'level' === $type && in_array( 'all_levels', $type_array, true ) ) { |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | if ( in_array( Course::PRICE_TYPE_PAID, $type_array, true ) ) { |
| 174 | $type_array = apply_filters( 'tutor_course_filter_price_type_paid', $type_array ); |
| 175 | } |
| 176 | |
| 177 | if ( count( $type_array ) > 0 ) { |
| 178 | $level_price[] = array( |
| 179 | 'key' => 'level' === $type ? '_tutor_course_level' : '_tutor_course_price_type', |
| 180 | 'value' => $type_array, |
| 181 | 'compare' => 'IN', |
| 182 | ); |
| 183 | } |
| 184 | } |
| 185 | count( $level_price ) ? $args['meta_query'] = $level_price : 0; |
| 186 | |
| 187 | $search_key = sanitize_text_field( tutils()->array_get( 'keyword', $sanitized_post, null ) ); |
| 188 | $search_key ? $args['s'] = $search_key : 0; |
| 189 | |
| 190 | if ( isset( $sanitized_post['tutor_course_filter'] ) ) { |
| 191 | switch ( $sanitized_post['tutor_course_filter'] ) { |
| 192 | |
| 193 | case 'newest_first': |
| 194 | $args['orderby'] = 'post_date'; |
| 195 | $args['order'] = 'desc'; |
| 196 | break; |
| 197 | |
| 198 | case 'oldest_first': |
| 199 | $args['orderby'] = 'post_date'; |
| 200 | $args['order'] = 'asc'; |
| 201 | break; |
| 202 | |
| 203 | case 'course_title_az': |
| 204 | $args['orderby'] = 'post_title'; |
| 205 | $args['order'] = 'asc'; |
| 206 | break; |
| 207 | |
| 208 | case 'course_title_za': |
| 209 | $args['orderby'] = 'post_title'; |
| 210 | $args['order'] = 'desc'; |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Return filters. |
| 216 | $filters = apply_filters( 'tutor_course_filter_args', $args ); |
| 217 | if ( $return_filter ) { |
| 218 | return $filters; |
| 219 | } |
| 220 | |
| 221 | ob_start(); |
| 222 | |
| 223 | query_posts( $filters ); |
| 224 | tutor_load_template( 'archive-course-init', array_merge( array( 'loop_content_only' => true ), $sanitized_post ) ); |
| 225 | |
| 226 | wp_send_json_success( array( 'html' => ob_get_clean() ) ); |
| 227 | exit; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Get current term ID |
| 232 | * |
| 233 | * @since 1.0.0 |
| 234 | * @return integer |
| 235 | */ |
| 236 | private function get_current_term_id() { |
| 237 | |
| 238 | if ( null === $this->current_term_id ) { |
| 239 | $queried = get_queried_object(); |
| 240 | $this->current_term_id = ( is_object( $queried ) && property_exists( $queried, 'term_id' ) ) ? $queried->term_id : false; |
| 241 | } |
| 242 | |
| 243 | return $this->current_term_id; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Sort terms hierarchically |
| 248 | * |
| 249 | * @since 1.0.0 |
| 250 | * |
| 251 | * @param array $terms term list. |
| 252 | * @param integer $parent_id parent ID. |
| 253 | * @return array |
| 254 | */ |
| 255 | private function sort_terms_hierarchically( $terms, $parent_id = 0 ) { |
| 256 | $term_array = array(); |
| 257 | |
| 258 | foreach ( $terms as $term ) { |
| 259 | if ( $term->parent == $parent_id ) { |
| 260 | $term->children = $this->sort_terms_hierarchically( $terms, $term->term_id ); |
| 261 | $term_array[] = $term; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return $term_array; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Render terms hierarchically |
| 270 | * |
| 271 | * @since 1.0.0 |
| 272 | * |
| 273 | * @param array $terms term list. |
| 274 | * @param string $taxonomy taxonomy name. |
| 275 | * |
| 276 | * @return void |
| 277 | */ |
| 278 | private function render_terms_hierarchically( $terms, $taxonomy ) { |
| 279 | $term_id = $this->get_current_term_id(); |
| 280 | |
| 281 | foreach ( $terms as $term ) { |
| 282 | ?> |
| 283 | <li class="tutor-list-item"> |
| 284 | <label> |
| 285 | <input type="checkbox" class="tutor-form-check-input" name="tutor-course-filter-<?php echo esc_attr( $taxonomy ); ?>" value="<?php echo esc_attr( $term->term_id ); ?>" <?php echo esc_attr( $term->term_id == $term_id ? 'checked="checked"' : '' ); ?>/> |
| 286 | <?php echo esc_html( $term->name ); ?> |
| 287 | </label> |
| 288 | </li> |
| 289 | <?php isset( $term->children ) ? $this->render_terms_hierarchically( $term->children, $taxonomy ) : 0; ?> |
| 290 | <?php |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Render terms |
| 296 | * |
| 297 | * @since 1.0.0 |
| 298 | * |
| 299 | * @param string $taxonomy taxonomy name. |
| 300 | * |
| 301 | * @return void |
| 302 | */ |
| 303 | public function render_terms( $taxonomy ) { |
| 304 | $terms = get_terms( |
| 305 | array( |
| 306 | 'taxonomy' => $this->$taxonomy, |
| 307 | 'hide_empty' => true, |
| 308 | ) |
| 309 | ); |
| 310 | $this->render_terms_hierarchically( $this->sort_terms_hierarchically( $terms ), $taxonomy ); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Filter course-category term's permalink |
| 315 | * |
| 316 | * Add a query param so that course filter can work |
| 317 | * |
| 318 | * @param string $termlink default term link. |
| 319 | * @param \WP_Term $term term obj. |
| 320 | * @param string $taxonomy taxonomy. |
| 321 | * |
| 322 | * @return string customized term link |
| 323 | */ |
| 324 | public static function filter_course_category_term_link( string $termlink, \WP_Term $term, string $taxonomy ) { |
| 325 | if ( CourseModel::COURSE_CATEGORY === $taxonomy ) { |
| 326 | $termlink = add_query_arg( 'tutor-course-filter-category', $term->term_id, $termlink ); |
| 327 | |
| 328 | } |
| 329 | return $termlink; |
| 330 | } |
| 331 | } |
| 332 |