PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3.2
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 / course / class-lp-course-item.php

class-lp-course-item.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.3.2, at inc/course/class-lp-course-item.php

639 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Course_Item.
4 *
5 * @author ThimPress
6 * @package LearnPress/Classes
7 * @version 3.0.0
8 */
9
10 defined( 'ABSPATH' ) || exit();
11
12 if ( ! class_exists( 'LP_Course_Item' ) ) {
13 /**
14 * Class LP_Course_Item.
15 */
16 class LP_Course_Item extends LP_Abstract_Post_Data implements ArrayAccess {
17
18 /**
19 * The icon maybe used somewhere.
20 *
21 * @var string
22 */
23 protected $_icon_class = '';
24
25 /**
26 * The type of item.
27 *
28 * @var string
29 */
30 protected $_item_type = '';
31
32 /**
33 * @var LP_Course
34 */
35 protected $_course = null;
36
37 /**
38 * @var LP_Course_Section
39 */
40 protected $_section = null;
41
42 /**
43 * @var null
44 */
45 protected $_content = null;
46
47 /**
48 * @var bool
49 */
50 protected $_preview = false;
51
52
53 /**
54 * LP_Course_Item constructor.
55 *
56 * @param $item mixed
57 * @param $args array
58 */
59 public function __construct( $item, $args = null ) {
60 parent::__construct( $item, $args );
61
62 $this->add_support( 'comments', get_post_field( 'comment_status', $this->get_id() ) === 'open' );
63 }
64
65 /**
66 * Get type of item.
67 *
68 * @param string $context
69 *
70 * @return string
71 */
72 public function get_item_type( $context = '' ) {
73 $post_type = $this->_item_type;
74
75 if ( $context === 'display' ) {
76 $post_type_object = get_post_type_object( $post_type );
77
78 if ( $post_type_object ) {
79 $post_type = $post_type_object->labels->singular_name;
80 }
81 }
82
83 return $post_type;
84 }
85
86 /**
87 * @return string
88 */
89 public function get_icon_class() {
90 return $this->_icon_class;
91 }
92
93 /**
94 * Check lesson is preview
95 *
96 * @param string $context
97 *
98 * @return bool
99 */
100 public function is_preview( $context = 'display' ): bool {
101 $item_id = $this->get_id();
102
103 $this->_preview = false;
104
105 if ( $item_id && LP_LESSON_CPT === $this->get_post_type() ) {
106 $is_lesson_preview = get_post_meta( $item_id, '_lp_preview', true );
107
108 $this->_preview = 'yes' === $is_lesson_preview;
109 }
110
111 return apply_filters( 'learnpress/course/item-preview', $this->_preview, $this->get_id() );
112 }
113
114 /**
115 * @param string $context
116 *
117 * @return string
118 */
119 public function get_title( $context = '' ) {
120 return apply_filters( 'learn-press/course-item-title', parent::get_title( $context ), $this->get_id() );
121 }
122
123 /**
124 * Get format of Post
125 *
126 * @return bool|false|mixed|string
127 */
128 public function get_format() {
129 $format = get_post_meta( $this->get_id(), 'post_format', true );
130
131 if ( ! $format ) {
132 $format = 'standard';
133 }
134
135 return $format;
136 }
137
138 /**
139 * Return true if item can be shown in course curriculum.
140 *
141 * @return mixed
142 * @deprecated 4.0.0
143 */
144 public function is_visible() {
145 $show = true;
146
147 return apply_filters( 'learn-press/course-item-visible', $show, $this->get_item_type(), $this->get_id() );
148 }
149
150 /**
151 * Get class of item.
152 *
153 * @param string $more
154 * @param int $user_id
155 *
156 * @return array
157 */
158 public function get_class( $more = '', $user_id = 0 ) {
159 $course_id = get_the_ID();
160
161 if ( empty( $GLOBALS['get_class'] ) ) {
162 $GLOBALS['get_class'] = 0;
163 }
164
165 $user_id = $user_id ? $user_id : get_current_user_id();
166 $t = microtime( true );
167 $classes = LP_Object_Cache::get( 'item-' . $user_id . '-' . $this->get_id(), 'learn-press/post-classes' );
168
169 if ( false === $classes ) {
170 $curd = new LP_User_Item_CURD();
171 $all_items = $curd->parse_items_classes( $course_id, $user_id, $more );
172
173 $classes = ! empty( $all_items[ $this->get_id() ] ) ? $all_items[ $this->get_id() ] : $defaults = array(
174 'course-item',
175 'course-item-' . $this->get_item_type(),
176 'course-item-' . $this->get_id(),
177 );
178 }
179
180 $GLOBALS['get_class'] += microtime( true ) - $t;
181
182 return apply_filters(
183 'learn-press/course-item-class-cached',
184 $classes,
185 $this->get_item_type(),
186 $this->get_id(),
187 $course_id
188 );
189 }
190
191 public function get_class_v2( $course_id, $item_id, $can_view_item, $more = array() ) {
192 $course = learn_press_get_course( $course_id );
193
194 if ( ! $course ) {
195 return $more;
196 }
197
198 $defaults = array_merge(
199 array(
200 'course-item',
201 'course-item-' . $this->get_item_type(),
202 'course-item-' . $item_id,
203 ),
204 (array) $more
205 );
206
207 $user = learn_press_get_user( get_current_user_id() );
208
209 if ( ! $user ) {
210 return $defaults;
211 }
212
213 $is_free = $course->is_free();
214 $enrolled = $user->has_enrolled_or_finished( $course_id );
215 $no_required_enroll = $course->is_no_required_enroll();
216
217 $post_format = $this->get_format();
218 if ( 'standard' !== $post_format && $post_format ) {
219 $defaults[] = 'course-item-type-' . $post_format;
220 }
221
222 if ( $no_required_enroll ) {
223 $defaults[] = 'item-free';
224 } elseif ( ! $enrolled ) {
225 $defaults['item-locked'] = 'item-locked';
226
227 if ( $this->is_preview() ) {
228 $defaults['item-preview'] = 'item-preview';
229 $defaults['has-status'] = 'has-status';
230 unset( $defaults['item-locked'] );
231 }
232 } elseif ( ! $can_view_item->flag ) {
233 $defaults[] = 'item-locked';
234 } else {
235 $item_status = $user->get_item_status( $item_id, $course_id );
236 $item_grade = $user->get_item_grade( $item_id, $course_id );
237
238 if ( $item_status ) {
239 $defaults[] = 'has-status';
240 $defaults[] = 'status-' . $item_status;
241 }
242
243 switch ( $item_status ) {
244 case 'started':
245 break;
246 case 'completed':
247 $defaults[] = $item_grade;
248 break;
249 default:
250 if ( $this->is_preview() ) {
251 $defaults['item-preview'] = 'item-preview';
252 $defaults['has-status'] = 'has-status';
253 }
254
255 $item_class = apply_filters(
256 'learn-press/course-item-status-class',
257 $item_status,
258 $item_grade,
259 $this->get_item_type(),
260 $item_id,
261 $course_id
262 );
263
264 if ( $item_class ) {
265 $defaults[] = $item_class;
266 }
267 }
268 }
269
270 $classes = apply_filters(
271 'learn-press/course-item-class',
272 $defaults,
273 $this->get_item_type(),
274 $item_id,
275 $course_id
276 );
277
278 // Filter unwanted values.
279 $classes = is_array( $classes ) ? $classes : explode( ' ', $classes );
280 $classes = array_filter( $classes );
281 $classes = array_unique( $classes );
282
283 return $classes;
284 }
285
286 public function get_status_title() {
287 $course_id = get_the_ID();
288 $status_message = '';
289
290 $user = learn_press_get_current_user();
291 if ( $user->get_item_status( $this->get_id(), $course_id ) === 'completed' ) {
292 $item_grade = $user->get_item_grade( $this->get_id(), $course_id );
293
294 if ( $item_grade === 'failed' ) {
295 $status_message = _x( 'Failed', 'course item status title', 'learnpress' );
296 } elseif ( $item_grade === 'passed' ) {
297 $status_message = _x( 'Passed', 'course item status title', 'learnpress' );
298 } else {
299 $status_message = _x( 'Completed', 'course item status title', 'learnpress' );
300 }
301 } else {
302 $status_message = _x( 'Unread', 'course item status title', 'learnpress' );
303 }
304
305 return apply_filters(
306 'learn-press/course-item-status-title',
307 $status_message,
308 $this->get_id(),
309 $course_id
310 );
311 }
312
313 /**
314 * Get permalink of item inside course.
315 *
316 * @return string
317 */
318 public function get_permalink() {
319 $link = false;
320
321 if ( $this->_course ) {
322 $link = $this->_course->get_item_link( $this->get_id() );
323 }
324
325 return apply_filters( 'learn-press/course-item-link', $link, $this );
326 }
327
328 /**
329 * Set course parent of this item.
330 *
331 * @param LP_Course|LP_Abstract_Course|int $course
332 */
333 public function set_course( $course ) {
334 if ( is_numeric( $course ) ) {
335 $this->_course = learn_press_get_course( $course );
336 } else {
337 $this->_course = $course;
338 }
339 }
340
341 /**
342 * Return course.
343 *
344 * @return LP_Course
345 */
346 public function get_course() {
347 return $this->_course;
348 }
349
350 public function get_course_id() {
351 $course = $this->get_course();
352
353 if ( $course ) {
354 return $course->get_id();
355 }
356
357 return false;
358 }
359
360 /**
361 * @param LP_Course_Section $section
362 */
363 public function set_section( $section ) {
364 $this->_section = $section;
365 }
366
367 /**
368 * @return LP_Course_Section
369 */
370 public function get_section() {
371 return $this->_section;
372 }
373
374 /**
375 * Get instance of an item from post
376 *
377 * @param int $item_id Item id.
378 * @param int $course_id .
379 *
380 * @return LP_Course_Item|false
381 * @Todo: tungnx - review - rewrite - set cache - check where call this function
382 * @editor tungnx
383 * @modify 4.1.3 - change cache
384 * @version 4.0.2
385 * @since 3.x.x
386 */
387 public static function get_item( $item_id = 0, $course_id = 0 ) {
388 /*
389 $lp_course_cache = LP_Course_Cache::instance();
390 $key_cache = sprintf( '%d/item_id/%d', $course_id, $item_id );
391 $item = $lp_course_cache->get_cache( $key_cache );*/
392
393 $item = false;
394
395 if ( false === $item ) {
396 $item_post = get_post( $item_id );
397
398 if ( ! $item_post ) {
399 return false;
400 }
401
402 $item_type = $item_post->post_type;
403
404 if ( learn_press_is_support_course_item_type( $item_type ) ) {
405 $type = str_replace( 'lp_', '', $item_type );
406
407 switch ( $type ) {
408 case 'lesson':
409 $item = LP_Lesson::get_lesson( $item_id );
410 break;
411 case 'quiz':
412 $item = LP_Quiz::get_quiz( $item_id );
413 break;
414 default:
415 $class_name = apply_filters(
416 'learn-press/course-item-object-class',
417 array(),
418 $type,
419 $item_type,
420 $item_id
421 );
422
423 if ( ! empty( $class_name ) && isset( $class_name[ $type ] ) ) {
424 $class = $class_name[ $type ];
425
426 if ( is_string( $class ) && class_exists( $class ) ) {
427 $item = new $class( $item_id );
428 } elseif ( $class instanceof LP_Course_Item ) {
429 $item = $class;
430 }
431 }
432
433 break;
434 }
435
436 // Todo: don't know why when set course here, theme ivy still null course
437 /*if ( $item instanceof LP_Course_Item ) {
438 $item->set_course( $course_id );
439 }*/
440
441 // $lp_course_cache->set_cache( $key_cache, $item );
442 }
443 }
444
445 return apply_filters( 'learn-press/get-course-item', $item, $item_type, $item_id );
446 }
447
448 /**
449 * Get template name of item.
450 *
451 * @return string
452 */
453 public function get_template() {
454 $item_type = $this->get_item_type();
455
456 return apply_filters(
457 'learn-press/section-item-template',
458 'item-' . str_replace( 'lp_', '', $item_type ),
459 $item_type
460 );
461 }
462
463 /**
464 * To array.
465 *
466 * @return array
467 * @since 3.0.0
468 */
469 public function to_array() {
470 $post = get_post( $this->get_id() );
471
472 return apply_filters(
473 'learn-press/item/to_array',
474 array(
475 'id' => $this->get_id(),
476 'type' => $this->get_item_type(),
477 'title' => $post->post_title,
478 'preview' => $this->is_preview(),
479 )
480 );
481 }
482
483 /**
484 * Create nonce for checking actions on an item.
485 *
486 * @param string $action
487 * @param int $course_id
488 * @param int $user_id
489 *
490 * @return string
491 */
492 public function create_nonce( $action = '', $course_id = 0, $user_id = 0 ) {
493 if ( ! $course_id && $this->get_course() ) {
494 $course_id = $this->get_course()->get_id();
495 }
496
497 if ( ! $user_id ) {
498 $user_id = get_current_user_id();
499 }
500
501 $action = $this->get_nonce_action( $action, $course_id, $user_id );
502
503 return wp_create_nonce( $action );
504 }
505
506 /**
507 * Verify nonce for an action on item.
508 *
509 * @param string $nonce
510 * @param string $action
511 * @param int $course_id
512 * @param int $user_id
513 *
514 * @return false|int
515 */
516 public function verify_nonce( $nonce, $action = '', $course_id = 0, $user_id = 0 ) {
517 if ( ! $course_id ) {
518 $course_id = $this->get_course()->get_id();
519 }
520
521 if ( ! $user_id ) {
522 $user_id = get_current_user_id();
523 }
524
525 $action = $this->get_nonce_action( $action, $course_id, $user_id );
526
527 return wp_verify_nonce( $nonce, $action );
528 }
529
530 /**
531 * @param $action
532 * @param $course_id
533 * @param $user_id
534 *
535 * @return string
536 */
537 public function get_nonce_action( $action, $course_id, $user_id ) {
538 return sprintf( '%s-item-%d-%d-%d', $action, $user_id, $course_id, $this->get_id() );
539 }
540
541 /**
542 * @param int $question_id
543 *
544 * @return mixed
545 * @deprecated 4.1.7.2
546 */
547 /*public function is_viewing_question( $question_id = 0 ) {
548 global $lp_quiz_question;
549 if ( $question_id ) {
550 $viewing = $lp_quiz_question && $lp_quiz_question->get_id() == $question_id;
551 } else {
552 $viewing = $lp_quiz_question ? $lp_quiz_question->get_id() : false;
553 }
554
555 return apply_filters( 'learn-press/quiz/is-viewing-question', $viewing, $question_id, $this->get_id() );
556 }*/
557
558 /**
559 * @param int $user_id
560 * @param int $course_id
561 *
562 * @return mixed
563 * @deprecated 4.1.7.2
564 */
565 /*public function get_status_classes( $user_id = 0, $course_id = 0 ) {
566 $status_classes = array();
567 $course = learn_press_get_course( $course_id );
568 $user = learn_press_get_user( $user_id, ! $user_id );
569
570 if ( $course ) {
571 if ( $this->is_preview() ) {
572 $status_classes[] = 'item-preview';
573 } elseif ( $course->is_free() && $course->is_no_required_enroll() ) {
574 $status_classes[] = 'item-free';
575 }
576 }
577
578 if ( $user ) {
579 $item_status = $user->get_item_status( $this->get_id(), $course_id );
580 $item_grade = $user->get_item_grade( $this->get_id(), $course_id );
581
582 if ( $item_status ) {
583 $status_classes[] = 'course-item-status';
584 $status_classes[] = 'item-' . $item_status;
585 }
586 switch ( $item_status ) {
587 case 'started':
588 break;
589 case 'completed':
590 $status_classes[] = $item_grade;
591 }
592 }
593
594 return apply_filters(
595 'learn-press/item-status-classes',
596 $status_classes,
597 $this->get_id(),
598 $course_id,
599 $user_id
600 );
601 }*/
602
603 /**
604 * Get duration of quiz
605 *
606 * @return LP_Duration
607 */
608 public function get_duration() {
609 $duration = $this->get_data( 'duration' );
610
611 if ( false === $duration || '' === $duration ) {
612 $duration = get_post_meta( $this->get_id(), '_lp_duration', true );
613
614 if ( $duration ) {
615 $duration = new LP_Duration( $duration );
616 } else {
617 $duration = new LP_Duration( 0 );
618 }
619
620 $this->_set_data( 'duration', $duration );
621 }
622
623 return apply_filters( 'learn-press/course-item-duration', $duration, $this->get_id() );
624 }
625
626 public function offsetExists( $offset ) {
627 }
628
629 public function offsetGet( $offset ) {
630 }
631
632 public function offsetSet( $offset, $value ) {
633 }
634
635 public function offsetUnset( $offset ) {
636 }
637 }
638 }
639