Skip to content

Commit

Permalink
= 4.2.7.4 =
Browse files Browse the repository at this point in the history
~ Added: set_meta_value_for_key, delete_meta methods on class UserItemModel.
~ Added: delete method on class UserItemMetaModel.
~ Added: variable global $lpCourseModel
  • Loading branch information
tungnxt89 committed Dec 2, 2024
1 parent e660426 commit 4099f36
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 38 deletions.
20 changes: 4 additions & 16 deletions inc/Models/CourseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down Expand Up @@ -73,10 +73,6 @@ class CourseModel {
*/
public $lang = null;
/********** Field not on table **********/
/**
* @var UserModel author model
*/
public $author;
/**
* @var stdClass all meta data
*/
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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() );
Expand Down
14 changes: 2 additions & 12 deletions inc/Models/PostModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 );
}

/**
Expand Down
17 changes: 16 additions & 1 deletion inc/Models/UserItemMeta/UserItemMetaModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down Expand Up @@ -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.
*
Expand Down
60 changes: 58 additions & 2 deletions inc/Models/UserItems/UserItemModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down Expand Up @@ -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();
}
}

/**
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion inc/TemplateHooks/Course/SingleCourseTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public function html_instructor( $course, bool $with_avatar = false ): string {
$link_instructor = sprintf(
'<a href="%s">%s %s</a>',
$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 )
);

Expand Down
2 changes: 1 addition & 1 deletion inc/TemplateHooks/Instructor/SingleInstructorTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 6 additions & 5 deletions inc/TemplateHooks/UserTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,22 @@ 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 {
if ( empty( $size_display ) ) {
$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'];
Expand All @@ -135,7 +136,7 @@ public function html_avatar( UserModel $user, array $size_display = [], string $
$section = apply_filters(
'learn-press/user/html-avatar',
[
'wrapper' => sprintf( '<div class="%s-avatar">', $class ),
'wrapper' => sprintf( '<div class="%s-avatar">', $class_name ),
'avatar' => $img_avatar,
'wrapper_end' => '</div>',
],
Expand Down
12 changes: 12 additions & 0 deletions inc/class-lp-page-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,18 @@ public function setup_data_for_item_course( $post ): WP_Post {
return $post;
}

/**
* Use for case not load course via send params, use global instead.
* Not recommended for use
* This method is only purpose instead get_the_ID(), on file item of course.
* When all file write by standard callback send param courseModel, this method will be removed.
*
* @return false|CourseModel
* @since 4.2.7.4
*/
global $lpCourseModel;
$lpCourseModel = CourseModel::find( $course->get_id(), true );

/**
* @deprecated v4.1.6.1 LearnPress::instance()->global['course'], $GLOBALS['course']
* Some theme still use: global $course; LearnPress::instance()->global['course']
Expand Down

0 comments on commit 4099f36

Please sign in to comment.