PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.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 4.2.1 All 138 releases
learnpress / inc / Services / CourseService.php

CourseService.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.8, at inc/Services/CourseService.php

366 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
366