| @@ -1,199 +1,199 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Class UserItemModel | |
| 5 | - * To replace class LP_User_Item | |
| 6 | - * | |
| 7 | - * @package LearnPress/Classes | |
| 8 | - * @version 1.0.1 | |
| 9 | - * @since 4.2.5 | |
| 10 | - */ | |
| 11 | - | |
| 12 | -namespace LearnPress\Models\UserItemMeta; | |
| 13 | - | |
| 14 | -use Exception; | |
| 15 | -use LP_User_Item_Meta_DB; | |
| 16 | -use LP_User_Item_Meta_Filter; | |
| 17 | -use stdClass; | |
| 18 | -use Throwable; | |
| 19 | - | |
| 20 | -class UserItemMetaModel { | |
| 21 | - /** | |
| 22 | - * Auto increment | |
| 23 | - * | |
| 24 | - * @var int | |
| 25 | - */ | |
| 26 | - private $meta_id = 0; | |
| 27 | - /** | |
| 28 | - * @var string User ID, foreign key | |
| 29 | - */ | |
| 30 | - public $learnpress_user_item_id = 0; | |
| 31 | - /** | |
| 32 | - * @var string meta key | |
| 33 | - */ | |
| 34 | - public $meta_key = 0; | |
| 35 | - /** | |
| 36 | - * @var string meta value | |
| 37 | - */ | |
| 38 | - public $meta_value = ''; | |
| 39 | - /** | |
| 40 | - * @var string Extra value | |
| 41 | - */ | |
| 42 | - public $extra_value = ''; | |
| 43 | - | |
| 44 | - /** | |
| 45 | - * If data get from database, map to object. | |
| 46 | - * Else create new object to save data to database. | |
| 47 | - * | |
| 48 | - * @param array|object|mixed $data | |
| 49 | - */ | |
| 50 | - public function __construct( $data = null ) { | |
| 51 | - if ( $data ) { | |
| 52 | - $this->map_to_object( $data ); | |
| 53 | - } | |
| 54 | - } | |
| 55 | - | |
| 56 | - /** | |
| 57 | - * Map array, object data to UserItemModel. | |
| 58 | - * Use for data get from database. | |
| 59 | - * | |
| 60 | - * @param array|object|mixed $data | |
| 61 | - * @return UserItemMetaModel | |
| 62 | - */ | |
| 63 | - public function map_to_object( $data ): UserItemMetaModel { | |
| 64 | - foreach ( $data as $key => $value ) { | |
| 65 | - if ( property_exists( $this, $key ) ) { | |
| 66 | - $this->{$key} = $value; | |
| 67 | - } | |
| 68 | - } | |
| 69 | - | |
| 70 | - return $this; | |
| 71 | - } | |
| 72 | - | |
| 73 | - /** | |
| 74 | - * Get meta id | |
| 75 | - * | |
| 76 | - * @return int | |
| 77 | - */ | |
| 78 | - public function get_meta_id(): int { | |
| 79 | - return $this->meta_id; | |
| 80 | - } | |
| 81 | - | |
| 82 | - /** | |
| 83 | - * Set meta id | |
| 84 | - * | |
| 85 | - * @param int $meta_id | |
| 86 | - */ | |
| 87 | - public function set_meta_id( int $meta_id ) { | |
| 88 | - $this->meta_id = $meta_id; | |
| 89 | - } | |
| 90 | - | |
| 91 | - /** | |
| 92 | - * Get all data, all keys of a user item | |
| 93 | - * | |
| 94 | - * @throws Exception | |
| 95 | - * @return stdClass|false | |
| 96 | - */ | |
| 97 | - public static function get_all_data( $user_item_id ) { | |
| 98 | - $lp_user_item_meta_db = LP_User_Item_Meta_DB::getInstance(); | |
| 99 | - $filter = new LP_User_Item_Meta_Filter(); | |
| 100 | - $filter->learnpress_user_item_id = $user_item_id; | |
| 101 | - $filter->run_query_count = false; | |
| 102 | - $user_itemmeta_rs = $lp_user_item_meta_db->get_user_item_metas( $filter ); | |
| 103 | - $all_data = false; | |
| 104 | - if ( $user_itemmeta_rs instanceof stdClass ) { | |
| 105 | - $all_data = new stdClass(); | |
| 106 | - foreach ( $user_itemmeta_rs as $value ) { | |
| 107 | - $all_data->{$value->meta_key} = new static( $value ); | |
| 108 | - } | |
| 109 | - } | |
| 110 | - | |
| 111 | - return $all_data; | |
| 112 | - } | |
| 113 | - | |
| 114 | - /** | |
| 115 | - * Get user item from database by le, item_id, item_type. | |
| 116 | - * If not exists, return false. | |
| 117 | - * If exists, return UserItemModel. | |
| 118 | - * | |
| 119 | - * @param LP_User_Item_Meta_Filter $filter | |
| 120 | - * @param bool $no_cache | |
| 121 | - * @return UserItemMetaModel|false | |
| 122 | - */ | |
| 123 | - public static function get_user_item_meta_model_from_db( LP_User_Item_Meta_Filter $filter, bool $no_cache = true ) { | |
| 124 | - $lp_user_item_meta_db = LP_User_Item_Meta_DB::getInstance(); | |
| 125 | - $user_item_meta_model = false; | |
| 126 | - | |
| 127 | - try { | |
| 128 | - $lp_user_item_meta_db->get_query_single_row( $filter ); | |
| 129 | - $query_single_row = $lp_user_item_meta_db->get_user_item_metas( $filter ); | |
| 130 | - $user_item_rs = $lp_user_item_meta_db->wpdb->get_row( $query_single_row ); | |
| 131 | - if ( $user_item_rs instanceof stdClass ) { | |
| 132 | - $user_item_meta_model = new static( $user_item_rs ); | |
| 133 | - } | |
| 134 | - } catch ( Throwable $e ) { | |
| 135 | - error_log( __METHOD__ . ': ' . $e->getMessage() ); | |
| 136 | - } | |
| 137 | - | |
| 138 | - return $user_item_meta_model; | |
| 139 | - } | |
| 140 | - | |
| 141 | - /** | |
| 142 | - * Update data to database. | |
| 143 | - * | |
| 144 | - * If user_item_id is empty, insert new data, else update data. | |
| 145 | - * | |
| 146 | - * @return UserItemMetaModel | |
| 147 | - * @throws Exception | |
| 148 | - * @since 4.2.5 | |
| 149 | - * @version 1.0.0 | |
| 150 | - */ | |
| 151 | - public function save(): UserItemMetaModel { | |
| 152 | - $lp_user_item_meta_db = LP_User_Item_Meta_DB::getInstance(); | |
| 153 | - $data = []; | |
| 154 | - foreach ( get_object_vars( $this ) as $property => $value ) { | |
| 155 | - $data[ $property ] = $value; | |
| 156 | - } | |
| 157 | - | |
| 158 | - // Check if exists user_item_id. | |
| 159 | - if ( empty( $this->get_meta_id() ) ) { // Insert data. | |
| 160 | - $meta_id_new = $lp_user_item_meta_db->insert_data( $data ); | |
| 161 | - if ( empty( $meta_id_new ) ) { | |
| 162 | - throw new Exception( __METHOD__ . ': ' . 'Cannot insert data to database.' ); | |
| 163 | - } | |
| 164 | - } else { // Update data. | |
| 165 | - $lp_user_item_meta_db->update_data( $data ); | |
| 166 | - } | |
| 167 | - | |
| 168 | - if ( ! empty( $meta_id_new ) ) { | |
| 169 | - $this->set_meta_id( $meta_id_new ); | |
| 170 | - } | |
| 171 | - | |
| 172 | - $this->clean_caches(); | |
| 173 | - | |
| 174 | - return $this; | |
| 175 | - } | |
| 176 | - | |
| 177 | - /** | |
| 178 | - * Delete user_item_meta | |
| 179 | - * | |
| 180 | - * @param mixed $meta_value | |
| 181 | - * @param bool $delete_all | |
| 182 | - * | |
| 183 | - * @return void | |
| 184 | - * @since 4.2.7.4 | |
| 185 | - */ | |
| 186 | - public function delete( $meta_value = '', bool $delete_all = false ) { | |
| 187 | - learn_press_delete_user_item_meta( $this->learnpress_user_item_id, $this->meta_key, $meta_value, $delete_all ); | |
| 188 | - | |
| 189 | - $this->clean_caches(); | |
| 190 | - } | |
| 191 | - | |
| 192 | - /** | |
| 193 | - * Clean caches. | |
| 194 | - * | |
| 195 | - * @return void | |
| 196 | - */ | |
| 197 | - public function clean_caches() { | |
| 198 | - } | |
| 199 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Class UserItemModel | |
| 5 | + * To replace class LP_User_Item | |
| 6 | + * | |
| 7 | + * @package LearnPress/Classes | |
| 8 | + * @version 1.0.1 | |
| 9 | + * @since 4.2.5 | |
| 10 | + */ | |
| 11 | + | |
| 12 | +namespace LearnPress\Models\UserItemMeta; | |
| 13 | + | |
| 14 | +use Exception; | |
| 15 | +use LP_User_Item_Meta_DB; | |
| 16 | +use LP_User_Item_Meta_Filter; | |
| 17 | +use stdClass; | |
| 18 | +use Throwable; | |
| 19 | + | |
| 20 | +class UserItemMetaModel { | |
| 21 | + /** | |
| 22 | + * Auto increment | |
| 23 | + * | |
| 24 | + * @var int | |
| 25 | + */ | |
| 26 | + private $meta_id = 0; | |
| 27 | + /** | |
| 28 | + * @var string User ID, foreign key | |
| 29 | + */ | |
| 30 | + public $learnpress_user_item_id = 0; | |
| 31 | + /** | |
| 32 | + * @var string meta key | |
| 33 | + */ | |
| 34 | + public $meta_key = 0; | |
| 35 | + /** | |
| 36 | + * @var string meta value | |
| 37 | + */ | |
| 38 | + public $meta_value = ''; | |
| 39 | + /** | |
| 40 | + * @var string Extra value | |
| 41 | + */ | |
| 42 | + public $extra_value = ''; | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * If data get from database, map to object. | |
| 46 | + * Else create new object to save data to database. | |
| 47 | + * | |
| 48 | + * @param array|object|mixed $data | |
| 49 | + */ | |
| 50 | + public function __construct( $data = null ) { | |
| 51 | + if ( $data ) { | |
| 52 | + $this->map_to_object( $data ); | |
| 53 | + } | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * Map array, object data to UserItemModel. | |
| 58 | + * Use for data get from database. | |
| 59 | + * | |
| 60 | + * @param array|object|mixed $data | |
| 61 | + * @return UserItemMetaModel | |
| 62 | + */ | |
| 63 | + public function map_to_object( $data ): UserItemMetaModel { | |
| 64 | + foreach ( $data as $key => $value ) { | |
| 65 | + if ( property_exists( $this, $key ) ) { | |
| 66 | + $this->{$key} = $value; | |
| 67 | + } | |
| 68 | + } | |
| 69 | + | |
| 70 | + return $this; | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * Get meta id | |
| 75 | + * | |
| 76 | + * @return int | |
| 77 | + */ | |
| 78 | + public function get_meta_id(): int { | |
| 79 | + return $this->meta_id; | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Set meta id | |
| 84 | + * | |
| 85 | + * @param int $meta_id | |
| 86 | + */ | |
| 87 | + public function set_meta_id( int $meta_id ) { | |
| 88 | + $this->meta_id = $meta_id; | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 92 | + * Get all data, all keys of a user item | |
| 93 | + * | |
| 94 | + * @throws Exception | |
| 95 | + * @return stdClass|false | |
| 96 | + */ | |
| 97 | + public static function get_all_data( $user_item_id ) { | |
| 98 | + $lp_user_item_meta_db = LP_User_Item_Meta_DB::getInstance(); | |
| 99 | + $filter = new LP_User_Item_Meta_Filter(); | |
| 100 | + $filter->learnpress_user_item_id = $user_item_id; | |
| 101 | + $filter->run_query_count = false; | |
| 102 | + $user_itemmeta_rs = $lp_user_item_meta_db->get_user_item_metas( $filter ); | |
| 103 | + $all_data = false; | |
| 104 | + if ( $user_itemmeta_rs instanceof stdClass ) { | |
| 105 | + $all_data = new stdClass(); | |
| 106 | + foreach ( $user_itemmeta_rs as $value ) { | |
| 107 | + $all_data->{$value->meta_key} = new static( $value ); | |
| 108 | + } | |
| 109 | + } | |
| 110 | + | |
| 111 | + return $all_data; | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Get user item from database by le, item_id, item_type. | |
| 116 | + * If not exists, return false. | |
| 117 | + * If exists, return UserItemModel. | |
| 118 | + * | |
| 119 | + * @param LP_User_Item_Meta_Filter $filter | |
| 120 | + * @param bool $no_cache | |
| 121 | + * @return UserItemMetaModel|false | |
| 122 | + */ | |
| 123 | + public static function get_user_item_meta_model_from_db( LP_User_Item_Meta_Filter $filter, bool $no_cache = true ) { | |
| 124 | + $lp_user_item_meta_db = LP_User_Item_Meta_DB::getInstance(); | |
| 125 | + $user_item_meta_model = false; | |
| 126 | + | |
| 127 | + try { | |
| 128 | + $lp_user_item_meta_db->get_query_single_row( $filter ); | |
| 129 | + $query_single_row = $lp_user_item_meta_db->get_user_item_metas( $filter ); | |
| 130 | + $user_item_rs = $lp_user_item_meta_db->wpdb->get_row( $query_single_row ); | |
| 131 | + if ( $user_item_rs instanceof stdClass ) { | |
| 132 | + $user_item_meta_model = new static( $user_item_rs ); | |
| 133 | + } | |
| 134 | + } catch ( Throwable $e ) { | |
| 135 | + error_log( __METHOD__ . ': ' . $e->getMessage() ); | |
| 136 | + } | |
| 137 | + | |
| 138 | + return $user_item_meta_model; | |
| 139 | + } | |
| 140 | + | |
| 141 | + /** | |
| 142 | + * Update data to database. | |
| 143 | + * | |
| 144 | + * If user_item_id is empty, insert new data, else update data. | |
| 145 | + * | |
| 146 | + * @return UserItemMetaModel | |
| 147 | + * @throws Exception | |
| 148 | + * @since 4.2.5 | |
| 149 | + * @version 1.0.0 | |
| 150 | + */ | |
| 151 | + public function save(): UserItemMetaModel { | |
| 152 | + $lp_user_item_meta_db = LP_User_Item_Meta_DB::getInstance(); | |
| 153 | + $data = []; | |
| 154 | + foreach ( get_object_vars( $this ) as $property => $value ) { | |
| 155 | + $data[ $property ] = $value; | |
| 156 | + } | |
| 157 | + | |
| 158 | + // Check if exists user_item_id. | |
| 159 | + if ( empty( $this->get_meta_id() ) ) { // Insert data. | |
| 160 | + $meta_id_new = $lp_user_item_meta_db->insert_data( $data ); | |
| 161 | + if ( empty( $meta_id_new ) ) { | |
| 162 | + throw new Exception( __METHOD__ . ': ' . 'Cannot insert data to database.' ); | |
| 163 | + } | |
| 164 | + } else { // Update data. | |
| 165 | + $lp_user_item_meta_db->update_data( $data ); | |
| 166 | + } | |
| 167 | + | |
| 168 | + if ( ! empty( $meta_id_new ) ) { | |
| 169 | + $this->set_meta_id( $meta_id_new ); | |
| 170 | + } | |
| 171 | + | |
| 172 | + $this->clean_caches(); | |
| 173 | + | |
| 174 | + return $this; | |
| 175 | + } | |
| 176 | + | |
| 177 | + /** | |
| 178 | + * Delete user_item_meta | |
| 179 | + * | |
| 180 | + * @param mixed $meta_value | |
| 181 | + * @param bool $delete_all | |
| 182 | + * | |
| 183 | + * @return void | |
| 184 | + * @since 4.2.7.4 | |
| 185 | + */ | |
| 186 | + public function delete( $meta_value = '', bool $delete_all = false ) { | |
| 187 | + learn_press_delete_user_item_meta( $this->learnpress_user_item_id, $this->meta_key, $meta_value, $delete_all ); | |
| 188 | + | |
| 189 | + $this->clean_caches(); | |
| 190 | + } | |
| 191 | + | |
| 192 | + /** | |
| 193 | + * Clean caches. | |
| 194 | + * | |
| 195 | + * @return void | |
| 196 | + */ | |
| 197 | + public function clean_caches() { | |
| 198 | + } | |
| 199 | +} | |