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

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