← All changes
|
inc/background-process/class-lp-background-single-course.php
+74
-153
4.1.7
→
4.4.8
View file →
| @@ -5,10 +5,15 @@ | ||
| 5 | 5 | * Single to run not schedule, run one time and done when be call |
| 6 | 6 | * |
| 7 | 7 | * @since 4.1.1 |
| 8 | 8 | * @author tungnx |
| 9 | - * @version 1.0.1 | |
| 9 | + * @version 1.0.2 | |
| 10 | 10 | */ |
| 11 | + | |
| 12 | +use LearnPress\Models\CourseModel; | |
| 13 | +use LearnPress\Models\CoursePostModel; | |
| 14 | +use LearnPress\Models\PostModel; | |
| 15 | + | |
| 11 | 16 | defined( 'ABSPATH' ) || exit; |
| 12 | 17 | |
| 13 | 18 | if ( ! class_exists( 'LP_Background_Single_Course' ) ) { |
| 14 | 19 | class LP_Background_Single_Course extends LP_Async_Request { |
| @@ -14,9 +19,9 @@ | ||
| 14 | 19 | class LP_Background_Single_Course extends LP_Async_Request { |
| 15 | 20 | protected $action = 'background_single_course'; |
| 16 | 21 | protected static $instance; |
| 17 | 22 | /** |
| 18 | - * @var $lp_course LP_Course | |
| 23 | + * @var $lp_course CourseModel | |
| 19 | 24 | */ |
| 20 | 25 | protected $lp_course; |
| 21 | 26 | /** |
| 22 | 27 | * @var array |
| @@ -24,23 +29,23 @@ | ||
| 24 | 29 | protected $data = array(); |
| 25 | 30 | |
| 26 | 31 | /** |
| 27 | 32 | * Get params via $_POST and handle |
| 28 | - * @in_array | |
| 29 | - * @see LP_Course_Post_Type::save | |
| 33 | + * | |
| 34 | + * @see LP_Course_Post_Type::save_post() | |
| 30 | 35 | */ |
| 31 | 36 | protected function handle() { |
| 37 | + ini_set( 'max_execution_time', 0 ); | |
| 32 | 38 | try { |
| 33 | - $handle_name = LP_Helper::sanitize_params_submitted( $_POST['handle_name'] ?? '' ); | |
| 39 | + $handle_name = LP_Request::get_param( 'handle_name', '', 'key', 'post' ); | |
| 34 | 40 | $course_id = intval( $_POST['course_id'] ?? 0 ); |
| 35 | 41 | if ( empty( $handle_name ) || ! $course_id ) { |
| 36 | 42 | return; |
| 37 | 43 | } |
| 38 | 44 | |
| 39 | - $this->lp_course = learn_press_get_course( $course_id ); | |
| 40 | - $this->data = LP_Helper::sanitize_params_submitted( $_POST['data'] ?? '' ); | |
| 41 | - | |
| 42 | - if ( empty( $this->lp_course ) ) { | |
| 45 | + $this->data = LP_Request::get_param( 'data', [], 'text', 'post' ); | |
| 46 | + $this->lp_course = CourseModel::find( $course_id, true ); | |
| 47 | + if ( ! $this->lp_course instanceof CourseModel ) { | |
| 43 | 48 | return; |
| 44 | 49 | } |
| 45 | 50 | |
| 46 | 51 | switch ( $handle_name ) { |
| @@ -52,26 +57,68 @@ | ||
| 52 | 57 | } |
| 53 | 58 | } catch ( Throwable $e ) { |
| 54 | 59 | error_log( $e->getMessage() ); |
| 55 | 60 | } |
| 61 | + ini_set( 'max_execution_time', LearnPress::$time_limit_default_of_sever ); | |
| 62 | + die; | |
| 56 | 63 | } |
| 57 | 64 | |
| 58 | 65 | /** |
| 59 | - * Save course post data | |
| 66 | + * Save course via post data | |
| 60 | 67 | * |
| 61 | 68 | * @throws Exception |
| 69 | + * @since 4.1.3 | |
| 70 | + * @version 1.0.4 | |
| 62 | 71 | */ |
| 63 | 72 | protected function save_post() { |
| 64 | - $this->save_price(); | |
| 65 | - $this->save_extra_info(); | |
| 73 | + if ( ! current_user_can( 'edit_lp_courses' ) ) { | |
| 74 | + error_log( 'Not permission save background course' ); | |
| 75 | + } | |
| 76 | + | |
| 77 | + $courseModel = $this->lp_course; | |
| 78 | + // Unset value of keys for calculate again | |
| 79 | + unset( $courseModel->first_item_id ); | |
| 80 | + unset( $courseModel->total_items ); | |
| 81 | + unset( $courseModel->sections_items ); | |
| 82 | + unset( $courseModel->meta_data->_lp_final_quiz ); | |
| 83 | + unset( $courseModel->categories ); | |
| 84 | + unset( $courseModel->tags ); | |
| 85 | + //unset( $courseModel->image_url ); | |
| 86 | + $courseModel->get_author_model(); | |
| 87 | + $courseModel->get_first_item_id(); | |
| 88 | + $courseModel->get_total_items(); | |
| 89 | + $sections_items = $courseModel->get_section_items(); | |
| 90 | + // Update for case data old, section_order and item_order begin = 0 | |
| 91 | + $section_curd = new LP_Section_CURD( $courseModel->get_id() ); | |
| 92 | + $section_ids = LP_Database::get_values_by_key( $sections_items, 'section_id' ); | |
| 93 | + $section_curd->update_sections_order( $section_ids ); | |
| 94 | + | |
| 95 | + foreach ( $sections_items as $section_items ) { | |
| 96 | + $section_curd->update_section_items( $section_items->section_id, $section_items->items ); | |
| 97 | + } | |
| 98 | + // End | |
| 99 | + $courseModel->get_final_quiz(); | |
| 100 | + $courseModel->get_categories(); | |
| 101 | + $courseModel->get_tags(); | |
| 102 | + /*$size_img_setting = LP_Settings::get_option( 'course_thumbnail_dimensions', [] ); | |
| 103 | + $size_img_send = [ | |
| 104 | + $size_img_setting['width'] ?? 500, | |
| 105 | + $size_img_setting['height'] ?? 300, | |
| 106 | + ]; | |
| 107 | + $courseModel->get_image_url( $size_img_send );*/ | |
| 108 | + $courseModel->save(); | |
| 109 | + | |
| 110 | + //$this->save_extra_info(); | |
| 111 | + $this->clean_data_invalid(); | |
| 66 | 112 | $this->review_post_author(); |
| 67 | 113 | |
| 68 | - do_action( 'lp/background/course/save', $this->lp_course, $this->data ); | |
| 114 | + // Old hook, addon wpml and assignment is using | |
| 115 | + do_action( 'lp/background/course/save', learn_press_get_course( $this->lp_course->get_id() ), $this->data ); | |
| 116 | + // New hook from v4.2.6.9 | |
| 117 | + do_action( 'learnPress/background/course/save', $courseModel, $this->data ); | |
| 69 | 118 | |
| 70 | 119 | /** |
| 71 | 120 | * Clean cache courses |
| 72 | - * | |
| 73 | - * @see LP_Course::get_courses() where set cache | |
| 74 | 121 | */ |
| 75 | 122 | $keys_cache = LP_Courses_Cache::instance()->get_cache( LP_Courses_Cache::$keys ); |
| 76 | 123 | if ( $keys_cache ) { |
| 77 | 124 | foreach ( $keys_cache as $key ) { |
| @@ -78,156 +125,30 @@ | ||
| 78 | 125 | LP_Courses_Cache::instance()->clear( $key ); |
| 79 | 126 | } |
| 80 | 127 | LP_Courses_Cache::instance()->clear( LP_Courses_Cache::$keys ); |
| 81 | 128 | } |
| 129 | + | |
| 130 | + // Clear total courses free. | |
| 131 | + $lp_courses_cache = new LP_Courses_Cache( true ); | |
| 132 | + $lp_courses_cache->clear_cache_on_group( LP_Courses_Cache::KEYS_COUNT_COURSES_FREE ); | |
| 82 | 133 | // End |
| 83 | - } | |
| 84 | 134 | |
| 85 | - /** | |
| 86 | - * Save price course | |
| 87 | - * | |
| 88 | - * @return void | |
| 89 | - */ | |
| 90 | - protected function save_price() { | |
| 91 | - $has_sale_price = false; | |
| 92 | - $regular_price = $this->data['_lp_regular_price'] ?? ''; | |
| 93 | - $sale_price = $this->data['_lp_sale_price'] ?? ''; | |
| 94 | - $start_date = $this->data['_lp_sale_start'] ?? ''; | |
| 95 | - $end_date = $this->data['_lp_sale_end'] ?? ''; | |
| 96 | - $price = 0; | |
| 97 | - | |
| 98 | - if ( '' != $regular_price ) { | |
| 99 | - $price = $regular_price; | |
| 100 | - | |
| 101 | - if ( '' != $sale_price ) { | |
| 102 | - if ( floatval( $sale_price ) < floatval( $regular_price ) ) { | |
| 103 | - $price = $sale_price; | |
| 104 | - $has_sale_price = true; | |
| 105 | - } | |
| 106 | - | |
| 107 | - // Check in days sale | |
| 108 | - if ( '' !== $start_date && '' !== $end_date ) { | |
| 109 | - $now = strtotime( get_date_from_gmt( gmdate( 'Y-m-d H:i:s', time() ) ) ); | |
| 110 | - $end = strtotime( $end_date ); | |
| 111 | - $start = strtotime( $start_date ); | |
| 112 | - | |
| 113 | - $has_sale_price = $now >= $start && $now <= $end; | |
| 114 | - } | |
| 115 | - } | |
| 116 | - } | |
| 117 | - | |
| 118 | - update_post_meta( $this->lp_course->get_id(), '_lp_price', $price ); | |
| 119 | - | |
| 120 | - // Update course is sale | |
| 121 | - if ( $has_sale_price ) { | |
| 122 | - update_post_meta( $this->lp_course->get_id(), '_lp_course_is_sale', 1 ); | |
| 123 | - } else { | |
| 124 | - delete_post_meta( $this->lp_course->get_id(), '_lp_course_is_sale' ); | |
| 125 | - } | |
| 135 | + $lp_courses_cache->clear_cache_on_group( LP_Courses_Cache::KEYS_QUERY_COURSES_APP ); | |
| 126 | 136 | } |
| 127 | 137 | |
| 128 | 138 | /** |
| 129 | - * Save Extra info of course | |
| 139 | + * Clean data invalid | |
| 130 | 140 | * |
| 131 | - * @author tungnx | |
| 132 | - * @since 4.1.4.1 | |
| 141 | + * @throws Exception | |
| 142 | + * @since 4.2.6.4 | |
| 133 | 143 | * @version 1.0.0 |
| 134 | 144 | */ |
| 135 | - protected function save_extra_info() { | |
| 136 | - $lp_course_db = LP_Course_DB::getInstance(); | |
| 137 | - $lp_course = $this->lp_course; | |
| 138 | - $lp_course_cache = LP_Course_Cache::instance(); | |
| 139 | - $course_id = $lp_course->get_id(); | |
| 140 | - | |
| 141 | - try { | |
| 142 | - $extra_info = $this->lp_course->get_info_extra_for_fast_query(); | |
| 143 | - | |
| 144 | - // Get and set first item id | |
| 145 | - // Clean cache | |
| 146 | - $key_cache_first_item_id = "$course_id/first_item_id"; | |
| 147 | - $lp_course_cache->clear( $key_cache_first_item_id ); | |
| 148 | - $first_item_id = $lp_course_db->get_first_item_id( $lp_course->get_id() ); | |
| 149 | - $extra_info->first_item_id = $first_item_id; | |
| 150 | - | |
| 151 | - // Get and set total items course | |
| 152 | - // Clean cache | |
| 153 | - $key_cache_total_items = "$course_id/total_items"; | |
| 154 | - $lp_course_cache->clear( $key_cache_total_items ); | |
| 155 | - $total_items = $lp_course_db->get_total_items( $lp_course->get_id() ); | |
| 156 | - $extra_info->total_items = $total_items; | |
| 157 | - | |
| 158 | - // Get and set sections, items of course | |
| 159 | - // Clean cache | |
| 160 | - $key_cache_sections_items = "$course_id/sections_items"; | |
| 161 | - $lp_course_cache->clear( $key_cache_sections_items ); | |
| 162 | - $sections_items = $lp_course->get_sections_and_items_course_from_db_and_sort(); | |
| 163 | - $extra_info->sections_items = $sections_items; | |
| 164 | - | |
| 165 | - // Check items removed course, will delete on 'learnpress_user_items', 'learnpress_user_item_results' table | |
| 166 | - $this->delete_user_items_data( $sections_items ); | |
| 167 | - | |
| 168 | - // Save post meta | |
| 169 | - $lp_course->set_info_extra_for_fast_query( $extra_info ); | |
| 170 | - // End set first item id | |
| 171 | - } catch ( Throwable $e ) { | |
| 172 | - error_log( $e->getMessage() ); | |
| 173 | - } | |
| 145 | + protected function clean_data_invalid() { | |
| 146 | + // Delete items of course not in table Post, can error from old data, delete item, but not remove it in sections course | |
| 147 | + LP_Section_Items_DB::getInstance()->delete_item_not_in_tb_post( $this->lp_course->get_id() ); | |
| 174 | 148 | } |
| 175 | 149 | |
| 176 | 150 | /** |
| 177 | - * Delete items removed on course on tables: | |
| 178 | - * learnpress_user_items, learnpress_user_itemmeta, learnpress_user_item_results | |
| 179 | - * | |
| 180 | - * @return void | |
| 181 | - * @since 4.1.6.9 | |
| 182 | - * @version 1.0.0 | |
| 183 | - */ | |
| 184 | - private function delete_user_items_data() { | |
| 185 | - // Get all user_item_id | |
| 186 | - $lp_user_items_db = LP_User_Items_DB::getInstance(); | |
| 187 | - $lp_user_item_results_db = LP_User_Items_Result_DB::instance(); | |
| 188 | - | |
| 189 | - try { | |
| 190 | - $course = $this->lp_course; | |
| 191 | - | |
| 192 | - // Get all items of course is attend | |
| 193 | - $filter_user_items = new LP_User_Items_Filter(); | |
| 194 | - $filter_user_items->only_fields = [ 'item_id', 'user_item_id' ]; | |
| 195 | - $filter_user_items->ref_id = $course->get_id(); | |
| 196 | - $filter_user_items->ref_type = LP_COURSE_CPT; | |
| 197 | - $users_items_result = $lp_user_items_db->get_user_items( $filter_user_items ); | |
| 198 | - | |
| 199 | - $item_ids = $course->get_item_ids(); | |
| 200 | - | |
| 201 | - $users_items_ids_need_delete = []; | |
| 202 | - foreach ( $users_items_result as $user_item ) { | |
| 203 | - //$users_items[ $user_item->item_id ] = $user_item->user_item_id; | |
| 204 | - $item_id = $user_item->item_id; | |
| 205 | - if ( ! in_array( $item_id, $item_ids ) ) { | |
| 206 | - $users_items_ids_need_delete[] = $user_item->user_item_id; | |
| 207 | - } | |
| 208 | - } | |
| 209 | - | |
| 210 | - if ( empty( $users_items_ids_need_delete ) ) { | |
| 211 | - return; | |
| 212 | - } | |
| 213 | - | |
| 214 | - // Delete on tb lp_user_items | |
| 215 | - $filter_delete = new LP_User_Items_Filter(); | |
| 216 | - $filter_delete->user_item_ids = $users_items_ids_need_delete; | |
| 217 | - $lp_user_items_db->remove_user_item_ids( $filter_delete ); | |
| 218 | - | |
| 219 | - // Delete user_itemmeta | |
| 220 | - $lp_user_items_db->remove_user_itemmeta( $filter_delete ); | |
| 221 | - | |
| 222 | - // Delete user_item_results | |
| 223 | - $lp_user_item_results_db->remove_user_item_results( $filter_delete ); | |
| 224 | - } catch ( Throwable $e ) { | |
| 225 | - error_log( $e->getMessage() ); | |
| 226 | - } | |
| 227 | - } | |
| 228 | - | |
| 229 | - /** | |
| 230 | 151 | * Check user is Instructor and enable review post of Instructor |
| 231 | 152 | * |
| 232 | 153 | * @author tungnx |
| 233 | 154 | * @since 4.1.4.1 |
| @@ -238,9 +159,9 @@ | ||
| 238 | 159 | |
| 239 | 160 | $user = learn_press_get_current_user(); |
| 240 | 161 | $required_review = LP_Settings::get_option( 'required_review', 'yes' ) === 'yes'; |
| 241 | 162 | |
| 242 | - if ( $user->is_instructor() && $required_review ) { | |
| 163 | + if ( $user->is_instructor() && $required_review && $lp_course->post_status === 'publish' ) { | |
| 243 | 164 | wp_update_post( |
| 244 | 165 | array( |
| 245 | 166 | 'ID' => $lp_course->get_id(), |
| 246 | 167 | 'post_status' => 'pending', |