PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 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 All 140 releases
← All changes | inc/Ajax/EditCurriculumAjax.php +533 -526 4.3.9 → 4.4.9 View file →
@@ -1,526 +1,533 @@
1 -<?php
2 -/**
3 - * class EditCurriculumAjax
4 - *
5 - * This class handles the AJAX request to edit the curriculum of a course.
6 - *
7 - * @since 4.2.8.6
8 - * @version 1.0.1
9 - */
10 -
11 -namespace LearnPress\Ajax;
12 -
13 -use Exception;
14 -use LearnPress\Models\CourseModel;
15 -use LearnPress\Models\CoursePostModel;
16 -use LearnPress\Models\CourseSectionItemModel;
17 -use LearnPress\Models\CourseSectionModel;
18 -use LearnPress\Models\LessonPostModel;
19 -use LearnPress\Models\PostModel;
20 -use LearnPress\TemplateHooks\Course\AdminEditCurriculumTemplate;
21 -use LP_Helper;
22 -use LP_REST_Response;
23 -use Throwable;
24 -
25 -class EditCurriculumAjax extends AbstractAjax {
26 - /**
27 - * Check permissions and validate parameters.
28 - *
29 - * @throws Exception
30 - *
31 - * @since 4.2.8.6
32 - * @version 1.0.1
33 - */
34 - public static function check_valid() {
35 - $params = wp_unslash( $_REQUEST['data'] ?? '' );
36 - if ( empty( $params ) ) {
37 - throw new Exception( 'Error: params invalid!' );
38 - }
39 -
40 - return LP_Helper::json_decode( $params, true );
41 - }
42 -
43 - /**
44 - * Add section
45 - *
46 - * JS file edit-section.js: function addSection call this method to update the section description.
47 - *
48 - * @since 4.2.8.6
49 - * @version 1.0.1
50 - */
51 - public static function course_add_section() {
52 - $response = new LP_REST_Response();
53 -
54 - try {
55 - $data = self::check_valid();
56 -
57 - $course_id = $data['course_id'] ?? 0;
58 - $courseModel = CourseModel::find( $course_id, true );
59 - if ( ! $courseModel ) {
60 - throw new Exception( __( 'Course not found', 'learnpress' ) );
61 - }
62 -
63 - $section_name = trim( $data['section_name'] ?? '' );
64 - if ( empty( $section_name ) ) {
65 - throw new Exception( __( 'Section title is required', 'learnpress' ) );
66 - }
67 -
68 - // Add section to course.
69 - $coursePostModel = new CoursePostModel( $courseModel );
70 - $sectionNew = $coursePostModel->add_section(
71 - [
72 - 'section_name' => $section_name,
73 - 'section_description' => $data['section_description'] ?? '',
74 - ]
75 - );
76 -
77 - $response->data->section = $sectionNew;
78 - $response->status = 'success';
79 - $response->message = __( 'Section added successfully', 'learnpress' );
80 - } catch ( Throwable $e ) {
81 - $response->message = $e->getMessage();
82 - }
83 -
84 - wp_send_json( $response );
85 - }
86 -
87 - /**
88 - * Update section
89 - *
90 - * JS file edit-section.js: function updateSectionTitle call this method to update the section title.
91 - * JS file edit-section.js: function updateSectionDescription call this method to update the section description.
92 - *
93 - * @since 4.2.8.6
94 - * @version 1.0.1
95 - */
96 - public static function course_update_section() {
97 - $response = new LP_REST_Response();
98 -
99 - try {
100 - $data = self::check_valid();
101 - $course_id = $data['course_id'] ?? 0;
102 - $section_id = $data['section_id'] ?? 0;
103 -
104 - $courseModel = CourseModel::find( $course_id, true );
105 - if ( ! $courseModel ) {
106 - throw new Exception( __( 'Course not found', 'learnpress' ) );
107 - }
108 -
109 - $courseSectionModel = CourseSectionModel::find( $section_id, $course_id, true );
110 - if ( ! $courseSectionModel ) {
111 - throw new Exception( __( 'Section not found', 'learnpress' ) );
112 - }
113 -
114 - // Update section of course
115 - $coursePostModel = new CoursePostModel( $courseModel );
116 - $coursePostModel->update_section( $courseSectionModel, $data );
117 -
118 - $response->status = 'success';
119 - $response->message = __( 'Section updated successfully', 'learnpress' );
120 - } catch ( Throwable $e ) {
121 - $response->message = $e->getMessage();
122 - }
123 -
124 - wp_send_json( $response );
125 - }
126 -
127 - /**
128 - * Delete section
129 - *
130 - * JS file edit-section.js: function deleteSection call this method to update the section title.
131 - *
132 - * @since 4.2.8.6
133 - * @version 1.0.0
134 - */
135 - public static function course_delete_section() {
136 - $response = new LP_REST_Response();
137 -
138 - try {
139 - $data = self::check_valid();
140 - $course_id = $data['course_id'] ?? 0;
141 - $section_id = $data['section_id'] ?? 0;
142 -
143 - $courseModel = CourseModel::find( $course_id, true );
144 - if ( ! $courseModel ) {
145 - throw new Exception( __( 'Course not found', 'learnpress' ) );
146 - }
147 -
148 - $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
149 - if ( ! $courseSectionModel ) {
150 - throw new Exception( __( 'Section not found', 'learnpress' ) );
151 - }
152 -
153 - $courseSectionModel->delete();
154 -
155 - $response->status = 'success';
156 - $response->message = __( 'Section updated successfully', 'learnpress' );
157 - } catch ( Throwable $e ) {
158 - $response->message = $e->getMessage();
159 - }
160 -
161 - wp_send_json( $response );
162 - }
163 -
164 - /**
165 - * Update sections position
166 - * new_position => list of section id by order
167 - *
168 - * JS file edit-section.js: function sortAbleSection call this method.
169 - *
170 - * @since 4.2.8.6
171 - * @version 1.0.1
172 - */
173 - public static function course_update_section_position() {
174 - $response = new LP_REST_Response();
175 -
176 - try {
177 - $data = self::check_valid();
178 - $course_id = $data['course_id'] ?? 0;
179 - $new_position = $data['new_position'] ?? [];
180 - if ( ! is_array( $new_position ) ) {
181 - throw new Exception( __( 'Invalid section position', 'learnpress' ) );
182 - }
183 -
184 - $courseModel = CourseModel::find( $course_id, true );
185 - if ( ! $courseModel ) {
186 - throw new Exception( __( 'Course not found', 'learnpress' ) );
187 - }
188 -
189 - // Update all sections position
190 - $coursePostMoel = new CoursePostModel( $courseModel );
191 - $coursePostMoel->update_sections_position( [ 'new_position' => $new_position ] );
192 -
193 - $courseModel->sections_items = null;
194 - $courseModel->save();
195 -
196 - $response->status = 'success';
197 - $response->message = __( 'Section updated successfully', 'learnpress' );
198 - } catch ( Throwable $e ) {
199 - $response->message = $e->getMessage();
200 - }
201 -
202 - wp_send_json( $response );
203 - }
204 -
205 - /**
206 - * Create item and add to section
207 - *
208 - * $data['course_id'] => ID of course
209 - * $data['section_id'] => ID of section
210 - * $data['item_type'] => Type of item (e.g., 'lesson', 'quiz', etc.)
211 - * $data['item_title'] => Title of the item
212 - *
213 - * JS file edit-section-item.js: function addItemToSection call this method.
214 - *
215 - * @since 4.2.8.6
216 - * @version 1.0.1
217 - */
218 - public static function create_item_add_to_section() {
219 - $response = new LP_REST_Response();
220 -
221 - try {
222 - $data = self::check_valid();
223 - $course_id = $data['course_id'] ?? 0;
224 - $section_id = $data['section_id'] ?? 0;
225 -
226 - $courseModel = CourseModel::find( $course_id, true );
227 - if ( ! $courseModel ) {
228 - throw new Exception( __( 'Course not found', 'learnpress' ) );
229 - }
230 -
231 - $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
232 - if ( ! $courseSectionModel ) {
233 - throw new Exception( __( 'Section not found', 'learnpress' ) );
234 - }
235 -
236 - $courseSectionItemModel = $courseSectionModel->create_item_and_add( $data );
237 -
238 - $response->data->section_item = $courseSectionItemModel;
239 -
240 - /**
241 - * @var $itemModel PostModel
242 - */
243 - $itemModel = $courseModel->get_item_model(
244 - $courseSectionItemModel->item_id,
245 - $courseSectionItemModel->item_type
246 - );
247 - $response->data->item_link = $itemModel ? $itemModel->get_edit_link() : '';
248 -
249 - $response->status = 'success';
250 - $response->message = __( 'Item added to section successfully', 'learnpress' );
251 - } catch ( Throwable $e ) {
252 - $response->message = $e->getMessage();
253 - }
254 -
255 - wp_send_json( $response );
256 - }
257 -
258 - /**
259 - * Add items selected to section
260 - *
261 - * $data['course_id'] => ID of course
262 - * $data['section_id'] => ID of section
263 - * $data['items'] => [ item_id => 0, item_type => '' ]
264 - *
265 - * JS file edit-section-item.js: function addItemsSelectedToSection call this method.
266 - *
267 - * @since 4.2.8.6
268 - * @version 1.0.1
269 - */
270 - public static function add_items_to_section() {
271 - $response = new LP_REST_Response();
272 -
273 - try {
274 - $data = self::check_valid();
275 - $course_id = $data['course_id'] ?? 0;
276 - $section_id = $data['section_id'] ?? 0;
277 -
278 - $courseModel = CourseModel::find( $course_id, true );
279 - if ( ! $courseModel ) {
280 - throw new Exception( __( 'Course not found', 'learnpress' ) );
281 - }
282 -
283 - $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
284 - if ( ! $courseSectionModel ) {
285 - throw new Exception( __( 'Section not found', 'learnpress' ) );
286 - }
287 -
288 - $courseSectionItems = $courseSectionModel->add_items( $data );
289 - if ( empty( $courseSectionItems ) ) {
290 - throw new Exception( __( 'No items were added to the section', 'learnpress' ) );
291 - }
292 -
293 - // Set context_data for Course Builder context
294 - $is_course_builder = ! empty( $data['is_course_builder'] );
295 - AdminEditCurriculumTemplate::instance()->context_data = [
296 - 'is_course_builder' => $is_course_builder,
297 - ];
298 -
299 - $response->data->html = '';
300 - /**
301 - * @var $courseSectionItem CourseSectionItemModel
302 - */
303 - foreach ( $courseSectionItems as $courseSectionItem ) {
304 - $courseSectionItemAlias = (object) get_object_vars( $courseSectionItem );
305 - $itemModel = $courseModel->get_item_model(
306 - $courseSectionItem->item_id,
307 - $courseSectionItem->item_type
308 - );
309 - $courseSectionItemAlias->title = $itemModel ? $itemModel->get_the_title() : '';
310 - $response->data->html .= AdminEditCurriculumTemplate::instance()->html_section_item(
311 - $courseModel,
312 - $courseSectionItemAlias
313 - );
314 - }
315 -
316 - $response->status = 'success';
317 - $response->message = __( 'Items added to section successfully', 'learnpress' );
318 - } catch ( Throwable $e ) {
319 - $response->message = $e->getMessage();
320 - }
321 -
322 - wp_send_json( $response );
323 - }
324 -
325 - /**
326 - * Delete item from section
327 - *
328 - * $data['course_id'] => ID of course
329 - * $data['section_id'] => ID of section
330 - * $data['item_id'] => ID of item to delete
331 - *
332 - * JS file edit-section-item.js: function deleteItem call this method.
333 - */
334 - public static function delete_item_from_section() {
335 - $response = new LP_REST_Response();
336 -
337 - try {
338 - $data = self::check_valid();
339 - $course_id = $data['course_id'] ?? 0;
340 - $section_id = $data['section_id'] ?? 0;
341 - $item_id = $data['item_id'] ?? 0;
342 -
343 - $courseModel = CourseModel::find( $course_id, true );
344 - if ( ! $courseModel ) {
345 - throw new Exception( __( 'Course not found', 'learnpress' ) );
346 - }
347 -
348 - $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
349 - if ( ! $courseSectionModel ) {
350 - throw new Exception( __( 'Section not found', 'learnpress' ) );
351 - }
352 -
353 - // Find item of section id
354 - $courseSectionItemModel = CourseSectionItemModel::find( $section_id, $item_id );
355 - if ( ! $courseSectionItemModel ) {
356 - throw new Exception( __( 'Item not found in section', 'learnpress' ) );
357 - }
358 -
359 - // Delete item from section
360 - $courseSectionItemModel->section_course_id = $course_id;
361 - $courseSectionItemModel->delete();
362 -
363 - $response->status = 'success';
364 - $response->message = __( 'Item deleted from section successfully', 'learnpress' );
365 - } catch ( Throwable $e ) {
366 - $response->message = $e->getMessage();
367 - }
368 -
369 - wp_send_json( $response );
370 - }
371 -
372 - /**
373 - * Update item on new section and position
374 - *
375 - * $data['course_id'] => ID of course
376 - * $data['items_position'] => list of item id by order on section new
377 - * $data['item_id_change'] => ID of item to change section
378 - * $data['section_id_new_of_item'] => ID of new section of item
379 - * $data['section_id_old_of_item'] => ID of old section of item
380 - *
381 - * JS file edit-section-item.js: function sortAbleItem call this method.
382 - *
383 - * @since 4.2.8.6
384 - * @version 1.0.1
385 - */
386 - public static function update_item_section_and_position() {
387 - $response = new LP_REST_Response();
388 -
389 - try {
390 - $data = self::check_valid();
391 - $course_id = $data['course_id'] ?? 0;
392 -
393 - $courseModel = CourseModel::find( $course_id, true );
394 - if ( ! $courseModel ) {
395 - throw new Exception( __( 'Course not found', 'learnpress' ) );
396 - }
397 -
398 - $coursePostModel = new CoursePostModel( $courseModel );
399 - $coursePostModel->update_items_position( $data );
400 -
401 - $response->status = 'success';
402 - $response->message = __( 'Item position updated successfully', 'learnpress' );
403 - } catch ( Throwable $e ) {
404 - $response->message = $e->getMessage();
405 - }
406 -
407 - wp_send_json( $response );
408 - }
409 -
410 - /**
411 - * Update data of item in section
412 - *
413 - * $data['course_id'] => ID of course
414 - * $data['section_id'] => ID of section
415 - * $data['item_id'] => ID of item to update
416 - * $data['item_type'] => Type of item (e.g., 'lesson', 'quiz', etc.)
417 - * $data['item_title'] => New title of the item
418 - *
419 - * JS file edit-section-item.js: function updateTitle call this method.
420 - *
421 - * @since 4.2.8.6
422 - * @version 1.0.0
423 - */
424 - public static function update_item_of_section() {
425 - $response = new LP_REST_Response();
426 -
427 - try {
428 - $data = self::check_valid();
429 - $course_id = $data['course_id'] ?? 0;
430 - $section_id = $data['section_id'] ?? 0;
431 - $item_id = $data['item_id'] ?? 0;
432 - $item_type = $data['item_type'] ?? '';
433 - $item_title = $data['item_title'] ?? '';
434 -
435 - if ( empty( $item_title ) ) {
436 - throw new Exception( __( 'Item title is required', 'learnpress' ) );
437 - }
438 -
439 - $courseModel = CourseModel::find( $course_id, true );
440 - if ( ! $courseModel ) {
441 - throw new Exception( __( 'Course not found', 'learnpress' ) );
442 - }
443 -
444 - $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
445 - if ( ! $courseSectionModel ) {
446 - throw new Exception( __( 'Section not found', 'learnpress' ) );
447 - }
448 -
449 - $sectionItemModel = CourseSectionItemModel::find( $section_id, $item_id );
450 - if ( ! $sectionItemModel ) {
451 - throw new Exception( __( 'Item not found in section', 'learnpress' ) );
452 - }
453 -
454 - /**
455 - * @var $itemModel PostModel
456 - */
457 - $itemModel = $courseModel->get_item_model( $item_id, $item_type );
458 - if ( ! $itemModel ) {
459 - throw new Exception( __( 'Item not found', 'learnpress' ) );
460 - }
461 -
462 - $itemModel->post_title = $item_title;
463 - $itemModel->save();
464 -
465 - $courseModel->sections_items = null;
466 - $courseModel->save();
467 -
468 - $response->status = 'success';
469 - $response->message = __( 'Item updated successfully', 'learnpress' );
470 - } catch ( Throwable $e ) {
471 - $response->message = $e->getMessage();
472 - }
473 -
474 - wp_send_json( $response );
475 - }
476 -
477 - /**
478 - * Update item preview.
479 - *
480 - * JS file edit-section-item.js: function updatePreviewItem call this method.
481 - *
482 - * @since 4.2.8.6
483 - * @version 1.0.2
484 - */
485 - public static function update_item_preview() {
486 - $response = new LP_REST_Response();
487 -
488 - try {
489 - $data = self::check_valid();
490 - $course_id = $data['course_id'] ?? 0;
491 - $item_id = $data['item_id'] ?? 0;
492 - $item_type = $data['item_type'] ?? '';
493 - $enable_preview = $data['enable_preview'] ?? 0;
494 -
495 - $courseModel = CourseModel::find( $course_id, true );
496 - if ( ! $courseModel ) {
497 - throw new Exception( __( 'Course not found', 'learnpress' ) );
498 - }
499 -
500 - if ( $item_type !== LP_LESSON_CPT ) {
501 - throw new Exception( __( 'Only lesson can be set preview', 'learnpress' ) );
502 - }
503 -
504 - /**
505 - * @var $itemModel LessonPostModel
506 - */
507 - $itemModel = $courseModel->get_item_model( $item_id, $item_type );
508 - if ( ! $itemModel ) {
509 - throw new Exception( __( 'Item not found', 'learnpress' ) );
510 - }
511 -
512 - $itemModel->set_preview( $enable_preview == 1 );
513 -
514 - // Save course to update preview data of item in the curriculum.
515 - $coursePostModel = new CoursePostModel( $courseModel );
516 - $coursePostModel->save();
517 -
518 - $response->status = 'success';
519 - $response->message = __( 'Item updated successfully', 'learnpress' );
520 - } catch ( Throwable $e ) {
521 - $response->message = $e->getMessage();
522 - }
523 -
524 - wp_send_json( $response );
525 - }
526 -}
1 +<?php
2 +/**
3 + * class EditCurriculumAjax
4 + *
5 + * This class handles the AJAX request to edit the curriculum of a course.
6 + *
7 + * @since 4.2.8.6
8 + * @version 1.0.1
9 + */
10 +
11 +namespace LearnPress\Ajax;
12 +
13 +use Exception;
14 +use LearnPress\Models\CourseModel;
15 +use LearnPress\Models\CoursePostModel;
16 +use LearnPress\Models\CourseSectionItemModel;
17 +use LearnPress\Models\CourseSectionModel;
18 +use LearnPress\Models\LessonPostModel;
19 +use LearnPress\Models\PostModel;
20 +use LearnPress\TemplateHooks\Course\AdminEditCurriculumTemplate;
21 +use LP_Helper;
22 +use LP_REST_Response;
23 +use Throwable;
24 +
25 +class EditCurriculumAjax extends AbstractAjax {
26 + /**
27 + * Check permissions and validate parameters.
28 + *
29 + * @throws Exception
30 + *
31 + * @since 4.2.8.6
32 + * @version 1.0.1
33 + */
34 + public static function check_valid() {
35 + $params = wp_unslash( $_REQUEST['data'] ?? '' );
36 + if ( empty( $params ) ) {
37 + throw new Exception( 'Error: params invalid!' );
38 + }
39 +
40 + return LP_Helper::json_decode( $params, true );
41 + }
42 +
43 + /**
44 + * Add section
45 + *
46 + * JS file edit-section.js: function addSection call this method to update the section description.
47 + *
48 + * @since 4.2.8.6
49 + * @version 1.0.1
50 + */
51 + public static function course_add_section() {
52 + $response = new LP_REST_Response();
53 +
54 + try {
55 + $data = self::check_valid();
56 +
57 + $course_id = $data['course_id'] ?? 0;
58 + $courseModel = CourseModel::find( $course_id, true );
59 + if ( ! $courseModel ) {
60 + throw new Exception( __( 'Course not found', 'learnpress' ) );
61 + }
62 +
63 + $section_name = trim( $data['section_name'] ?? '' );
64 + if ( empty( $section_name ) ) {
65 + throw new Exception( __( 'Section title is required', 'learnpress' ) );
66 + }
67 +
68 + // Add section to course.
69 + $coursePostModel = new CoursePostModel( $courseModel );
70 + $sectionNew = $coursePostModel->add_section(
71 + [
72 + 'section_name' => $section_name,
73 + 'section_description' => $data['section_description'] ?? '',
74 + ]
75 + );
76 +
77 + $response->data->section = $sectionNew;
78 + $response->status = 'success';
79 + $response->message = __( 'Section added successfully', 'learnpress' );
80 + } catch ( Throwable $e ) {
81 + $response->message = $e->getMessage();
82 + }
83 +
84 + wp_send_json( $response );
85 + }
86 +
87 + /**
88 + * Update section
89 + *
90 + * JS file edit-section.js: function updateSectionTitle call this method to update the section title.
91 + * JS file edit-section.js: function updateSectionDescription call this method to update the section description.
92 + *
93 + * @since 4.2.8.6
94 + * @version 1.0.1
95 + */
96 + public static function course_update_section() {
97 + $response = new LP_REST_Response();
98 +
99 + try {
100 + $data = self::check_valid();
101 + $course_id = $data['course_id'] ?? 0;
102 + $section_id = $data['section_id'] ?? 0;
103 +
104 + $courseModel = CourseModel::find( $course_id, true );
105 + if ( ! $courseModel ) {
106 + throw new Exception( __( 'Course not found', 'learnpress' ) );
107 + }
108 +
109 + $courseSectionModel = CourseSectionModel::find( $section_id, $course_id, true );
110 + if ( ! $courseSectionModel ) {
111 + throw new Exception( __( 'Section not found', 'learnpress' ) );
112 + }
113 +
114 + // Update section of course
115 + $coursePostModel = new CoursePostModel( $courseModel );
116 + $coursePostModel->update_section( $courseSectionModel, $data );
117 +
118 + $response->status = 'success';
119 + $response->message = __( 'Section updated successfully', 'learnpress' );
120 + } catch ( Throwable $e ) {
121 + $response->message = $e->getMessage();
122 + }
123 +
124 + wp_send_json( $response );
125 + }
126 +
127 + /**
128 + * Delete section
129 + *
130 + * JS file edit-section.js: function deleteSection call this method to update the section title.
131 + *
132 + * @since 4.2.8.6
133 + * @version 1.0.0
134 + */
135 + public static function course_delete_section() {
136 + $response = new LP_REST_Response();
137 +
138 + try {
139 + $data = self::check_valid();
140 + $course_id = $data['course_id'] ?? 0;
141 + $section_id = $data['section_id'] ?? 0;
142 +
143 + $courseModel = CourseModel::find( $course_id, true );
144 + if ( ! $courseModel ) {
145 + throw new Exception( __( 'Course not found', 'learnpress' ) );
146 + }
147 +
148 + $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
149 + if ( ! $courseSectionModel ) {
150 + throw new Exception( __( 'Section not found', 'learnpress' ) );
151 + }
152 +
153 + $courseSectionModel->delete();
154 +
155 + $response->status = 'success';
156 + $response->message = __( 'Section updated successfully', 'learnpress' );
157 + } catch ( Throwable $e ) {
158 + $response->message = $e->getMessage();
159 + }
160 +
161 + wp_send_json( $response );
162 + }
163 +
164 + /**
165 + * Update sections position
166 + * new_position => list of section id by order
167 + *
168 + * JS file edit-section.js: function sortAbleSection call this method.
169 + *
170 + * @since 4.2.8.6
171 + * @version 1.0.1
172 + */
173 + public static function course_update_section_position() {
174 + $response = new LP_REST_Response();
175 +
176 + try {
177 + $data = self::check_valid();
178 + $course_id = $data['course_id'] ?? 0;
179 + $new_position = $data['new_position'] ?? [];
180 + if ( ! is_array( $new_position ) ) {
181 + throw new Exception( __( 'Invalid section position', 'learnpress' ) );
182 + }
183 +
184 + $courseModel = CourseModel::find( $course_id, true );
185 + if ( ! $courseModel ) {
186 + throw new Exception( __( 'Course not found', 'learnpress' ) );
187 + }
188 +
189 + // Update all sections position
190 + $coursePostMoel = new CoursePostModel( $courseModel );
191 + $coursePostMoel->update_sections_position( [ 'new_position' => $new_position ] );
192 +
193 + $courseModel->sections_items = null;
194 + $courseModel->save();
195 +
196 + $response->status = 'success';
197 + $response->message = __( 'Section updated successfully', 'learnpress' );
198 + } catch ( Throwable $e ) {
199 + $response->message = $e->getMessage();
200 + }
201 +
202 + wp_send_json( $response );
203 + }
204 +
205 + /**
206 + * Create item and add to section
207 + *
208 + * $data['course_id'] => ID of course
209 + * $data['section_id'] => ID of section
210 + * $data['item_type'] => Type of item (e.g., 'lesson', 'quiz', etc.)
211 + * $data['item_title'] => Title of the item
212 + *
213 + * JS file edit-section-item.js: function addItemToSection call this method.
214 + *
215 + * @since 4.2.8.6
216 + * @version 1.0.2
217 + */
218 + public static function create_item_add_to_section() {
219 + $response = new LP_REST_Response();
220 +
221 + try {
222 + $data = self::check_valid();
223 + $course_id = $data['course_id'] ?? 0;
224 + $section_id = $data['section_id'] ?? 0;
225 +
226 + $courseModel = CourseModel::find( $course_id, true );
227 + if ( ! $courseModel ) {
228 + throw new Exception( __( 'Course not found', 'learnpress' ) );
229 + }
230 +
231 + $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
232 + if ( ! $courseSectionModel ) {
233 + throw new Exception( __( 'Section not found', 'learnpress' ) );
234 + }
235 +
236 + $courseSectionItemModel = $courseSectionModel->create_item_and_add( $data );
237 +
238 + // Reload course model to get updated sections
239 + $courseModel = CourseModel::find( $course_id, true );
240 +
241 + $response->data->section_item = $courseSectionItemModel;
242 +
243 + /**
244 + * @var $itemModel PostModel
245 + */
246 + $itemModel = $courseModel->get_item_model(
247 + $courseSectionItemModel->item_id,
248 + $courseSectionItemModel->item_type
249 + );
250 + $response->data->item_link = $itemModel ? $itemModel->get_edit_link() : '';
251 +
252 + $response->status = 'success';
253 + $response->message = __( 'Item added to section successfully', 'learnpress' );
254 + } catch ( Throwable $e ) {
255 + $response->message = $e->getMessage();
256 + }
257 +
258 + wp_send_json( $response );
259 + }
260 +
261 + /**
262 + * Add items selected to section
263 + *
264 + * $data['course_id'] => ID of course
265 + * $data['section_id'] => ID of section
266 + * $data['items'] => [ item_id => 0, item_type => '' ]
267 + *
268 + * JS file edit-section-item.js: function addItemsSelectedToSection call this method.
269 + *
270 + * @since 4.2.8.6
271 + * @version 1.0.2
272 + */
273 + public static function add_items_to_section() {
274 + $response = new LP_REST_Response();
275 +
276 + try {
277 + $data = self::check_valid();
278 + $course_id = $data['course_id'] ?? 0;
279 + $section_id = $data['section_id'] ?? 0;
280 +
281 + $courseModel = CourseModel::find( $course_id, true );
282 + if ( ! $courseModel ) {
283 + throw new Exception( __( 'Course not found', 'learnpress' ) );
284 + }
285 +
286 + $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
287 + if ( ! $courseSectionModel ) {
288 + throw new Exception( __( 'Section not found', 'learnpress' ) );
289 + }
290 +
291 + $courseSectionItems = $courseSectionModel->add_items( $data );
292 + if ( empty( $courseSectionItems ) ) {
293 + throw new Exception( __( 'No items were added to the section', 'learnpress' ) );
294 + }
295 +
296 + // Reload course model to get updated sections
297 + $courseModel = CourseModel::find( $course_id, true );
298 +
299 + // Set context_data for Course Builder context
300 + $is_course_builder = ! empty( $data['is_course_builder'] );
301 + AdminEditCurriculumTemplate::instance()->context_data = [
302 + 'is_course_builder' => $is_course_builder,
303 + ];
304 +
305 + $response->data->html = '';
306 + /**
307 + * @var $courseSectionItem CourseSectionItemModel
308 + */
309 + foreach ( $courseSectionItems as $courseSectionItem ) {
310 + $courseSectionItemAlias = (object) get_object_vars( $courseSectionItem );
311 + $itemModel = $courseModel->get_item_model(
312 + $courseSectionItem->item_id,
313 + $courseSectionItem->item_type
314 + );
315 + $courseSectionItemAlias->title = $itemModel ? $itemModel->get_the_title() : '';
316 + $response->data->html .= AdminEditCurriculumTemplate::instance()->html_section_item(
317 + $courseModel,
318 + $courseSectionItemAlias
319 + );
320 + }
321 +
322 + $response->status = 'success';
323 + $response->message = __( 'Items added to section successfully', 'learnpress' );
324 + } catch ( Throwable $e ) {
325 + $response->message = $e->getMessage();
326 + }
327 +
328 + wp_send_json( $response );
329 + }
330 +
331 + /**
332 + * Delete item from section
333 + *
334 + * $data['course_id'] => ID of course
335 + * $data['section_id'] => ID of section
336 + * $data['item_id'] => ID of item to delete
337 + *
338 + * JS file edit-section-item.js: function deleteItem call this method.
339 + */
340 + public static function delete_item_from_section() {
341 + $response = new LP_REST_Response();
342 +
343 + try {
344 + $data = self::check_valid();
345 + $course_id = $data['course_id'] ?? 0;
346 + $section_id = $data['section_id'] ?? 0;
347 + $item_id = $data['item_id'] ?? 0;
348 +
349 + $courseModel = CourseModel::find( $course_id, true );
350 + if ( ! $courseModel ) {
351 + throw new Exception( __( 'Course not found', 'learnpress' ) );
352 + }
353 +
354 + $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
355 + if ( ! $courseSectionModel ) {
356 + throw new Exception( __( 'Section not found', 'learnpress' ) );
357 + }
358 +
359 + // Find item of section id
360 + $courseSectionItemModel = CourseSectionItemModel::find( $section_id, $item_id );
361 + if ( ! $courseSectionItemModel ) {
362 + throw new Exception( __( 'Item not found in section', 'learnpress' ) );
363 + }
364 +
365 + // Delete item from section
366 + $courseSectionItemModel->section_course_id = $course_id;
367 + $courseSectionItemModel->delete();
368 +
369 + $response->status = 'success';
370 + $response->message = __( 'Item deleted from section successfully', 'learnpress' );
371 + } catch ( Throwable $e ) {
372 + $response->message = $e->getMessage();
373 + }
374 +
375 + wp_send_json( $response );
376 + }
377 +
378 + /**
379 + * Update item on new section and position
380 + *
381 + * $data['course_id'] => ID of course
382 + * $data['items_position'] => list of item id by order on section new
383 + * $data['item_id_change'] => ID of item to change section
384 + * $data['section_id_new_of_item'] => ID of new section of item
385 + * $data['section_id_old_of_item'] => ID of old section of item
386 + *
387 + * JS file edit-section-item.js: function sortAbleItem call this method.
388 + *
389 + * @since 4.2.8.6
390 + * @version 1.0.1
391 + */
392 + public static function update_item_section_and_position() {
393 + $response = new LP_REST_Response();
394 +
395 + try {
396 + $data = self::check_valid();
397 + $course_id = $data['course_id'] ?? 0;
398 +
399 + $courseModel = CourseModel::find( $course_id, true );
400 + if ( ! $courseModel ) {
401 + throw new Exception( __( 'Course not found', 'learnpress' ) );
402 + }
403 +
404 + $coursePostModel = new CoursePostModel( $courseModel );
405 + $coursePostModel->update_items_position( $data );
406 +
407 + $response->status = 'success';
408 + $response->message = __( 'Item position updated successfully', 'learnpress' );
409 + } catch ( Throwable $e ) {
410 + $response->message = $e->getMessage();
411 + }
412 +
413 + wp_send_json( $response );
414 + }
415 +
416 + /**
417 + * Update data of item in section
418 + *
419 + * $data['course_id'] => ID of course
420 + * $data['section_id'] => ID of section
421 + * $data['item_id'] => ID of item to update
422 + * $data['item_type'] => Type of item (e.g., 'lesson', 'quiz', etc.)
423 + * $data['item_title'] => New title of the item
424 + *
425 + * JS file edit-section-item.js: function updateTitle call this method.
426 + *
427 + * @since 4.2.8.6
428 + * @version 1.0.0
429 + */
430 + public static function update_item_of_section() {
431 + $response = new LP_REST_Response();
432 +
433 + try {
434 + $data = self::check_valid();
435 + $course_id = $data['course_id'] ?? 0;
436 + $section_id = $data['section_id'] ?? 0;
437 + $item_id = $data['item_id'] ?? 0;
438 + $item_type = $data['item_type'] ?? '';
439 + $item_title = $data['item_title'] ?? '';
440 +
441 + if ( empty( $item_title ) ) {
442 + throw new Exception( __( 'Item title is required', 'learnpress' ) );
443 + }
444 +
445 + $courseModel = CourseModel::find( $course_id, true );
446 + if ( ! $courseModel ) {
447 + throw new Exception( __( 'Course not found', 'learnpress' ) );
448 + }
449 +
450 + $courseSectionModel = CourseSectionModel::find( $section_id, $course_id );
451 + if ( ! $courseSectionModel ) {
452 + throw new Exception( __( 'Section not found', 'learnpress' ) );
453 + }
454 +
455 + $sectionItemModel = CourseSectionItemModel::find( $section_id, $item_id );
456 + if ( ! $sectionItemModel ) {
457 + throw new Exception( __( 'Item not found in section', 'learnpress' ) );
458 + }
459 +
460 + /**
461 + * @var $itemModel PostModel
462 + */
463 + $itemModel = $courseModel->get_item_model( $item_id, $item_type );
464 + if ( ! $itemModel ) {
465 + throw new Exception( __( 'Item not found', 'learnpress' ) );
466 + }
467 +
468 + $itemModel->post_title = $item_title;
469 + $itemModel->save();
470 +
471 + $courseModel->sections_items = null;
472 + $courseModel->save();
473 +
474 + $response->status = 'success';
475 + $response->message = __( 'Item updated successfully', 'learnpress' );
476 + } catch ( Throwable $e ) {
477 + $response->message = $e->getMessage();
478 + }
479 +
480 + wp_send_json( $response );
481 + }
482 +
483 + /**
484 + * Update item preview.
485 + *
486 + * JS file edit-section-item.js: function updatePreviewItem call this method.
487 + *
488 + * @since 4.2.8.6
489 + * @version 1.0.3
490 + */
491 + public static function update_item_preview() {
492 + $response = new LP_REST_Response();
493 +
494 + try {
495 + $data = self::check_valid();
496 + $course_id = absint( $data['course_id'] ?? 0 );
497 + $item_id = absint( $data['item_id'] ?? 0 );
498 + $item_type = LP_Helper::sanitize_params_submitted( $data['item_type'] ?? '' );
499 + $enable_preview = absint( $data['enable_preview'] ?? 0 );
500 +
501 + $courseModel = CourseModel::find( $course_id, true );
502 + if ( ! $courseModel ) {
503 + throw new Exception( __( 'Course not found', 'learnpress' ) );
504 + }
505 +
506 + if ( $item_type !== LP_LESSON_CPT ) {
507 + throw new Exception( __( 'Only lesson can be set preview', 'learnpress' ) );
508 + }
509 +
510 + /**
511 + * @var $itemModel LessonPostModel
512 + */
513 + $itemModel = $courseModel->get_item_model( $item_id, $item_type );
514 + if ( ! $itemModel ) {
515 + throw new Exception( __( 'Item not found', 'learnpress' ) );
516 + }
517 +
518 + $itemModel->set_preview( $enable_preview == 1 );
519 + $itemModel->save();
520 +
521 + // Save course to update preview data of item in the curriculum.
522 + $coursePostModel = new CoursePostModel( $courseModel );
523 + $coursePostModel->save();
524 +
525 + $response->status = 'success';
526 + $response->message = __( 'Item updated successfully', 'learnpress' );
527 + } catch ( Throwable $e ) {
528 + $response->message = $e->getMessage();
529 + }
530 +
531 + wp_send_json( $response );
532 + }
533 +}