← All changes
|
inc/background-process/class-lp-background-single-course.php
+189
-189
4.3.8
→
4.4.8
View file →
| @@ -1,189 +1,189 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Class LP_Background_Single_Course | |
| 4 | - * | |
| 5 | - * Single to run not schedule, run one time and done when be call | |
| 6 | - * | |
| 7 | - * @since 4.1.1 | |
| 8 | - * @author tungnx | |
| 9 | - * @version 1.0.2 | |
| 10 | - */ | |
| 11 | - | |
| 12 | -use LearnPress\Models\CourseModel; | |
| 13 | -use LearnPress\Models\CoursePostModel; | |
| 14 | -use LearnPress\Models\PostModel; | |
| 15 | - | |
| 16 | -defined( 'ABSPATH' ) || exit; | |
| 17 | - | |
| 18 | -if ( ! class_exists( 'LP_Background_Single_Course' ) ) { | |
| 19 | - class LP_Background_Single_Course extends LP_Async_Request { | |
| 20 | - protected $action = 'background_single_course'; | |
| 21 | - protected static $instance; | |
| 22 | - /** | |
| 23 | - * @var $lp_course CourseModel | |
| 24 | - */ | |
| 25 | - protected $lp_course; | |
| 26 | - /** | |
| 27 | - * @var array | |
| 28 | - */ | |
| 29 | - protected $data = array(); | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * Get params via $_POST and handle | |
| 33 | - * | |
| 34 | - * @see LP_Course_Post_Type::save_post() | |
| 35 | - */ | |
| 36 | - protected function handle() { | |
| 37 | - ini_set( 'max_execution_time', 0 ); | |
| 38 | - try { | |
| 39 | - $handle_name = LP_Request::get_param( 'handle_name', '', 'key', 'post' ); | |
| 40 | - $course_id = intval( $_POST['course_id'] ?? 0 ); | |
| 41 | - if ( empty( $handle_name ) || ! $course_id ) { | |
| 42 | - return; | |
| 43 | - } | |
| 44 | - | |
| 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 ) { | |
| 48 | - return; | |
| 49 | - } | |
| 50 | - | |
| 51 | - switch ( $handle_name ) { | |
| 52 | - case 'save_post': | |
| 53 | - $this->save_post(); | |
| 54 | - break; | |
| 55 | - default: | |
| 56 | - break; | |
| 57 | - } | |
| 58 | - } catch ( Throwable $e ) { | |
| 59 | - error_log( $e->getMessage() ); | |
| 60 | - } | |
| 61 | - ini_set( 'max_execution_time', LearnPress::$time_limit_default_of_sever ); | |
| 62 | - die; | |
| 63 | - } | |
| 64 | - | |
| 65 | - /** | |
| 66 | - * Save course via post data | |
| 67 | - * | |
| 68 | - * @throws Exception | |
| 69 | - * @since 4.1.3 | |
| 70 | - * @version 1.0.4 | |
| 71 | - */ | |
| 72 | - protected function save_post() { | |
| 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(); | |
| 112 | - $this->review_post_author(); | |
| 113 | - | |
| 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 ); | |
| 118 | - | |
| 119 | - /** | |
| 120 | - * Clean cache courses | |
| 121 | - */ | |
| 122 | - $keys_cache = LP_Courses_Cache::instance()->get_cache( LP_Courses_Cache::$keys ); | |
| 123 | - if ( $keys_cache ) { | |
| 124 | - foreach ( $keys_cache as $key ) { | |
| 125 | - LP_Courses_Cache::instance()->clear( $key ); | |
| 126 | - } | |
| 127 | - LP_Courses_Cache::instance()->clear( LP_Courses_Cache::$keys ); | |
| 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 ); | |
| 133 | - // End | |
| 134 | - | |
| 135 | - $lp_courses_cache->clear_cache_on_group( LP_Courses_Cache::KEYS_QUERY_COURSES_APP ); | |
| 136 | - } | |
| 137 | - | |
| 138 | - /** | |
| 139 | - * Clean data invalid | |
| 140 | - * | |
| 141 | - * @throws Exception | |
| 142 | - * @since 4.2.6.4 | |
| 143 | - * @version 1.0.0 | |
| 144 | - */ | |
| 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() ); | |
| 148 | - } | |
| 149 | - | |
| 150 | - /** | |
| 151 | - * Check user is Instructor and enable review post of Instructor | |
| 152 | - * | |
| 153 | - * @author tungnx | |
| 154 | - * @since 4.1.4.1 | |
| 155 | - * @version 1.0.0 | |
| 156 | - */ | |
| 157 | - protected function review_post_author() { | |
| 158 | - $lp_course = $this->lp_course; | |
| 159 | - | |
| 160 | - $user = learn_press_get_current_user(); | |
| 161 | - $required_review = LP_Settings::get_option( 'required_review', 'yes' ) === 'yes'; | |
| 162 | - | |
| 163 | - if ( $user->is_instructor() && $required_review && $lp_course->post_status === 'publish' ) { | |
| 164 | - wp_update_post( | |
| 165 | - array( | |
| 166 | - 'ID' => $lp_course->get_id(), | |
| 167 | - 'post_status' => 'pending', | |
| 168 | - ), | |
| 169 | - array( '%d', '%s' ) | |
| 170 | - ); | |
| 171 | - } | |
| 172 | - // End | |
| 173 | - } | |
| 174 | - | |
| 175 | - /** | |
| 176 | - * @return LP_Background_Single_Course | |
| 177 | - */ | |
| 178 | - public static function instance(): self { | |
| 179 | - if ( is_null( self::$instance ) ) { | |
| 180 | - self::$instance = new self(); | |
| 181 | - } | |
| 182 | - | |
| 183 | - return self::$instance; | |
| 184 | - } | |
| 185 | - } | |
| 186 | - | |
| 187 | - // Must run instance to register ajax. | |
| 188 | - LP_Background_Single_Course::instance(); | |
| 189 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Class LP_Background_Single_Course | |
| 4 | + * | |
| 5 | + * Single to run not schedule, run one time and done when be call | |
| 6 | + * | |
| 7 | + * @since 4.1.1 | |
| 8 | + * @author tungnx | |
| 9 | + * @version 1.0.2 | |
| 10 | + */ | |
| 11 | + | |
| 12 | +use LearnPress\Models\CourseModel; | |
| 13 | +use LearnPress\Models\CoursePostModel; | |
| 14 | +use LearnPress\Models\PostModel; | |
| 15 | + | |
| 16 | +defined( 'ABSPATH' ) || exit; | |
| 17 | + | |
| 18 | +if ( ! class_exists( 'LP_Background_Single_Course' ) ) { | |
| 19 | + class LP_Background_Single_Course extends LP_Async_Request { | |
| 20 | + protected $action = 'background_single_course'; | |
| 21 | + protected static $instance; | |
| 22 | + /** | |
| 23 | + * @var $lp_course CourseModel | |
| 24 | + */ | |
| 25 | + protected $lp_course; | |
| 26 | + /** | |
| 27 | + * @var array | |
| 28 | + */ | |
| 29 | + protected $data = array(); | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Get params via $_POST and handle | |
| 33 | + * | |
| 34 | + * @see LP_Course_Post_Type::save_post() | |
| 35 | + */ | |
| 36 | + protected function handle() { | |
| 37 | + ini_set( 'max_execution_time', 0 ); | |
| 38 | + try { | |
| 39 | + $handle_name = LP_Request::get_param( 'handle_name', '', 'key', 'post' ); | |
| 40 | + $course_id = intval( $_POST['course_id'] ?? 0 ); | |
| 41 | + if ( empty( $handle_name ) || ! $course_id ) { | |
| 42 | + return; | |
| 43 | + } | |
| 44 | + | |
| 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 ) { | |
| 48 | + return; | |
| 49 | + } | |
| 50 | + | |
| 51 | + switch ( $handle_name ) { | |
| 52 | + case 'save_post': | |
| 53 | + $this->save_post(); | |
| 54 | + break; | |
| 55 | + default: | |
| 56 | + break; | |
| 57 | + } | |
| 58 | + } catch ( Throwable $e ) { | |
| 59 | + error_log( $e->getMessage() ); | |
| 60 | + } | |
| 61 | + ini_set( 'max_execution_time', LearnPress::$time_limit_default_of_sever ); | |
| 62 | + die; | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Save course via post data | |
| 67 | + * | |
| 68 | + * @throws Exception | |
| 69 | + * @since 4.1.3 | |
| 70 | + * @version 1.0.4 | |
| 71 | + */ | |
| 72 | + protected function save_post() { | |
| 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(); | |
| 112 | + $this->review_post_author(); | |
| 113 | + | |
| 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 ); | |
| 118 | + | |
| 119 | + /** | |
| 120 | + * Clean cache courses | |
| 121 | + */ | |
| 122 | + $keys_cache = LP_Courses_Cache::instance()->get_cache( LP_Courses_Cache::$keys ); | |
| 123 | + if ( $keys_cache ) { | |
| 124 | + foreach ( $keys_cache as $key ) { | |
| 125 | + LP_Courses_Cache::instance()->clear( $key ); | |
| 126 | + } | |
| 127 | + LP_Courses_Cache::instance()->clear( LP_Courses_Cache::$keys ); | |
| 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 ); | |
| 133 | + // End | |
| 134 | + | |
| 135 | + $lp_courses_cache->clear_cache_on_group( LP_Courses_Cache::KEYS_QUERY_COURSES_APP ); | |
| 136 | + } | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * Clean data invalid | |
| 140 | + * | |
| 141 | + * @throws Exception | |
| 142 | + * @since 4.2.6.4 | |
| 143 | + * @version 1.0.0 | |
| 144 | + */ | |
| 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() ); | |
| 148 | + } | |
| 149 | + | |
| 150 | + /** | |
| 151 | + * Check user is Instructor and enable review post of Instructor | |
| 152 | + * | |
| 153 | + * @author tungnx | |
| 154 | + * @since 4.1.4.1 | |
| 155 | + * @version 1.0.0 | |
| 156 | + */ | |
| 157 | + protected function review_post_author() { | |
| 158 | + $lp_course = $this->lp_course; | |
| 159 | + | |
| 160 | + $user = learn_press_get_current_user(); | |
| 161 | + $required_review = LP_Settings::get_option( 'required_review', 'yes' ) === 'yes'; | |
| 162 | + | |
| 163 | + if ( $user->is_instructor() && $required_review && $lp_course->post_status === 'publish' ) { | |
| 164 | + wp_update_post( | |
| 165 | + array( | |
| 166 | + 'ID' => $lp_course->get_id(), | |
| 167 | + 'post_status' => 'pending', | |
| 168 | + ), | |
| 169 | + array( '%d', '%s' ) | |
| 170 | + ); | |
| 171 | + } | |
| 172 | + // End | |
| 173 | + } | |
| 174 | + | |
| 175 | + /** | |
| 176 | + * @return LP_Background_Single_Course | |
| 177 | + */ | |
| 178 | + public static function instance(): self { | |
| 179 | + if ( is_null( self::$instance ) ) { | |
| 180 | + self::$instance = new self(); | |
| 181 | + } | |
| 182 | + | |
| 183 | + return self::$instance; | |
| 184 | + } | |
| 185 | + } | |
| 186 | + | |
| 187 | + // Must run instance to register ajax. | |
| 188 | + LP_Background_Single_Course::instance(); | |
| 189 | +} | |