PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.9.1
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
learnpress / inc / admin / class-lp-admin-ajax.php

class-lp-admin-ajax.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.9.1, at inc/admin/class-lp-admin-ajax.php

713 lines 20.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class LP_Admin_Ajax
5 *
6 * @author ThimPress
7 * @package LearnPress/Classes
8 * @version 3.0.0
9 */
10
11 /**
12 * Prevent loading this file directly
13 */
14
15 use LearnPress\Helpers\Template;
16
17 defined( 'ABSPATH' ) || exit();
18
19 if ( ! class_exists( 'LP_Admin_Ajax' ) ) {
20
21 /**
22 * Class LP_Admin_Ajax
23 */
24 class LP_Admin_Ajax {
25 public function __construct() {
26 }
27
28 /**
29 * Add action ajax
30 */
31 public static function init() {
32 if ( ! is_user_logged_in() ) {
33 return;
34 }
35
36 $ajax_events = array(
37 'create_page' => false, // Use create new page on Settings
38 //'load_chart' => false,
39 'search_course_category' => false,
40 //'custom_stats' => false,
41 'get_page_permalink' => false,
42 );
43
44 foreach ( $ajax_events as $ajax_event => $nopriv ) {
45 add_action( 'wp_ajax_learnpress_' . $ajax_event, array( __CLASS__, $ajax_event ) );
46
47 // enable for non-logged in users
48 if ( $nopriv ) {
49 add_action( 'wp_ajax_nopriv_learnpress_' . $ajax_event, array( __CLASS__, $ajax_event ) );
50 }
51 }
52
53 do_action( 'learn-press/ajax/admin-load', __CLASS__ );
54
55 $ajax_events = array(
56 'search_items' => 'modal_search_items',
57 'update-payment-order',
58 // Update ordering of payments when user changing.
59 'update-payment-status',
60 // Enable type payment
61
62 // admin editor
63 'admin_course_editor',
64 'admin_quiz_editor',
65 'admin_question_editor',
66 'duplicator',
67 // Duplicate course, lesson, quiz, question.
68 'modal_search_items',
69 // Used to search courses on LP Order
70 //'modal_search_users',
71 // Used to search users on LP Order
72 'add_items_to_order',
73 // Used to add courses on LP Order
74 'remove_items_from_order',
75 // Used to remove items from LP Order
76 'update_email_status',
77 // Use for enable email on LP Settings
78 'search-authors',
79 // Used to search username on input some page (list courses, lp orders, quizzes, questions... on the Backend
80 //'skip-notice-install',
81 );
82
83 foreach ( $ajax_events as $action => $callback ) {
84 if ( is_numeric( $action ) ) {
85 $action = $callback;
86 }
87
88 $actions = LP_Request::parse_action( $action );
89 $method = $actions['action'];
90
91 if ( ! is_callable( $callback ) ) {
92 $method = preg_replace( '/-/', '_', $method );
93 $callback = array( __CLASS__, $method );
94 }
95
96 LP_Request::register_ajax( $action, $callback );
97 }
98 }
99
100 /**
101 * Search user on some pages on the Backend
102 */
103 public static function search_authors() {
104 $args = array(
105 'orderby' => 'name',
106 'order' => 'ASC',
107 'search' => sprintf( '*%s*', esc_attr( LP_Request::get_string( 'term' ) ) ),
108 'search_columns' => array( 'user_login', 'user_email' ),
109 );
110 $q = new WP_User_Query( $args );
111 $users = array();
112
113 $results = $q->get_results();
114
115 if ( $results ) {
116 foreach ( $results as $result ) {
117 $users[] = array(
118 'id' => $result->ID,
119 'text' => learn_press_get_profile_display_name( $result->ID ),
120 );
121 }
122 }
123 echo json_encode(
124 array(
125 'results' => $users,
126 )
127 );
128 die();
129 }
130
131 /**
132 * Handle ajax admin course editor.
133 *
134 * @since 3.0.0
135 */
136 public static function admin_course_editor() {
137 $editor = LP_Admin_Editor::get_editor_course();
138 self::admin_editor( $editor );
139 }
140
141 /**
142 * Handle ajax admin question editor.
143 *
144 * @since 3.0.0
145 */
146 public static function admin_question_editor() {
147 $editor = LP_Admin_Editor::get_editor_question();
148 self::admin_editor( $editor );
149 }
150
151 /**
152 * Handle ajax admin quiz editor.
153 *
154 * @since 3.0.0
155 */
156 public static function admin_quiz_editor() {
157 $editor = LP_Admin_Editor::get_editor_quiz();
158 self::admin_editor( $editor );
159 }
160
161 /**
162 * @param LP_Admin_Editor $editor
163 *
164 * @since 3.0.2
165 */
166 public static function admin_editor( &$editor ) {
167 $result = $editor->dispatch();
168
169 if ( is_wp_error( $result ) ) {
170 learn_press_send_json_error( $result->get_error_message() );
171 } elseif ( ! $result ) {
172 learn_press_send_json_error();
173 }
174
175 learn_press_send_json_success( $result );
176 }
177
178 /**
179 * Duplicate course, lesson, quiz, question.
180 *
181 * @since 3.0.0
182 *
183 * @note tungnx checked has use
184 */
185 public static function duplicator() {
186 $nonce = LP_Request::get_param( 'nonce' );
187 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
188 learn_press_send_json_error( __( 'Nonce is invalid!', 'learnpress' ) );
189 }
190
191 $post_id = intval( $_GET['id'] ?? 0 );
192 $post_type = learn_press_get_post_type( $post_id );
193
194 if ( ! $post_id ) {
195 learn_press_send_json_error( __( 'Oops! ID not found', 'learnpress' ) );
196 } else {
197 $can_duplicate = apply_filters( 'learn-press/can-duplicate-course', true, $post_id, $post_type );
198 if ( ! current_user_can( ADMIN_ROLE ) ) {
199 $post_author = get_post_field( 'post_author', $post_id );
200 if ( get_current_user_id() != $post_author ) {
201 $can_duplicate = false;
202 }
203 }
204
205 if ( ! $can_duplicate ) {
206 learn_press_send_json_error( __( 'You cannot duplicate this item.', 'learnpress' ) );
207 }
208
209 $new_item_id = '';
210 $duplicate_args = apply_filters( 'learn-press/duplicate-post-args', array( 'post_status' => 'publish' ) );
211
212 switch ( $post_type ) {
213 case LP_COURSE_CPT:
214 $curd = new LP_Course_CURD();
215 $new_item_id = $curd->duplicate(
216 $post_id,
217 array(
218 'exclude_meta' => array(
219 'order-pending',
220 'order-processing',
221 'order-completed',
222 'order-cancelled',
223 'order-failed',
224 'count_enrolled_users',
225 '_lp_sample_data',
226 '_lp_retake_count',
227 ),
228 )
229 );
230 break;
231 case LP_LESSON_CPT:
232 $curd = new LP_Lesson_CURD();
233 $new_item_id = $curd->duplicate( $post_id, $duplicate_args );
234 break;
235 case LP_QUIZ_CPT:
236 $curd = new LP_Quiz_CURD();
237 $new_item_id = $curd->duplicate( $post_id, $duplicate_args );
238 break;
239 case LP_QUESTION_CPT:
240 $curd = new LP_Question_CURD();
241 $new_item_id = $curd->duplicate( $post_id, $duplicate_args );
242 break;
243 default:
244 break;
245 }
246
247 if ( is_wp_error( $new_item_id ) ) {
248 learn_press_send_json_error( __( 'Duplicate post failed. Please try again', 'learnpress' ) );
249 } else {
250 learn_press_send_json_success( admin_url( 'post.php?post=' . $new_item_id . '&action=edit' ) );
251 }
252 }
253 }
254
255 /**
256 * Update ordering of payments when user changing.
257 *
258 * @since 3.0.0
259 * @version 1.0.1
260 * @note tungnx checked has use
261 */
262 public static function update_payment_order() {
263 if ( ! current_user_can( ADMIN_ROLE ) ) { // Fix security.
264 return;
265 }
266
267 $nonce = LP_Request::get_param( 'nonce' );
268 if ( ! wp_verify_nonce( $nonce, 'lp-settings' ) ) {
269 die( 'Nonce is invalid!' );
270 }
271
272 $payment_order = learn_press_get_request( 'order' );
273 update_option( 'learn_press_payment_order', $payment_order );
274
275 die( 'Order of Payment Gateway is updated success' );
276 }
277
278 /**
279 * Enable type payment
280 *
281 * @since 3.0.0
282 * @version 1.0.1
283 * @note tungnx checked has use
284 */
285 public static function update_payment_status() {
286 $payment_id = LP_Request::get_param( 'id' );
287 $status = LP_Request::get_param( 'status' );
288 $payment = LP_Gateways::instance()->get_gateway( $payment_id );
289
290 if ( ! $payment ) {
291 return;
292 }
293
294 if ( ! current_user_can( ADMIN_ROLE ) ) { // Fix security.
295 return;
296 }
297
298 $nonce = LP_Request::get_param( 'nonce' );
299 if ( ! wp_verify_nonce( $nonce, 'lp-settings' ) ) {
300 die( 'Nonce is invalid!' );
301 }
302
303 $response[ $payment->id ] = $payment->enable( $status == 'yes' );
304
305 $lp_settings_cache = new LP_Settings_Cache( true );
306 $lp_settings_cache->clean_lp_settings();
307
308 learn_press_send_json( $response );
309 }
310
311 /**
312 * nable email on LP Settings
313 *
314 * @since 3.0.0
315 * @note tungnnx checked has use
316 */
317 public static function update_email_status() {
318 $email_id = LP_Request::get_string( 'id' );
319 $status = LP_Request::get_string( 'status' );
320 $response = array();
321
322 if ( ! current_user_can( ADMIN_ROLE ) ) { // Fix security.
323 return;
324 }
325
326 $nonce = LP_Request::get_param( 'nonce' );
327 if ( ! wp_verify_nonce( $nonce, 'lp-settings' ) ) {
328 die( 'Nonce is invalid!' );
329 }
330
331 if ( $email_id ) {
332
333 $email = LP_Emails::get_email( $email_id );
334 if ( ! $email ) {
335 return;
336 }
337
338 $response[ $email->id ] = $email->enable( $status == 'yes' );
339 } else {
340 $emails = LP_Emails::instance()->emails;
341 foreach ( $emails as $email ) {
342 $response[ $email->id ] = $email->enable( $status == 'yes' );
343 }
344 }
345
346 $lp_settings_cache = new LP_Settings_Cache( true );
347 $lp_settings_cache->clean_lp_settings();
348
349 learn_press_send_json( $response );
350 }
351
352 /**
353 * Search items by requesting params.
354 */
355 public static function modal_search_items() {
356 $term = LP_Request::get_param( 'term' );
357 $type = LP_Request::get_param( 'type' );
358 $context = LP_Request::get_param( 'context' );
359 $context_id = LP_Request::get_param( 'context_id' );
360 $paged = LP_Request::get_param( 'paged' );
361 $exclude = LP_Request::get_param( 'exclude' );
362
363 if ( ! current_user_can( ADMIN_ROLE ) ) { // Fix security
364 $roles_accept = apply_filters( 'lp/backend/roles/can-search-items', [ ADMIN_ROLE ] );
365
366 $flag = false;
367 foreach ( $roles_accept as $role ) {
368 if ( current_user_can( $role ) ) {
369 $flag = true;
370 }
371 }
372
373 if ( ! $flag ) {
374 return;
375 }
376 }
377
378 $nonce = LP_Request::get_param( 'nonce' );
379 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
380 die( 'Nonce is invalid!' );
381 }
382
383 $search = new LP_Modal_Search_Items( compact( 'term', 'type', 'context', 'context_id', 'paged', 'exclude' ) );
384
385 learn_press_send_json(
386 array(
387 'html' => $search->get_html_items(),
388 'nav' => $search->get_pagination(),
389 'items' => $search->get_items(),
390 )
391 );
392 }
393
394 /**
395 * Search items by requesting params.
396 *
397 * @note tungnx checked has use
398 * @deprecated 4.2.6.9.3
399 */
400 /*public static function modal_search_users() {
401 $term = LP_Request::get_param( 'term' );
402 $type = LP_Request::get_param( 'type' );
403 $context = LP_Request::get_param( 'context' );
404 $context_id = LP_Request::get_param( 'context_id' );
405 $paged = LP_Request::get_param( 'paged' );
406 $multiple = LP_Request::get_param( 'multiple' ) == 'yes';
407 $text_format = LP_Request::get_param( 'text_format' );
408 $exclude = LP_Request::get_param( 'exclude' );
409 $roles_accept = apply_filters(
410 'lp/backend/roles/can-search-users',
411 [ ADMIN_ROLE ]
412 );
413
414 $flag = false;
415 foreach ( $roles_accept as $role ) {
416 if ( current_user_can( $role ) ) {
417 $flag = true;
418 }
419 }
420
421 if ( ! $flag ) {
422 return;
423 }
424
425 $nonce = LP_Request::get_param( 'nonce' );
426 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
427 die( 'Nonce is invalid!' );
428 }
429
430 $search = new LP_Modal_Search_Users( compact( 'term', 'type', 'context', 'context_id', 'paged', 'multiple', 'text_format', 'exclude' ) );
431
432 learn_press_send_json(
433 array(
434 'html' => $search->get_html_items(),
435 'nav' => $search->get_pagination(),
436 'users' => $search->get_items(),
437 )
438 );
439 }*/
440
441 /**
442 * Search course category.
443 */
444 public static function search_course_category() {
445 global $wpdb;
446 $sql = 'SELECT `t`.`term_id` as `id`, '
447 . ' `t`.`name` `text` '
448 . " FROM {$wpdb->terms} t "
449 . " INNER JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id AND taxonomy='course_category' "
450 . ' WHERE `t`.`name` LIKE %s';
451 $s = '%' . filter_input( INPUT_GET, 'q' ) . '%';
452 $query = $wpdb->prepare( $sql, $s );
453 $items = $wpdb->get_results( $query );
454 $data = array( 'items' => $items );
455 echo json_encode( $data );
456 exit();
457 }
458
459 /**
460 * Remove an item from lp order
461 *
462 * @note tungnx checked has use
463 */
464 public static function remove_items_from_order() {
465 $response = new LP_REST_Response();
466
467 // ensure that user has permission
468 if ( ! current_user_can( 'edit_lp_orders' ) ) {
469 die( __( 'Access denied', 'learnpress' ) );
470 }
471
472 // verify nonce
473 $nonce = LP_Request::get_param( 'nonce' );
474 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
475 die( __( 'Nonce check failed', 'learnpress' ) );
476 }
477
478 // validate order
479 $order_id = LP_Request::get_param( 'order_id', 0, 'int' );
480 if ( learn_press_get_post_type( $order_id ) != 'lp_order' ) {
481 die( __( 'Invalid order', 'learnpress' ) );
482 }
483
484 // validate item
485 $item_ids_str = LP_Request::get_param( 'items', '' );
486 if ( empty( $item_ids_str ) ) {
487 die( __( 'Invalid item', 'learnpress' ) );
488 }
489
490 $item_ids = array_map( 'absint', explode( ',', $item_ids_str ) );
491 $order = learn_press_get_order( $order_id );
492
493 foreach ( $item_ids as $item_id ) {
494 $order->remove_item( $item_id );
495 }
496
497 $order_data = learn_press_update_order_items( $order_id );
498 $currency_symbol = learn_press_get_currency_symbol( $order_data['currency'] );
499 $order_data['subtotal_html'] = learn_press_format_price( $order_data['subtotal'], $currency_symbol );
500 $order_data['total_html'] = learn_press_format_price( $order_data['total'], $currency_symbol );
501 $order_items = $order->get_items();
502 $html = '';
503 if ( $order_items ) {
504 foreach ( $order_items as $item ) {
505 ob_start();
506 include learn_press_get_admin_view( 'meta-boxes/order/order-item.php' );
507 $html .= ob_get_clean();
508 }
509 }
510
511 $response->status = 'success';
512 $response->data->item_html = $html;
513 $response->data->order_data = $order_data;
514
515 wp_send_json( $response );
516 }
517
518 /**
519 * Add courses to order
520 *
521 * @note tungnx checked has use
522 */
523 public static function add_items_to_order() {
524 $response = new LP_REST_Response();
525
526 $roles_accept = apply_filters( 'lp/backend/roles/can-add-items', [ ADMIN_ROLE ] );
527
528 $flag = false;
529 foreach ( $roles_accept as $role ) {
530 if ( current_user_can( $role ) ) {
531 $flag = true;
532 }
533 }
534
535 if ( ! $flag ) {
536 return;
537 }
538
539 $nonce = LP_Request::get_param( 'nonce' );
540 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
541 die( 'Nonce is invalid!' );
542 }
543
544 // ensure that user has permission
545 if ( ! current_user_can( 'edit_lp_orders' ) ) {
546 die( __( 'Permission denied', 'learnpress' ) );
547 }
548
549 // validate order
550 $order_id = LP_Request::get_param( 'order_id', 0 );
551 if ( ! is_numeric( $order_id ) || learn_press_get_post_type( $order_id ) != 'lp_order' ) {
552 die( __( 'Invalid order', 'learnpress' ) );
553 }
554
555 // validate item
556 $item_ids_str = LP_Request::get_param( 'items', '' );
557 if ( empty( $item_ids_str ) ) {
558 die( __( 'Invalid item', 'learnpress' ) );
559 }
560
561 $item_ids = array_map( 'absint', explode( ',', $item_ids_str ) );
562 $order = learn_press_get_order( $order_id );
563 $order_item = $order->add_items( $item_ids );
564 if ( $order_item ) {
565 $html = '';
566 $order_items = $order->get_items();
567 $order_data = learn_press_update_order_items( $order_id );
568 $currency_symbol = learn_press_get_currency_symbol( $order_data['currency'] );
569 $order_data['subtotal_html'] = learn_press_format_price( $order_data['subtotal'], $currency_symbol );
570 $order_data['total_html'] = learn_press_format_price( $order_data['total'], $currency_symbol );
571
572 if ( $order_items ) {
573 foreach ( $order_items as $item ) {
574 if ( ! in_array( $item['id'], $order_item ) ) {
575 continue;
576 }
577
578 ob_start();
579 Template::instance()->get_admin_template( 'meta-boxes/order/order-item.php', compact( 'item', 'order' ) );
580 $html .= ob_get_clean();
581 }
582 }
583
584 $response->status = 'success';
585 $response->data->item_html = $html;
586 $response->data->order_data = $order_data;
587 }
588
589 wp_send_json( $response );
590 }
591
592 /*public static function load_chart() {
593 if ( ! class_exists( 'LP_Submenu_Statistics' ) ) {
594 $statistic = include_once LP_PLUGIN_PATH . '/inc/admin/sub-menus/class-lp-submenu-statistics.php';
595 } else {
596 $statistic = new LP_Submenu_Statistics();
597 }
598 $statistic->load_chart();
599 }*/
600
601 public static function json_search_customer_name( $query ) {
602 global $wpdb;
603
604 $term = LP_Helper::sanitize_params_submitted( $_REQUEST['term'] );
605 if ( method_exists( $wpdb, 'esc_like' ) ) {
606 $term = $wpdb->esc_like( $term );
607 } else {
608 $term = $wpdb->esc_like( $term );
609 }
610
611 $query->query_from .= " INNER JOIN {$wpdb->usermeta} AS user_name ON {$wpdb->users}.ID = user_name.user_id AND ( user_name.meta_key = 'first_name' OR user_name.meta_key = 'last_name' ) ";
612 $query->query_where .= $wpdb->prepare( ' OR user_name.meta_value LIKE %s ', '%' . $term . '%' );
613 }
614
615 /**
616 * create new page on LP Settings
617 *
618 * @note tungnnx checked use
619 */
620 public static function create_page() {
621 $response = array(
622 'code' => 0,
623 'message' => '',
624 );
625
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 * @author tungnx
635 */
636 if ( ! current_user_can( 'edit_pages' ) || empty( $_POST['page_name'] ) ) {
637 $response['message'] = 'Request invalid';
638 learn_press_send_json( $response );
639 }
640
641 // Check nonce
642 $nonce = LP_Request::get_param( 'nonce' );
643 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
644 $response['message'] = 'Request invalid';
645 learn_press_send_json( $response );
646 }
647
648 $page_name = LP_Helper::sanitize_params_submitted( $_POST['page_name'] );
649 $field_name = LP_Request::get_param( 'field_name' );
650
651 if ( $page_name ) {
652 $data_create_page = array(
653 'post_title' => $page_name,
654 );
655
656 $page_id = LP_Helper::create_page( $data_create_page, $field_name );
657
658 if ( $page_id ) {
659 $response['code'] = 1;
660 $response['message'] = 'create page success';
661 $response['page'] = get_post( $page_id );
662 $html = learn_press_pages_dropdown( '', '', array( 'echo' => false ) );
663 preg_match_all( '!value=\"([0-9]+)\"!', $html, $matches );
664 $response['positions'] = $matches[1];
665 $response['html'] = '<a href="' . get_edit_post_link( $page_id ) . '" target="_blank">' . __( 'Edit Page', 'learnpress' ) . '</a>&nbsp;';
666 $response['html'] .= '<a href="' . get_permalink( $page_id ) . '" target="_blank">' . __( 'View Page', 'learnpress' ) . '</a>';
667 } else {
668 $response['error'] = __( 'Error! Page creation failed. Please try again.', 'learnpress' );
669 }
670 } else {
671 $response['error'] = __( 'Empty page name!', 'learnpress' );
672 }
673 learn_press_send_json( $response );
674 }
675
676 /**
677 * Get edit|view link of a page
678 */
679 public static function get_page_permalink() {
680 $page_id = (int) $_REQUEST['page_id'] ?? 0;
681 ?>
682
683 <a href="<?php echo get_edit_post_link( $page_id ); ?>"
684 target="_blank"><?php _e( 'Edit Page', 'learnpress' ); ?></a>
685 <a href="<?php echo get_permalink( $page_id ); ?>"
686 target="_blank"><?php _e( 'View Page', 'learnpress' ); ?></a>
687
688 <?php
689 die();
690 }
691
692 /**
693 * Get date from, to for static chart
694 *
695 * @deprecated 4.2.6.9.3
696 */
697 /*public static function custom_stats() {
698 $from = LP_Helper::sanitize_params_submitted( $_REQUEST['from'] ?? 0 );
699 $to = LP_Helper::sanitize_params_submitted( $_REQUEST['to'] ?? 0 );
700 $date_diff = strtotime( $to ) - strtotime( $from );
701 if ( $date_diff <= 0 || $from == 0 || $to == 0 ) {
702 die();
703 }
704 learn_press_process_chart( learn_press_get_chart_students( $to, 'days', floor( $date_diff / ( 60 * 60 * 24 ) ) + 1 ) );
705 die();
706 }*/
707 }
708
709 add_action( 'init', array( 'LP_Admin_Ajax', 'init' ) );
710 }
711
712 new LP_Admin_Ajax();
713