| @@ -1,712 +1,708 @@ | ||
| 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> '; | |
| 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(); | |
| 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(); | |