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.2.3 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / classes / Course_Filter.php
tutor / classes Last commit date
Addons.php 1 month ago Admin.php 1 month ago Ajax.php 6 days ago Announcements.php 1 month ago Assets.php 3 weeks ago Backend_Page_Trait.php 1 year ago BaseController.php 1 year ago Config.php 1 month ago Container.php 1 year ago Course.php 4 days ago Course_Embed.php 3 years ago Course_Filter.php 6 days ago Course_List.php 1 month ago Course_Settings_Tabs.php 1 month ago Course_Widget.php 1 year ago Custom_Validation.php 1 month ago Dashboard.php 1 month ago Earnings.php 10 months ago FormHandler.php 1 month ago Frontend.php 1 month ago Gutenberg.php 1 year ago Icon.php 1 week ago Input.php 1 month ago Instructor.php 1 month ago Instructors_List.php 1 month ago Lesson.php 6 days ago Options_V2.php 1 month ago Permalink.php 1 month ago Post_types.php 1 month ago Private_Course_Access.php 1 month ago Q_And_A.php 1 month ago Question_Answers_List.php 1 year ago Quiz.php 1 week ago QuizBuilder.php 1 week ago Quiz_Attempts_List.php 1 month ago RestAPI.php 1 month ago Reviews.php 1 month ago Rewrite_Rules.php 2 years ago SampleCourse.php 1 month ago Shortcode.php 1 month ago Singleton.php 1 year ago Student.php 1 month ago Students_List.php 1 year ago Taxonomies.php 1 year ago Template.php 1 month ago Theme_Compatibility.php 3 years ago Tools.php 1 year ago Tools_V2.php 2 months ago Tutor.php 6 days ago TutorEDD.php 1 month ago Tutor_Base.php 2 years ago Tutor_Setup.php 1 month ago Upgrader.php 1 month ago User.php 3 weeks ago UserPreference.php 1 month ago Utils.php 6 days ago Video_Stream.php 6 days ago WhatsNew.php 1 month ago Withdraw.php 1 month ago Withdraw_Requests_List.php 1 year ago WooCommerce.php 1 week ago
Course_Filter.php
333 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 unset( $sanitized_post['template'], $sanitized_post['data'] );
225 tutor_load_template( 'archive-course-init', array_merge( array( 'loop_content_only' => true ), $sanitized_post ) );
226
227 wp_send_json_success( array( 'html' => ob_get_clean() ) );
228 exit;
229 }
230
231 /**
232 * Get current term ID
233 *
234 * @since 1.0.0
235 * @return integer
236 */
237 private function get_current_term_id() {
238
239 if ( null === $this->current_term_id ) {
240 $queried = get_queried_object();
241 $this->current_term_id = ( is_object( $queried ) && property_exists( $queried, 'term_id' ) ) ? $queried->term_id : false;
242 }
243
244 return $this->current_term_id;
245 }
246
247 /**
248 * Sort terms hierarchically
249 *
250 * @since 1.0.0
251 *
252 * @param array $terms term list.
253 * @param integer $parent_id parent ID.
254 * @return array
255 */
256 private function sort_terms_hierarchically( $terms, $parent_id = 0 ) {
257 $term_array = array();
258
259 foreach ( $terms as $term ) {
260 if ( $term->parent == $parent_id ) {
261 $term->children = $this->sort_terms_hierarchically( $terms, $term->term_id );
262 $term_array[] = $term;
263 }
264 }
265
266 return $term_array;
267 }
268
269 /**
270 * Render terms hierarchically
271 *
272 * @since 1.0.0
273 *
274 * @param array $terms term list.
275 * @param string $taxonomy taxonomy name.
276 *
277 * @return void
278 */
279 private function render_terms_hierarchically( $terms, $taxonomy ) {
280 $term_id = $this->get_current_term_id();
281
282 foreach ( $terms as $term ) {
283 ?>
284 <li class="tutor-list-item">
285 <label>
286 <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"' : '' ); ?>/>
287 <?php echo esc_html( $term->name ); ?>
288 </label>
289 </li>
290 <?php isset( $term->children ) ? $this->render_terms_hierarchically( $term->children, $taxonomy ) : 0; ?>
291 <?php
292 }
293 }
294
295 /**
296 * Render terms
297 *
298 * @since 1.0.0
299 *
300 * @param string $taxonomy taxonomy name.
301 *
302 * @return void
303 */
304 public function render_terms( $taxonomy ) {
305 $terms = get_terms(
306 array(
307 'taxonomy' => $this->$taxonomy,
308 'hide_empty' => true,
309 )
310 );
311 $this->render_terms_hierarchically( $this->sort_terms_hierarchically( $terms ), $taxonomy );
312 }
313
314 /**
315 * Filter course-category term's permalink
316 *
317 * Add a query param so that course filter can work
318 *
319 * @param string $termlink default term link.
320 * @param \WP_Term $term term obj.
321 * @param string $taxonomy taxonomy.
322 *
323 * @return string customized term link
324 */
325 public static function filter_course_category_term_link( string $termlink, \WP_Term $term, string $taxonomy ) {
326 if ( CourseModel::COURSE_CATEGORY === $taxonomy ) {
327 $termlink = add_query_arg( 'tutor-course-filter-category', $term->term_id, $termlink );
328
329 }
330 return $termlink;
331 }
332 }
333