PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9
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.1.6.9, at inc/abstracts/abstract-object-data.php

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