← All changes
|
inc/rest-api/v1/frontend/class-lp-rest-lazy-load-controller.php
+133
-126
4.1.7.2
→
4.4.10
View file →
| @@ -1,6 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +use LearnPress\Helpers\Template; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * Class LP_REST_Lazy_Load_Controller | |
| 7 | + */ | |
| 3 | 8 | class LP_REST_Lazy_Load_Controller extends LP_Abstract_REST_Controller { |
| 4 | 9 | public function __construct() { |
| 5 | 10 | $this->namespace = 'lp/v1'; |
| 6 | 11 | $this->rest_base = 'lazy-load'; |
| @@ -9,9 +14,9 @@ | ||
| 9 | 14 | } |
| 10 | 15 | |
| 11 | 16 | public function register_routes() { |
| 12 | 17 | $this->routes = array( |
| 13 | - 'course-progress' => array( | |
| 18 | + /*'course-progress' => array( | |
| 14 | 19 | array( |
| 15 | 20 | 'methods' => WP_REST_Server::CREATABLE, |
| 16 | 21 | 'callback' => array( $this, 'user_progress' ), |
| 17 | 22 | 'permission_callback' => function () { |
| @@ -17,28 +22,30 @@ | ||
| 17 | 22 | 'permission_callback' => function () { |
| 18 | 23 | return is_user_logged_in(); |
| 19 | 24 | }, |
| 20 | 25 | ), |
| 21 | - ), | |
| 22 | - 'course-curriculum' => array( | |
| 26 | + ),*/ | |
| 27 | + /*'course-curriculum' => array( | |
| 23 | 28 | array( |
| 24 | 29 | 'methods' => WP_REST_Server::READABLE, |
| 25 | 30 | 'callback' => array( $this, 'course_curriculum' ), |
| 26 | 31 | 'permission_callback' => '__return_true', |
| 27 | 32 | ), |
| 28 | - ), | |
| 29 | - 'course-curriculum-items' => array( | |
| 33 | + ),*/ | |
| 34 | + /*'course-curriculum-items' => array( | |
| 30 | 35 | array( |
| 31 | 36 | 'methods' => WP_REST_Server::READABLE, |
| 32 | 37 | 'callback' => array( $this, 'course_curriculum_items' ), |
| 33 | 38 | 'permission_callback' => '__return_true', |
| 34 | 39 | ), |
| 35 | - ), | |
| 40 | + ),*/ | |
| 36 | 41 | 'items-progress' => array( |
| 37 | 42 | array( |
| 38 | 43 | 'methods' => WP_REST_Server::READABLE, |
| 39 | 44 | 'callback' => array( $this, 'items_progress' ), |
| 40 | - 'permission_callback' => '__return_true', | |
| 45 | + 'permission_callback' => function () { | |
| 46 | + return is_user_logged_in(); | |
| 47 | + }, | |
| 41 | 48 | ), |
| 42 | 49 | ), |
| 43 | 50 | ); |
| 44 | 51 | |
| @@ -63,13 +70,13 @@ | ||
| 63 | 70 | |
| 64 | 71 | try { |
| 65 | 72 | $course = learn_press_get_course( $course_id ); |
| 66 | 73 | if ( ! $course ) { |
| 67 | - throw new Exception( esc_html__( 'Course is invalid!', 'learnpress' ) ); | |
| 74 | + throw new Exception( esc_html__( 'The course is invalid!', 'learnpress' ) ); | |
| 68 | 75 | } |
| 69 | 76 | |
| 70 | 77 | if ( $course->is_no_required_enroll() ) { |
| 71 | - throw new Exception( esc_html__( 'Course is no require enroll!', 'learnpress' ) ); | |
| 78 | + throw new Exception( esc_html__( 'The course is not required to enroll!', 'learnpress' ) ); | |
| 72 | 79 | } |
| 73 | 80 | |
| 74 | 81 | $user = learn_press_get_user( $user_id ); |
| 75 | 82 | |
| @@ -103,34 +110,41 @@ | ||
| 103 | 110 | * @author Nhamdv <[email protected]> |
| 104 | 111 | * @editor tungnx |
| 105 | 112 | * @version 1.0.1 |
| 106 | 113 | * @sicne 4.0.0 |
| 114 | + * @deprecated 4.4.0 | |
| 107 | 115 | */ |
| 108 | 116 | public function user_progress( WP_REST_Request $request ): LP_REST_Response { |
| 117 | + _deprecated_function( __METHOD__, '4.4.0' ); | |
| 118 | + $response = new LP_REST_Response(); | |
| 119 | + return $response; | |
| 109 | 120 | $params = $request->get_params(); |
| 110 | - $course_id = $params['courseId'] ?? false; | |
| 111 | - $user_id = $params['userId'] ?? false; | |
| 112 | - $response = new LP_REST_Response(); | |
| 121 | + $course_id = intval( $params['courseId'] ?? 0 ); | |
| 122 | + $user_id = intval( $params['userId'] ?? 0 ); | |
| 113 | 123 | $response->data = ''; |
| 114 | 124 | |
| 115 | - if ( $course_id && $user_id ) { | |
| 125 | + try { | |
| 126 | + if ( ! $course_id || ! $user_id ) { | |
| 127 | + throw new Exception( __( 'Params is invalid!', 'learnpress' ) ); | |
| 128 | + } | |
| 129 | + | |
| 116 | 130 | $course = learn_press_get_course( $course_id ); |
| 117 | 131 | if ( ! $course ) { |
| 118 | - $response->message = __( 'Course is invalid', 'learnpress' ); | |
| 119 | - | |
| 120 | - return $response; | |
| 132 | + throw new Exception( __( 'The course is invalid!', 'learnpress' ) ); | |
| 121 | 133 | } |
| 122 | 134 | |
| 123 | 135 | $user = learn_press_get_user( $user_id ); |
| 124 | - if ( $user->is_guest() ) { | |
| 125 | - $response->message = __( 'You are Guest', 'learnpress' ); | |
| 136 | + if ( ! $user || $user->is_guest() ) { | |
| 137 | + throw new Exception( __( 'You are a Guest', 'learnpress' ) ); | |
| 138 | + } | |
| 126 | 139 | |
| 127 | - return $response; | |
| 140 | + if ( ! $user->can_create_course() && get_current_user_id() !== $user_id ) { | |
| 141 | + throw new Exception( __( 'You are a not permission!', 'learnpress' ) ); | |
| 128 | 142 | } |
| 129 | 143 | |
| 130 | 144 | $course_data = $user->get_course_data( $course->get_id() ); |
| 131 | 145 | if ( ! $course_data ) { |
| 132 | - return $response; | |
| 146 | + throw new Exception( __( 'You are a not enroll course', 'learnpress' ) ); | |
| 133 | 147 | } |
| 134 | 148 | |
| 135 | 149 | $course_results = $course_data->calculate_course_results(); |
| 136 | 150 | |
| @@ -138,8 +152,10 @@ | ||
| 138 | 152 | $response->data = learn_press_get_template_content( |
| 139 | 153 | 'single-course/sidebar/user-progress', |
| 140 | 154 | compact( 'user', 'course', 'course_data', 'course_results' ) |
| 141 | 155 | ); |
| 156 | + } catch ( Throwable $e ) { | |
| 157 | + $response->message = $e->getMessage(); | |
| 142 | 158 | } |
| 143 | 159 | |
| 144 | 160 | return $response; |
| 145 | 161 | } |
| @@ -151,32 +167,33 @@ | ||
| 151 | 167 | * |
| 152 | 168 | * @return LP_REST_Response |
| 153 | 169 | * @author nhamdv |
| 154 | 170 | * @since 4.1.5 |
| 155 | - * @version 1.0.1 | |
| 171 | + * @version 1.0.2 | |
| 172 | + * @depreacted 4.2.8.7.1 | |
| 173 | + * use new curriculum from LP v4.2.8, so now deprecated this function | |
| 156 | 174 | */ |
| 157 | 175 | public function course_curriculum( WP_REST_Request $request ): LP_REST_Response { |
| 176 | + _deprecated_function( __METHOD__, '4.2.8.7.1' ); | |
| 158 | 177 | $response = new LP_REST_Response(); |
| 159 | - $params = $request->get_params(); | |
| 160 | - $content = ''; | |
| 178 | + return $response; | |
| 179 | + $params = $request->get_params(); | |
| 180 | + $total_rows = 0; | |
| 181 | + $course_id = absint( $params['courseId'] ?? 0 ); | |
| 182 | + $per_page = LP_Settings::get_option( 'section_per_page', - 1 ); | |
| 183 | + $page = absint( $params['page'] ?? 1 ); | |
| 184 | + $section_id = wp_unslash( $params['sectionID'] ?? false ); | |
| 185 | + $item_viewing = absint( $params['idItemViewing'] ?? 0 ); | |
| 161 | 186 | |
| 162 | - $course_id = absint( $params['courseId'] ?? 0 ); | |
| 163 | - $per_page = LP_Settings::instance()->get( 'section_per_page', 2 ); | |
| 164 | - $page = absint( $params['page'] ?? 1 ); | |
| 165 | - $order = wp_unslash( $params['order'] ?? 'ASC' ); | |
| 166 | - $search = wp_unslash( $params['search'] ?? '' ); | |
| 167 | - $include = wp_unslash( $params['include'] ?? array() ); | |
| 168 | - $exclude = wp_unslash( $params['exclude'] ?? array() ); | |
| 169 | - $section_id = wp_unslash( $params['sectionID'] ?? false ); | |
| 170 | - | |
| 171 | 187 | try { |
| 188 | + ob_start(); | |
| 172 | 189 | if ( empty( $course_id ) ) { |
| 173 | - throw new Exception( esc_html__( 'Course is invalid!', 'learnpress' ) ); | |
| 190 | + throw new Exception( esc_html__( 'The course is invalid!', 'learnpress' ) ); | |
| 174 | 191 | } |
| 175 | 192 | |
| 176 | 193 | $course = learn_press_get_course( $course_id ); |
| 177 | 194 | if ( ! $course ) { |
| 178 | - throw new Exception( esc_html__( 'Course is invalid!', 'learnpress' ) ); | |
| 195 | + throw new Exception( esc_html__( 'The course is invalid!', 'learnpress' ) ); | |
| 179 | 196 | } |
| 180 | 197 | |
| 181 | 198 | $filters = new LP_Section_Filter(); |
| 182 | 199 | $filters->section_course_id = $course_id; |
| @@ -181,50 +198,50 @@ | ||
| 181 | 198 | $filters = new LP_Section_Filter(); |
| 182 | 199 | $filters->section_course_id = $course_id; |
| 183 | 200 | $filters->limit = $per_page; |
| 184 | 201 | $filters->page = $page; |
| 185 | - $filters->order = $order; | |
| 186 | - $filters->search_section = $search; | |
| 187 | - $filters->section_ids = $include; | |
| 188 | - $filters->section_not_ids = $exclude; | |
| 202 | + $sections_result = LP_Section_DB::getInstance()->get_sections( $filters, $total_rows ); | |
| 203 | + $sections_tmp = []; | |
| 204 | + foreach ( $sections_result as $section ) { | |
| 205 | + $sections_tmp[] = (array) $section; | |
| 206 | + } | |
| 189 | 207 | |
| 190 | - $sections = LP_Section_DB::getInstance()->get_sections_by_course_id( $filters ); | |
| 191 | - | |
| 192 | - if ( is_wp_error( $sections ) ) { | |
| 193 | - throw new Exception( $sections->get_error_message() ); | |
| 208 | + $total_page = 1; | |
| 209 | + if ( $filters->limit > 0 ) { | |
| 210 | + $total_page = LP_Database::get_total_pages( $filters->limit, $total_rows ); | |
| 194 | 211 | } |
| 212 | + $sections = array( | |
| 213 | + 'results' => $sections_tmp, | |
| 214 | + 'total' => $total_rows, | |
| 215 | + 'pages' => $total_page, | |
| 216 | + ); | |
| 195 | 217 | |
| 196 | 218 | if ( ! empty( $params['loadMore'] ) ) { |
| 197 | - foreach ( $sections['results'] as $section ) { | |
| 198 | - $content .= learn_press_get_template_content( | |
| 199 | - 'loop/single-course/loop-section', | |
| 219 | + foreach ( $sections_tmp as $section ) { | |
| 220 | + Template::instance()->get_frontend_template( | |
| 221 | + 'loop/single-course/loop-section.php', | |
| 200 | 222 | compact( 'sections', 'section', 'course_id', 'filters' ) |
| 201 | 223 | ); |
| 202 | 224 | } |
| 203 | 225 | } else { |
| 204 | - $content = learn_press_get_template_content( | |
| 205 | - 'single-course/tabs/curriculum-v2', | |
| 226 | + Template::instance()->get_frontend_template( | |
| 227 | + 'single-course/tabs/curriculum-v2.php', | |
| 206 | 228 | compact( 'sections', 'course_id', 'filters' ) |
| 207 | 229 | ); |
| 208 | 230 | } |
| 209 | 231 | |
| 210 | 232 | if ( $section_id ) { |
| 211 | - $response->data->section_ids = wp_list_pluck( $sections['results'], 'section_id' ); | |
| 233 | + $response->data->section_ids = LP_Database::get_values_by_key( $sections_result, 'section_id' ); | |
| 212 | 234 | } |
| 213 | 235 | |
| 214 | 236 | $response->status = 'success'; |
| 215 | - $response->data->pages = $sections['pages']; | |
| 237 | + $response->data->pages = $total_page; | |
| 216 | 238 | $response->data->page = $filters->page; |
| 217 | - $response->data->content = $content; | |
| 218 | - | |
| 219 | - // For old value use on theme Eduma <= v4.6.0 - deprecated 4.1.6.1 | |
| 220 | - if ( defined( 'THIM_THEME_VERSION' ) && version_compare( THIM_THEME_VERSION, '4.6.3', '<=' ) ) { | |
| 221 | - $response->pages = $sections['pages']; | |
| 222 | - $response->data = $content; | |
| 223 | - $response->section_ids = wp_list_pluck( $sections['results'], 'section_id' ); | |
| 224 | - } | |
| 225 | - } catch ( \Throwable $th ) { | |
| 226 | - $response->message = $th->getMessage(); | |
| 239 | + $response->data->content = ob_get_clean(); | |
| 240 | + } catch ( Throwable $e ) { | |
| 241 | + ob_end_clean(); | |
| 242 | + error_log( __METHOD__ . ': ' . $e->getMessage() ); | |
| 243 | + $response->message = $e->getMessage(); | |
| 227 | 244 | } |
| 228 | 245 | |
| 229 | 246 | return $response; |
| 230 | 247 | } |
| @@ -233,94 +250,84 @@ | ||
| 233 | 250 | * Load items' section on the single course |
| 234 | 251 | * |
| 235 | 252 | * @param WP_REST_Request $request |
| 236 | 253 | * |
| 254 | + * @return WP_REST_Response|WP_Error | |
| 255 | + * @since 4.1.5 | |
| 256 | + * @version 1.0.2 | |
| 237 | 257 | * @author nhamdv |
| 238 | - * @since 4.1.5 | |
| 239 | - * @version 1.0.1 | |
| 240 | - * @return WP_REST_Response|WP_Error | |
| 258 | + * @deprecated 4.4.0 | |
| 241 | 259 | */ |
| 242 | 260 | public function course_curriculum_items( WP_REST_Request $request ) { |
| 261 | + _deprecated_function( __METHOD__, '4.4.0' ); | |
| 262 | + $response = new LP_REST_Response(); | |
| 263 | + return $response; | |
| 243 | 264 | $params = $request->get_params(); |
| 244 | 265 | |
| 245 | - $section_id = absint( $params['sectionId'] ?? 0 ); | |
| 246 | - $per_page = LP_Settings::instance()->get( 'course_item_per_page', 5 ); | |
| 247 | - $page = absint( $params['page'] ?? 1 ); | |
| 248 | - $order = LP_Helper::sanitize_params_submitted( $params['order'] ?? 'ASC' ); | |
| 249 | - $search = LP_Helper::sanitize_params_submitted( $params['search'] ?? '' ); | |
| 250 | - $include = LP_Helper::sanitize_params_submitted( $params['include'] ?? array() ); | |
| 251 | - $exclude = LP_Helper::sanitize_params_submitted( $params['exclude'] ?? array() ); | |
| 266 | + $section_id = absint( $params['sectionId'] ?? 0 ); | |
| 267 | + $course_id = absint( $params['courseId'] ?? 0 ); | |
| 268 | + $per_page = LP_Settings::get_option( 'course_item_per_page', - 1 ); | |
| 269 | + $page = absint( $params['page'] ?? 1 ); | |
| 270 | + $response->data->content = ''; | |
| 252 | 271 | |
| 253 | - $response = new LP_REST_Response(); | |
| 254 | - | |
| 255 | 272 | try { |
| 273 | + ob_start(); | |
| 256 | 274 | if ( empty( $section_id ) ) { |
| 257 | - throw new Exception( esc_html__( 'Section is invalid!', 'learnpress' ) ); | |
| 275 | + throw new Exception( esc_html__( 'The section is invalid!', 'learnpress' ) ); | |
| 258 | 276 | } |
| 259 | 277 | |
| 260 | - $filters = new LP_Section_Items_Filter(); | |
| 261 | - $filters->section_id = $section_id; | |
| 262 | - $filters->limit = $per_page; | |
| 263 | - $filters->page = $page; | |
| 264 | - $filters->order = $order; | |
| 265 | - $filters->search_title = $search; | |
| 266 | - $filters->item_ids = $include; | |
| 267 | - $filters->item_not_ids = $exclude; | |
| 268 | - | |
| 269 | - $section_items = LP_Section_DB::getInstance()->get_section_items_by_section_id( $filters ); | |
| 270 | - | |
| 271 | - if ( is_wp_error( $section_items ) ) { | |
| 272 | - throw new Exception( $section_items->get_error_message() ); | |
| 278 | + $filters = new LP_Section_Items_Filter(); | |
| 279 | + $filters->only_fields = [ 'ID', 'post_title' ]; | |
| 280 | + $filters->section_id = $section_id; | |
| 281 | + $filters->limit = $per_page; | |
| 282 | + $filters->page = $page; | |
| 283 | + $total_rows = 0; | |
| 284 | + $section_items_result = LP_Section_DB::getInstance()->get_items( $filters, $total_rows ); | |
| 285 | + $total_pages = 1; | |
| 286 | + if ( $filters->limit > 0 ) { | |
| 287 | + $total_pages = LP_Database::get_total_pages( $filters->limit, $total_rows ); | |
| 273 | 288 | } |
| 274 | 289 | |
| 275 | - $content = ''; | |
| 290 | + foreach ( $section_items_result as $key => $section_item ) { | |
| 291 | + $section_item = (array) $section_item; | |
| 292 | + if ( ! $course_id ) { | |
| 293 | + $course_id = LP_Section_DB::getInstance()->get_course_id_by_section( $section_id ); | |
| 294 | + } | |
| 295 | + if ( ! $course_id ) { | |
| 296 | + throw new Exception( esc_html__( 'Item not assign to course', 'learnpress' ) ); | |
| 297 | + } | |
| 276 | 298 | |
| 277 | - foreach ( $section_items['results'] as $key => $section_item ) { | |
| 278 | - $course_id = LP_Section_DB::getInstance()->get_course_id_by_section( $section_id ); | |
| 299 | + $course = learn_press_get_course( $course_id ); | |
| 300 | + if ( ! $course ) { | |
| 301 | + throw new Exception( 'Course is not exists!' ); | |
| 302 | + } | |
| 279 | 303 | |
| 280 | - if ( $course_id ) { | |
| 281 | - $course = learn_press_get_course( $course_id ); | |
| 304 | + $course_item = $course->get_item( absint( $section_item['ID'] ?? 0 ) ); | |
| 305 | + $can_view_item = new LP_Model_User_Can_View_Course_Item(); | |
| 306 | + $user = learn_press_get_user( get_current_user_id() ); | |
| 307 | + if ( $user ) { | |
| 308 | + $can_view_content_course = $user->can_view_content_course( absint( $course_id ) ); | |
| 309 | + $can_view_item = $user->can_view_item( $section_item['ID'], $can_view_content_course ); | |
| 310 | + } | |
| 282 | 311 | |
| 283 | - if ( ! $course ) { | |
| 284 | - throw new Exception( 'Course is not exists!' ); | |
| 285 | - } | |
| 312 | + // Ordinal numbers | |
| 313 | + $key = absint( ( ( $page - 1 ) * $per_page ) + $key + 1 ); | |
| 286 | 314 | |
| 287 | - $course_item = $course->get_item( absint( $section_item['ID'] ?? 0 ) ); | |
| 288 | - | |
| 289 | - $can_view_item = new LP_Model_User_Can_View_Course_Item(); | |
| 290 | - | |
| 291 | - $user = learn_press_get_user( get_current_user_id() ); | |
| 292 | - | |
| 293 | - if ( $user ) { | |
| 294 | - $can_view_content_course = $user->can_view_content_course( absint( $course_id ) ); | |
| 295 | - $can_view_item = $user->can_view_item( $section_item['ID'], $can_view_content_course ); | |
| 296 | - } | |
| 297 | - | |
| 298 | - // Ordinal numbers | |
| 299 | - $key = absint( ( ( $page - 1 ) * $per_page ) + $key + 1 ); | |
| 300 | - | |
| 301 | - $content .= learn_press_get_template_content( | |
| 302 | - 'loop/single-course/loop-section-item', | |
| 303 | - compact( 'section_item', 'course_item', 'can_view_item', 'course_id', 'user', 'key' ) | |
| 304 | - ); | |
| 305 | - } | |
| 315 | + Template::instance()->get_frontend_template( | |
| 316 | + 'loop/single-course/loop-section-item.php', | |
| 317 | + compact( 'section_item', 'course_item', 'can_view_item', 'course_id', 'user', 'key' ) | |
| 318 | + ); | |
| 306 | 319 | } |
| 307 | 320 | |
| 308 | - $response->data->pages = $section_items['pages']; | |
| 321 | + $response->data->pages = $total_pages; | |
| 309 | 322 | $response->data->page = $filters->page; |
| 310 | - $response->data->content = $content; | |
| 311 | - $response->data->item_ids = wp_list_pluck( $section_items['results'], 'ID' ); | |
| 312 | - | |
| 313 | - $response->status = 'success'; | |
| 314 | - // For old value use on theme Eduma <= v4.6.0 - deprecated 4.1.6.1 | |
| 315 | - if ( defined( 'THIM_THEME_VERSION' ) && version_compare( THIM_THEME_VERSION, '4.6.3', '<=' ) ) { | |
| 316 | - $response->pages = $section_items['pages']; | |
| 317 | - $response->page = $filters->page; | |
| 318 | - $response->data = $content; | |
| 319 | - $response->item_ids = wp_list_pluck( $section_items['results'], 'ID' ); | |
| 320 | - } | |
| 321 | - } catch ( \Throwable $th ) { | |
| 322 | - $response->message = $th->getMessage(); | |
| 323 | + $response->data->content = ob_get_clean(); | |
| 324 | + $response->data->item_ids = LP_Database::get_values_by_key( $section_items_result ); | |
| 325 | + $response->status = 'success'; | |
| 326 | + } catch ( Throwable $e ) { | |
| 327 | + ob_end_clean(); | |
| 328 | + error_log( __METHOD__ . ': ' . $e->getMessage() ); | |
| 329 | + $response->message = $e->getMessage(); | |
| 323 | 330 | } |
| 324 | 331 | |
| 325 | 332 | return rest_ensure_response( $response ); |
| 326 | 333 | } |