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
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.4.8, at inc/admin/class-lp-admin-ajax.php

709 lines 19.1 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\Response;
16 use LearnPress\Helpers\Template;
17 use LearnPress\Models\UserModel;
18
19 defined( 'ABSPATH' ) || exit();
20
21 if ( ! class_exists( 'LP_Admin_Ajax' ) ) {
22
23 /**
24 * Class LP_Admin_Ajax
25 */
26 class LP_Admin_Ajax {
27 public function __construct() {
28 }
29
30 /**
31 * Add action ajax
32 */
33 public static function init() {
34 if ( ! is_user_logged_in() ) {
35 return;
36 }
37
38 $ajax_events = array(
39 'create_page' => false, // Use create new page on Settings
40 //'load_chart' => false,
41 'search_course_category' => false,
42 //'custom_stats' => false,
43 'get_page_permalink' => false,
44 );
45
46 foreach ( $ajax_events as $ajax_event => $nopriv ) {
47 add_action( 'wp_ajax_learnpress_' . $ajax_event, array( __CLASS__, $ajax_event ) );
48
49 // enable for non-logged in users
50 if ( $nopriv ) {
51 add_action( 'wp_ajax_nopriv_learnpress_' . $ajax_event, array( __CLASS__, $ajax_event ) );
52 }
53 }
54
55 do_action( 'learn-press/ajax/admin-load', __CLASS__ );
56
57 $ajax_events = array(
58 'search_items' => 'modal_search_items',
59 'update-payment-order',
60 // Update ordering of payments when user changing.
61 'update-payment-status',
62 // Enable type payment
63
64 // admin editor
65 'admin_course_editor',
66 'admin_quiz_editor',
67 'admin_question_editor',
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',
83 );
84
85 foreach ( $ajax_events as $action => $callback ) {
86 if ( is_numeric( $action ) ) {
87 $action = $callback;
88 }
89
90 $actions = LP_Request::parse_action( $action );
91 $method = $actions['action'];
92
93 if ( ! is_callable( $callback ) ) {
94 $method = preg_replace( '/-/', '_', $method );
95 $callback = array( __CLASS__, $method );
96 }
97
98 LP_Request::register_ajax( $action, $callback );
99 }
100 }
101
102 /**
103 * Search user on some pages on the Backend
104 */
105 public static function search_authors() {
106 $args = array(
107 'orderby' => 'name',
108 'order' => 'ASC',
109 'search' => sprintf( '*%s*', esc_attr( LP_Request::get_string( 'term' ) ) ),
110 'search_columns' => array( 'user_login', 'user_email' ),
111 );
112 $q = new WP_User_Query( $args );
113 $users = array();
114
115 $results = $q->get_results();
116
117 if ( $results ) {
118 foreach ( $results as $result ) {
119 $users[] = array(
120 'id' => $result->ID,
121 'text' => learn_press_get_profile_display_name( $result->ID ),
122 );
123 }
124 }
125 echo json_encode(
126 array(
127 'results' => $users,
128 )
129 );
130 die();
131 }
132
133 /**
134 * Handle ajax admin course editor.
135 *
136 * @since 3.0.0
137 */
138 public static function admin_course_editor() {
139 $editor = LP_Admin_Editor::get_editor_course();
140 self::admin_editor( $editor );
141 }
142
143 /**
144 * Handle ajax admin question editor.
145 *
146 * @since 3.0.0
147 */
148 public static function admin_question_editor() {
149 $editor = LP_Admin_Editor::get_editor_question();
150 self::admin_editor( $editor );
151 }
152
153 /**
154 * Handle ajax admin quiz editor.
155 *
156 * @since 3.0.0
157 */
158 public static function admin_quiz_editor() {
159 $editor = LP_Admin_Editor::get_editor_quiz();
160 self::admin_editor( $editor );
161 }
162
163 /**
164 * @param LP_Admin_Editor $editor
165 *
166 * @since 3.0.2
167 */
168 public static function admin_editor( &$editor ) {
169 $result = $editor->dispatch();
170
171 if ( is_wp_error( $result ) ) {
172 learn_press_send_json_error( $result->get_error_message() );
173 } elseif ( ! $result ) {
174 learn_press_send_json_error();
175 }
176
177 learn_press_send_json_success( $result );
178 }
179
180 /**
181 * Duplicate course, lesson, quiz, question.
182 *
183 * @since 3.0.0
184 *
185 * @note tungnx checked has use
186 */
187 public static function duplicator() {
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 }
192
193 $post_id = intval( $_GET['id'] ?? 0 );
194 $post_type = learn_press_get_post_type( $post_id );
195
196 if ( ! $post_id ) {
197 learn_press_send_json_error( __( 'Oops! ID not found', 'learnpress' ) );
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 }
206
207 if ( ! $can_duplicate ) {
208 learn_press_send_json_error( __( 'You cannot duplicate this item.', 'learnpress' ) );
209 }
210
211 $new_item_id = '';
212 $duplicate_args = apply_filters( 'learn-press/duplicate-post-args', array( 'post_status' => 'publish' ) );
213
214 switch ( $post_type ) {
215 case LP_COURSE_CPT:
216 $curd = new LP_Course_CURD();
217 $new_item_id = $curd->duplicate(
218 $post_id,
219 array(
220 'exclude_meta' => array(
221 'order-pending',
222 'order-processing',
223 'order-completed',
224 'order-cancelled',
225 'order-failed',
226 'count_enrolled_users',
227 '_lp_sample_data',
228 '_lp_retake_count',
229 ),
230 )
231 );
232 break;
233 case LP_LESSON_CPT:
234 $curd = new LP_Lesson_CURD();
235 $new_item_id = $curd->duplicate( $post_id, $duplicate_args );
236 break;
237 case LP_QUIZ_CPT:
238 $curd = new LP_Quiz_CURD();
239 $new_item_id = $curd->duplicate( $post_id, $duplicate_args );
240 break;
241 case LP_QUESTION_CPT:
242 $curd = new LP_Question_CURD();
243 $new_item_id = $curd->duplicate( $post_id, $duplicate_args );
244 break;
245 default:
246 break;
247 }
248
249 if ( is_wp_error( $new_item_id ) ) {
250 learn_press_send_json_error( __( 'Duplicate post failed. Please try again', 'learnpress' ) );
251 } else {
252 learn_press_send_json_success( admin_url( 'post.php?post=' . $new_item_id . '&action=edit' ) );
253 }
254 }
255 }
256
257 /**
258 * Update ordering of payments when user changing.
259 *
260 * @since 3.0.0
261 * @version 1.0.1
262 * @note tungnx checked has use
263 */
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
274 $payment_order = learn_press_get_request( 'order' );
275 update_option( 'learn_press_payment_order', $payment_order );
276
277 die( 'Order of Payment Gateway is updated success' );
278 }
279
280 /**
281 * Enable type payment
282 *
283 * @since 3.0.0
284 * @version 1.0.1
285 * @note tungnx checked has use
286 */
287 public static function update_payment_status() {
288 $payment_id = LP_Request::get_param( 'id' );
289 $status = LP_Request::get_param( 'status' );
290 $payment = LP_Gateways::instance()->get_gateway( $payment_id );
291
292 if ( ! $payment ) {
293 return;
294 }
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
305 $response[ $payment->id ] = $payment->enable( $status == 'yes' );
306
307 $lp_settings_cache = new LP_Settings_Cache( true );
308 $lp_settings_cache->clean_lp_settings();
309
310 learn_press_send_json( $response );
311 }
312
313 /**
314 * nable email on LP Settings
315 *
316 * @since 3.0.0
317 * @note tungnnx checked has use
318 */
319 public static function update_email_status() {
320 $email_id = LP_Request::get_string( 'id' );
321 $status = LP_Request::get_string( 'status' );
322 $response = array();
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
333 if ( $email_id ) {
334
335 $email = LP_Emails::get_email( $email_id );
336 if ( ! $email ) {
337 return;
338 }
339
340 $response[ $email->id ] = $email->enable( $status == 'yes' );
341 } else {
342 $emails = LP_Emails::instance()->emails;
343 foreach ( $emails as $email ) {
344 $response[ $email->id ] = $email->enable( $status == 'yes' );
345 }
346 }
347
348 $lp_settings_cache = new LP_Settings_Cache( true );
349 $lp_settings_cache->clean_lp_settings();
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_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' );
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
385 $search = new LP_Modal_Search_Items( compact( 'term', 'type', 'context', 'context_id', 'paged', 'exclude' ) );
386
387 learn_press_send_json(
388 array(
389 'html' => $search->get_html_items(),
390 'nav' => $search->get_pagination(),
391 'items' => $search->get_items(),
392 )
393 );
394 }
395
396 /**
397 * Search items by requesting params.
398 *
399 * @note tungnx checked has use
400 * @deprecated 4.2.6.9.3
401 */
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 );
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
432 $search = new LP_Modal_Search_Users( compact( 'term', 'type', 'context', 'context_id', 'paged', 'multiple', 'text_format', 'exclude' ) );
433
434 learn_press_send_json(
435 array(
436 'html' => $search->get_html_items(),
437 'nav' => $search->get_pagination(),
438 'users' => $search->get_items(),
439 )
440 );
441 }*/
442
443 /**
444 * Search course category.
445 */
446 public static function search_course_category() {
447 global $wpdb;
448 $sql = 'SELECT `t`.`term_id` as `id`, '
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';
453 $s = '%' . filter_input( INPUT_GET, 'q' ) . '%';
454 $query = $wpdb->prepare( $sql, $s );
455 $items = $wpdb->get_results( $query );
456 $data = array( 'items' => $items );
457 echo json_encode( $data );
458 exit();
459 }
460
461 /**
462 * Remove an item from lp order
463 *
464 * @note tungnx checked has use
465 */
466 public static function remove_items_from_order() {
467 $response = new LP_REST_Response();
468
469 // ensure that user has permission
470 if ( ! current_user_can( 'edit_lp_orders' ) ) {
471 die( __( 'Access denied', 'learnpress' ) );
472 }
473
474 // verify nonce
475 $nonce = LP_Request::get_param( 'nonce' );
476 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
477 die( __( 'Nonce check failed', 'learnpress' ) );
478 }
479
480 // validate order
481 $order_id = LP_Request::get_param( 'order_id', 0, 'int' );
482 if ( learn_press_get_post_type( $order_id ) != 'lp_order' ) {
483 die( __( 'Invalid order', 'learnpress' ) );
484 }
485
486 // validate item
487 $item_ids_str = LP_Request::get_param( 'items', '' );
488 if ( empty( $item_ids_str ) ) {
489 die( __( 'Invalid item', 'learnpress' ) );
490 }
491
492 $item_ids = array_map( 'absint', explode( ',', $item_ids_str ) );
493 $order = learn_press_get_order( $order_id );
494
495 foreach ( $item_ids as $item_id ) {
496 $order->remove_item( $item_id );
497 }
498
499 $order_data = learn_press_update_order_items( $order_id );
500 $currency_symbol = learn_press_get_currency_symbol( $order_data['currency'] );
501 $order_data['subtotal_html'] = learn_press_format_price( $order_data['subtotal'], $currency_symbol );
502 $order_data['total_html'] = learn_press_format_price( $order_data['total'], $currency_symbol );
503 $order_items = $order->get_items();
504 $html = '';
505 if ( $order_items ) {
506 foreach ( $order_items as $item ) {
507 ob_start();
508 include learn_press_get_admin_view( 'meta-boxes/order/order-item.php' );
509 $html .= ob_get_clean();
510 }
511 }
512
513 $response->status = 'success';
514 $response->data->item_html = $html;
515 $response->data->order_data = $order_data;
516
517 wp_send_json( $response );
518 }
519
520 /**
521 * Add courses to order
522 *
523 * @note tungnx checked has use
524 */
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
546 // ensure that user has permission
547 if ( ! current_user_can( 'edit_lp_orders' ) ) {
548 die( __( 'Permission denied', 'learnpress' ) );
549 }
550
551 // validate order
552 $order_id = LP_Request::get_param( 'order_id', 0 );
553 if ( ! is_numeric( $order_id ) || learn_press_get_post_type( $order_id ) != 'lp_order' ) {
554 die( __( 'Invalid order', 'learnpress' ) );
555 }
556
557 // validate item
558 $item_ids_str = LP_Request::get_param( 'items', '' );
559 if ( empty( $item_ids_str ) ) {
560 die( __( 'Invalid item', 'learnpress' ) );
561 }
562
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();
569 $order_data = learn_press_update_order_items( $order_id );
570 $currency_symbol = learn_press_get_currency_symbol( $order_data['currency'] );
571 $order_data['subtotal_html'] = learn_press_format_price( $order_data['subtotal'], $currency_symbol );
572 $order_data['total_html'] = learn_press_format_price( $order_data['total'], $currency_symbol );
573
574 if ( $order_items ) {
575 foreach ( $order_items as $item ) {
576 if ( ! in_array( $item['id'], $order_item ) ) {
577 continue;
578 }
579
580 ob_start();
581 Template::instance()->get_admin_template( 'meta-boxes/order/order-item.php', compact( 'item', 'order' ) );
582 $html .= ob_get_clean();
583 }
584 }
585
586 $response->status = 'success';
587 $response->data->item_html = $html;
588 $response->data->order_data = $order_data;
589 }
590
591 wp_send_json( $response );
592 }
593
594 /*public static function load_chart() {
595 if ( ! class_exists( 'LP_Submenu_Statistics' ) ) {
596 $statistic = include_once LP_PLUGIN_PATH . '/inc/admin/sub-menus/class-lp-submenu-statistics.php';
597 } else {
598 $statistic = new LP_Submenu_Statistics();
599 }
600 $statistic->load_chart();
601 }*/
602
603 public static function json_search_customer_name( $query ) {
604 global $wpdb;
605
606 $term = LP_Helper::sanitize_params_submitted( $_REQUEST['term'] );
607 if ( method_exists( $wpdb, 'esc_like' ) ) {
608 $term = $wpdb->esc_like( $term );
609 } else {
610 $term = $wpdb->esc_like( $term );
611 }
612
613 $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' ) ";
614 $query->query_where .= $wpdb->prepare( ' OR user_name.meta_value LIKE %s ', '%' . $term . '%' );
615 }
616
617 /**
618 * create new page on LP Settings
619 *
620 * @note tungnnx checked use
621 */
622 public static function create_page() {
623 $response = new Response();
624
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 }
639
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 }
645
646 $page_name = LP_Helper::sanitize_params_submitted( $_POST['page_name'] );
647 $field_name = LP_Request::get_param( 'field_name' );
648
649 if ( ! $page_name ) {
650 throw new Exception( __( 'Empty page name!', 'learnpress' ) );
651 }
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();
667 }
668
669 wp_send_json( $response );
670 }
671
672 /**
673 * Get edit|view link of a page
674 */
675 public static function get_page_permalink() {
676 $page_id = (int) $_REQUEST['page_id'] ?? 0;
677 ?>
678
679 <a href="<?php echo get_edit_post_link( $page_id ); ?>"
680 target="_blank"><?php _e( 'Edit Page', 'learnpress' ); ?></a>
681 <a href="<?php echo get_permalink( $page_id ); ?>"
682 target="_blank"><?php _e( 'View Page', 'learnpress' ); ?></a>
683
684 <?php
685 die();
686 }
687
688 /**
689 * Get date from, to for static chart
690 *
691 * @deprecated 4.2.6.9.3
692 */
693 /*public static function custom_stats() {
694 $from = LP_Helper::sanitize_params_submitted( $_REQUEST['from'] ?? 0 );
695 $to = LP_Helper::sanitize_params_submitted( $_REQUEST['to'] ?? 0 );
696 $date_diff = strtotime( $to ) - strtotime( $from );
697 if ( $date_diff <= 0 || $from == 0 || $to == 0 ) {
698 die();
699 }
700 learn_press_process_chart( learn_press_get_chart_students( $to, 'days', floor( $date_diff / ( 60 * 60 * 24 ) ) + 1 ) );
701 die();
702 }*/
703 }
704
705 add_action( 'init', array( 'LP_Admin_Ajax', 'init' ) );
706 }
707
708 new LP_Admin_Ajax();
709