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.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 3 weeks ago Admin.php 1 week ago Ajax.php 3 weeks ago Announcements.php 3 weeks ago Assets.php 3 weeks ago Backend_Page_Trait.php 1 year ago BaseController.php 1 year ago Config.php 3 weeks ago Container.php 1 year ago Course.php 3 weeks ago Course_Embed.php 3 years ago Course_Filter.php 3 weeks ago Course_List.php 3 weeks ago Course_Settings_Tabs.php 3 weeks ago Course_Widget.php 1 year ago Custom_Validation.php 3 weeks ago Dashboard.php 3 weeks ago Earnings.php 10 months ago FormHandler.php 3 weeks ago Frontend.php 3 weeks ago Gutenberg.php 1 year ago Icon.php 1 week ago Input.php 3 weeks ago Instructor.php 3 weeks ago Instructors_List.php 3 weeks ago Lesson.php 3 weeks ago Options_V2.php 3 weeks ago Permalink.php 3 weeks ago Post_types.php 3 weeks ago Private_Course_Access.php 3 weeks ago Q_And_A.php 3 weeks ago Question_Answers_List.php 1 year ago Quiz.php 1 week ago QuizBuilder.php 3 weeks ago Quiz_Attempts_List.php 3 weeks ago RestAPI.php 3 weeks ago Reviews.php 3 weeks ago Rewrite_Rules.php 2 years ago SampleCourse.php 3 weeks ago Shortcode.php 3 weeks ago Singleton.php 1 year ago Student.php 3 weeks ago Students_List.php 1 year ago Taxonomies.php 1 year ago Template.php 3 weeks ago Theme_Compatibility.php 3 years ago Tools.php 1 year ago Tools_V2.php 1 month ago Tutor.php 3 weeks ago TutorEDD.php 3 weeks ago Tutor_Base.php 2 years ago Tutor_Setup.php 3 weeks ago Upgrader.php 3 weeks ago User.php 1 week ago UserPreference.php 3 weeks ago Utils.php 1 week ago Video_Stream.php 3 years ago WhatsNew.php 3 weeks ago Withdraw.php 3 weeks ago Withdraw_Requests_List.php 1 year ago WooCommerce.php 3 weeks 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