diff --git a/inc/Models/CourseModel.php b/inc/Models/CourseModel.php index 1f2618244..e92cdf11d 100644 --- a/inc/Models/CourseModel.php +++ b/inc/Models/CourseModel.php @@ -7,7 +7,7 @@ * Another fields for query list courses faster * * @package LearnPress/Classes - * @version 1.0.1 + * @version 1.0.2 * @since 4.2.6.9 */ @@ -73,10 +73,6 @@ class CourseModel { */ public $lang = null; /********** Field not on table **********/ - /** - * @var UserModel author model - */ - public $author; /** * @var stdClass all meta data */ @@ -180,14 +176,8 @@ public function get_image_url(): string { * @return UserModel|false */ public function get_author_model() { - if ( isset( $this->author ) ) { - return $this->author; - } - - $post = new CoursePostModel( $this ); - $this->author = $post->get_author_model(); - - return $this->author; + $post = new CoursePostModel( $this ); + return $post->get_author_model(); } /** @@ -1098,9 +1088,7 @@ public static function get_item_model_from_db( LP_Course_JSON_Filter $filter, bo $course_model = new static( $course_obj ); //$course_model->json = $course_rs->json; $course_model->post_content = $course_rs->post_content; - if ( $course_model->author instanceof stdClass ) { - $course_model->author = new UserModel( $course_model->author ); - } + $course_model->get_author_model(); } } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); diff --git a/inc/Models/PostModel.php b/inc/Models/PostModel.php index 8c2878ee1..dc57e4137 100644 --- a/inc/Models/PostModel.php +++ b/inc/Models/PostModel.php @@ -41,10 +41,6 @@ class PostModel { * @var string author id, foreign key */ public $post_author = 0; - /** - * @var UserModel author model - */ - public $author; /** * @var string post date */ @@ -116,19 +112,13 @@ public function __construct( $data = null ) { * @return false|UserModel */ public function get_author_model() { - if ( ! empty( $this->author ) ) { - return $this->author; - } - - if ( empty( $this->post_author ) ) { + if ( ! empty( $this->post_author ) ) { $author_id = $this->post_author; } else { $author_id = get_post_field( 'post_author', $this ); } - $this->author = UserModel::find( $author_id, true ); - - return $this->author; + return UserModel::find( $author_id, true ); } /** diff --git a/inc/Models/UserItemMeta/UserItemMetaModel.php b/inc/Models/UserItemMeta/UserItemMetaModel.php index 494960b55..7d0decd70 100644 --- a/inc/Models/UserItemMeta/UserItemMetaModel.php +++ b/inc/Models/UserItemMeta/UserItemMetaModel.php @@ -5,7 +5,7 @@ * To replace class LP_User_Item * * @package LearnPress/Classes - * @version 1.0.0 + * @version 1.0.1 * @since 4.2.5 */ @@ -174,6 +174,21 @@ public function save(): UserItemMetaModel { return $this; } + /** + * Delete user_item_meta + * + * @param mixed $meta_value + * @param bool $delete_all + * + * @return void + * @since 4.2.7.4 + */ + public function delete( $meta_value = '', bool $delete_all = false ) { + learn_press_delete_user_item_meta( $this->learnpress_user_item_id, $this->meta_key, $meta_value, $delete_all ); + + $this->clean_caches(); + } + /** * Clean caches. * diff --git a/inc/Models/UserItems/UserItemModel.php b/inc/Models/UserItems/UserItemModel.php index 1df936ce5..984577945 100644 --- a/inc/Models/UserItems/UserItemModel.php +++ b/inc/Models/UserItems/UserItemModel.php @@ -5,7 +5,7 @@ * To replace class LP_User_Item * * @package LearnPress/Classes - * @version 1.0.0 + * @version 1.0.1 * @since 4.2.5 */ @@ -115,6 +115,10 @@ public function __construct( $data = null ) { $this->map_to_object( $data ); $this->get_user_model(); } + + if ( ! $this->meta_data instanceof stdClass ) { + $this->meta_data = new stdClass(); + } } /** @@ -284,6 +288,10 @@ public static function find_user_item( $userItemModel = static::get_user_item_model_from_db( $filter ); // Set cache if ( $userItemModel instanceof UserItemModel ) { + if ( ! $userItemModel->meta_data instanceof stdClass ) { + $userItemModel->meta_data = new stdClass(); + } + $lpUserItemCache->set_cache( $key_cache, $userItemModel ); } @@ -328,18 +336,66 @@ public function get_meta_value_from_key( string $key, bool $get_extra = false ) if ( ! $this->meta_data instanceof stdClass ) { $this->meta_data = new stdClass(); } - $this->meta_data->{$key} = $user_item_metadata; if ( $get_extra ) { $data = $user_item_metadata->extra_value; } else { $data = $user_item_metadata->meta_value; } + + $this->meta_data->{$key} = $data; } return $data; } + /** + * Update meta value for key. + * + * @param string $key + * @param mixed $value + * @param bool $is_extra + * + * @return void + * @since 4.2.7.4 + * @version 1.0.0 + */ + public function set_meta_value_for_key( string $key, $value, bool $is_extra = false ) { + if ( ! $this->meta_data instanceof stdClass ) { + $this->meta_data = new stdClass(); + } + + $this->meta_data->{$key} = $value; + $lp_db = LP_User_Items_DB::getInstance(); + if ( $is_extra ) { + if ( is_object( $value ) || is_array( $value ) ) { + $value = json_encode( $value ); + } + $lp_db->update_extra_value( $this->get_user_item_id(), $key, $value ); + } else { + learn_press_update_user_item_meta( $this->get_user_item_id(), $key, $value ); + } + + //$this->clean_caches(); + } + + /** + * Delete meta from key. + * @param string $key + * + * @return void + * @since 4.2.7.4 + * @version 1.0.0 + */ + public function delete_meta( string $key ) { + $user_item_metadata = $this->get_meta_model_from_key( $key ); + if ( $user_item_metadata instanceof UserItemMetaModel ) { + $user_item_metadata->delete(); + $this->meta_data->{$key} = null; + $this->clean_caches(); + } + } + /** * Update data to database. * diff --git a/inc/TemplateHooks/Course/SingleCourseTemplate.php b/inc/TemplateHooks/Course/SingleCourseTemplate.php index 0ddc4f8c7..ee2f6e53d 100644 --- a/inc/TemplateHooks/Course/SingleCourseTemplate.php +++ b/inc/TemplateHooks/Course/SingleCourseTemplate.php @@ -268,7 +268,7 @@ public function html_instructor( $course, bool $with_avatar = false ): string { $link_instructor = sprintf( '%s %s', $instructor->get_url_instructor(), - $with_avatar ? UserTemplate::instance()->html_avatar( $instructor, 'instructor' ) : '', + $with_avatar ? UserTemplate::instance()->html_avatar( $instructor, [], 'instructor' ) : '', $singleInstructorTemplate->html_display_name( $instructor ) ); diff --git a/inc/TemplateHooks/Instructor/SingleInstructorTemplate.php b/inc/TemplateHooks/Instructor/SingleInstructorTemplate.php index d8b002e35..7cac81004 100644 --- a/inc/TemplateHooks/Instructor/SingleInstructorTemplate.php +++ b/inc/TemplateHooks/Instructor/SingleInstructorTemplate.php @@ -112,7 +112,7 @@ public function html_description( $instructor ): string { */ public function html_avatar( $instructor, array $size_display = [] ): string { $userTemplate = UserTemplate::instance(); - $html = ''; + $html = ''; if ( ! $instructor ) { return $html; } diff --git a/inc/TemplateHooks/UserTemplate.php b/inc/TemplateHooks/UserTemplate.php index d37534ba3..21de7f945 100644 --- a/inc/TemplateHooks/UserTemplate.php +++ b/inc/TemplateHooks/UserTemplate.php @@ -103,13 +103,13 @@ public function html_description( $instructor ): string { * * @param UserModel $user * @param array $size_display [ 'width' => 100, 'height' => 100 ] - * @param string $class + * @param string $class_name * * @return string * @since 4.2.7.2 - * @version 1.0.2 + * @version 1.0.3 */ - public function html_avatar( UserModel $user, array $size_display = [], string $class = 'user' ): string { + public function html_avatar( UserModel $user, array $size_display = [], string $class_name = 'user' ): string { $html = ''; try { @@ -117,7 +117,8 @@ public function html_avatar( UserModel $user, array $size_display = [], string $ $size_display = learn_press_get_avatar_thumb_size(); } - $width = $height = $size_display; + $width = $size_display; + $height = $size_display; if ( is_array( $size_display ) ) { $width = $size_display['width']; $height = $size_display['height']; @@ -135,7 +136,7 @@ public function html_avatar( UserModel $user, array $size_display = [], string $ $section = apply_filters( 'learn-press/user/html-avatar', [ - 'wrapper' => sprintf( '