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 / lesson / class-lp-lesson.php

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

247 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Lesson.
4 *
5 * @author ThimPress
6 * @package LearnPress/Classes
7 * @version 3.0.0
8 */
9
10 /**
11 * Prevent loading this file directly
12 */
13 defined( 'ABSPATH' ) || exit();
14
15 if ( ! function_exists( 'LP_Lesson' ) ) {
16
17 /**
18 * Class LP_Lesson.
19 */
20 class LP_Lesson extends LP_Course_Item {
21
22 /**
23 * @var mixed|string|void
24 */
25 public $content = '';
26
27 /**
28 *
29 * @var string
30 */
31 public $lesson_type = null;
32
33 /**
34 * @var string
35 */
36 protected $_item_type = LP_LESSON_CPT;
37
38 /**
39 * @var int
40 */
41 protected static $_loaded = 0;
42
43 /**
44 * LP_Lesson constructor.
45 *
46 * @param $lesson
47 * @param string $args
48 *
49 * @throws Exception
50 */
51 public function __construct( $lesson, $args = '' ) {
52 parent::__construct( $lesson, $args );
53 $this->_curd = new LP_Lesson_CURD();
54
55 if ( $this->get_id() > 0 ) {
56 $this->load();
57 }
58
59 self::$_loaded ++;
60 if ( self::$_loaded == 1 ) {
61 add_filter( 'debug_data', array( __CLASS__, 'log' ) );
62 }
63 }
64
65 /**
66 * Read course data, curriculum: sections, items, etc...
67 *
68 * @since 3.0.0
69 *
70 * @throws Exception
71 */
72 public function load() {
73 $this->_curd->load( $this );
74 }
75
76 /**
77 * Debug log.
78 *
79 * @param $data
80 *
81 * @return array
82 */
83 public static function log( $data ) {
84 $data[] = __CLASS__ . '( ' . self::$_loaded . ' )';
85
86 return $data;
87 }
88
89 /**
90 * @param $tag
91 *
92 * @return mixed
93 * @throws Exception
94 */
95 public function is( $tag ) {
96 _deprecated_function( __FUNCTION__, '3.0.8' );
97 $args = func_get_args();
98 unset( $args[0] );
99 $method = 'is_' . preg_replace( '!-!', '_', $tag );
100 $callback = array( $this, $method );
101 if ( is_callable( $callback ) ) {
102 return call_user_func_array( $callback, $args );
103 } else {
104 throw new Exception( sprintf( __( 'The function %s doesn\'t exist', 'learnpress' ), $tag ) );
105 }
106 }
107
108 /**
109 * Get LP Lesson.
110 *
111 * @param mixed $the_lesson
112 * @param array $args
113 *
114 * @return LP_Lesson|bool
115 */
116 public static function get_lesson( $the_lesson = false, $args = array() ) {
117
118 if ( is_numeric( $the_lesson ) && isset( LP_Global::$lessons[ $the_lesson ] ) ) {
119 return LP_Global::$lessons[ $the_lesson ];
120 }
121
122 $the_lesson = self::get_lesson_object( $the_lesson );
123
124 if ( ! $the_lesson ) {
125 return false;
126 }
127
128 if ( isset( LP_Global::$lessons[ $the_lesson->ID ] ) ) {
129 return LP_Global::$lessons[ $the_lesson->ID ];
130 }
131
132 if ( ! empty( $args['force'] ) ) {
133 $force = ! ! $args['force'];
134 unset( $args['force'] );
135 } else {
136 $force = false;
137 }
138
139 $key_args = wp_parse_args(
140 $args,
141 array(
142 'id' => $the_lesson->ID,
143 'type' => $the_lesson->post_type,
144 )
145 );
146
147 $key = LP_Helper::array_to_md5( $key_args );
148
149 if ( $force ) {
150 LP_Global::$lessons[ $key ] = false;
151 LP_Global::$lessons[ $the_lesson->ID ] = false;
152 }
153
154 if ( empty( LP_Global::$lessons[ $key ] ) ) {
155 $class_name = self::get_lesson_class( $the_lesson, $args );
156 if ( is_string( $class_name ) && class_exists( $class_name ) ) {
157 $lesson = new $class_name( $the_lesson->ID, $args );
158 } elseif ( $class_name instanceof LP_Course_Item ) {
159 $lesson = $class_name;
160 } else {
161 $lesson = new self( $the_lesson->ID, $args );
162 }
163 LP_Global::$lessons[ $key ] = $lesson;
164 LP_Global::$lessons[ $the_lesson->ID ] = $lesson;
165 }
166
167 return LP_Global::$lessons[ $key ];
168 }
169
170 /**
171 * Get default meta.
172 *
173 * @since 3.0.0
174 *
175 * @return mixed
176 */
177 public static function get_default_meta() {
178 $meta = array(
179 'duration' => '0 minute',
180 'preview' => 'no',
181 );
182
183 return apply_filters( 'learn-press/course/lesson/default-meta', $meta );
184 }
185
186 /**
187 * Get duration of lesson
188 *
189 * @return LP_Duration
190 */
191 public function get_duration() {
192 $duration = parent::get_duration();
193
194 return apply_filters( 'learn-press/lesson-duration', $duration, $this->get_id() );
195 }
196
197 /**
198 * @param string $lesson_type
199 *
200 * @return string|false
201 */
202 private static function get_class_name_from_lesson_type( $lesson_type ) {
203 return LP_LESSON_CPT === $lesson_type ? __CLASS__ : 'LP_Lesson_' . implode( '_', array_map( 'ucfirst', explode( '-', $lesson_type ) ) );
204 }
205
206 /**
207 * Get the lesson class name
208 *
209 * @param WP_Post $the_lesson
210 * @param array $args (default: array())
211 *
212 * @return string
213 */
214 private static function get_lesson_class( $the_lesson, $args = array() ) {
215 $lesson_id = absint( $the_lesson->ID );
216 $type = $the_lesson->post_type;
217
218 $class_name = self::get_class_name_from_lesson_type( $type );
219
220 // Filter class name so that the class can be overridden if extended.
221 return apply_filters( 'learn-press/lesson/object-class', $class_name, $type, $lesson_id );
222 }
223
224 /**
225 * Get the lesson object
226 *
227 * @param mixed $the_lesson
228 *
229 * @uses WP_Post
230 * @return WP_Post|bool false on failure
231 */
232 private static function get_lesson_object( $the_lesson ) {
233 if ( false === $the_lesson ) {
234 $the_lesson = get_post_type() === LP_LESSON_CPT ? $GLOBALS['post'] : false;
235 } elseif ( is_numeric( $the_lesson ) ) {
236 $the_lesson = get_post( $the_lesson );
237 } elseif ( $the_lesson instanceof LP_Course_Item ) {
238 $the_lesson = get_post( $the_lesson->get_id() );
239 } elseif ( ! ( $the_lesson instanceof WP_Post ) ) {
240 $the_lesson = false;
241 }
242
243 return apply_filters( 'learn-press/lesson/post-object', $the_lesson );
244 }
245 }
246 }
247