PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.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 All 140 releases
← All changes | inc/Models/PostModel.php +52 -14 4.4.2 → 4.4.9 View file →
@@ -17,8 +17,9 @@
17 17 use LearnPress\Filters\FilterBase;
18 18 use LearnPress\Filters\PostFilter;
19 19 use LearnPress\Services\CourseService;
20 20 use LP_Cache;
21 +use LP_Debug;
21 22 use LP_Post_Meta_DB;
22 23 use LP_Post_Meta_Filter;
23 24 use LP_Post_Type_Filter;
24 25
@@ -82,11 +83,9 @@
82 83 if ( $data ) {
83 84 $this->map_to_object( $data );
84 85 }
85 86
86 - if ( is_null( $this->meta_data ) ) {
87 - $this->meta_data = new stdClass();
88 - }
87 + $this->get_metadata();
89 88 }
90 89
91 90 /**
92 91 * Get user model
@@ -192,9 +191,9 @@
192 191 if ( $post_rs instanceof stdClass ) {
193 192 $post_model = new static( $post_rs );
194 193 }
195 194 } catch ( Throwable $e ) {
196 - error_log( __METHOD__ . ': ' . $e->getMessage() );
195 + LP_Debug::error_log( $e );
197 196 }
198 197
199 198 return $post_model;
200 199 }
@@ -199,8 +198,23 @@
199 198 return $post_model;
200 199 }
201 200
202 201 /**
202 + * Return object metadata
203 + *
204 + * @return stdClass
205 + * @since 4.4.8.1
206 + * @version 1.0.0
207 + */
208 + public function get_metadata(): stdClass {
209 + if ( is_null( $this->meta_data ) ) {
210 + $this->meta_data = new stdClass();
211 + }
212 +
213 + return $this->meta_data;
214 + }
215 +
216 + /**
203 217 * Get all meta_data, all keys of a user it
204 218 *
205 219 * @return stdClass|null
206 220 * @throws Exception
@@ -291,16 +305,17 @@
291 305 * If user_item_id is empty, insert new data, else update data.
292 306 *
293 307 * @throws Exception
294 308 * @since 4.2.5
295 - * @version 1.0.5
309 + * @version 1.0.6
296 310 */
297 311 public function save( bool $force_save = false ) {
298 312 $data = get_object_vars( $this );
299 313
300 - if ( ! empty( $this->meta_data ) ) {
314 + $metadata = $this->get_metadata();
315 + if ( ! empty( $metadata ) ) {
301 316 $data['meta_input'] = [];
302 - foreach ( $this->meta_data as $key_meta => $value_meta ) {
317 + foreach ( $metadata as $key_meta => $value_meta ) {
303 318 $data['meta_input'][ $key_meta ] = $value_meta;
304 319 }
305 320 }
306 321
@@ -450,12 +465,15 @@
450 465 * @param mixed $default_value
451 466 * @param bool $single
452 467 *
453 468 * @return false|mixed
469 + * @since 4.2.6.9
470 + * @version 1.0.3
454 471 */
455 472 public function get_meta_value_by_key( string $key, $default_value = false, bool $single = true ) {
456 - if ( $this->meta_data instanceof stdClass && isset( $this->meta_data->{$key} ) ) {
457 - return maybe_unserialize( $this->meta_data->{$key} );
473 + $metadata = $this->get_metadata();
474 + if ( isset( $metadata->{$key} ) ) {
475 + return maybe_unserialize( $metadata->{$key} );
458 476 }
459 477
460 478 $value = get_post_meta( $this->ID, $key, $single );
461 479 if ( empty( $value ) ) {
@@ -462,14 +480,31 @@
462 480 $value = $default_value;
463 481 }
464 482
465 483 $value = maybe_unserialize( $value );
466 - $this->meta_data->{$key} = $value;
484 + $this->set_meta_value_by_key( $key, $value );
467 485
468 486 return $value;
469 487 }
470 488
471 489 /**
490 + * Set meta value by key
491 + *
492 + * @param string $key
493 + * @param mixed $value
494 + *
495 + * @since 4.4.8.1
496 + * @version 1.0.0
497 + * @return void
498 + */
499 + public function set_meta_value_by_key( string $key, $value ) {
500 + $metadata = $this->get_metadata();
501 + $metadata->{$key} = $value;
502 +
503 + $this->meta_data = $metadata;
504 + }
505 +
506 + /**
472 507 * Get meta value by key.
473 508 *
474 509 * @param string $key
475 510 * @param mixed $value
@@ -477,20 +512,23 @@
477 512 *
478 513 * @return void
479 514 * @throws Exception
480 515 * @since 4.2.6.9
481 - * @version 1.0.3
516 + * @version 1.0.4
482 517 */
483 518 public function save_meta_value_by_key( string $key, $value, bool $fore_update = false ) {
484 519 // Check permission
485 520 if ( ! $fore_update ) {
486 - if ( ! $this->check_capabilities_update() ) {
521 + // Check permission if is item type of course
522 + if ( CourseService::check_is_item_type_of_course( $this->post_type )
523 + || LP_QUESTION_CPT === $this->post_type ) {
524 + $this->check_capabilities_update_item_course();
525 + } elseif ( ! $this->check_capabilities_update() ) {
487 526 throw new Exception( __( 'You do not have permission to edit this item.', 'learnpress' ) );
488 527 }
489 - $this->check_capabilities_create_item_course();
490 528 }
491 529
492 - $this->meta_data->{$key} = $value;
530 + $this->set_meta_value_by_key( $key, $value );
493 531 update_post_meta( $this->ID, $key, $value );
494 532 }
495 533
496 534 /**