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 +72 -43 4.3.7 → 4.4.9 View file →
@@ -15,9 +15,11 @@
15 15 use LearnPress;
16 16 use LearnPress\Databases\PostDB;
17 17 use LearnPress\Filters\FilterBase;
18 18 use LearnPress\Filters\PostFilter;
19 +use LearnPress\Services\CourseService;
19 20 use LP_Cache;
21 +use LP_Debug;
20 22 use LP_Post_Meta_DB;
21 23 use LP_Post_Meta_Filter;
22 24 use LP_Post_Type_Filter;
23 25
@@ -81,11 +83,9 @@
81 83 if ( $data ) {
82 84 $this->map_to_object( $data );
83 85 }
84 86
85 - if ( is_null( $this->meta_data ) ) {
86 - $this->meta_data = new stdClass();
87 - }
87 + $this->get_metadata();
88 88 }
89 89
90 90 /**
91 91 * Get user model
@@ -128,9 +128,9 @@
128 128 * @param int $post_id
129 129 * @param bool $check_cache
130 130 *
131 131 * @return false|PostModel
132 - * @version 1.0.0
132 + * @version 1.0.1
133 133 * @since 4.3.2
134 134 */
135 135 public static function find_by_id( int $post_id, bool $check_cache = false ) {
136 136 $filter = new PostFilter();
@@ -148,11 +148,11 @@
148 148 }
149 149
150 150 // Check cache
151 151 if ( $check_cache ) {
152 - $quizPostModel = $lp_cache->get_cache( $key_cache );
153 - if ( $quizPostModel instanceof QuizPostModel ) {
154 - return $quizPostModel;
152 + $postModel = $lp_cache->get_cache( $key_cache );
153 + if ( $postModel instanceof PostModel ) {
154 + return $postModel;
155 155 }
156 156 }
157 157
158 158 $postModel = self::get_item_model_from_db( $filter );
@@ -191,9 +191,9 @@
191 191 if ( $post_rs instanceof stdClass ) {
192 192 $post_model = new static( $post_rs );
193 193 }
194 194 } catch ( Throwable $e ) {
195 - error_log( __METHOD__ . ': ' . $e->getMessage() );
195 + LP_Debug::error_log( $e );
196 196 }
197 197
198 198 return $post_model;
199 199 }
@@ -198,8 +198,23 @@
198 198 return $post_model;
199 199 }
200 200
201 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 + /**
202 217 * Get all meta_data, all keys of a user it
203 218 *
204 219 * @return stdClass|null
205 220 * @throws Exception
@@ -232,11 +247,12 @@
232 247 * Check capabilities to create new post.
233 248 *
234 249 * @return bool
235 250 * @since 4.2.9.4
236 - * @version 1.0.0
251 + * @version 1.0.1
237 252 */
238 253 public function check_capabilities_create(): bool {
254 + //return current_user_can( 'edit_' . $this->post_type . 's' );
239 255 return true;
240 256 }
241 257
242 258 /**
@@ -243,11 +259,12 @@
243 259 * Check capabilities to update post.
244 260 *
245 261 * @return bool
246 262 * @since 4.2.9.4
247 - * @version 1.0.0
263 + * @version 1.0.1
248 264 */
249 265 public function check_capabilities_update(): bool {
266 + //return current_user_can( 'edit_' . $this->post_type, $this->ID );
250 267 return true;
251 268 }
252 269
253 270 /**
@@ -255,19 +272,12 @@
255 272 * Check user current can edit it.
256 273 *
257 274 * @return void
258 275 * @throws Exception
259 - * @version 1.0.0
276 + * @version 1.0.1
260 277 * @since 4.2.9
261 278 */
262 279 public function check_capabilities_create_item_course() {
263 - $course_item_types = CourseModel::item_types_support();
264 - // Questions not type item of course, it is type item of quiz, but need check here.
265 - $course_item_types[] = LP_QUESTION_CPT;
266 - if ( ! in_array( $this->post_type, $course_item_types, true ) ) {
267 - return;
268 - }
269 -
270 280 $user = wp_get_current_user();
271 281 if ( ! user_can( $user, 'edit_' . LP_LESSON_CPT . 's' ) ) {
272 282 throw new Exception( __( 'You do not have permission to create item.', 'learnpress' ) );
273 283 }
@@ -278,19 +288,12 @@
278 288 * Check user current can edit it.
279 289 *
280 290 * @return void
281 291 * @throws Exception
282 - * @version 1.0.0
292 + * @version 1.0.1
283 293 * @since 4.2.9
284 294 */
285 295 public function check_capabilities_update_item_course() {
286 - $course_item_types = CourseModel::item_types_support();
287 - // Questions not type item of course, it is type item of quiz, but need check here.
288 - $course_item_types[] = LP_QUESTION_CPT;
289 - if ( ! in_array( $this->post_type, $course_item_types, true ) ) {
290 - return;
291 - }
292 -
293 296 $user = wp_get_current_user();
294 297 if ( ! user_can( $user, 'edit_' . LP_LESSON_CPT, $this->ID ) ) {
295 298 throw new Exception( __( 'You do not have permission to edit this item.', 'learnpress' ) );
296 299 }
@@ -302,16 +305,17 @@
302 305 * If user_item_id is empty, insert new data, else update data.
303 306 *
304 307 * @throws Exception
305 308 * @since 4.2.5
306 - * @version 1.0.4
309 + * @version 1.0.6
307 310 */
308 311 public function save( bool $force_save = false ) {
309 312 $data = get_object_vars( $this );
310 313
311 - if ( ! empty( $this->meta_data ) ) {
314 + $metadata = $this->get_metadata();
315 + if ( ! empty( $metadata ) ) {
312 316 $data['meta_input'] = [];
313 - foreach ( $this->meta_data as $key_meta => $value_meta ) {
317 + foreach ( $metadata as $key_meta => $value_meta ) {
314 318 $data['meta_input'][ $key_meta ] = $value_meta;
315 319 }
316 320 }
317 321
@@ -318,13 +322,14 @@
318 322 // Check if exists course id.
319 323 if ( empty( $this->ID ) ) { // Insert data.
320 324 // Check permission
321 325 if ( ! $force_save ) {
322 - if ( ! $this->check_capabilities_create() ) {
326 + if ( CourseService::check_is_item_type_of_course( $this->post_type )
327 + || LP_QUESTION_CPT === $this->post_type ) {
328 + $this->check_capabilities_create_item_course();
329 + } elseif ( ! $this->check_capabilities_create() ) {
323 330 throw new Exception( __( 'You do not have permission to create item.', 'learnpress' ) );
324 331 }
325 -
326 - $this->check_capabilities_create_item_course();
327 332 }
328 333
329 334 unset( $data['ID'] );
330 335 $post_id = wp_insert_post( $data, true );
@@ -330,13 +335,14 @@
330 335 $post_id = wp_insert_post( $data, true );
331 336 } else { // Update data.
332 337 // Check permission
333 338 if ( ! $force_save ) {
334 - if ( ! $this->check_capabilities_update() ) {
335 - throw new Exception( __( 'You do not have permission to edit this item.', 'learnpress' ) );
339 + if ( courseservice::check_is_item_type_of_course( $this->post_type )
340 + || LP_QUESTION_CPT === $this->post_type ) {
341 + $this->check_capabilities_update_item_course();
342 + } elseif ( ! $this->check_capabilities_update() ) {
343 + throw new exception( __( 'you do not have permission to edit this item.', 'learnpress' ) );
336 344 }
337 -
338 - $this->check_capabilities_update_item_course();
339 345 }
340 346
341 347 $post_id = wp_update_post( $data, true );
342 348 }
@@ -459,12 +465,15 @@
459 465 * @param mixed $default_value
460 466 * @param bool $single
461 467 *
462 468 * @return false|mixed
469 + * @since 4.2.6.9
470 + * @version 1.0.3
463 471 */
464 472 public function get_meta_value_by_key( string $key, $default_value = false, bool $single = true ) {
465 - if ( $this->meta_data instanceof stdClass && isset( $this->meta_data->{$key} ) ) {
466 - return maybe_unserialize( $this->meta_data->{$key} );
473 + $metadata = $this->get_metadata();
474 + if ( isset( $metadata->{$key} ) ) {
475 + return maybe_unserialize( $metadata->{$key} );
467 476 }
468 477
469 478 $value = get_post_meta( $this->ID, $key, $single );
470 479 if ( empty( $value ) ) {
@@ -471,14 +480,31 @@
471 480 $value = $default_value;
472 481 }
473 482
474 483 $value = maybe_unserialize( $value );
475 - $this->meta_data->{$key} = $value;
484 + $this->set_meta_value_by_key( $key, $value );
476 485
477 486 return $value;
478 487 }
479 488
480 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 + /**
481 507 * Get meta value by key.
482 508 *
483 509 * @param string $key
484 510 * @param mixed $value
@@ -486,20 +512,23 @@
486 512 *
487 513 * @return void
488 514 * @throws Exception
489 515 * @since 4.2.6.9
490 - * @version 1.0.3
516 + * @version 1.0.4
491 517 */
492 518 public function save_meta_value_by_key( string $key, $value, bool $fore_update = false ) {
493 519 // Check permission
494 520 if ( ! $fore_update ) {
495 - 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() ) {
496 526 throw new Exception( __( 'You do not have permission to edit this item.', 'learnpress' ) );
497 527 }
498 - $this->check_capabilities_create_item_course();
499 528 }
500 529
501 - $this->meta_data->{$key} = $value;
530 + $this->set_meta_value_by_key( $key, $value );
502 531 update_post_meta( $this->ID, $key, $value );
503 532 }
504 533
505 534 /**