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

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