PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
← All changes | inc/Services/CourseService.php +377 -365 4.3.94.4.8 View file →
@@ -1,365 +1,377 @@
1 -<?php
2 -
3 -namespace LearnPress\Services;
4 -
5 -use Exception;
6 -use LearnPress\Databases\Course\CourseJsonDB;
7 -use LearnPress\Filters\Course\CourseJsonFilter;
8 -use LearnPress\Helpers\Singleton;
9 -use LearnPress\Models\CourseModel;
10 -use LearnPress\Models\CoursePostModel;
11 -use LearnPress\Models\PostModel;
12 -use LearnPress\Models\UserModel;
13 -use LP_Debug;
14 -use LP_Helper;
15 -use LP_Settings;
16 -use Throwable;
17 -
18 -/**
19 - * Class CourseService
20 - *
21 - * Create course with data.
22 - *
23 - * @package LearnPress\Services
24 - * @since 4.3.0
25 - * @version 1.0.1
26 - */
27 -class CourseService {
28 - use Singleton;
29 -
30 - public function init() {
31 - }
32 -
33 - /**
34 - * Create course info main
35 - *
36 - * meta_input for metadata
37 - *
38 - * @param array $data [ 'post_title' => '', 'post_content' => '', 'post_status' => '', 'post_author' => , 'meta_input' => [] ]
39 - *
40 - * @throws Exception
41 - */
42 - public function create_info_main( array $data ): CoursePostModel {
43 - $coursePostModelNew = new CoursePostModel( $data );
44 -
45 - // Set meta data
46 - if ( isset( $data['meta_input'] ) ) {
47 - $coursePostModelNew->meta_data = (object) $data['meta_input'];
48 - }
49 -
50 - $coursePostModelNew->save();
51 -
52 - return $coursePostModelNew;
53 - }
54 -
55 - /**
56 - * Update categories for course
57 - * Create categories if not exists
58 - *
59 - * @param $course_id
60 - * @param int[] $category_ids
61 - *
62 - * @return void
63 - * @since 4.3.6
64 - * @version 1.0.0
65 - */
66 - public function update_categories( $course_id, array $category_ids ) {
67 - // Create categories if not exists
68 - foreach ( $category_ids as $category_id ) {
69 - $term_check = term_exists( $category_id, CoursePostModel::TAXONOMY_CATEGORY );
70 - if ( ! $term_check ) {
71 - wp_insert_term( $category_id, CoursePostModel::TAXONOMY_CATEGORY );
72 - }
73 - }
74 -
75 - wp_set_post_terms( $course_id, $category_ids, CoursePostModel::TAXONOMY_CATEGORY );
76 - }
77 -
78 - /**
79 - * Update tags for course
80 - * Create tags if not exists
81 - *
82 - * @param $course_id
83 - * @param array $tag_ids
84 - *
85 - * @return void
86 - * @since 4.3.6
87 - * @version 1.0.0
88 - */
89 - public function update_tags( $course_id, array $tag_ids ) {
90 - // Create tags if not exists
91 - foreach ( $tag_ids as $tag_id ) {
92 - $term_check = term_exists( $tag_id, CoursePostModel::TAXONOMY_TAG );
93 - if ( ! $term_check ) {
94 - wp_insert_term( $tag_id, CoursePostModel::TAXONOMY_TAG );
95 - }
96 - }
97 -
98 - wp_set_post_terms( $course_id, $tag_ids, CoursePostModel::TAXONOMY_TAG );
99 - }
100 -
101 - /**
102 - * Duplicate course
103 - *
104 - * @throws Exception
105 - * @since 4.3.6
106 - * @version 1.0.0
107 - */
108 - public function duplicate( CourseModel $courseModel ): CourseModel {
109 - $coursePostModel = new CoursePostModel( $courseModel );
110 - $coursePostModel->get_all_metadata();
111 - $coursePostModelNew = new CoursePostModel( $coursePostModel );
112 - $coursePostModelNew->ID = null;
113 - $coursePostModelNew->post_title = $coursePostModelNew->post_title . ' (Copy)';
114 - $coursePostModelNew->save();
115 -
116 - // Duplicate sections
117 - $sections = $courseModel->get_section_items();
118 - foreach ( $sections as $section ) {
119 - $section_name = $section->section_name ?? $section->title ?? '';
120 - $section_description = $section->section_description ?? $section->description ?? '';
121 -
122 - $courseSectionModel = $coursePostModelNew->add_section(
123 - [
124 - 'section_name' => $section_name,
125 - 'section_description' => $section_description,
126 - ]
127 - );
128 -
129 - // Duplicate items for section
130 - $items = $section->items ?? [];
131 - foreach ( $items as $item ) {
132 - $item_title = $item->title ?? '';
133 - $item_type = $item->type ?? $item->item_type ?? '';
134 - $item_content = '';
135 -
136 - // Get item content from post
137 - $item_post = get_post( $item->item_id ?? $item->id ?? 0 );
138 - if ( $item_post ) {
139 - $item_content = $item_post->post_content ?? '';
140 - }
141 -
142 - $courseSectionModel->create_item_and_add(
143 - [
144 - 'item_title' => $item_title,
145 - 'item_type' => $item_type,
146 - 'item_content' => $item_content,
147 - ]
148 - );
149 - }
150 - }
151 -
152 - return $courseModel;
153 - }
154 -
155 - /**
156 - * Handle params before query list courses on table learnpress_courses
157 - *
158 - * @param CourseJsonFilter $filter
159 - * @param array $param
160 - *
161 - * @return void
162 - * @since 4.3.7
163 - * @version 1.0.0
164 - */
165 - public static function handle_params_for_query_list_courses( CourseJsonFilter &$filter, array $param = [] ) {
166 - $filter->page = absint( $param['paged'] ?? 1 );
167 - $filter->post_title = LP_Helper::sanitize_params_submitted( trim( $param['c_search'] ?? '' ) );
168 - $db = CourseJsonDB::getInstance();
169 -
170 - // Get Columns
171 - $fields_str = LP_Helper::sanitize_params_submitted( urldecode( $param['c_fields'] ?? '' ) );
172 - if ( ! empty( $fields_str ) ) {
173 - $fields = explode( ',', $fields_str );
174 - foreach ( $fields as $key => $field ) {
175 - $fields[ $key ] = $db->wpdb->prepare( '%i', $field );
176 - }
177 - $filter->fields = $fields;
178 - }
179 -
180 - // Get only columns
181 - $fields_only_str = LP_Helper::sanitize_params_submitted( urldecode( $param['c_only_fields'] ?? '' ) );
182 - if ( ! empty( $fields_only_str ) ) {
183 - $fields_only = explode( ',', $fields_only_str );
184 - foreach ( $fields_only as $key => $field ) {
185 - $fields_only[ $key ] = $db->wpdb->prepare( '%i', $field );
186 - }
187 - $filter->only_fields = $fields_only;
188 - }
189 -
190 - // Exclude Columns
191 - $fields_exclude_str = LP_Helper::sanitize_params_submitted( urldecode( $param['c_exclude_fields'] ?? '' ) );
192 - if ( ! empty( $fields_exclude_str ) ) {
193 - $fields_exclude = explode( ',', $fields_exclude_str );
194 - $filter->exclude_fields = $fields_exclude;
195 - }
196 -
197 - // Find by ids
198 - $course_ids_str = LP_Helper::sanitize_params_submitted( urldecode( $param['ids'] ?? '' ) );
199 - if ( ! empty( $course_ids_str ) ) {
200 - $course_ids = explode( ',', $course_ids_str );
201 - $filter->ids = $course_ids;
202 - }
203 -
204 - // Author
205 - $c_author = LP_Helper::sanitize_params_submitted( $param['c_author'] ?? 0 );
206 - if ( ! empty( $c_author ) ) {
207 - $filter->post_author = $c_author;
208 - }
209 - $author_ids_str = LP_Helper::sanitize_params_submitted( $param['c_authors'] ?? '' );
210 - if ( ! empty( $author_ids_str ) ) {
211 - $author_ids = explode( ',', $author_ids_str );
212 - $filter->post_authors = $author_ids;
213 - }
214 -
215 - // Find by status
216 - $post_status = LP_Helper::sanitize_params_submitted( $param['c_status'] ?? '' );
217 - if ( ! empty( $post_status ) ) {
218 - if ( 'all' !== $post_status ) {
219 - $filter->post_status = explode( ',', $post_status );
220 - }
221 -
222 - if ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR )
223 - || ! current_user_can( UserModel::ROLE_INSTRUCTOR ) ) {
224 - $filter->post_status = [ PostModel::STATUS_PUBLISH ];
225 - }
226 - }
227 -
228 - // Type price
229 - if ( ! empty( $param['c_type_price'] ) ) {
230 - $filter->type_price = explode( ',', LP_Helper::sanitize_params_submitted( urldecode( $param['c_type_price'] ) ) );
231 - }
232 -
233 - // On sale
234 - if ( isset( $param['on_sale'] ) ) {
235 - $filter->is_sale = 1;
236 - }
237 -
238 - // On feature
239 - if ( isset( $param['on_feature'] ) ) {
240 - $filter->is_feature = 1;
241 - }
242 -
243 - // Sort by level
244 - $levels_str = LP_Helper::sanitize_params_submitted( urldecode( $param['c_level'] ?? '' ) );
245 - if ( ! empty( $levels_str ) ) {
246 - $levels = explode( ',', $levels_str );
247 - if ( in_array( 'all', $levels ) ) {
248 - $levels[] = '';
249 - }
250 - $filter->levels = $levels;
251 - }
252 -
253 - // Sort by type (oline/offline)
254 - $course_type = LP_Helper::sanitize_params_submitted( urldecode( $param['c_type'] ?? '' ) );
255 - if ( ! empty( $course_type ) ) {
256 - $course_type = explode( ',', $course_type );
257 - if ( in_array( 'online', $course_type ) && in_array( 'offline', $course_type ) ) {
258 - $filter->type = 'all';
259 - } else {
260 - $filter->type = $course_type[0];
261 - }
262 - }
263 -
264 - // Check is in category page.
265 - if ( ! empty( $param['page_term_id_current'] ) && empty( $param['term_id'] ) ) {
266 - $filter->term_ids[] = $param['page_term_id_current'];
267 - } // Check is in tag page.
268 - elseif ( ! empty( $param['page_tag_id_current'] ) && empty( $param['tag_id'] ) ) {
269 - $filter->tag_ids[] = $param['page_tag_id_current'];
270 - }
271 -
272 - // Find by category
273 - $term_ids_str = LP_Helper::sanitize_params_submitted( urldecode( $param['term_id'] ?? '' ) );
274 - if ( ! empty( $term_ids_str ) ) {
275 - $term_ids = explode( ',', $term_ids_str );
276 - $filter->term_ids = array_merge( $filter->term_ids, $term_ids );
277 - }
278 -
279 - // Find by tag
280 - $tag_ids_str = LP_Helper::sanitize_params_submitted( urldecode( $param['tag_id'] ?? '' ) );
281 - if ( ! empty( $tag_ids_str ) ) {
282 - $tag_ids = explode( ',', $tag_ids_str );
283 - $filter->tag_ids = array_merge( $filter->tag_ids, $tag_ids );
284 - }
285 -
286 - // Order by
287 - $order_by = LP_Helper::sanitize_params_submitted( $param['order_by'] ?? 'post_date_gmt', 'key' );
288 - if ( $order_by === 'post_date' ) {
289 - $order_by = 'post_date_gmt';
290 - }
291 - $filter->order_by = $order_by;
292 - $filter->order = LP_Helper::sanitize_params_submitted( $param['order'] ?? 'DESC', 'key' );
293 - $filter->limit = $param['limit'] ?? LP_Settings::get_option( 'archive_course_limit', 10 );
294 -
295 - // For search suggest courses
296 - if ( ! empty( $param['c_suggest'] ) ) {
297 - $filter->only_fields = [ 'ID', 'post_title' ];
298 - $filter->limit = apply_filters( 'learn-press/services/rest-api/courses/suggest-limit', 10 );
299 - }
300 -
301 - do_action( 'learn-press/services/courses/handle_params_for_query_list_courses', $filter, $param );
302 - }
303 -
304 - /**
305 - * Get list courses on table learnpress_courses
306 - *
307 - * @param CourseJsonFilter $filter
308 - * @param int $total_rows
309 - *
310 - * @return array
311 - * @throws Exception
312 - * @version 1.0.0
313 - * @since 4.3.7
314 - */
315 - public static function get_list_courses( CourseJsonFilter $filter, int &$total_rows = 0 ): array {
316 - $db = CourseJsonDB::getInstance();
317 -
318 - try {
319 - $is_order_by_popular = 'popular' === $filter->order_by;
320 -
321 - // Order by
322 - switch ( $filter->order_by ) {
323 - case 'price':
324 - case 'price_low':
325 - if ( 'price_low' === $filter->order_by ) {
326 - $filter->order = 'ASC';
327 - } else {
328 - $filter->order = 'DESC';
329 - }
330 -
331 - $filter->order_by = 'price_to_sort';
332 - break;
333 - case 'popular':
334 - $filter = $db->get_courses_order_by_popular( $filter );
335 - break;
336 - case 'post_title':
337 - $filter->order = 'ASC';
338 - break;
339 - case 'post_title_desc':
340 - $filter->order_by = 'post_title';
341 - $filter->order = 'DESC';
342 - break;
343 - case 'menu_order':
344 - $filter->order_by = 'menu_order';
345 - $filter->order = 'ASC';
346 - break;
347 - default:
348 - $filter = apply_filters( 'lp/services/courses/filter/order_by/' . $filter->order_by, $filter );
349 - break;
350 - }
351 -
352 - // Query get results
353 - /**
354 - * @var CourseJsonFilter $filter
355 - */
356 - $filter = apply_filters( 'lp/services/courses/filter', $filter );
357 - $courses = $db->get_courses( $filter, $total_rows );
358 - } catch ( Throwable $e ) {
359 - $courses = [];
360 - LP_Debug::error_log( $e );
361 - }
362 -
363 - return $courses;
364 - }
365 -}
1 +<?php
2 +
3 +namespace LearnPress\Services;
4 +
5 +use Exception;
6 +use LearnPress\Databases\Course\CourseJsonDB;
7 +use LearnPress\Filters\Course\CourseJsonFilter;
8 +use LearnPress\Helpers\Singleton;
9 +use LearnPress\Models\CourseModel;
10 +use LearnPress\Models\CoursePostModel;
11 +use LearnPress\Models\PostModel;
12 +use LearnPress\Models\UserModel;
13 +use LP_Debug;
14 +use LP_Helper;
15 +use LP_Settings;
16 +use Throwable;
17 +
18 +/**
19 + * Class CourseService
20 + *
21 + * Create course with data.
22 + *
23 + * @package LearnPress\Services
24 + * @since 4.3.0
25 + * @version 1.0.1
26 + */
27 +class CourseService {
28 + use Singleton;
29 +
30 + public function init() {
31 + }
32 +
33 + /**
34 + * Create course info main
35 + * Data of columns posts
36 + *
37 + * meta_input for metadata
38 + *
39 + * @param array $data [ 'post_title' => '', 'post_content' => '', 'post_status' => '', 'post_author' => , 'meta_input' => [] ]
40 + *
41 + * @throws Exception
42 + */
43 + public function create_info_main( array $data ): CoursePostModel {
44 + $coursePostModelNew = new CoursePostModel( $data );
45 +
46 + // Set meta data
47 + if ( isset( $data['meta_input'] ) ) {
48 + $coursePostModelNew->meta_data = (object) $data['meta_input'];
49 + }
50 +
51 + $coursePostModelNew->save();
52 +
53 + return $coursePostModelNew;
54 + }
55 +
56 + /**
57 + * Update categories for course
58 + * Create categories if not exists
59 + *
60 + * @param $course_id
61 + * @param int[] $category_ids
62 + *
63 + * @return void
64 + * @since 4.3.6
65 + * @version 1.0.0
66 + */
67 + public function update_categories( $course_id, array $category_ids ) {
68 + // Create categories if not exists
69 + foreach ( $category_ids as $category_id ) {
70 + $term_check = term_exists( $category_id, CoursePostModel::TAXONOMY_CATEGORY );
71 + if ( ! $term_check ) {
72 + wp_insert_term( $category_id, CoursePostModel::TAXONOMY_CATEGORY );
73 + }
74 + }
75 +
76 + wp_set_post_terms( $course_id, $category_ids, CoursePostModel::TAXONOMY_CATEGORY );
77 + }
78 +
79 + /**
80 + * Update tags for course
81 + * Create tags if not exists
82 + *
83 + * @param $course_id
84 + * @param array $tag_ids
85 + *
86 + * @return void
87 + * @since 4.3.6
88 + * @version 1.0.0
89 + */
90 + public function update_tags( $course_id, array $tag_ids ) {
91 + // Create tags if not exists
92 + foreach ( $tag_ids as $tag_id ) {
93 + $term_check = term_exists( $tag_id, CoursePostModel::TAXONOMY_TAG );
94 + if ( ! $term_check ) {
95 + wp_insert_term( $tag_id, CoursePostModel::TAXONOMY_TAG );
96 + }
97 + }
98 +
99 + wp_set_post_terms( $course_id, $tag_ids, CoursePostModel::TAXONOMY_TAG );
100 + }
101 +
102 + /**
103 + * Duplicate course
104 + *
105 + * @throws Exception
106 + * @since 4.3.6
107 + * @version 1.0.0
108 + */
109 + public function duplicate( CourseModel $courseModel ): CourseModel {
110 + $coursePostModel = new CoursePostModel( $courseModel );
111 + $coursePostModel->get_all_metadata();
112 + $coursePostModelNew = new CoursePostModel( $coursePostModel );
113 + $coursePostModelNew->ID = null;
114 + $coursePostModelNew->post_title = $coursePostModelNew->post_title . ' (Copy)';
115 + $coursePostModelNew->save();
116 +
117 + // Duplicate sections
118 + $sections = $courseModel->get_section_items();
119 + foreach ( $sections as $section ) {
120 + $section_name = $section->section_name ?? $section->title ?? '';
121 + $section_description = $section->section_description ?? $section->description ?? '';
122 +
123 + $courseSectionModel = $coursePostModelNew->add_section(
124 + [
125 + 'section_name' => $section_name,
126 + 'section_description' => $section_description,
127 + ]
128 + );
129 +
130 + // Duplicate items for section
131 + $items = $section->items ?? [];
132 + foreach ( $items as $item ) {
133 + $item_title = $item->title ?? '';
134 + $item_type = $item->type ?? $item->item_type ?? '';
135 + $item_content = '';
136 +
137 + // Get item content from post
138 + $item_post = get_post( $item->item_id ?? $item->id ?? 0 );
139 + if ( $item_post ) {
140 + $item_content = $item_post->post_content ?? '';
141 + }
142 +
143 + $courseSectionModel->create_item_and_add(
144 + [
145 + 'item_title' => $item_title,
146 + 'item_type' => $item_type,
147 + 'item_content' => $item_content,
148 + ]
149 + );
150 + }
151 + }
152 +
153 + return $courseModel;
154 + }
155 +
156 + /**
157 + * Handle params before query list courses on table learnpress_courses
158 + *
159 + * @param CourseJsonFilter $filter
160 + * @param array $param
161 + *
162 + * @return void
163 + * @since 4.3.7
164 + * @version 1.0.0
165 + */
166 + public static function handle_params_for_query_list_courses( CourseJsonFilter &$filter, array $param = [] ) {
167 + $filter->page = absint( $param['paged'] ?? 1 );
168 + $filter->post_title = LP_Helper::sanitize_params_submitted( trim( $param['c_search'] ?? '' ) );
169 + $db = CourseJsonDB::getInstance();
170 +
171 + // Get Columns
172 + $fields_str = LP_Helper::sanitize_params_submitted( urldecode( $param['c_fields'] ?? '' ) );
173 + if ( ! empty( $fields_str ) ) {
174 + $fields = explode( ',', $fields_str );
175 + foreach ( $fields as $key => $field ) {
176 + $fields[ $key ] = $db->wpdb->prepare( '%i', $field );
177 + }
178 + $filter->fields = $fields;
179 + }
180 +
181 + // Get only columns
182 + $fields_only_str = LP_Helper::sanitize_params_submitted( urldecode( $param['c_only_fields'] ?? '' ) );
183 + if ( ! empty( $fields_only_str ) ) {
184 + $fields_only = explode( ',', $fields_only_str );
185 + foreach ( $fields_only as $key => $field ) {
186 + $fields_only[ $key ] = $db->wpdb->prepare( '%i', $field );
187 + }
188 + $filter->only_fields = $fields_only;
189 + }
190 +
191 + // Exclude Columns
192 + $fields_exclude_str = LP_Helper::sanitize_params_submitted( urldecode( $param['c_exclude_fields'] ?? '' ) );
193 + if ( ! empty( $fields_exclude_str ) ) {
194 + $fields_exclude = explode( ',', $fields_exclude_str );
195 + $filter->exclude_fields = $fields_exclude;
196 + }
197 +
198 + // Find by ids
199 + $course_ids_str = LP_Helper::sanitize_params_submitted( urldecode( $param['ids'] ?? '' ) );
200 + if ( ! empty( $course_ids_str ) ) {
201 + $course_ids = explode( ',', $course_ids_str );
202 + $filter->ids = $course_ids;
203 + }
204 +
205 + // Author
206 + $c_author = LP_Helper::sanitize_params_submitted( $param['c_author'] ?? 0 );
207 + if ( ! empty( $c_author ) ) {
208 + $filter->post_author = $c_author;
209 + }
210 + $author_ids_str = LP_Helper::sanitize_params_submitted( $param['c_authors'] ?? '' );
211 + if ( ! empty( $author_ids_str ) ) {
212 + $author_ids = explode( ',', $author_ids_str );
213 + $filter->post_authors = $author_ids;
214 + }
215 +
216 + // Find by status
217 + $post_status = LP_Helper::sanitize_params_submitted( $param['c_status'] ?? '' );
218 + if ( ! empty( $post_status ) ) {
219 + if ( 'all' !== $post_status ) {
220 + $filter->post_status = explode( ',', $post_status );
221 + }
222 +
223 + if ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR )
224 + || ! current_user_can( UserModel::ROLE_INSTRUCTOR ) ) {
225 + $filter->post_status = [ PostModel::STATUS_PUBLISH ];
226 + }
227 + }
228 +
229 + // Type price
230 + if ( ! empty( $param['c_type_price'] ) ) {
231 + $filter->type_price = explode( ',', LP_Helper::sanitize_params_submitted( urldecode( $param['c_type_price'] ) ) );
232 + }
233 +
234 + // On sale
235 + if ( isset( $param['on_sale'] ) ) {
236 + $filter->is_sale = 1;
237 + }
238 +
239 + // On feature
240 + if ( isset( $param['on_feature'] ) ) {
241 + $filter->is_feature = 1;
242 + }
243 +
244 + // Sort by level
245 + $levels_str = LP_Helper::sanitize_params_submitted( urldecode( $param['c_level'] ?? '' ) );
246 + if ( ! empty( $levels_str ) ) {
247 + $levels = explode( ',', $levels_str );
248 + if ( in_array( 'all', $levels ) ) {
249 + $levels[] = '';
250 + }
251 + $filter->levels = $levels;
252 + }
253 +
254 + // Sort by type (oline/offline)
255 + $course_type = LP_Helper::sanitize_params_submitted( urldecode( $param['c_type'] ?? '' ) );
256 + if ( ! empty( $course_type ) ) {
257 + $course_type = explode( ',', $course_type );
258 + if ( in_array( 'online', $course_type ) && in_array( 'offline', $course_type ) ) {
259 + $filter->type = 'all';
260 + } else {
261 + $filter->type = $course_type[0];
262 + }
263 + }
264 +
265 + // Check is in category page.
266 + if ( ! empty( $param['page_term_id_current'] ) && empty( $param['term_id'] ) ) {
267 + $filter->term_ids[] = $param['page_term_id_current'];
268 + } // Check is in tag page.
269 + elseif ( ! empty( $param['page_tag_id_current'] ) && empty( $param['tag_id'] ) ) {
270 + $filter->tag_ids[] = $param['page_tag_id_current'];
271 + }
272 +
273 + // Find by category
274 + $term_ids_str = LP_Helper::sanitize_params_submitted( urldecode( $param['term_id'] ?? '' ) );
275 + if ( ! empty( $term_ids_str ) ) {
276 + $term_ids = explode( ',', $term_ids_str );
277 + $filter->term_ids = array_merge( $filter->term_ids, $term_ids );
278 + }
279 +
280 + // Find by tag
281 + $tag_ids_str = LP_Helper::sanitize_params_submitted( urldecode( $param['tag_id'] ?? '' ) );
282 + if ( ! empty( $tag_ids_str ) ) {
283 + $tag_ids = explode( ',', $tag_ids_str );
284 + $filter->tag_ids = array_merge( $filter->tag_ids, $tag_ids );
285 + }
286 +
287 + // Order by
288 + $order_by = LP_Helper::sanitize_params_submitted( $param['order_by'] ?? 'post_date_gmt', 'key' );
289 + if ( $order_by === 'post_date' ) {
290 + $order_by = 'post_date_gmt';
291 + }
292 + $filter->order_by = $order_by;
293 + $filter->order = LP_Helper::sanitize_params_submitted( $param['order'] ?? 'DESC', 'key' );
294 + $filter->limit = $param['limit'] ?? LP_Settings::get_option( 'archive_course_limit', 10 );
295 +
296 + // For search suggest courses
297 + if ( ! empty( $param['c_suggest'] ) ) {
298 + $filter->only_fields = [ 'ID', 'post_title' ];
299 + $filter->limit = apply_filters( 'learn-press/services/rest-api/courses/suggest-limit', 10 );
300 + }
301 +
302 + do_action( 'learn-press/services/courses/handle_params_for_query_list_courses', $filter, $param );
303 + }
304 +
305 + /**
306 + * Get list courses on table learnpress_courses
307 + *
308 + * @param CourseJsonFilter $filter
309 + * @param int $total_rows
310 + *
311 + * @return array
312 + * @throws Exception
313 + * @version 1.0.0
314 + * @since 4.3.7
315 + */
316 + public static function get_list_courses( CourseJsonFilter $filter, int &$total_rows = 0 ): array {
317 + $db = CourseJsonDB::getInstance();
318 +
319 + try {
320 + $is_order_by_popular = 'popular' === $filter->order_by;
321 +
322 + // Order by
323 + switch ( $filter->order_by ) {
324 + case 'price':
325 + case 'price_low':
326 + if ( 'price_low' === $filter->order_by ) {
327 + $filter->order = 'ASC';
328 + } else {
329 + $filter->order = 'DESC';
330 + }
331 +
332 + $filter->order_by = 'price_to_sort';
333 + break;
334 + case 'popular':
335 + $filter = $db->get_courses_order_by_popular( $filter );
336 + break;
337 + case 'post_title':
338 + $filter->order = 'ASC';
339 + break;
340 + case 'post_title_desc':
341 + $filter->order_by = 'post_title';
342 + $filter->order = 'DESC';
343 + break;
344 + case 'menu_order':
345 + $filter->order_by = 'menu_order';
346 + $filter->order = 'ASC';
347 + break;
348 + default:
349 + $filter = apply_filters( 'lp/services/courses/filter/order_by/' . $filter->order_by, $filter );
350 + break;
351 + }
352 +
353 + // Query get results
354 + /**
355 + * @var CourseJsonFilter $filter
356 + */
357 + $filter = apply_filters( 'lp/services/courses/filter', $filter );
358 + $courses = $db->get_courses( $filter, $total_rows );
359 + } catch ( Throwable $e ) {
360 + $courses = [];
361 + LP_Debug::error_log( $e );
362 + }
363 +
364 + return $courses;
365 + }
366 +
367 + /**
368 + * Check item type is support of course
369 + *
370 + * @param string $item_type
371 + * @return bool
372 + */
373 + public static function check_is_item_type_of_course( string $item_type ): bool {
374 + $course_item_types = coursemodel::item_types_support();
375 + return in_array( $item_type, $course_item_types );
376 + }
377 +}