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 / abstracts / abstract-object-data.php

abstract-object-data.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.8, at inc/abstracts/abstract-object-data.php

595 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Abstract_Object_Data.
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_Abstract_Object_Data' ) ) {
13
14 /**
15 * Class LP_Abstract_Object_Data
16 */
17 abstract class LP_Abstract_Object_Data {
18 /**
19 * @var int
20 */
21 protected $_id = 0;
22
23 /**
24 * @var array
25 */
26 protected $_data = array();
27
28 /**
29 * @var array
30 */
31 protected $_extra_data = array();
32
33 /**
34 * @var bool
35 */
36 protected $_no_cache = false;
37
38 /**
39 * @var array
40 */
41 protected $_supports = array();
42
43 /**
44 * Store new changes
45 *
46 * @var array
47 */
48 protected $_changes = array();
49
50 /**
51 * @var array
52 */
53 protected $_extra_data_changes = array();
54
55 /**
56 * Object meta
57 *
58 * @var array
59 */
60 protected $_meta_data = array();
61
62 /**
63 * @var array
64 */
65 protected $_meta_keys = array();
66
67 /**
68 * CURD class to manipulation with database.
69 *
70 * @var null
71 */
72 protected $_curd = null;
73
74 /**
75 * Mark the $post object is set up to this class
76 *
77 * @var bool
78 */
79 protected $_setup_postdata = false;
80
81 public $object_type = 'object-data';
82
83 /**
84 * LP_Abstract_Object_Data constructor.
85 *
86 * @param null $data
87 */
88 public function __construct( $data = null ) {
89 $this->_data = (array) $data;
90 if ( array_key_exists( 'id', $this->_data ) ) {
91 $this->set_id( absint( $this->_data['id'] ) );
92 unset( $this->_data['id'] );
93 }
94 $this->load_curd();
95 }
96
97 /**
98 * Load curd.
99 */
100 protected function load_curd() {
101 if ( is_string( $this->_curd ) && $this->_curd ) {
102 $this->_curd = new $this->_curd();
103 }
104 }
105
106 /**
107 * Set id of object in database
108 *
109 * @param $id
110 */
111 public function set_id( $id ) {
112 $this->_id = $id;
113 }
114
115 /**
116 * Get id of object in database
117 *
118 * @return int
119 */
120 public function get_id() {
121 return absint( $this->_id );
122 }
123
124 /**
125 * Get post object assigned to this class.
126 *
127 * @return array|null|WP_Post
128 * @since 3.0.0
129 */
130 public function get_post() {
131 return get_post( $this->get_id() );
132 }
133
134 /**
135 * Get global $post object and set it up with new data
136 *
137 * @return bool|WP_Post
138 * @since 3.0.0
139 */
140 public function setup_postdata() {
141 global $post;
142
143 $post = $this->get_post();
144
145 if ( $post ) {
146 setup_postdata( $post );
147 $this->_setup_postdata = true;
148
149 return $post;
150 }
151
152 return false;
153 }
154
155 /**
156 * Reset global $post to WP default.
157 *
158 * @return bool
159 * @since 3.0.0
160 */
161 public function reset_postdata() {
162 if ( $this->_setup_postdata ) {
163 wp_reset_postdata();
164
165 return true;
166 }
167
168 return false;
169 }
170
171 /**
172 * Get object data.
173 *
174 * @updated 3.2.0
175 * @date 13 Nov 2018
176 *
177 * @param string $name - Optional. Name of data want to get, true if return all.
178 * @param mixed $default
179 *
180 * @return array|mixed
181 */
182 public function get_data( $name = '', $default = '' ) {
183 if ( is_string( $name ) && $name ) {
184 return array_key_exists( $name, $this->_data ) ? $this->_data[ $name ] : ( array_key_exists( $name, $this->_extra_data ) ? $this->_extra_data[ $name ] : $default );
185 } elseif ( is_array( $name ) ) {
186 $data = array();
187
188 foreach ( $name as $key ) {
189 $data[ $key ] = $this->get_data( $key, $default );
190 }
191
192 return $data;
193 }
194
195 return array_merge( $this->_data, $this->_extra_data );
196 }
197
198 /**
199 * Get data as LP_Datetime object
200 *
201 * @param string $name
202 *
203 * @return array|LP_Datetime|mixed
204 * @since 3.2.0
205 */
206 public function get_data_date( $name ) {
207 $data = $this->get_data( $name );
208
209 return is_a( $data, 'LP_Datetime' ) ? $data : new LP_Datetime( $data );
210 }
211
212
213 /**
214 * @param string $name
215 * @param mixed $default
216 *
217 * @return array|bool|mixed|string
218 */
219 public function get_extra_data( string $name = '', $default = false ) {
220 if ( is_string( $name ) ) {
221 return array_key_exists( $name, $this->_extra_data ) && ! empty( $this->_extra_data[ $name ] ) ? $this->_extra_data[ $name ] : $default;
222 } elseif ( is_array( $name ) ) {
223 $data = array();
224 foreach ( $name as $key ) {
225 $data[ $key ] = $this->get_extra_data( $key, $default );
226 }
227
228 return $data;
229 }
230
231 return $default;
232 }
233
234 /**
235 * Set object data.
236 *
237 * @param mixed $key_or_data
238 * @param mixed $value
239 * @param bool $extra
240 */
241 protected function _set_data( $key_or_data, $value = '', $extra = false ) {
242 if ( is_array( $key_or_data ) ) {
243 foreach ( $key_or_data as $key => $value ) {
244 $this->_set_data( $key, $value, $extra );
245 }
246 } elseif ( $key_or_data ) {
247 if ( $extra ) {
248 // Do not allow to add extra data with the same key in data
249 if ( ! array_key_exists( $key_or_data, $this->_data ) ) {
250 $this->_extra_data[ $key_or_data ] = $value;
251 }
252 } else {
253 try {
254 if ( ! is_string( $key_or_data ) && ! is_numeric( $key_or_data ) ) {
255 throw new Exception( 'error' );
256 }
257
258 // Only change the data is already existed
259 // if ( array_key_exists( $key_or_data, $this->_data ) ) {
260 $this->_data[ $key_or_data ] = $value;
261 // } else {
262 // $this->_extra_data[ $key_or_data ] = $value;
263 // }
264 } catch ( Throwable $ex ) {
265 error_log( $ex->getMessage() );
266 }
267 }
268 }
269 }
270
271 /**
272 * Set extra data
273 *
274 * @param array|string $key_or_data
275 * @param string|mixed $value
276 * @return $this
277 */
278 public function set_data( $key_or_data, $value = '' ) {
279 $this->_set_data( $key_or_data, $value );
280 return $this;
281 }
282
283 /**
284 * Set data datetime
285 * if $value is empty, not set is LP_Datetime object
286 *
287 * @param $key
288 * @param $value
289 *
290 * @version 4.0.1
291 * @return LP_Abstract_Object_Data
292 */
293 public function set_data_date( $key, $value ): LP_Abstract_Object_Data {
294 $lp_date = '';
295 if ( ! empty( $value ) ) {
296 $lp_date = new LP_Datetime( $value );
297 }
298
299 $this->set_data( $key, $lp_date );
300
301 return $this;
302 }
303
304 /**
305 * @param $key
306 * @param $value
307 * @deprecated 4.1.7.3
308 */
309 protected function _set_data_date( $key, $value, $extra = false ) {
310 _deprecated_function( __FUNCTION__, '4.1.7.3', 'set_data_date' );
311 $this->set_data_date( $key, $value );
312 }
313
314 /**
315 * @deprecated 4.1.7.3
316 */
317 public function set_data_null_date( $key ) {
318 _deprecated_function( __METHOD__, '4.1.7.3' );
319 $this->_set_data( $key, LP_Datetime::getSqlNullDate() );
320 }
321
322 /**
323 * Set data via methods in array
324 *
325 * @param array $data - Array with key is method and value is value to set
326 *
327 * @throws Exception
328 * @deprecated 4.4.2 addon h5p v4.0.8 and assignment v4.2.0 still using this function
329 */
330 public function set_data_via_methods( $data ) {
331 $errors = array_keys( $data );
332 foreach ( $data as $prop => $value ) {
333 $setter = "set_$prop";
334 if ( is_callable( array( $this, $setter ) ) ) {
335 $reflection = new ReflectionMethod( $this, $setter );
336
337 if ( $reflection->isPublic() ) {
338 $this->{$setter}( $value );
339 $errors = array_diff( $errors, array( $prop ) );
340 }
341 }
342 }
343
344 // If there is at least one method failed
345 if ( $errors ) {
346 $errors = array_map( array( $this, 'prefix_set_method' ), $errors );
347 throw new Exception( sprintf( __( 'The following functions %1$s do not exist in %2$s', 'learnpress' ), join( ',', $errors ), get_class( $this ) ) );
348 }
349 }
350
351 /**
352 * Return the keys of data
353 *
354 * @param bool $extra - Optional. TRUE if including extra data
355 *
356 * @return array
357 */
358 public function get_data_keys( $extra = true ) {
359 return $extra ? array_merge( array_keys( $this->_data ), array_keys( $this->_extra_data ) ) : array_keys( $this->_data );
360 }
361
362 /**
363 * @param $method
364 *
365 * @return string
366 */
367 public function prefix_set_method( $method ) {
368 return "set_{$method}";
369 }
370
371 /**
372 * Apply the changes
373 */
374 public function apply_changes() {
375 $this->_data = array_replace_recursive( $this->_data, $this->_changes );
376 $this->_changes = array();
377 }
378
379 /**
380 * Get the changes.
381 *
382 * @return array
383 */
384 public function get_changes() {
385 return $this->_changes;
386 }
387
388 /**
389 * Check if question is support feature.
390 *
391 * @param string $feature
392 * @param string $type
393 *
394 * @return bool
395 */
396 public function is_support( $feature, $type = '' ) {
397 $feature = $this->_sanitize_feature_key( $feature );
398
399 return LP_Global::object_is_support_feature( $this->object_type, $feature, $type );
400 }
401
402 /**
403 * Add a feature that question is supported
404 *
405 * @param $feature
406 * @param string $type
407 */
408 public function add_support( $feature, $type = 'yes' ) {
409
410 $feature = $this->_sanitize_feature_key( $feature );
411 LP_Global::add_object_feature( $this->object_type, $feature, $type );
412 }
413
414 /**
415 * @param $feature
416 *
417 * @return mixed
418 */
419 protected function _sanitize_feature_key( $feature ) {
420 return preg_replace( '~[_]+~', '-', $feature );
421 }
422
423 /**
424 * Get all features are supported by question.
425 *
426 * @return array
427 * @deprecated 4.1.7.3
428 */
429 public function get_supports() {
430 _deprecated_function( __FUNCTION__, '4.1.7.3' );
431 return LP_Global::get_object_supports( $this->object_type );
432 }
433
434 /**
435 * @param $value
436 * @deprecated 4.1.7.3
437 */
438 /*public function set_no_cache( $value ) {
439 $this->_no_cache = $value;
440 }*/
441
442 /**
443 * @return bool
444 * @deprecated 4.1.7.3
445 */
446 /*public function get_no_cache() {
447 return $this->_no_cache;
448 }*/
449
450 /**
451 * Read all metas and set to object
452 */
453 public function read_meta() {
454 $meta_data = $this->_curd->read_meta( $this );
455
456 if ( $meta_data ) {
457 $external_metas = array_filter( $meta_data, array( $this, 'exclude_metas' ) );
458
459 foreach ( $external_metas as $meta ) {
460 $this->_meta_data[] = $meta;
461 }
462 }
463 }
464
465 /**
466 * Callback function for excluding meta keys.
467 *
468 * @param $meta
469 *
470 * @return bool
471 */
472 protected function exclude_metas( $meta ) {
473 $exclude_keys = array_merge( array_keys( $this->_meta_keys ), array_keys( $this->_data ) );
474
475 return ! in_array( $meta->meta_key, $exclude_keys ) && 0 !== stripos( $meta->meta_key, 'wp_' );
476 }
477
478 /**
479 * Add new meta data to object.
480 *
481 * @param string|array $key_or_array
482 * @param string $value
483 */
484 public function add_meta( $key_or_array, $value = '' ) {
485 if ( is_array( $key_or_array ) ) {
486 foreach ( $key_or_array as $key => $value ) {
487 $this->add_meta( $key, $value );
488 }
489 } elseif ( is_string( $key_or_array ) ) {
490 $this->_meta_data[] = (object) array(
491 'meta_key' => $key_or_array,
492 'meta_value' => $value,
493 );
494 } else {
495 $this->_meta_data[] = $key_or_array;
496 }
497 }
498
499 /**
500 * Delete meta data by key.
501 * If meta value is passed, compare with existing value in database (string only).
502 *
503 * @param string $meta_key
504 * @param bool $meta_value - Optional. FALSE to abort comparison.
505 *
506 * @return bool
507 */
508 public function delete_meta( $meta_key, $meta_value = false ) {
509 if ( empty( $this->_meta_data ) ) {
510 return false;
511 } else {
512 $new_meta_data = array();
513 foreach ( $this->_meta_data as $k => $meta_data ) {
514 if ( $meta_data->meta_key === $meta_key ) {
515 if ( false !== $meta_value && $meta_data->meta_value === $meta_value ) {
516 continue;
517 }
518 continue;
519 }
520 $new_meta_data[] = $meta_data;
521 }
522 // Assign new meta
523 $this->_meta_data = $new_meta_data;
524
525 // Force to deleting from database
526 delete_post_meta( $this->get_id(), $meta_key, $meta_value );
527 }
528
529 return true;
530 }
531
532 /**
533 * Update item meta if it is already existed. Otherwise, add a new once.
534 *
535 * @param $meta_key
536 * @param $meta_value
537 */
538 public function set_meta( $meta_key, $meta_value ) {
539 if ( empty( $this->_meta_data ) ) {
540 $this->add_meta( $meta_key, $meta_value );
541 } else {
542 $found = false;
543 foreach ( $this->_meta_data as $k => $meta_data ) {
544 if ( $meta_data->meta_key === $meta_key ) {
545 $this->_meta_data[ $k ]->meta_value = $meta_value;
546 $found = true;
547 }
548 }
549 if ( ! $found ) {
550 $this->add_meta( $meta_key, $meta_value );
551 }
552 }
553 }
554
555 /**
556 * Update meta.
557 *
558 * @updated 3.1.0
559 *
560 * @param string $key
561 * @param mixed $value
562 * @param mixed $prev_value
563 */
564 public function update_meta( $key = '', $value = '', $prev_value = '' ) {
565 if ( func_num_args() == 0 ) {
566 if ( $this->_meta_data ) {
567 foreach ( $this->_meta_data as $meta_data ) {
568 $this->_curd->update_meta( $this, $meta_data );
569 }
570 }
571 } else {
572 $update = update_post_meta( $this->get_id(), $key, $value, $prev_value );
573 if ( ! is_bool( $update ) && $update ) {
574 $this->_meta_data = (object) array(
575 'meta_key' => $key,
576 'meta_value' => $value,
577 );
578 }
579 }
580 }
581
582
583 /**
584 * Get meta keys.
585 *
586 * @return array
587 */
588 public function get_meta_keys() {
589 return $this->_meta_keys;
590 }
591
592 }
593
594 }
595