PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.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 4.2.0 All 139 releases
learnpress / inc / curds / class-lp-section-curd.php

class-lp-section-curd.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.6.9, at inc/curds/class-lp-section-curd.php

693 lines 16.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class LP_Section_CURD
5 *
6 * @since 3.0.0
7 */
8 class LP_Section_CURD extends LP_Object_Data_CURD implements LP_Interface_CURD {
9 /**
10 * @var int
11 *
12 * @since 3.0.0
13 */
14 private $course_id = null;
15
16 /**
17 * LP_Section_CURD constructor.
18 *
19 * @param $course_id
20 *
21 * @since 3.0.0
22 */
23 public function __construct( $course_id ) {
24 $this->course_id = $course_id;
25 }
26
27 /**
28 * Create item and insert to database.
29 *
30 * @param $args array
31 *
32 * @return mixed
33 * @since 3.0.0
34 */
35 public function create( &$args ) {
36 global $wpdb;
37 $section = [];
38
39 try {
40 $section = $this->parse( $args );
41 $section = stripslashes_deep( $section );
42 $last_section_order_number = LP_Section_DB::getInstance()->get_last_number_order( $section['section_course_id'] );
43 $section['section_order'] = $last_section_order_number + 1;
44 $insert_data = array(
45 'section_course_id' => $this->course_id,
46 'section_name' => $section['section_name'],
47 'section_order' => $section['section_order'],
48 'section_description' => $section['section_description'],
49 );
50
51 $wpdb->insert(
52 $wpdb->learnpress_sections,
53 $insert_data,
54 array( '%d', '%s', '%d', '%s' )
55 );
56 $section['section_id'] = $wpdb->insert_id;
57 } catch ( Throwable $e ) {
58 error_log( $e->getMessage() );
59 }
60
61 return $section;
62 }
63
64 /**
65 * Update data into database.
66 *
67 * @param $args
68 *
69 * @return mixed
70 * @since 3.0.0
71 */
72 public function update( &$args ) {
73
74 $section = $this->parse( $args );
75
76 if ( empty( $section['section_id'] ) ) {
77 return $this->create( $args );
78 }
79
80 $section_id = $section['section_id'];
81 $update_data = array(
82 'section_name' => $section['section_name'],
83 'section_course_id' => $section['section_course_id'],
84 'section_order' => $section['section_order'],
85 'section_description' => $section['section_description'],
86 );
87
88 global $wpdb;
89
90 $wpdb->update(
91 $wpdb->learnpress_sections,
92 $update_data,
93 array( 'section_id' => $section_id )
94 );
95 $section['section_id'] = $section_id;
96
97 return $section;
98 }
99
100 /**
101 * Delete section data from database.
102 *
103 * @param $id string
104 *
105 * @return bool
106 * @since 3.0.0
107 */
108 public function delete( &$id ) {
109
110 global $wpdb;
111
112 // allow hook
113 do_action( 'learn-press/before-delete-section', $this->course_id, $id );
114
115 // Remove section items.
116 $wpdb->delete( $wpdb->learnpress_section_items, array( 'section_id' => $id ) );
117 learn_press_reset_auto_increment( 'learnpress_section_items' );
118
119 // Remove section
120 $result = $wpdb->delete( $wpdb->learnpress_sections, array( 'section_id' => $id ) );
121 learn_press_reset_auto_increment( 'learnpress_sections' );
122
123 return ! ! $result;
124 }
125
126 public function duplicate( &$section, $args = array() ) {
127 // TODO: Implement duplicate() method.
128 }
129
130 /**
131 * Remove all items from each section and delete course's sections.
132 *
133 * @return bool
134 * @since 3.0.0
135 * @editor tungnx
136 * @modify 4.1.4.1 - comment - not use
137 */
138 /*public function clear() {
139
140 $sections_ids = LP_Object_Cache::get( 'course-' . $this->course_id, 'learn-press/course-sections-ids' );
141
142 if ( ! $sections_ids ) {
143 return false;
144 }
145
146 global $wpdb;
147
148 do_action( 'learn-press/before-clear-section', $this->course_id );
149
150 // Remove all items in course's sections
151 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}learnpress_section_items WHERE %d AND section_id IN(" . join( ',', $sections_ids ) . ')', 1 ) );
152 learn_press_reset_auto_increment( 'learnpress_section_items' );
153 // delete sections ids cache
154 // wp_cache_delete( 'course-' . $this->course_id, 'learn-press/course-sections-ids' );
155
156 // delete sections in course
157 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}learnpress_sections WHERE section_course_id = %d", $this->course_id ) );
158 learn_press_reset_auto_increment( 'learnpress_sections' );
159 // delete sections cache
160 // wp_cache_delete( 'course-' . $this->course_id, 'learn-press/course-sections' );
161
162 LP_Course_Utils::set_cache_db_sections( $this->course_id, false );
163
164 return true;
165 }*/
166
167 /**
168 * Load data from database.
169 *
170 * @param object $object
171 *
172 * @return mixed
173 * @since 3.0.0
174 */
175 public function load( &$object ) {
176 // TODO: Implement load() method.
177 }
178
179 /**
180 * Parse input data.
181 *
182 * @param $args
183 *
184 * @return array
185 * @since 3.0.0
186 */
187 private function parse( $args ) {
188 $data = wp_parse_args(
189 $args,
190 array(
191 'section_name' => '',
192 'section_description' => '',
193 'section_course_id' => 0,
194 'section_order' => 0,
195 'items' => array(),
196 )
197 );
198
199 return $data;
200 }
201
202 /**
203 * Get course sections ids and set data to cache.
204 *
205 * @return array
206 * @since 3.0.0
207 * @deprecated 4.1.6.3
208 */
209 /*public function read_sections_ids() {
210
211 // Get course's sections id data from cache
212 $ids = LP_Object_Cache::get( 'course-' . $this->course_id, 'learn-press/course-sections-ids' );
213
214 if ( ! $ids ) {
215 global $wpdb;
216 // get sections id
217 $ids = $wpdb->get_col( $wpdb->prepare( "SELECT section_id FROM {$wpdb->prefix}learnpress_sections WHERE section_course_id = %d", $this->course_id ) );
218 // Set cache
219 LP_Object_Cache::set( 'course-' . $this->course_id, $ids, 'learn-press/course-sections-ids' );
220 }
221
222 return $ids;
223 }*/
224
225 /**
226 * Read all items from DB
227 *
228 * @param int $section_id
229 *
230 * @return array
231 * @depecated 4.1.6.9 Remove when release Addon Frontend Editor 4.0.1
232 */
233 public function read_items( $section_id ) {
234 global $wpdb;
235
236 $query = $wpdb->prepare(
237 "
238 SELECT item_id id
239 FROM {$wpdb->learnpress_section_items} si
240 INNER JOIN {$wpdb->learnpress_sections} s ON si.section_id = s.section_id
241 INNER JOIN {$wpdb->posts} c ON c.ID = s.section_course_id
242 INNER JOIN {$wpdb->posts} it ON it.ID = si.item_id
243 WHERE s.section_id = %d
244 AND it.post_status = %s
245 ORDER BY si.item_order, si.section_item_id ASC
246 ",
247 $section_id,
248 'publish'
249 );
250
251 return $wpdb->get_col( $query );
252 }
253
254 /**
255 * Update sort sections.
256 *
257 * @param string[] $sections_new_order
258 *
259 * @return array
260 * @version 4.0.1
261 */
262 public function update_sections_order( $sections_new_order ) {
263 global $wpdb;
264
265 //$current_sections = LP_Course_Utils::get_cached_db_sections( $this->course_id );
266 //$new_sections = array();
267 //$course = learn_press_get_course( $this->course_id );
268 //$sections_items = $course->get_full_sections_and_items_course();
269
270 $orders = array();
271
272 foreach ( $sections_new_order as $order_new => $section_id ) {
273 $order_new = $order_new + 1;
274
275 $wpdb->update(
276 $wpdb->learnpress_sections,
277 array( 'section_order' => $order_new ),
278 array( 'section_id' => $section_id )
279 );
280
281 // $this->get_section_items( $section_id );
282 }
283
284 /*foreach ( $sections as $index => $section_id ) {
285 $order = $index + 1;
286
287 $orders[ $section_id ] = $order;
288
289 $wpdb->update(
290 $wpdb->learnpress_sections,
291 array( 'section_order' => $order ),
292 array( 'section_id' => $section_id )
293 );
294
295 foreach ( $current_sections as $current_section ) {
296 if ( $current_section->section_id == $section_id ) {
297 $new_sections[ $index ] = $current_section;
298 }
299 }
300
301 $this->get_section_items( $section_id );
302 }*/
303
304 //LP_Course_Utils::set_cache_db_sections( $this->course_id, $new_sections );
305
306 return $orders;
307 }
308
309 /**
310 * Get list items of section.
311 *
312 * @param $section_id
313 *
314 * @return array
315 * @since 3.0.0
316 */
317 public function get_section_items( $section_id ) {
318 $course = learn_press_get_course( $this->course_id );
319
320 $sections = $course->get_curriculum_raw();
321
322 $return = array();
323
324 if ( ! empty( $sections ) ) {
325 foreach ( $sections as $section ) {
326 if ( $section['id'] == $section_id ) {
327 if ( isset( $section['items'] ) && is_array( $section['items'] ) ) {
328 $return = $section['items'];
329 }
330 }
331 }
332 }
333
334 // LP_Object_Cache::set( 'course-' . $this->course_id . '-' . $section_id, $return, 'learn-press/course-section-items' );
335
336 return $return;
337 }
338
339 /**
340 * Create new section item and add to course.
341 *
342 * @param int $section_id
343 * @param array $item
344 *
345 * @return array | bool
346 * @since 3.0.0
347 */
348 public function new_item( $section_id, $item ) {
349 // course author, for case co-instructor add new items.
350 $author_id = get_current_user_id();
351
352 if ( ! $author_id ) {
353 $author_id = get_post_field( 'post_author', $this->course_id ) ? get_post_field( 'post_author', $this->course_id ) : learn_press_get_current_user_id();
354 }
355
356 // $item = wp_parse_args( $item, array( 'title' => '', 'type' => '' ) );
357 $item = array_merge(
358 array(
359 'title' => '',
360 'type' => '',
361 ),
362 $item
363 );
364
365 $args = array(
366 'title' => $item['title'],
367 'author' => $author_id,
368 );
369
370 if ( ! empty( $item['id'] ) ) {
371 $item['old_id'] = $item ['id'];
372 }
373 if ( $item['type'] == LP_LESSON_CPT ) {
374 $lesson_curd = new LP_Lesson_CURD();
375 $item['id'] = $lesson_curd->create( $args );
376 } elseif ( $item['type'] == LP_QUIZ_CPT ) {
377 $quiz_curd = new LP_Quiz_CURD();
378 $item['id'] = $quiz_curd->create( $args );
379 } else {
380 $item['id'] = apply_filters( 'learn-press/new-section-item-data', $item['id'], $item, $args, $this->course_id );
381 }
382
383 if ( is_wp_error( $item['id'] ) || ! $item['id'] ) {
384 return false;
385 }
386
387 $item['preview'] = get_post_meta( $item['id'], '_lp_preview', true ) == 'yes';
388
389 // allow hook
390 do_action( 'learn-press/after-new-section-item', $item['id'], $section_id, $this->course_id );
391
392 // add item to section
393 return $this->add_items_section( $section_id, array( $item ) );
394 }
395
396 /**
397 * Add list new items to section.
398 *
399 * @param $section_id
400 * @param array $items
401 *
402 * @return array
403 */
404 public function add_items_section( $section_id, $items = array() ) {
405 // $order = 1;
406 $current_items = $this->get_section_items( $section_id );
407 // allow hook
408 do_action( 'learn-press/before-add-items-section', $items, $section_id, $this->course_id );
409
410 global $wpdb;
411
412 $all_items = array_merge( $current_items, $items );
413 $result = array();
414 foreach ( $all_items as $key => $item ) {
415
416 $item = (array) $item;
417 $exist = $this->item_section_exist( $section_id, $item['id'] );
418
419 if ( $exist ) {
420 $a = $wpdb->update(
421 $wpdb->learnpress_section_items,
422 array( 'item_order' => $key ),
423 array(
424 'section_id' => $section_id,
425 'item_id' => $item['id'],
426 )
427 );
428 } else {
429 $a = $wpdb->insert(
430 $wpdb->learnpress_section_items,
431 array(
432 'section_id' => $section_id,
433 'item_id' => $item['id'],
434 'item_order' => $key,
435 'item_type' => $item['type'],
436 )
437 );
438 }
439 // get WP Post
440 $post = get_post( $item['id'] );
441 $item = array_merge(
442 $item,
443 array(
444 'id' => $post->ID,
445 'title' => $post->post_title,
446 'type' => $post->post_type,
447 'preview' => get_post_meta( $post->ID, '_lp_preview', true ) == 'yes',
448 )
449 );
450
451 if ( ! $exist ) {
452 do_action( 'learn-press/added-item-to-section', $item, $section_id, $this->course_id );
453 } else {
454 do_action( 'learn-press/updated-item-to-section', $item, $section_id, $this->course_id );
455 }
456
457 $result[] = $item;
458 // $order ++;
459 }
460
461 // LP_Object_Cache::set( 'course-' . $this->course_id . '-' . $section_id, $all_items, 'learn-press/course-section-items' );
462
463 return $result;
464 }
465
466 /**
467 * Check item was been added to any section.
468 *
469 * @param $section_id
470 * @param $item_id
471 *
472 * @return bool
473 * @since 3.0.0
474 */
475 private function item_section_exist( $section_id, $item_id ) {
476 global $wpdb;
477
478 $section_id = intval( $section_id );
479 $item_id = intval( $item_id );
480
481 $query = $wpdb->prepare( "SELECT * FROM {$wpdb->learnpress_section_items} WHERE section_id = %d AND item_id = %d", $section_id, $item_id );
482 $item = $wpdb->get_row( $query, ARRAY_A );
483
484 return ! ! $item;
485 }
486
487 /**
488 * Get section id of lesson, quiz id in curriculum.
489 *
490 * @param $item_id
491 *
492 * @return array|bool|null|object
493 */
494 public function get_item_section( $item_id ) {
495 global $wpdb;
496
497 $query = $wpdb->prepare( "SELECT section_id, item_order FROM {$wpdb->learnpress_section_items} WHERE item_id = %d", $item_id );
498 $section = $wpdb->get_row( $query, ARRAY_A );
499
500 return $section;
501 }
502
503 /**
504 * Update course final item.
505 *
506 * @return bool
507 * @version 4.0.0
508 * @depecated 4.1.6.9
509 */
510 public function update_final_item() {
511 _deprecated_function( __FUNCTION__, '4.1.6.9' );
512 /*$sections = LP_Course_Utils::get_cached_db_sections( $this->course_id );
513
514 if ( ! $sections ) {
515 return false;
516 }
517
518 $last_section = end( $sections );
519 $section_id = $last_section->section_id;
520
521 // get last section items
522 $section_items = LP_Object_Cache::get( 'course-' . $this->course_id . '-' . $section_id, 'learn-press/course-section-items' );
523
524 $types = apply_filters( 'learn-press/post-types-support-assessment-by-final-item', array( LP_QUIZ_CPT ) );
525
526 if ( $section_items ) {
527 // last item in last section
528 $final = end( $section_items );
529
530 if ( is_array( $types ) && in_array( $final['type'], $types ) ) {
531 update_post_meta( $this->course_id, '_' . substr_replace( $final['type'], '_final_', 2, 1 ), $final['id'] );
532 $diff = array_diff( $types, array( $final['type'] ) );
533 foreach ( $diff as $type ) {
534 // delete all other final meta
535 delete_post_meta( $this->course_id, '_' . substr_replace( $type, '_final_', 2, 1 ) );
536 }
537 } else {
538 // for last item is not post type need check final item
539 foreach ( $types as $type ) {
540 delete_post_meta( $this->course_id, '_' . substr_replace( $type, '_final_', 2, 1 ) );
541 }
542 }
543 } else {
544 // for last section does not has any item
545 foreach ( $types as $type ) {
546 delete_post_meta( $this->course_id, '_' . substr_replace( $type, '_final_', 2, 1 ) );
547 }
548 }
549
550 return true;*/
551
552 }
553
554 /**
555 * Remove section item.
556 *
557 * @param $section_id
558 * @param $item_id
559 *
560 * @return bool
561 * @since 3.0.0
562 */
563 public function remove_section_item( $section_id, $item_id ) {
564 global $wpdb;
565
566 $result = $wpdb->delete(
567 $wpdb->learnpress_section_items,
568 array(
569 'section_id' => $section_id,
570 'item_id' => $item_id,
571 ),
572 array(
573 '%d',
574 '%d',
575 )
576 );
577
578 return ! ! $result;
579 }
580
581 /**
582 * Update section items.
583 *
584 * @param $section_id
585 * @param $items array
586 *
587 * @return array
588 * @since 3.0.0
589 */
590 public function update_section_items( $section_id, $items ) {
591 $current_items = $this->get_section_items( $section_id );
592
593 global $wpdb;
594
595 foreach ( $items as $index => $item ) {
596 $order = $index + 1;
597 $exist = $this->item_section_exist( $section_id, $item['id'] );
598
599 if ( $exist ) {
600 $wpdb->update(
601 $wpdb->learnpress_section_items,
602 array( 'item_order' => $order ),
603 array(
604 'section_id' => $section_id,
605 'item_id' => $item['id'],
606 )
607 );
608 } else {
609 $wpdb->insert(
610 $wpdb->learnpress_section_items,
611 array(
612 'section_id' => $section_id,
613 'item_id' => $item['id'],
614 'item_order' => $order,
615 'item_type' => $item['type'],
616 )
617 );
618 }
619 }
620
621 /**
622 * Remove non-existent items.
623 */
624 foreach ( $current_items as $item ) {
625 $find = $this->check_item_exist( $items, $item['id'] );
626
627 if ( ! $find ) {
628 $wpdb->delete(
629 $wpdb->learnpress_section_items,
630 array(
631 'section_id' => $section_id,
632 'item_id' => $item['id'],
633 )
634 );
635 }
636 }
637
638 return $items;
639 }
640
641 /**
642 * Check item exist.
643 *
644 * @param $items array
645 * @param $item_id string
646 *
647 * @return bool
648 * @since 3.0.0
649 */
650 private function check_item_exist( $items, $item_id ) {
651 foreach ( $items as $item ) {
652 if ( $item['id'] == $item_id ) {
653 return true;
654 }
655 }
656
657 return false;
658 }
659
660 /**
661 * Update lesson, quiz title in admin course editor.
662 *
663 * @param $item
664 *
665 * @return array
666 * @since 3.0.0
667 */
668 public function update_item( $item ) {
669 $item = wp_parse_args(
670 $item,
671 array(
672 'id' => '',
673 'title' => '',
674 )
675 );
676
677 wp_update_post(
678 array(
679 'ID' => $item['id'],
680 'post_title' => $item['title'],
681 )
682 );
683
684 if ( isset( $item['preview'] ) && $item['preview'] ) {
685 update_post_meta( $item['id'], '_lp_preview', 'yes' );
686 } else {
687 delete_post_meta( $item['id'], '_lp_preview' );
688 }
689
690 return $item;
691 }
692 }
693