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