PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7
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 4.2.1 All 138 releases
learnpress / inc / class-lp-global.php

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

363 lines 6.9 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_Global
5 */
6 class LP_Global {
7 /**
8 * @var array
9 */
10 public static $users = array();
11
12 /**
13 * @var array
14 */
15 public static $courses = array();
16
17 /**
18 * @var array
19 */
20 public static $lessons = array();
21
22 /**
23 * @var array
24 */
25 public static $quizzes = array();
26
27 /**
28 * @var array
29 */
30 public static $questions = array();
31
32 /**
33 * @var bool
34 */
35 protected static $_user = false;
36
37 /**
38 * @var bool
39 */
40 protected static $_course = false;
41
42 /**
43 * @var bool
44 */
45 protected static $_course_item = false;
46
47 /**
48 * @var LP_Profile
49 */
50 protected static $_profile = false;
51
52 /**
53 * @var array
54 */
55 public static $custom_posts = array();
56
57 /**
58 * @var array
59 *
60 * @since 3.3.0
61 */
62 public static $object_support_features = array();
63
64 /**
65 * @return LP_Quiz|LP_Lesson
66 */
67 public static function course_item() {
68 global $lp_course_item;
69
70 return $lp_course_item;
71 }
72
73 public static function set_course_item( $course_item ) {
74 global $lp_course_item;
75
76 if ( self::$_course_item === false ) {
77 self::$_course_item = $lp_course_item;
78 }
79
80 $lp_course_item = $course_item;
81 }
82
83 /**
84 * Check if current course item is viewing is a $type
85 *
86 * @param string $type
87 *
88 * @return bool
89 */
90 public static function is_course_item_type( $type ) {
91 $item = self::course_item();
92
93 if ( $item ) {
94 return $type === get_post_type( $item->get_id() );
95 }
96
97 return false;
98 }
99
100 /**
101 * @param bool $get_only_id
102 * Todo: new call not use $get_only_id, only call LP_Global::courses()
103 * Recommend use learn_press_get_course()
104 *
105 * @editor tungnx
106 * @since 3.0.0
107 * @version 1.0.1
108 * @return LP_Course|bool|int
109 */
110 public static function course( $get_only_id = false ) {
111 $lp_course = learn_press_get_course();
112
113 // Fix for old version use param $get_only_id
114 if ( $get_only_id ) {
115 if ( $lp_course ) {
116 return $lp_course->get_id();
117 } else {
118 return 0;
119 }
120 } else {
121 return $lp_course;
122 }
123 }
124
125 public static function set_course( $course ) {
126 global $lp_course;
127
128 if ( self::$_course === false ) {
129 self::$_course = $lp_course;
130 }
131 $lp_course = $course;
132 }
133
134 /**
135 * @return LP_User|LP_User_Guest|false
136 */
137 public static function user() {
138 /**
139 * @see learn_press_setup_user
140 */
141 global $lp_user;
142
143 return $lp_user;
144 }
145
146 /**
147 * Set global user
148 *
149 * @param LP_User $user
150 */
151 // public static function set_user( $user = null ) {
152 // global $lp_user;
153 //
154 // var_dump(debug_backtrace());
155 //
156 // if ( false === self::$_user ) {
157 // self::$_user = $lp_user;
158 // }
159 //
160 // $lp_user = $user;
161 // }
162
163 /**
164 * Alias of course item for highlighting in dev
165 *
166 * @return LP_Quiz|bool
167 */
168 public static function course_item_quiz() {
169 $item = self::course_item();
170
171 return $item instanceof LP_Quiz ? $item : false;
172 }
173
174 /**
175 * @return LP_Question
176 */
177 public static function quiz_question() {
178 global $lp_quiz_question;
179
180 return $lp_quiz_question;
181 }
182
183 /**
184 * Reset global variables to default
185 */
186 public static function reset() {
187 global $lp_user, $lp_course, $lp_course_item, $lp_quiz_question;
188
189 if ( self::$_user ) {
190 $lp_user = self::$_user;
191 }
192
193 if ( self::$_course ) {
194 $lp_course = self::$_course;
195 }
196
197 if ( self::$_course_item ) {
198 $lp_course_item = self::$_course_item;
199 }
200 }
201
202 /**
203 * @param bool $global
204 * @param bool $reset
205 *
206 * @return LP_Profile|WP_Error
207 */
208 public static function profile( $global = false, $reset = false ) {
209 global $profile;
210
211 if ( ! $profile ) {
212 //self::$_profile = $profile = LP_Profile::instance( get_current_user_id() );
213 }
214
215 return LP_Profile::instance();
216
217 /**
218 * Get origin global $profile (stored in class) if $global = TRUE
219 */
220 if ( $global ) {
221
222 /**
223 * If $reset = TRUE then set global $profile to origin global $profile (stored in class)
224 */
225 if ( ! $reset ) {
226 return self::$_profile;
227 }
228
229 $profile = self::$_profile;
230 }
231
232 return $profile;
233 }
234
235 public static function init() {
236 global $profile;
237 self::$_profile = $profile = LP_Profile::instance( get_current_user_id() );
238 }
239
240 /**
241 * @param $key
242 *
243 * @return bool|mixed
244 */
245 public static function get_custom_posts( $key ) {
246 if ( array_key_exists( $key, self::$custom_posts ) ) {
247 return self::$custom_posts[ $key ];
248 }
249
250 return false;
251 }
252
253 /**
254 * @param string $object_type
255 * @param string $feature
256 * @param string $type
257 *
258 * @return mixed
259 * @since 3.3.0
260 *
261 */
262 public static function add_object_feature( $object_type, $feature, $type = 'yes' ) {
263 $namespace = explode( '.', $object_type );
264 $object_type = $namespace[0];
265 $namespace = isset( $namespace[1] ) ? $namespace[1] : false;
266
267 if ( ! isset( self::$object_support_features[ $object_type ] ) ) {
268 self::$object_support_features[ $object_type ] = array();
269 }
270
271 if ( $namespace ) {
272 if ( ! isset( self::$object_support_features[ $object_type ][ $namespace ] ) ) {
273 self::$object_support_features[ $object_type ][ $namespace ] = array();
274 }
275
276 return self::$object_support_features[ $object_type ][ $namespace ][ $feature ] = $type === null ? 'yes' : $type;
277 }
278
279 return self::$object_support_features[ $object_type ][ $feature ] = $type === null ? 'yes' : $type;
280 }
281
282 /**
283 * Checks if an object is support a feature.
284 *
285 * @param string $object_type
286 * @param string $feature
287 * @param mixed $type
288 *
289 * @return bool
290 * @since 3.3.0
291 *
292 */
293 public static function object_is_support_feature( $object_type, $feature, $type = null ) {
294 $objects = self::$object_support_features;
295 $namespace = explode( '.', $object_type );
296 $object_type = $namespace[0];
297 $namespace = isset( $namespace[1] ) ? $namespace[1] : false;
298
299 if ( empty( $objects[ $object_type ] ) ) {
300 return false;
301 }
302
303 if ( $namespace && empty( $objects[ $object_type ][ $namespace ] ) ) {
304 return false;
305 }
306
307 if ( $namespace ) {
308 $is_support = array_key_exists( $feature, $objects[ $object_type ][ $namespace ] ) ? true : false;
309
310 if ( $type && $is_support ) {
311 return $objects[ $object_type ][ $namespace ][ $feature ] === $type;
312 }
313
314 return $is_support;
315 }
316
317 $is_support = array_key_exists( $feature, $objects[ $object_type ] ) ? true : false;
318
319 if ( $type && $is_support ) {
320 return $objects[ $object_type ][ $feature ] === $type;
321 }
322
323 return $is_support;
324 }
325
326 /**
327 * Get all features that an object support.
328 *
329 * @param string $object_type
330 *
331 * @return array|mixed
332 * @since 3.3.0
333 *
334 */
335 public static function get_object_supports( $object_type ) {
336 $namespace = explode( '.', $object_type );
337 $object_type = $namespace[0];
338 $namespace = isset( $namespace[1] ) ? $namespace[1] : false;
339
340 if ( empty( self::$object_support_features[ $object_type ] ) ) {
341 return array();
342 }
343
344 if ( $namespace ) {
345 if ( empty( self::$object_support_features[ $object_type ][ $namespace ] ) ) {
346 return array();
347 }
348 }
349
350 return self::$object_support_features[ $object_type ];
351 }
352
353 /**
354 * @param $user
355 *
356 * @return false
357 * @deprecated 4.0.0
358 */
359 public function set_user( $user ) {
360 return false;
361 }
362 }
363