PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
← All changes | inc/admin/class-lp-admin-ajax.php +248 -233 4.2.04.4.8 View file →
@@ -10,8 +10,13 @@
10 10
11 11 /**
12 12 * Prevent loading this file directly
13 13 */
14 +
15 +use LearnPress\Helpers\Response;
16 +use LearnPress\Helpers\Template;
17 +use LearnPress\Models\UserModel;
18 +
14 19 defined( 'ABSPATH' ) || exit();
15 20
16 21 if ( ! class_exists( 'LP_Admin_Ajax' ) ) {
17 22
@@ -31,28 +36,12 @@
31 36 }
32 37
33 38 $ajax_events = array(
34 39 'create_page' => false, // Use create new page on Settings
35 - // 'plugin_action' => false,
36 - // 'modal_search_items' => false,
37 - //'dismiss_notice' => false,
38 - //'search_users' => false,
39 - 'load_chart' => false,
40 + //'load_chart' => false,
40 41 'search_course_category' => false,
41 - 'custom_stats' => false,
42 - //'ignore_setting_up' => false,
42 + //'custom_stats' => false,
43 43 'get_page_permalink' => false,
44 - //'dummy_image' => false,
45 - // 'update_add_on_status' => false,
46 - // 'plugin_install' => false,
47 - //'bundle_activate_add_ons' => false,
48 - //'install_sample_data' => false,
49 -
50 - // Remove Notice
51 - //'remove_notice_popup' => false,
52 - // Update order status
53 - // 'update_order_status' => false,
54 - 'update_order_exports' => false,
55 44 );
56 45
57 46 foreach ( $ajax_events as $ajax_event => $nopriv ) {
58 47 add_action( 'wp_ajax_learnpress_' . $ajax_event, array( __CLASS__, $ajax_event ) );
@@ -66,40 +55,32 @@
66 55 do_action( 'learn-press/ajax/admin-load', __CLASS__ );
67 56
68 57 $ajax_events = array(
69 58 'search_items' => 'modal_search_items',
70 - 'update-payment-order', // Update ordering of payments when user changing.
71 - 'update-payment-status', // Enable type payment
72 - //'toggle_item_preview',
59 + 'update-payment-order',
60 + // Update ordering of payments when user changing.
61 + 'update-payment-status',
62 + // Enable type payment
73 63
74 64 // admin editor
75 65 'admin_course_editor',
76 66 'admin_quiz_editor',
77 67 'admin_question_editor',
78 - // duplicator
79 - 'duplicator', // Duplicate course, lesson, quiz, question.
80 -
81 - //'add_item_to_order',
82 - //'remove_order_item',
83 -
84 - 'modal_search_items', // Used to search courses on LP Order
85 - 'modal_search_users', // Used to search users on LP Order
86 - 'add_items_to_order', // Used to add courses on LP Order
87 - 'remove_items_from_order', // Used to remove items from LP Order
88 - 'update_email_status', // Use for enable email on LP Settings
89 - //'create-pages',
90 - 'search-authors', // Used to search username on input some page (list courses, lp orders, quizzes, questions... on the Backend
91 - 'skip-notice-install',
92 - //'join_newsletter',
93 - //'dashboard-order-status',
94 - //'dashboard-plugin-status',
95 - //'dismiss-notice',
96 - //'sync-user-orders',
97 - //'sync-course-final-quiz',
98 - //'sync-remove-older-data',
99 - //'sync-calculate-course-results',
100 - //'create-question-type',
101 - // 'sync-user-courses',
68 + 'duplicator',
69 + // Duplicate course, lesson, quiz, question.
70 + 'modal_search_items',
71 + // Used to search courses on LP Order
72 + //'modal_search_users',
73 + // Used to search users on LP Order
74 + 'add_items_to_order',
75 + // Used to add courses on LP Order
76 + 'remove_items_from_order',
77 + // Used to remove items from LP Order
78 + 'update_email_status',
79 + // Use for enable email on LP Settings
80 + 'search-authors',
81 + // Used to search username on input some page (list courses, lp orders, quizzes, questions... on the Backend
82 + //'skip-notice-install',
102 83 );
103 84
104 85 foreach ( $ajax_events as $action => $callback ) {
105 86 if ( is_numeric( $action ) ) {
@@ -149,15 +130,8 @@
149 130 die();
150 131 }
151 132
152 133 /**
153 - * Hide notice install
154 - */
155 - public static function skip_notice_install() {
156 - delete_option( 'learn_press_install' );
157 - }
158 -
159 - /**
160 134 * Handle ajax admin course editor.
161 135 *
162 136 * @since 3.0.0
163 137 */
@@ -210,19 +184,32 @@
210 184 *
211 185 * @note tungnx checked has use
212 186 */
213 187 public static function duplicator() {
214 - $post_id = intval( $_GET['id'] ?? 0 );
188 + $nonce = LP_Request::get_param( 'nonce' );
189 + if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
190 + learn_press_send_json_error( __( 'Nonce is invalid!', 'learnpress' ) );
191 + }
215 192
216 - // get post type
193 + $post_id = intval( $_GET['id'] ?? 0 );
217 194 $post_type = learn_press_get_post_type( $post_id );
218 195
219 196 if ( ! $post_id ) {
220 197 learn_press_send_json_error( __( 'Oops! ID not found', 'learnpress' ) );
221 198 } else {
199 + $can_duplicate = apply_filters( 'learn-press/can-duplicate-course', true, $post_id, $post_type );
200 + if ( ! current_user_can( ADMIN_ROLE ) ) {
201 + $post_author = get_post_field( 'post_author', $post_id );
202 + if ( get_current_user_id() != $post_author ) {
203 + $can_duplicate = false;
204 + }
205 + }
222 206
223 - $new_item_id = '';
207 + if ( ! $can_duplicate ) {
208 + learn_press_send_json_error( __( 'You cannot duplicate this item.', 'learnpress' ) );
209 + }
224 210
211 + $new_item_id = '';
225 212 $duplicate_args = apply_filters( 'learn-press/duplicate-post-args', array( 'post_status' => 'publish' ) );
226 213
227 214 switch ( $post_type ) {
228 215 case LP_COURSE_CPT:
@@ -270,14 +257,25 @@
270 257 /**
271 258 * Update ordering of payments when user changing.
272 259 *
273 260 * @since 3.0.0
274 - * @use for sorting by type payment gateway
261 + * @version 1.0.1
275 262 * @note tungnx checked has use
276 263 */
277 264 public static function update_payment_order() {
265 + if ( ! current_user_can( ADMIN_ROLE ) ) { // Fix security.
266 + return;
267 + }
268 +
269 + $nonce = LP_Request::get_param( 'nonce' );
270 + if ( ! wp_verify_nonce( $nonce, 'lp-settings' ) ) {
271 + die( 'Nonce is invalid!' );
272 + }
273 +
278 274 $payment_order = learn_press_get_request( 'order' );
279 275 update_option( 'learn_press_payment_order', $payment_order );
276 +
277 + die( 'Order of Payment Gateway is updated success' );
280 278 }
281 279
282 280 /**
283 281 * Enable type payment
@@ -282,14 +280,14 @@
282 280 /**
283 281 * Enable type payment
284 282 *
285 283 * @since 3.0.0
286 - * @use for enable type payment gateway
284 + * @version 1.0.1
287 285 * @note tungnx checked has use
288 286 */
289 287 public static function update_payment_status() {
290 - $payment_id = learn_press_get_request( 'id' );
291 - $status = LP_Request::get_string( 'status' );
288 + $payment_id = LP_Request::get_param( 'id' );
289 + $status = LP_Request::get_param( 'status' );
292 290 $payment = LP_Gateways::instance()->get_gateway( $payment_id );
293 291
294 292 if ( ! $payment ) {
295 293 return;
@@ -294,10 +292,22 @@
294 292 if ( ! $payment ) {
295 293 return;
296 294 }
297 295
296 + if ( ! current_user_can( ADMIN_ROLE ) ) { // Fix security.
297 + return;
298 + }
299 +
300 + $nonce = LP_Request::get_param( 'nonce' );
301 + if ( ! wp_verify_nonce( $nonce, 'lp-settings' ) ) {
302 + die( 'Nonce is invalid!' );
303 + }
304 +
298 305 $response[ $payment->id ] = $payment->enable( $status == 'yes' );
299 306
307 + $lp_settings_cache = new LP_Settings_Cache( true );
308 + $lp_settings_cache->clean_lp_settings();
309 +
300 310 learn_press_send_json( $response );
301 311 }
302 312
303 313 /**
@@ -306,13 +316,21 @@
306 316 * @since 3.0.0
307 317 * @note tungnnx checked has use
308 318 */
309 319 public static function update_email_status() {
310 -
311 320 $email_id = LP_Request::get_string( 'id' );
312 321 $status = LP_Request::get_string( 'status' );
313 322 $response = array();
314 323
324 + if ( ! current_user_can( ADMIN_ROLE ) ) { // Fix security.
325 + return;
326 + }
327 +
328 + $nonce = LP_Request::get_param( 'nonce' );
329 + if ( ! wp_verify_nonce( $nonce, 'lp-settings' ) ) {
330 + die( 'Nonce is invalid!' );
331 + }
332 +
315 333 if ( $email_id ) {
316 334
317 335 $email = LP_Emails::get_email( $email_id );
318 336 if ( ! $email ) {
@@ -325,8 +343,12 @@
325 343 foreach ( $emails as $email ) {
326 344 $response[ $email->id ] = $email->enable( $status == 'yes' );
327 345 }
328 346 }
347 +
348 + $lp_settings_cache = new LP_Settings_Cache( true );
349 + $lp_settings_cache->clean_lp_settings();
350 +
329 351 learn_press_send_json( $response );
330 352 }
331 353
332 354 /**
@@ -332,15 +354,35 @@
332 354 /**
333 355 * Search items by requesting params.
334 356 */
335 357 public static function modal_search_items() {
336 - $term = LP_Helper::sanitize_params_submitted( $_POST['term'] ?? '' );
337 - $type = LP_Helper::sanitize_params_submitted( $_POST['type'] ?? '' );
338 - $context = LP_Helper::sanitize_params_submitted( $_POST['context'] ?? '' );
339 - $context_id = LP_Helper::sanitize_params_submitted( $_POST['context_id'] ?? '' );
340 - $paged = LP_Helper::sanitize_params_submitted( $_POST['paged'] ?? '' );
341 - $exclude = LP_Request::get( 'exclude' );
358 + $term = LP_Request::get_param( 'term' );
359 + $type = LP_Request::get_param( 'type' );
360 + $context = LP_Request::get_param( 'context' );
361 + $context_id = LP_Request::get_param( 'context_id' );
362 + $paged = LP_Request::get_param( 'paged' );
363 + $exclude = LP_Request::get_param( 'exclude' );
342 364
365 + if ( ! current_user_can( ADMIN_ROLE ) ) { // Fix security
366 + $roles_accept = apply_filters( 'lp/backend/roles/can-search-items', [ ADMIN_ROLE ] );
367 +
368 + $flag = false;
369 + foreach ( $roles_accept as $role ) {
370 + if ( current_user_can( $role ) ) {
371 + $flag = true;
372 + }
373 + }
374 +
375 + if ( ! $flag ) {
376 + return;
377 + }
378 + }
379 +
380 + $nonce = LP_Request::get_param( 'nonce' );
381 + if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
382 + die( 'Nonce is invalid!' );
383 + }
384 +
343 385 $search = new LP_Modal_Search_Items( compact( 'term', 'type', 'context', 'context_id', 'paged', 'exclude' ) );
344 386
345 387 learn_press_send_json(
346 388 array(
@@ -354,19 +396,40 @@
354 396 /**
355 397 * Search items by requesting params.
356 398 *
357 399 * @note tungnx checked has use
400 + * @deprecated 4.2.6.9.3
358 401 */
359 - public static function modal_search_users() {
360 - $term = LP_Helper::sanitize_params_submitted( $_POST['term'] ?? '' );
361 - $type = LP_Helper::sanitize_params_submitted( $_POST['type'] ?? '' );
362 - $context = LP_Helper::sanitize_params_submitted( $_POST['context'] ?? '' );
363 - $context_id = LP_Helper::sanitize_params_submitted( $_POST['context_id'] ?? '' );
364 - $paged = LP_Helper::sanitize_params_submitted( $_POST['paged'] ?? '' );
365 - $multiple = LP_Helper::sanitize_params_submitted( $_POST['multiple'] ?? '' ) == 'yes';
366 - $text_format = LP_Helper::sanitize_params_submitted( $_POST['text_format'] ?? '' );
367 - $exclude = LP_Request::get( 'exclude' );
402 + /*public static function modal_search_users() {
403 + $term = LP_Request::get_param( 'term' );
404 + $type = LP_Request::get_param( 'type' );
405 + $context = LP_Request::get_param( 'context' );
406 + $context_id = LP_Request::get_param( 'context_id' );
407 + $paged = LP_Request::get_param( 'paged' );
408 + $multiple = LP_Request::get_param( 'multiple' ) == 'yes';
409 + $text_format = LP_Request::get_param( 'text_format' );
410 + $exclude = LP_Request::get_param( 'exclude' );
411 + $roles_accept = apply_filters(
412 + 'lp/backend/roles/can-search-users',
413 + [ ADMIN_ROLE ]
414 + );
368 415
416 + $flag = false;
417 + foreach ( $roles_accept as $role ) {
418 + if ( current_user_can( $role ) ) {
419 + $flag = true;
420 + }
421 + }
422 +
423 + if ( ! $flag ) {
424 + return;
425 + }
426 +
427 + $nonce = LP_Request::get_param( 'nonce' );
428 + if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
429 + die( 'Nonce is invalid!' );
430 + }
431 +
369 432 $search = new LP_Modal_Search_Users( compact( 'term', 'type', 'context', 'context_id', 'paged', 'multiple', 'text_format', 'exclude' ) );
370 433
371 434 learn_press_send_json(
372 435 array(
@@ -374,9 +437,9 @@
374 437 'nav' => $search->get_pagination(),
375 438 'users' => $search->get_items(),
376 439 )
377 440 );
378 - }
441 + }*/
379 442
380 443 /**
381 444 * Search course category.
382 445 */
@@ -382,12 +445,12 @@
382 445 */
383 446 public static function search_course_category() {
384 447 global $wpdb;
385 448 $sql = 'SELECT `t`.`term_id` as `id`, '
386 - . ' `t`.`name` `text` '
387 - . " FROM {$wpdb->terms} t "
388 - . " INNER JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id AND taxonomy='course_category' "
389 - . ' WHERE `t`.`name` LIKE %s';
449 + . ' `t`.`name` `text` '
450 + . " FROM {$wpdb->terms} t "
451 + . " INNER JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id AND taxonomy='course_category' "
452 + . ' WHERE `t`.`name` LIKE %s';
390 453 $s = '%' . filter_input( INPUT_GET, 'q' ) . '%';
391 454 $query = $wpdb->prepare( $sql, $s );
392 455 $items = $wpdb->get_results( $query );
393 456 $data = array( 'items' => $items );
@@ -400,8 +463,10 @@
400 463 *
401 464 * @note tungnx checked has use
402 465 */
403 466 public static function remove_items_from_order() {
467 + $response = new LP_REST_Response();
468 +
404 469 // ensure that user has permission
405 470 if ( ! current_user_can( 'edit_lp_orders' ) ) {
406 471 die( __( 'Access denied', 'learnpress' ) );
407 472 }
@@ -406,27 +471,29 @@
406 471 die( __( 'Access denied', 'learnpress' ) );
407 472 }
408 473
409 474 // verify nonce
410 - $nonce = learn_press_get_request( 'remove_nonce' );
411 - if ( ! wp_verify_nonce( $nonce, 'remove_order_item' ) ) {
475 + $nonce = LP_Request::get_param( 'nonce' );
476 + if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
412 477 die( __( 'Nonce check failed', 'learnpress' ) );
413 478 }
414 479
415 480 // validate order
416 - $order_id = learn_press_get_request( 'order_id' );
417 - if ( ! is_numeric( $order_id ) || learn_press_get_post_type( $order_id ) != 'lp_order' ) {
481 + $order_id = LP_Request::get_param( 'order_id', 0, 'int' );
482 + if ( learn_press_get_post_type( $order_id ) != 'lp_order' ) {
418 483 die( __( 'Invalid order', 'learnpress' ) );
419 484 }
420 485
421 486 // validate item
422 - $items = learn_press_get_request( 'items' );
487 + $item_ids_str = LP_Request::get_param( 'items', '' );
488 + if ( empty( $item_ids_str ) ) {
489 + die( __( 'Invalid item', 'learnpress' ) );
490 + }
423 491
424 - $order = learn_press_get_order( $order_id );
492 + $item_ids = array_map( 'absint', explode( ',', $item_ids_str ) );
493 + $order = learn_press_get_order( $order_id );
425 494
426 - global $wpdb;
427 -
428 - foreach ( $items as $item_id ) {
495 + foreach ( $item_ids as $item_id ) {
429 496 $order->remove_item( $item_id );
430 497 }
431 498
432 499 $order_data = learn_press_update_order_items( $order_id );
@@ -433,10 +500,10 @@
433 500 $currency_symbol = learn_press_get_currency_symbol( $order_data['currency'] );
434 501 $order_data['subtotal_html'] = learn_press_format_price( $order_data['subtotal'], $currency_symbol );
435 502 $order_data['total_html'] = learn_press_format_price( $order_data['total'], $currency_symbol );
436 503 $order_items = $order->get_items();
504 + $html = '';
437 505 if ( $order_items ) {
438 - $html = '';
439 506 foreach ( $order_items as $item ) {
440 507 ob_start();
441 508 include learn_press_get_admin_view( 'meta-boxes/order/order-item.php' );
442 509 $html .= ob_get_clean();
@@ -442,15 +509,13 @@
442 509 $html .= ob_get_clean();
443 510 }
444 511 }
445 512
446 - learn_press_send_json(
447 - array(
448 - 'result' => 'success',
449 - 'item_html' => $html,
450 - 'order_data' => $order_data,
451 - )
452 - );
513 + $response->status = 'success';
514 + $response->data->item_html = $html;
515 + $response->data->order_data = $order_data;
516 +
517 + wp_send_json( $response );
453 518 }
454 519
455 520 /**
456 521 * Add courses to order
@@ -457,8 +522,28 @@
457 522 *
458 523 * @note tungnx checked has use
459 524 */
460 525 public static function add_items_to_order() {
526 + $response = new LP_REST_Response();
527 +
528 + $roles_accept = apply_filters( 'lp/backend/roles/can-add-items', [ ADMIN_ROLE ] );
529 +
530 + $flag = false;
531 + foreach ( $roles_accept as $role ) {
532 + if ( current_user_can( $role ) ) {
533 + $flag = true;
534 + }
535 + }
536 +
537 + if ( ! $flag ) {
538 + return;
539 + }
540 +
541 + $nonce = LP_Request::get_param( 'nonce' );
542 + if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
543 + die( 'Nonce is invalid!' );
544 + }
545 +
461 546 // ensure that user has permission
462 547 if ( ! current_user_can( 'edit_lp_orders' ) ) {
463 548 die( __( 'Permission denied', 'learnpress' ) );
464 549 }
@@ -463,27 +548,25 @@
463 548 die( __( 'Permission denied', 'learnpress' ) );
464 549 }
465 550
466 551 // validate order
467 - $order_id = learn_press_get_request( 'order_id' );
552 + $order_id = LP_Request::get_param( 'order_id', 0 );
468 553 if ( ! is_numeric( $order_id ) || learn_press_get_post_type( $order_id ) != 'lp_order' ) {
469 554 die( __( 'Invalid order', 'learnpress' ) );
470 555 }
471 556
472 557 // validate item
473 - $item_ids = learn_press_get_request( 'items' );
474 - $order = learn_press_get_order( $order_id );
558 + $item_ids_str = LP_Request::get_param( 'items', '' );
559 + if ( empty( $item_ids_str ) ) {
560 + die( __( 'Invalid item', 'learnpress' ) );
561 + }
475 562
476 - $response = array(
477 - 'result' => 'error',
478 - );
479 -
480 - $order_item_ids = $order->add_items( $item_ids );
481 -
482 - if ( $order_item_ids ) {
483 - $html = '';
484 - $order_items = $order->get_items();
485 -
563 + $item_ids = array_map( 'absint', explode( ',', $item_ids_str ) );
564 + $order = learn_press_get_order( $order_id );
565 + $order_item = $order->add_items( $item_ids );
566 + if ( $order_item ) {
567 + $html = '';
568 + $order_items = $order->get_items();
486 569 $order_data = learn_press_update_order_items( $order_id );
487 570 $currency_symbol = learn_press_get_currency_symbol( $order_data['currency'] );
488 571 $order_data['subtotal_html'] = learn_press_format_price( $order_data['subtotal'], $currency_symbol );
489 572 $order_data['total_html'] = learn_press_format_price( $order_data['total'], $currency_symbol );
@@ -489,79 +572,27 @@
489 572 $order_data['total_html'] = learn_press_format_price( $order_data['total'], $currency_symbol );
490 573
491 574 if ( $order_items ) {
492 575 foreach ( $order_items as $item ) {
493 -
494 - if ( ! in_array( $item['id'], $order_item_ids ) ) {
576 + if ( ! in_array( $item['id'], $order_item ) ) {
495 577 continue;
496 578 }
497 579
498 580 ob_start();
499 - include learn_press_get_admin_view( 'meta-boxes/order/order-item.php' );
581 + Template::instance()->get_admin_template( 'meta-boxes/order/order-item.php', compact( 'item', 'order' ) );
500 582 $html .= ob_get_clean();
501 583 }
502 584 }
503 585
504 - $response = array(
505 - 'result' => 'success',
506 - 'item_html' => $html,
507 - 'order_data' => $order_data,
508 - );
586 + $response->status = 'success';
587 + $response->data->item_html = $html;
588 + $response->data->order_data = $order_data;
509 589 }
510 590
511 - learn_press_send_json( $response );
591 + wp_send_json( $response );
512 592 }
513 593
514 - /**
515 - * Get content send via payload and parse to json.
516 - *
517 - * @param mixed $params (Optional) List of keys want to get from payload.
518 - *
519 - * @return array|bool|mixed|object
520 - * @deprecated 4.1.6.9
521 - */
522 - /*public static function get_php_input( $params = '' ) {
523 - static $data = false;
524 - if ( false === $data ) {
525 - try {
526 - $data = json_decode( file_get_contents( 'php://input' ), true );
527 - } catch ( Exception $exception ) {
528 - }
529 - }
530 -
531 - if ( $data && func_num_args() > 0 ) {
532 - $params = is_array( func_get_arg( 0 ) ) ? func_get_arg( 0 ) : func_get_args();
533 - if ( $params ) {
534 - $request = array();
535 - foreach ( $params as $key ) {
536 - $request[] = array_key_exists( $key, $data ) ? $data[ $key ] : false;
537 - }
538 -
539 - return $request;
540 - }
541 - }
542 -
543 - return $data;
544 - }*/
545 -
546 - /**
547 - * Parse request content into var.
548 - * Normally, parse and assign to $_POST or $_GET.
549 - *
550 - * @param $var
551 - * @deprecated 4.1.6.9
552 - */
553 - /*public static function parsePhpInput( &$var ) {
554 - $data = self::get_php_input();
555 -
556 - if ( $data ) {
557 - foreach ( $data as $k => $v ) {
558 - $var[ $k ] = $v;
559 - }
560 - }
561 - }*/
562 -
563 - public static function load_chart() {
594 + /*public static function load_chart() {
564 595 if ( ! class_exists( 'LP_Submenu_Statistics' ) ) {
565 596 $statistic = include_once LP_PLUGIN_PATH . '/inc/admin/sub-menus/class-lp-submenu-statistics.php';
566 597 } else {
567 598 $statistic = new LP_Submenu_Statistics();
@@ -566,9 +597,9 @@
566 597 } else {
567 598 $statistic = new LP_Submenu_Statistics();
568 599 }
569 600 $statistic->load_chart();
570 - }
601 + }*/
571 602
572 603 public static function json_search_customer_name( $query ) {
573 604 global $wpdb;
574 605
@@ -588,49 +619,55 @@
588 619 *
589 620 * @note tungnnx checked use
590 621 */
591 622 public static function create_page() {
592 - $response = array(
593 - 'code' => 0,
594 - 'message' => '',
595 - );
623 + $response = new Response();
596 624
597 - /**
598 - * Check valid
599 - *
600 - * 1. Capability - user can edit pages (add\edit\delete)
601 - * 2. Check nonce return true
602 - * 3. param post page_name not empty
603 - *
604 - * @since 3.2.6.8
605 - * @author tungnx
606 - */
607 - if ( ! current_user_can( 'edit_pages' ) || empty( $_POST['page_name'] ) ) {
608 - $response['message'] = 'Request invalid';
609 - learn_press_send_json( $response );
610 - }
625 + try {
626 + /**
627 + * Check valid
628 + *
629 + * 1. Capability - user can edit pages (add\edit\delete)
630 + * 2. Check nonce return true
631 + * 3. param post page_name not empty
632 + *
633 + * @since 3.2.6.8
634 + */
635 + if ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR )
636 + || empty( $_POST['page_name'] ) ) {
637 + throw new Exception( 'Request invalid' );
638 + }
611 639
612 - $page_name = LP_Helper::sanitize_params_submitted( $_POST['page_name'] );
640 + // Check nonce
641 + $nonce = LP_Request::get_param( 'nonce' );
642 + if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
643 + throw new Exception( 'Request invalid' );
644 + }
613 645
614 - if ( $page_name ) {
615 - $page_id = LP_Helper::create_page( $page_name );
646 + $page_name = LP_Helper::sanitize_params_submitted( $_POST['page_name'] );
647 + $field_name = LP_Request::get_param( 'field_name' );
616 648
617 - if ( $page_id ) {
618 - $response['code'] = 1;
619 - $response['message'] = 'create page success';
620 - $response['page'] = get_post( $page_id );
621 - $html = learn_press_pages_dropdown( '', '', array( 'echo' => false ) );
622 - preg_match_all( '!value=\"([0-9]+)\"!', $html, $matches );
623 - $response['positions'] = $matches[1];
624 - $response['html'] = '<a href="' . get_edit_post_link( $page_id ) . '" target="_blank">' . __( 'Edit Page', 'learnpress' ) . '</a>&nbsp;';
625 - $response['html'] .= '<a href="' . get_permalink( $page_id ) . '" target="_blank">' . __( 'View Page', 'learnpress' ) . '</a>';
626 - } else {
627 - $response['error'] = __( 'Error! Page creation failed. Please try again.', 'learnpress' );
649 + if ( ! $page_name ) {
650 + throw new Exception( __( 'Empty page name!', 'learnpress' ) );
628 651 }
629 - } else {
630 - $response['error'] = __( 'Empty page name!', 'learnpress' );
652 +
653 + $data_create_page = array(
654 + 'post_title' => $page_name,
655 + );
656 +
657 + $page_id = LP_Helper::create_page( $data_create_page, $field_name );
658 +
659 + if ( ! $page_id ) {
660 + throw new Exception( __( 'Error! Page creation failed. Please try again.', 'learnpress' ) );
661 + }
662 +
663 + $response->status = Response::STATUS_SUCCESS;
664 + $response->message = 'create page success';
665 + } catch ( Exception $e ) {
666 + $response->message = $e->getMessage();
631 667 }
632 - learn_press_send_json( $response );
668 +
669 + wp_send_json( $response );
633 670 }
634 671
635 672 /**
636 673 * Get edit|view link of a page
@@ -639,11 +676,11 @@
639 676 $page_id = (int) $_REQUEST['page_id'] ?? 0;
640 677 ?>
641 678
642 679 <a href="<?php echo get_edit_post_link( $page_id ); ?>"
643 - target="_blank"><?php _e( 'Edit Page', 'learnpress' ); ?></a>
680 + target="_blank"><?php _e( 'Edit Page', 'learnpress' ); ?></a>
644 681 <a href="<?php echo get_permalink( $page_id ); ?>"
645 - target="_blank"><?php _e( 'View Page', 'learnpress' ); ?></a>
682 + target="_blank"><?php _e( 'View Page', 'learnpress' ); ?></a>
646 683
647 684 <?php
648 685 die();
649 686 }
@@ -649,10 +686,12 @@
649 686 }
650 687
651 688 /**
652 689 * Get date from, to for static chart
690 + *
691 + * @deprecated 4.2.6.9.3
653 692 */
654 - public static function custom_stats() {
693 + /*public static function custom_stats() {
655 694 $from = LP_Helper::sanitize_params_submitted( $_REQUEST['from'] ?? 0 );
656 695 $to = LP_Helper::sanitize_params_submitted( $_REQUEST['to'] ?? 0 );
657 696 $date_diff = strtotime( $to ) - strtotime( $from );
658 697 if ( $date_diff <= 0 || $from == 0 || $to == 0 ) {
@@ -659,33 +698,9 @@
659 698 die();
660 699 }
661 700 learn_press_process_chart( learn_press_get_chart_students( $to, 'days', floor( $date_diff / ( 60 * 60 * 24 ) ) + 1 ) );
662 701 die();
663 - }
664 -
665 - /**
666 - * Export Order invoice to PDF
667 - *
668 - * @since 3.2.7.8
669 - * @author hungkv
670 - */
671 - public static function update_order_exports() {
672 - $order_id = absint( $_POST['order_id'] );
673 - $order = learn_press_get_order( $order_id );
674 - $currency_symbol = learn_press_get_currency_symbol( $order->get_currency() );
675 -
676 - ob_start();
677 - learn_press_admin_view(
678 - 'meta-boxes/order/content-tab-preview-exports-invoice.php',
679 - array(
680 - 'order' => $order,
681 - 'currency_symbol' => $currency_symbol,
682 - )
683 - );
684 - $html = ob_get_clean();
685 - echo wp_kses_post( $html );
686 - die();
687 - }
702 + }*/
688 703 }
689 704
690 705 add_action( 'init', array( 'LP_Admin_Ajax', 'init' ) );
691 706 }