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
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.4.8, at inc/course/class-lp-course-item.php

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