| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_REST_Courses_Controller |
| 5 |
*/ |
| 6 |
class LP_REST_Courses_Controller extends LP_Abstract_REST_Controller { |
| 7 |
/** |
| 8 |
* LP_REST_Courses_Controller constructor. |
| 9 |
*/ |
| 10 |
public function __construct() { |
| 11 |
$this->namespace = 'lp/v1'; |
| 12 |
$this->rest_base = 'courses'; |
| 13 |
parent::__construct(); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Register routes API |
| 18 |
*/ |
| 19 |
public function register_routes() { |
| 20 |
$this->routes = array( |
| 21 |
'purchase-course' => array( |
| 22 |
array( |
| 23 |
'methods' => WP_REST_Server::CREATABLE, |
| 24 |
'callback' => array( $this, 'purchase_course' ), |
| 25 |
'permission_callback' => '__return_true', |
| 26 |
), |
| 27 |
), |
| 28 |
'enroll-course' => array( |
| 29 |
array( |
| 30 |
'methods' => WP_REST_Server::CREATABLE, |
| 31 |
'callback' => array( $this, 'enroll_courses' ), |
| 32 |
'permission_callback' => '__return_true', |
| 33 |
), |
| 34 |
), |
| 35 |
'retake-course' => array( |
| 36 |
array( |
| 37 |
'methods' => WP_REST_Server::CREATABLE, |
| 38 |
'callback' => array( $this, 'retake_course' ), |
| 39 |
'permission_callback' => function () { |
| 40 |
return is_user_logged_in(); |
| 41 |
}, |
| 42 |
'schema' => array( |
| 43 |
'type' => 'int', |
| 44 |
), |
| 45 |
), |
| 46 |
), |
| 47 |
'archive-course' => array( |
| 48 |
array( |
| 49 |
'methods' => WP_REST_Server::ALLMETHODS, |
| 50 |
'callback' => array( $this, 'list_courses' ), |
| 51 |
'permission_callback' => '__return_true', |
| 52 |
'args' => [], |
| 53 |
), |
| 54 |
), |
| 55 |
'(?P<key>[\w]+)' => array( |
| 56 |
'args' => array( |
| 57 |
'id' => array( |
| 58 |
'description' => __( 'Unique identifier for the resource.', 'learnpress' ), |
| 59 |
'type' => 'string', |
| 60 |
), |
| 61 |
), |
| 62 |
array( |
| 63 |
'methods' => WP_REST_Server::READABLE, |
| 64 |
'callback' => array( $this, 'get_item' ), |
| 65 |
'permission_callback' => array( $this, 'check_admin_permission' ), |
| 66 |
), |
| 67 |
array( |
| 68 |
'methods' => WP_REST_Server::EDITABLE, |
| 69 |
'callback' => array( $this, 'update_item' ), |
| 70 |
'permission_callback' => array( $this, 'check_admin_permission' ), |
| 71 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 72 |
), |
| 73 |
array( |
| 74 |
'methods' => WP_REST_Server::DELETABLE, |
| 75 |
'callback' => array( $this, 'delete_item' ), |
| 76 |
'permission_callback' => array( $this, 'check_admin_permission' ), |
| 77 |
), |
| 78 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 79 |
), |
| 80 |
'continue-course' => array( |
| 81 |
array( |
| 82 |
'methods' => WP_REST_Server::CREATABLE, |
| 83 |
'callback' => array( $this, 'continue_course' ), |
| 84 |
'permission_callback' => function () { |
| 85 |
return is_user_logged_in(); |
| 86 |
}, |
| 87 |
), |
| 88 |
), |
| 89 |
); |
| 90 |
|
| 91 |
parent::register_routes(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Check user is Admin |
| 96 |
* |
| 97 |
* @return bool |
| 98 |
*/ |
| 99 |
public function check_admin_permission(): bool { |
| 100 |
return LP_Abstract_API::check_admin_permission(); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get list courses |
| 105 |
* |
| 106 |
* @param WP_REST_Request $request |
| 107 |
* |
| 108 |
* @return LP_REST_Response |
| 109 |
*/ |
| 110 |
public function list_courses( WP_REST_Request $request ): LP_REST_Response { |
| 111 |
$response = new LP_REST_Response(); |
| 112 |
$response->data = new stdClass(); |
| 113 |
|
| 114 |
try { |
| 115 |
$filter = new LP_Course_Filter(); |
| 116 |
$filter->page = absint( $request['paged'] ?? 1 ); |
| 117 |
$filter->post_title = LP_Helper::sanitize_params_submitted( $request['c_search'] ?? '' ); |
| 118 |
$fields_str = LP_Helper::sanitize_params_submitted( urldecode( $request['c_fields'] ?? '' ) ); |
| 119 |
$fields_exclude_str = LP_Helper::sanitize_params_submitted( urldecode( $request['c_exclude_fields'] ?? '' ) ); |
| 120 |
if ( ! empty( $fields_str ) ) { |
| 121 |
$fields = explode( ',', $fields_str ); |
| 122 |
$filter->fields = $fields; |
| 123 |
} |
| 124 |
if ( ! empty( $fields_exclude_str ) ) { |
| 125 |
$fields_exclude = explode( ',', $fields_exclude_str ); |
| 126 |
$filter->exclude_fields = $fields_exclude; |
| 127 |
} |
| 128 |
$filter->post_author = LP_Helper::sanitize_params_submitted( $request['c_author'] ?? 0 ); |
| 129 |
$author_ids_str = LP_Helper::sanitize_params_submitted( $request['c_authors'] ?? 0 ); |
| 130 |
if ( ! empty( $author_ids_str ) ) { |
| 131 |
$author_ids = explode( ',', $author_ids_str ); |
| 132 |
$filter->post_authors = $author_ids; |
| 133 |
} |
| 134 |
$term_ids_str = LP_Helper::sanitize_params_submitted( urldecode( $request['term_id'] ) ?? '' ); |
| 135 |
if ( ! empty( $term_ids_str ) ) { |
| 136 |
$term_ids = explode( ',', $term_ids_str ); |
| 137 |
$filter->term_ids = $term_ids; |
| 138 |
} |
| 139 |
|
| 140 |
$on_sale = absint( $request['on_sale'] ?? '0' ); |
| 141 |
1 === $on_sale ? $filter->sort_by[] = 'on_sale' : ''; |
| 142 |
$on_feature = absint( $request['on_feature'] ?? '0' ); |
| 143 |
1 === $on_feature ? $filter->sort_by[] = 'on_feature' : ''; |
| 144 |
|
| 145 |
$filter->order_by = LP_Helper::sanitize_params_submitted( ! empty( $request['order_by'] ) ? $request['order_by'] : 'post_date' ); |
| 146 |
$filter->order = LP_Helper::sanitize_params_submitted( ! empty( $request['order'] ) ? $request['order'] : 'DESC' ); |
| 147 |
$filter->limit = $request['limit'] ?? LP_Settings::get_option( 'archive_course_limit', 10 ); |
| 148 |
$return_type = $request['return_type'] ?? 'html'; |
| 149 |
if ( 'json' !== $return_type ) { |
| 150 |
$filter->only_fields = array( 'DISTINCT(ID) AS ID' ); |
| 151 |
} |
| 152 |
|
| 153 |
$total_rows = 0; |
| 154 |
$filter = apply_filters( 'lp/api/courses/filter', $filter, $request ); |
| 155 |
$courses = LP_Course::get_courses( $filter, $total_rows ); |
| 156 |
$total_pages = LP_Database::get_total_pages( $filter->limit, $total_rows ); |
| 157 |
|
| 158 |
if ( 'json' === $return_type ) { |
| 159 |
$response->data->courses = $courses; |
| 160 |
$response->data->total_pages = $total_pages; |
| 161 |
} else { |
| 162 |
// For return data has html |
| 163 |
if ( $courses ) { |
| 164 |
global $wp, $post; |
| 165 |
|
| 166 |
// Pagination |
| 167 |
$archive_link = get_post_type_archive_link( LP_COURSE_CPT ); |
| 168 |
|
| 169 |
if ( isset( $term_link ) && ! is_wp_error( $term_link ) ) { |
| 170 |
$archive_link = $term_link; |
| 171 |
} |
| 172 |
|
| 173 |
$template_pagination_path = $request['template_pagination_path'] ?? ''; |
| 174 |
if ( ! isset( $request['no_pagination'] ) ) { |
| 175 |
if ( ! empty( $template_pagination_path ) ) { |
| 176 |
$response->data->pagination = include $template_pagination_path; |
| 177 |
} else { |
| 178 |
$response->data->pagination = learn_press_get_template_content( |
| 179 |
'loop/course/pagination.php', |
| 180 |
array( |
| 181 |
'total' => $total_pages, |
| 182 |
'paged' => $filter->page, |
| 183 |
) |
| 184 |
); |
| 185 |
} |
| 186 |
} |
| 187 |
// End Pagination |
| 188 |
|
| 189 |
// Content items |
| 190 |
ob_start(); |
| 191 |
$template_path_item = urldecode( $request['template_path_item'] ?? '' ); |
| 192 |
$template_path = urldecode( $request['template_path'] ?? '' ); // For wrapper all items, no foreach |
| 193 |
$args_custom = json_decode( wp_unslash( $request['args_custom'] ?? '' ), true ); |
| 194 |
|
| 195 |
// For custom template return all list courses no foreach |
| 196 |
if ( ! empty( $template_path ) ) { |
| 197 |
if ( is_array( $args_custom ) && ! empty( $args_custom ) ) { |
| 198 |
extract( $args_custom ); |
| 199 |
} |
| 200 |
|
| 201 |
if ( file_exists( $template_path ) ) { |
| 202 |
include $template_path; |
| 203 |
} |
| 204 |
} else { |
| 205 |
// For custom template return all list courses foreach |
| 206 |
if ( ! empty( $template_path_item ) ) { |
| 207 |
if ( isset( $request['args_custom'] ) ) { |
| 208 |
$args_custom = json_decode( $request['args_custom'], true ); |
| 209 |
} |
| 210 |
|
| 211 |
$template_path_item = urldecode( $template_path_item ); |
| 212 |
} |
| 213 |
|
| 214 |
// Todo: tungnx - should rewrite call template |
| 215 |
foreach ( $courses as $course ) { |
| 216 |
$post = get_post( $course->ID ); |
| 217 |
setup_postdata( $post ); |
| 218 |
|
| 219 |
if ( ! empty( $template_path_item ) ) { |
| 220 |
if ( $args_custom ) { |
| 221 |
extract( $args_custom ); |
| 222 |
} |
| 223 |
|
| 224 |
if ( file_exists( $template_path_item ) ) { |
| 225 |
include $template_path_item; |
| 226 |
} |
| 227 |
} else { |
| 228 |
learn_press_get_template_part( 'content', 'course' ); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
wp_reset_postdata(); |
| 233 |
} |
| 234 |
// End content items |
| 235 |
} else { |
| 236 |
LP()->template( 'course' )->no_courses_found(); |
| 237 |
} |
| 238 |
|
| 239 |
$response->data->content = ob_get_clean(); |
| 240 |
$response->data->totals = $total_rows; |
| 241 |
|
| 242 |
$from = 1 + ( $filter->page - 1 ) * $filter->limit; |
| 243 |
$to = ( $filter->page * $filter->limit > $total_rows ) ? $total_rows : $filter->page * $filter->limit; |
| 244 |
|
| 245 |
if ( 0 === $total_rows ) { |
| 246 |
$response->data->from_to = ''; |
| 247 |
} elseif ( 1 === $total_rows ) { |
| 248 |
$response->data->from_to = esc_html__( 'Showing only one result', 'learnpress' ); |
| 249 |
} else { |
| 250 |
if ( $from == $to ) { |
| 251 |
$response->data->from_to = sprintf( esc_html__( 'Showing last course of %s results', 'learnpress' ), $total_rows ); |
| 252 |
} else { |
| 253 |
$from_to = $from . '-' . $to; |
| 254 |
$response->data->from_to = sprintf( esc_html__( 'Showing %1$s of %2$s results', 'learnpress' ), $from_to, $total_rows ); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
$response->status = 'success'; |
| 260 |
} catch ( Throwable $e ) { |
| 261 |
$response->message = $e->getMessage(); |
| 262 |
} |
| 263 |
|
| 264 |
return apply_filters( 'lp/rest-api/frontend/course/archive_course/response', $response ); |
| 265 |
} |
| 266 |
|
| 267 |
/*public function archive_course( WP_REST_Request $request ) { |
| 268 |
$response = new LP_REST_Response(); |
| 269 |
$response->data = new stdClass(); |
| 270 |
|
| 271 |
$s = isset( $request['s'] ) ? sanitize_text_field( $request['s'] ) : false; |
| 272 |
$page = isset( $request['paged'] ) ? absint( wp_unslash( $request['paged'] ) ) : 1; |
| 273 |
$order = isset( $request['order'] ) ? wp_unslash( $request['order'] ) : false; |
| 274 |
$orderby = isset( $request['orderby'] ) ? wp_unslash( $request['orderby'] ) : false; |
| 275 |
$taxonomy = isset( $request['taxonomy'] ) ? wp_unslash( $request['taxonomy'] ) : false; |
| 276 |
$term_id = isset( $request['term_id'] ) ? wp_unslash( $request['term_id'] ) : false; |
| 277 |
$user_id = isset( $request['userID'] ) ? absint( wp_unslash( $request['userID'] ) ) : false; |
| 278 |
$limit = LP_Settings::get_option( 'archive_course_limit', -1 ); |
| 279 |
|
| 280 |
$args = array( |
| 281 |
'posts_per_page' => $limit, |
| 282 |
'paged' => $page, |
| 283 |
'post_type' => LP_COURSE_CPT, |
| 284 |
); |
| 285 |
|
| 286 |
if ( ! empty( $s ) ) { |
| 287 |
$args['s'] = $s; |
| 288 |
} |
| 289 |
|
| 290 |
if ( ! empty( $taxonomy ) && ! empty( $term_id ) ) { |
| 291 |
$args['tax_query'] = array( |
| 292 |
array( |
| 293 |
'taxonomy' => $taxonomy, |
| 294 |
'field' => 'term_id', |
| 295 |
'terms' => $term_id, |
| 296 |
), |
| 297 |
); |
| 298 |
|
| 299 |
$term_link = get_term_link( $term_id, $taxonomy ); |
| 300 |
} |
| 301 |
|
| 302 |
if ( ! empty( $order ) ) { |
| 303 |
$args['order'] = $order; |
| 304 |
} |
| 305 |
|
| 306 |
if ( ! empty( $orderby ) ) { |
| 307 |
$args['orderby'] = $orderby; |
| 308 |
} |
| 309 |
|
| 310 |
if ( $user_id && learn_press_user_maybe_is_a_teacher( $user_id ) ) { |
| 311 |
$args['post_status'] = array( 'publish', 'private' ); |
| 312 |
} |
| 313 |
|
| 314 |
$args = apply_filters( 'lp/rest-api/frontend/course/archive_course/query_args', $args, $request ); |
| 315 |
|
| 316 |
$query = new WP_Query( $args ); |
| 317 |
|
| 318 |
$num_pages = ! empty( $query->max_num_pages ) ? $query->max_num_pages : 1; |
| 319 |
|
| 320 |
$archive_link = get_post_type_archive_link( LP_COURSE_CPT ); |
| 321 |
|
| 322 |
if ( isset( $term_link ) && ! is_wp_error( $term_link ) ) { |
| 323 |
$archive_link = $term_link; |
| 324 |
} |
| 325 |
|
| 326 |
$base = esc_url_raw( str_replace( 999999999, '%#%', get_pagenum_link( 999999999, false ) ) ); |
| 327 |
|
| 328 |
global $wp; |
| 329 |
$base = str_replace( home_url( $wp->request ) . '/', $archive_link, $base ); |
| 330 |
|
| 331 |
$response->data->pagination = learn_press_get_template_content( |
| 332 |
'loop/course/pagination.php', |
| 333 |
array( |
| 334 |
'total' => $num_pages, |
| 335 |
'paged' => $page, |
| 336 |
'base' => $base, |
| 337 |
) |
| 338 |
); |
| 339 |
|
| 340 |
ob_start(); |
| 341 |
|
| 342 |
if ( $query->have_posts() ) { |
| 343 |
global $post; |
| 344 |
|
| 345 |
while ( $query->have_posts() ) { |
| 346 |
$query->the_post(); |
| 347 |
learn_press_get_template_part( 'content', 'course' ); |
| 348 |
} |
| 349 |
|
| 350 |
wp_reset_postdata(); |
| 351 |
} else { |
| 352 |
LP()->template( 'course' )->no_courses_found(); |
| 353 |
} |
| 354 |
|
| 355 |
$response->status = 'success'; |
| 356 |
$response->data->content = ob_get_clean(); |
| 357 |
|
| 358 |
return rest_ensure_response( apply_filters( 'lp/rest-api/frontend/course/archive_course/response', $response ) ); |
| 359 |
}*/ |
| 360 |
|
| 361 |
/** |
| 362 |
* Rest API for Enroll in single course. |
| 363 |
* |
| 364 |
* @param WP_REST_Request $request |
| 365 |
* |
| 366 |
* @throws Exception . |
| 367 |
* @author Nhamdv |
| 368 |
* @editor tungnx |
| 369 |
* @version 1.0.1 |
| 370 |
* @since 4.0.0 |
| 371 |
* @modify 4.1.2 |
| 372 |
*/ |
| 373 |
public function enroll_courses( WP_REST_Request $request ) { |
| 374 |
$response = new LP_REST_Response(); |
| 375 |
$response->data = new stdClass(); |
| 376 |
$lp_user_items_db = LP_User_Items_DB::getInstance(); |
| 377 |
|
| 378 |
try { |
| 379 |
if ( empty( absint( $request['id'] ) ) ) { |
| 380 |
throw new Exception( esc_html__( 'Error: No course available!.', 'learnpress' ) ); |
| 381 |
} |
| 382 |
|
| 383 |
$course_id = absint( $request['id'] ); |
| 384 |
$course = learn_press_get_course( $course_id ); |
| 385 |
$user = learn_press_get_current_user(); |
| 386 |
|
| 387 |
if ( ! $course ) { |
| 388 |
throw new Exception( esc_html__( 'Invalid course!', 'learnpress' ) ); |
| 389 |
} |
| 390 |
|
| 391 |
$can_enroll = $user->can_enroll_course( $course_id, false ); |
| 392 |
|
| 393 |
if ( ! $can_enroll->check ) { |
| 394 |
throw new Exception( $can_enroll->message ?? esc_html__( 'Error: Cannot enroll course.', 'learnpress' ) ); |
| 395 |
} |
| 396 |
|
| 397 |
$filter = new LP_User_Items_Filter(); |
| 398 |
$filter->user_id = get_current_user_id(); |
| 399 |
$filter->item_id = $course_id; |
| 400 |
$course_item = $lp_user_items_db->get_last_user_course( $filter ); |
| 401 |
|
| 402 |
// Case: if user bought course - or create order manual with order "completed". |
| 403 |
if ( $course_item && 'purchased' == $course_item->status ) { |
| 404 |
$user_item_data = [ |
| 405 |
'user_item_id' => $course_item->user_item_id, |
| 406 |
'graduation' => LP_COURSE_GRADUATION_IN_PROGRESS, |
| 407 |
'status' => LP_COURSE_ENROLLED, |
| 408 |
'start_time' => current_time( 'mysql', true ), |
| 409 |
]; |
| 410 |
|
| 411 |
$user_item_new_or_update = new LP_User_Item_Course( $user_item_data ); |
| 412 |
$result = $user_item_new_or_update->update(); |
| 413 |
|
| 414 |
if ( ! $result ) { |
| 415 |
throw new Exception( esc_html__( 'Error: Can\'t Enroll course.', 'learnpress' ) ); |
| 416 |
} |
| 417 |
|
| 418 |
do_action( 'learnpress/user/course-enrolled', $course_item->ref_id, $course_id, $user->get_id() ); |
| 419 |
} else { // Case enroll course free |
| 420 |
LP()->session->set( 'order_awaiting_payment', '' ); |
| 421 |
|
| 422 |
$cart = LP()->cart; |
| 423 |
$checkout = LP_Checkout::instance(); |
| 424 |
|
| 425 |
if ( ! learn_press_enable_cart() ) { |
| 426 |
$order_awaiting_payment = LP()->session->order_awaiting_payment; |
| 427 |
$cart->empty_cart(); |
| 428 |
LP()->session->order_awaiting_payment = $order_awaiting_payment; |
| 429 |
} |
| 430 |
|
| 431 |
$cart_id = $cart->add_to_cart( $course_id, 1, array() ); |
| 432 |
|
| 433 |
if ( ! $cart_id ) { |
| 434 |
throw new Exception( esc_html__( 'Error: Can\'t add Course to cart.', 'learnpress' ) ); |
| 435 |
} |
| 436 |
|
| 437 |
if ( is_user_logged_in() ) { |
| 438 |
$order_id = $checkout->create_order(); |
| 439 |
|
| 440 |
if ( is_wp_error( $order_id ) ) { |
| 441 |
throw new Exception( $order_id->get_error_message() ); |
| 442 |
} |
| 443 |
|
| 444 |
$order = new LP_Order( $order_id ); |
| 445 |
|
| 446 |
$order->payment_complete(); |
| 447 |
|
| 448 |
$cart->empty_cart(); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
if ( is_user_logged_in() ) { |
| 453 |
$response->status = 'success'; |
| 454 |
// Course has no items |
| 455 |
$response->message = esc_html__( |
| 456 |
'Congrats! You enroll course successfully. Redirecting...', |
| 457 |
'learnpress' |
| 458 |
); |
| 459 |
|
| 460 |
$response->data->redirect = $course->get_redirect_url_after_enroll(); |
| 461 |
|
| 462 |
if ( empty( $course->count_items() ) ) { |
| 463 |
$response->data->redirect = get_permalink( $course->get_id() ); |
| 464 |
} |
| 465 |
} else { |
| 466 |
$redirect_url = apply_filters( |
| 467 |
'learnpress/rest-api/courses/enroll/redirect', |
| 468 |
learn_press_get_page_link( 'checkout' ), |
| 469 |
$course_id |
| 470 |
); |
| 471 |
|
| 472 |
if ( empty( $redirect_url ) ) { |
| 473 |
throw new Exception( __( 'Error: Please setup page for checkout.', 'learnpress' ) ); |
| 474 |
} elseif ( ! is_user_logged_in() ) { // Fix case: cache page with user anonymous |
| 475 |
$redirect_url = LP_Helper::get_link_no_cache( $redirect_url ); |
| 476 |
} |
| 477 |
|
| 478 |
$response->message = esc_html__( 'Redirecting...', 'learnpress' ); |
| 479 |
$response->data->redirect = $redirect_url; |
| 480 |
} |
| 481 |
} catch ( Exception $e ) { |
| 482 |
$response->message = $e->getMessage(); |
| 483 |
} |
| 484 |
|
| 485 |
wp_send_json( $response ); |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Rest API for Purchase course in single course. |
| 490 |
* |
| 491 |
* @param WP_REST_Request $request . |
| 492 |
* |
| 493 |
* @return WP_REST_Response|WP_Error |
| 494 |
* @throws Exception . |
| 495 |
* @author Nhamdv |
| 496 |
*/ |
| 497 |
public function purchase_course( WP_REST_Request $request ) { |
| 498 |
$response = new LP_REST_Response(); |
| 499 |
$response->data = new stdClass(); |
| 500 |
$params = $request->get_params(); |
| 501 |
$lp_user_items_db = LP_User_Items_DB::getInstance(); |
| 502 |
|
| 503 |
try { |
| 504 |
$course_id = $params['id']; |
| 505 |
$allow_repurchase_type = $params['repurchaseType'] ?? false; |
| 506 |
|
| 507 |
if ( ! $course_id ) { |
| 508 |
throw new Exception( __( 'Error: Invalid Course ID.', 'learnpress' ) ); |
| 509 |
} |
| 510 |
|
| 511 |
$course = learn_press_get_course( $course_id ); |
| 512 |
|
| 513 |
if ( ! $course ) { |
| 514 |
throw new Exception( __( 'Error: No Course available.', 'learnpress' ) ); |
| 515 |
} |
| 516 |
|
| 517 |
$user = learn_press_get_current_user(); |
| 518 |
|
| 519 |
if ( ! $user->can_purchase_course( $course_id ) ) { |
| 520 |
throw new Exception( esc_html__( 'Error: Cannot purchase course!.', 'learnpress' ) ); |
| 521 |
} |
| 522 |
|
| 523 |
// Allow Repurchase. |
| 524 |
/*$latest_user_item_id = $wpdb->get_var( |
| 525 |
$wpdb->prepare( |
| 526 |
"SELECT MAX(user_item_id) user_item_id |
| 527 |
FROM {$wpdb->learnpress_user_items} |
| 528 |
WHERE ref_type = %s |
| 529 |
AND item_type = %s |
| 530 |
AND item_id = %d |
| 531 |
AND user_id = %d", |
| 532 |
LP_ORDER_CPT, |
| 533 |
LP_COURSE_CPT, |
| 534 |
$course_id, |
| 535 |
$user->get_id() |
| 536 |
) |
| 537 |
);*/ |
| 538 |
$latest_user_item_id = 0; |
| 539 |
|
| 540 |
$filter = new LP_User_Items_Filter(); |
| 541 |
$filter->user_id = get_current_user_id(); |
| 542 |
$filter->item_id = $course_id; |
| 543 |
$course_item = $lp_user_items_db->get_last_user_course( $filter ); |
| 544 |
|
| 545 |
if ( $course_item && isset( $course_item->user_item_id ) ) { |
| 546 |
$latest_user_item_id = $course_item->user_item_id; |
| 547 |
} |
| 548 |
|
| 549 |
if ( $course->allow_repurchase() && ! empty( $latest_user_item_id ) && empty( $allow_repurchase_type ) ) { |
| 550 |
if ( $course->allow_repurchase_course_option() === 'popup' ) { |
| 551 |
ob_start(); |
| 552 |
?> |
| 553 |
<div class="lp_allow_repuchase_select"> |
| 554 |
<ul> |
| 555 |
<li> |
| 556 |
<label> |
| 557 |
<input name="_lp_allow_repurchase_type" value="reset" type="radio" checked="checked" /> |
| 558 |
<?php esc_html_e( 'Reset Course progress', 'learnpress' ); ?> |
| 559 |
</label> |
| 560 |
</li> |
| 561 |
<li> |
| 562 |
<label> |
| 563 |
<input name="_lp_allow_repurchase_type" value="keep" type="radio" /> |
| 564 |
<?php esc_html_e( 'Continue Course progress', 'learnpress' ); ?> |
| 565 |
</label> |
| 566 |
</li> |
| 567 |
</ul> |
| 568 |
</div> |
| 569 |
<?php |
| 570 |
$response->data->html = ob_get_clean(); |
| 571 |
$response->data->type = 'allow_repurchase'; |
| 572 |
$response->data->titlePopup = esc_html__( 'Repurchase Options', 'learnpress' ); |
| 573 |
$response->status = 'success'; |
| 574 |
|
| 575 |
return rest_ensure_response( $response ); |
| 576 |
} else { |
| 577 |
learn_press_update_user_item_meta( $latest_user_item_id, '_lp_allow_repurchase_type', $course->allow_repurchase_course_option() ); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
LP()->session->set( 'order_awaiting_payment', '' ); |
| 582 |
|
| 583 |
$cart = LP()->cart; |
| 584 |
$checkout = LP_Checkout::instance(); |
| 585 |
|
| 586 |
if ( ! learn_press_enable_cart() ) { |
| 587 |
$order_awaiting_payment = LP()->session->order_awaiting_payment; |
| 588 |
$cart->empty_cart(); |
| 589 |
LP()->session->order_awaiting_payment = $order_awaiting_payment; |
| 590 |
} |
| 591 |
|
| 592 |
do_action( 'learnpress/rest-api/courses/purchase/before-add-to-cart' ); |
| 593 |
|
| 594 |
$cart_id = $cart->add_to_cart( $course_id, 1, array() ); |
| 595 |
|
| 596 |
if ( ! $cart_id ) { |
| 597 |
throw new Exception( __( 'Error: Can\'t add Course to cart.', 'learnpress' ) ); |
| 598 |
} |
| 599 |
|
| 600 |
if ( ! empty( $allow_repurchase_type ) ) { |
| 601 |
learn_press_update_user_item_meta( $latest_user_item_id, '_lp_allow_repurchase_type', $allow_repurchase_type ); |
| 602 |
} |
| 603 |
|
| 604 |
$redirect_url = apply_filters( |
| 605 |
'learnpress/rest-api/courses/purchase/redirect', |
| 606 |
learn_press_get_page_link( 'checkout' ), |
| 607 |
$course_id, |
| 608 |
$cart_id |
| 609 |
); |
| 610 |
|
| 611 |
if ( empty( $redirect_url ) ) { |
| 612 |
throw new Exception( __( 'Error: Please setup page for checkout.', 'learnpress' ) ); |
| 613 |
} elseif ( ! is_user_logged_in() ) { // Fix case: cache page with user anonymous |
| 614 |
$redirect_url = LP_Helper::get_link_no_cache( $redirect_url ); |
| 615 |
} |
| 616 |
|
| 617 |
$response->status = 'success'; |
| 618 |
$response->message = sprintf( |
| 619 |
esc_html__( '"%s" has been added to your cart.', 'learnpress' ), |
| 620 |
$course->get_title() |
| 621 |
); |
| 622 |
$response->data->redirect = $redirect_url; |
| 623 |
} catch ( Exception $e ) { |
| 624 |
$response->message = $e->getMessage(); |
| 625 |
} |
| 626 |
|
| 627 |
return rest_ensure_response( $response ); |
| 628 |
} |
| 629 |
|
| 630 |
/** |
| 631 |
* Rest API for retake course. |
| 632 |
* |
| 633 |
* @param WP_REST_Request $request . |
| 634 |
* |
| 635 |
* @throws Exception . |
| 636 |
*/ |
| 637 |
public function retake_course( WP_REST_Request $request ) { |
| 638 |
$response = new LP_REST_Response(); |
| 639 |
|
| 640 |
try { |
| 641 |
$course_id = $request->get_param( 'id' ); |
| 642 |
|
| 643 |
if ( ! $course_id ) { |
| 644 |
throw new Exception( __( 'Invalid params', 'learnpress' ) ); |
| 645 |
} |
| 646 |
|
| 647 |
$course = learn_press_get_course( $course_id ); |
| 648 |
|
| 649 |
if ( ! $course ) { |
| 650 |
throw new Exception( __( 'Invalid course', 'learnpress' ) ); |
| 651 |
} |
| 652 |
|
| 653 |
$user = learn_press_get_current_user(); |
| 654 |
|
| 655 |
// if ( ! is_user_logged_in() ) { |
| 656 |
// throw new Exception( esc_html__( 'Please login!', 'learnpress' ) ); |
| 657 |
// } |
| 658 |
|
| 659 |
$can_retry = $user->can_retry_course( $course_id ); |
| 660 |
|
| 661 |
if ( ! $can_retry ) { |
| 662 |
throw new Exception( __( 'You can\'t retry course', 'learnpress' ) ); |
| 663 |
} |
| 664 |
|
| 665 |
$user_course_data = $user->get_course_data( $course_id ); |
| 666 |
|
| 667 |
// Up retaken. |
| 668 |
$user_course_data->increase_retake_count(); |
| 669 |
|
| 670 |
// Set status, start_time, end_time of course to enrolled. |
| 671 |
$user_course_data->set_status( LP_COURSE_ENROLLED ) |
| 672 |
->set_start_time( current_time( 'mysql', true ) ) |
| 673 |
->set_end_time( '' ) |
| 674 |
->set_graduation( 'in-progress' ) |
| 675 |
->update(); |
| 676 |
|
| 677 |
// Remove items' course user learned. |
| 678 |
$filter_remove = new LP_User_Items_Filter(); |
| 679 |
$filter_remove->parent_id = $user_course_data->get_user_item_id(); |
| 680 |
$filter_remove->user_id = $user_course_data->get_user_id(); |
| 681 |
$filter_remove->limit = - 1; |
| 682 |
LP_User_Items_DB::getInstance()->remove_items_of_user_course( $filter_remove ); |
| 683 |
|
| 684 |
// Create new result in table learnpress_user_item_results. |
| 685 |
LP_User_Items_Result_DB::instance()->insert( $user_course_data->get_user_item_id() ); |
| 686 |
|
| 687 |
$response->status = 'success'; |
| 688 |
$response->message = esc_html__( 'Now you can learn this course', 'learnpress' ); |
| 689 |
$response->data->url_redirect = $course->get_redirect_url_after_enroll(); |
| 690 |
} catch ( Exception $e ) { |
| 691 |
$response->message = $e->getMessage(); |
| 692 |
} |
| 693 |
|
| 694 |
wp_send_json( $response ); |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
* @param WP_REST_Request $request |
| 699 |
* |
| 700 |
* @return WP_REST_Response |
| 701 |
*/ |
| 702 |
public function get_items( $request ) { |
| 703 |
$settings = LP_Settings::instance(); |
| 704 |
$response = array( |
| 705 |
'result' => $settings->get(), |
| 706 |
); |
| 707 |
|
| 708 |
return rest_ensure_response( $response ); |
| 709 |
} |
| 710 |
|
| 711 |
/** |
| 712 |
* @param WP_REST_Request $request |
| 713 |
* |
| 714 |
* @return WP_REST_Response |
| 715 |
*/ |
| 716 |
public function get_item( $request ) { |
| 717 |
$settings = LP_Settings::instance(); |
| 718 |
$response = array( |
| 719 |
'result' => $settings->get( $request['key'] ), |
| 720 |
); |
| 721 |
|
| 722 |
return rest_ensure_response( $response ); |
| 723 |
} |
| 724 |
|
| 725 |
/** |
| 726 |
* @param WP_REST_Request $request |
| 727 |
* |
| 728 |
* @return WP_REST_Response |
| 729 |
*/ |
| 730 |
public function update_item( $request ) { |
| 731 |
$response = array(); |
| 732 |
$settings = LP_Settings::instance(); |
| 733 |
$option = $settings->get( $request['key'] ); |
| 734 |
|
| 735 |
$settings->update( $request['key'], $request['data'] ); |
| 736 |
$new_option = $settings->get( $request['key'] ); |
| 737 |
$success = maybe_serialize( $option ) !== maybe_serialize( $new_option ); |
| 738 |
|
| 739 |
$response['success'] = $success; |
| 740 |
$response['result'] = $success ? $new_option : $option; |
| 741 |
|
| 742 |
return rest_ensure_response( $response ); |
| 743 |
} |
| 744 |
|
| 745 |
/** |
| 746 |
* @param WP_REST_Request $request |
| 747 |
* |
| 748 |
* @return WP_REST_Response |
| 749 |
*/ |
| 750 |
public function delete_item( $request ) { |
| 751 |
$response = array(); |
| 752 |
|
| 753 |
return rest_ensure_response( $response ); |
| 754 |
} |
| 755 |
|
| 756 |
/** |
| 757 |
* Rest API for Continue in single course. |
| 758 |
* |
| 759 |
* @param WP_REST_Request $request |
| 760 |
* @author minhpd |
| 761 |
* @editor tungnx |
| 762 |
* @since 4.1.4 |
| 763 |
* @version 1.0.1 |
| 764 |
*/ |
| 765 |
public function continue_course( WP_REST_Request $request ) { |
| 766 |
$params = $request->get_params(); |
| 767 |
$response = new LP_REST_Response(); |
| 768 |
$response->data = ''; |
| 769 |
|
| 770 |
try { |
| 771 |
$flag_found = false; |
| 772 |
$item_link = ''; |
| 773 |
$course_id = absint( $params['courseId'] ?? 0 ); |
| 774 |
$user_id = absint( $params['userId'] ?? 0 ); |
| 775 |
|
| 776 |
$user = learn_press_get_user( $user_id ); |
| 777 |
$course = learn_press_get_course( $course_id ); |
| 778 |
$sections_items = $course->get_full_sections_and_items_course(); |
| 779 |
$total_items = $course->count_items(); |
| 780 |
|
| 781 |
if ( ! empty( $total_items ) ) { |
| 782 |
foreach ( $sections_items as $section_items ) { |
| 783 |
if ( $flag_found ) { |
| 784 |
break; |
| 785 |
} |
| 786 |
|
| 787 |
foreach ( $section_items->items as $item ) { |
| 788 |
if ( ! $user->has_completed_item( $item->id, $course_id ) ) { |
| 789 |
$item_link = $course->get_item_link( $item->id ); |
| 790 |
$flag_found = true; |
| 791 |
break; |
| 792 |
} |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
if ( ! $flag_found ) { |
| 797 |
$item_link = $course->get_item_link( $course->get_first_item_id() ); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
$response->data = $item_link; |
| 802 |
$response->status = 'success'; |
| 803 |
$response->message = ''; |
| 804 |
} catch ( Exception $e ) { |
| 805 |
$response->message = $e->getMessage(); |
| 806 |
} |
| 807 |
|
| 808 |
wp_send_json( $response ); |
| 809 |
} |
| 810 |
|
| 811 |
} |
| 812 |
|