| @@ -10,8 +10,13 @@ | ||
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * Prevent loading this file directly |
| 13 | 13 | */ |
| 14 | + | |
| 15 | +use LearnPress\Helpers\Response; | |
| 16 | +use LearnPress\Helpers\Template; | |
| 17 | +use LearnPress\Models\UserModel; | |
| 18 | + | |
| 14 | 19 | defined( 'ABSPATH' ) || exit(); |
| 15 | 20 | |
| 16 | 21 | if ( ! class_exists( 'LP_Admin_Ajax' ) ) { |
| 17 | 22 | |
| @@ -18,14 +23,15 @@ | ||
| 18 | 23 | /** |
| 19 | 24 | * Class LP_Admin_Ajax |
| 20 | 25 | */ |
| 21 | 26 | class LP_Admin_Ajax { |
| 27 | + public function __construct() { | |
| 28 | + } | |
| 22 | 29 | |
| 23 | 30 | /** |
| 24 | 31 | * Add action ajax |
| 25 | 32 | */ |
| 26 | 33 | public static function init() { |
| 27 | - | |
| 28 | 34 | if ( ! is_user_logged_in() ) { |
| 29 | 35 | return; |
| 30 | 36 | } |
| 31 | 37 | |
| @@ -30,28 +36,12 @@ | ||
| 30 | 36 | } |
| 31 | 37 | |
| 32 | 38 | $ajax_events = array( |
| 33 | 39 | '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, | |
| 40 | + //'load_chart' => false, | |
| 39 | 41 | 'search_course_category' => false, |
| 40 | - 'custom_stats' => false, | |
| 41 | - //'ignore_setting_up' => false, | |
| 42 | + //'custom_stats' => false, | |
| 42 | 43 | '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 | 44 | ); |
| 55 | 45 | |
| 56 | 46 | foreach ( $ajax_events as $ajax_event => $nopriv ) { |
| 57 | 47 | add_action( 'wp_ajax_learnpress_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
| @@ -65,40 +55,32 @@ | ||
| 65 | 55 | do_action( 'learn-press/ajax/admin-load', __CLASS__ ); |
| 66 | 56 | |
| 67 | 57 | $ajax_events = array( |
| 68 | 58 | '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', | |
| 59 | + 'update-payment-order', | |
| 60 | + // Update ordering of payments when user changing. | |
| 61 | + 'update-payment-status', | |
| 62 | + // Enable type payment | |
| 72 | 63 | |
| 73 | 64 | // admin editor |
| 74 | 65 | 'admin_course_editor', |
| 75 | 66 | 'admin_quiz_editor', |
| 76 | 67 | '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', | |
| 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', | |
| 101 | 83 | ); |
| 102 | 84 | |
| 103 | 85 | foreach ( $ajax_events as $action => $callback ) { |
| 104 | 86 | if ( is_numeric( $action ) ) { |
| @@ -148,15 +130,8 @@ | ||
| 148 | 130 | die(); |
| 149 | 131 | } |
| 150 | 132 | |
| 151 | 133 | /** |
| 152 | - * Hide notice install | |
| 153 | - */ | |
| 154 | - public static function skip_notice_install() { | |
| 155 | - delete_option( 'learn_press_install' ); | |
| 156 | - } | |
| 157 | - | |
| 158 | - /** | |
| 159 | 134 | * Handle ajax admin course editor. |
| 160 | 135 | * |
| 161 | 136 | * @since 3.0.0 |
| 162 | 137 | */ |
| @@ -209,19 +184,32 @@ | ||
| 209 | 184 | * |
| 210 | 185 | * @note tungnx checked has use |
| 211 | 186 | */ |
| 212 | 187 | public static function duplicator() { |
| 213 | - $post_id = intval( $_GET['id'] ?? 0 ); | |
| 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 | + } | |
| 214 | 192 | |
| 215 | - // get post type | |
| 193 | + $post_id = intval( $_GET['id'] ?? 0 ); | |
| 216 | 194 | $post_type = learn_press_get_post_type( $post_id ); |
| 217 | 195 | |
| 218 | 196 | if ( ! $post_id ) { |
| 219 | - learn_press_send_json_error( __( 'Ops! ID not found', 'learnpress' ) ); | |
| 197 | + learn_press_send_json_error( __( 'Oops! ID not found', 'learnpress' ) ); | |
| 220 | 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 | + } | |
| 221 | 206 | |
| 222 | - $new_item_id = ''; | |
| 207 | + if ( ! $can_duplicate ) { | |
| 208 | + learn_press_send_json_error( __( 'You cannot duplicate this item.', 'learnpress' ) ); | |
| 209 | + } | |
| 223 | 210 | |
| 211 | + $new_item_id = ''; | |
| 224 | 212 | $duplicate_args = apply_filters( 'learn-press/duplicate-post-args', array( 'post_status' => 'publish' ) ); |
| 225 | 213 | |
| 226 | 214 | switch ( $post_type ) { |
| 227 | 215 | case LP_COURSE_CPT: |
| @@ -258,9 +246,9 @@ | ||
| 258 | 246 | break; |
| 259 | 247 | } |
| 260 | 248 | |
| 261 | 249 | if ( is_wp_error( $new_item_id ) ) { |
| 262 | - learn_press_send_json_error( __( 'Duplicate post fail, please try again', 'learnpress' ) ); | |
| 250 | + learn_press_send_json_error( __( 'Duplicate post failed. Please try again', 'learnpress' ) ); | |
| 263 | 251 | } else { |
| 264 | 252 | learn_press_send_json_success( admin_url( 'post.php?post=' . $new_item_id . '&action=edit' ) ); |
| 265 | 253 | } |
| 266 | 254 | } |
| @@ -269,14 +257,25 @@ | ||
| 269 | 257 | /** |
| 270 | 258 | * Update ordering of payments when user changing. |
| 271 | 259 | * |
| 272 | 260 | * @since 3.0.0 |
| 273 | - * @use for sorting by type payment gateway | |
| 261 | + * @version 1.0.1 | |
| 274 | 262 | * @note tungnx checked has use |
| 275 | 263 | */ |
| 276 | 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 | + | |
| 277 | 274 | $payment_order = learn_press_get_request( 'order' ); |
| 278 | 275 | update_option( 'learn_press_payment_order', $payment_order ); |
| 276 | + | |
| 277 | + die( 'Order of Payment Gateway is updated success' ); | |
| 279 | 278 | } |
| 280 | 279 | |
| 281 | 280 | /** |
| 282 | 281 | * Enable type payment |
| @@ -281,14 +280,14 @@ | ||
| 281 | 280 | /** |
| 282 | 281 | * Enable type payment |
| 283 | 282 | * |
| 284 | 283 | * @since 3.0.0 |
| 285 | - * @use for enable type payment gateway | |
| 284 | + * @version 1.0.1 | |
| 286 | 285 | * @note tungnx checked has use |
| 287 | 286 | */ |
| 288 | 287 | public static function update_payment_status() { |
| 289 | - $payment_id = learn_press_get_request( 'id' ); | |
| 290 | - $status = LP_Request::get_string( 'status' ); | |
| 288 | + $payment_id = LP_Request::get_param( 'id' ); | |
| 289 | + $status = LP_Request::get_param( 'status' ); | |
| 291 | 290 | $payment = LP_Gateways::instance()->get_gateway( $payment_id ); |
| 292 | 291 | |
| 293 | 292 | if ( ! $payment ) { |
| 294 | 293 | return; |
| @@ -293,10 +292,22 @@ | ||
| 293 | 292 | if ( ! $payment ) { |
| 294 | 293 | return; |
| 295 | 294 | } |
| 296 | 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 | + | |
| 297 | 305 | $response[ $payment->id ] = $payment->enable( $status == 'yes' ); |
| 298 | 306 | |
| 307 | + $lp_settings_cache = new LP_Settings_Cache( true ); | |
| 308 | + $lp_settings_cache->clean_lp_settings(); | |
| 309 | + | |
| 299 | 310 | learn_press_send_json( $response ); |
| 300 | 311 | } |
| 301 | 312 | |
| 302 | 313 | /** |
| @@ -305,13 +316,21 @@ | ||
| 305 | 316 | * @since 3.0.0 |
| 306 | 317 | * @note tungnnx checked has use |
| 307 | 318 | */ |
| 308 | 319 | public static function update_email_status() { |
| 309 | - | |
| 310 | 320 | $email_id = LP_Request::get_string( 'id' ); |
| 311 | 321 | $status = LP_Request::get_string( 'status' ); |
| 312 | 322 | $response = array(); |
| 313 | 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 | + | |
| 314 | 333 | if ( $email_id ) { |
| 315 | 334 | |
| 316 | 335 | $email = LP_Emails::get_email( $email_id ); |
| 317 | 336 | if ( ! $email ) { |
| @@ -324,8 +343,12 @@ | ||
| 324 | 343 | foreach ( $emails as $email ) { |
| 325 | 344 | $response[ $email->id ] = $email->enable( $status == 'yes' ); |
| 326 | 345 | } |
| 327 | 346 | } |
| 347 | + | |
| 348 | + $lp_settings_cache = new LP_Settings_Cache( true ); | |
| 349 | + $lp_settings_cache->clean_lp_settings(); | |
| 350 | + | |
| 328 | 351 | learn_press_send_json( $response ); |
| 329 | 352 | } |
| 330 | 353 | |
| 331 | 354 | /** |
| @@ -331,15 +354,35 @@ | ||
| 331 | 354 | /** |
| 332 | 355 | * Search items by requesting params. |
| 333 | 356 | */ |
| 334 | 357 | 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' ); | |
| 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' ); | |
| 341 | 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 | + | |
| 342 | 385 | $search = new LP_Modal_Search_Items( compact( 'term', 'type', 'context', 'context_id', 'paged', 'exclude' ) ); |
| 343 | 386 | |
| 344 | 387 | learn_press_send_json( |
| 345 | 388 | array( |
| @@ -353,19 +396,40 @@ | ||
| 353 | 396 | /** |
| 354 | 397 | * Search items by requesting params. |
| 355 | 398 | * |
| 356 | 399 | * @note tungnx checked has use |
| 400 | + * @deprecated 4.2.6.9.3 | |
| 357 | 401 | */ |
| 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' ); | |
| 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 | + ); | |
| 367 | 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 | + | |
| 368 | 432 | $search = new LP_Modal_Search_Users( compact( 'term', 'type', 'context', 'context_id', 'paged', 'multiple', 'text_format', 'exclude' ) ); |
| 369 | 433 | |
| 370 | 434 | learn_press_send_json( |
| 371 | 435 | array( |
| @@ -373,9 +437,9 @@ | ||
| 373 | 437 | 'nav' => $search->get_pagination(), |
| 374 | 438 | 'users' => $search->get_items(), |
| 375 | 439 | ) |
| 376 | 440 | ); |
| 377 | - } | |
| 441 | + }*/ | |
| 378 | 442 | |
| 379 | 443 | /** |
| 380 | 444 | * Search course category. |
| 381 | 445 | */ |
| @@ -381,12 +445,12 @@ | ||
| 381 | 445 | */ |
| 382 | 446 | public static function search_course_category() { |
| 383 | 447 | global $wpdb; |
| 384 | 448 | $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'; | |
| 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'; | |
| 389 | 453 | $s = '%' . filter_input( INPUT_GET, 'q' ) . '%'; |
| 390 | 454 | $query = $wpdb->prepare( $sql, $s ); |
| 391 | 455 | $items = $wpdb->get_results( $query ); |
| 392 | 456 | $data = array( 'items' => $items ); |
| @@ -399,8 +463,10 @@ | ||
| 399 | 463 | * |
| 400 | 464 | * @note tungnx checked has use |
| 401 | 465 | */ |
| 402 | 466 | public static function remove_items_from_order() { |
| 467 | + $response = new LP_REST_Response(); | |
| 468 | + | |
| 403 | 469 | // ensure that user has permission |
| 404 | 470 | if ( ! current_user_can( 'edit_lp_orders' ) ) { |
| 405 | 471 | die( __( 'Access denied', 'learnpress' ) ); |
| 406 | 472 | } |
| @@ -405,27 +471,29 @@ | ||
| 405 | 471 | die( __( 'Access denied', 'learnpress' ) ); |
| 406 | 472 | } |
| 407 | 473 | |
| 408 | 474 | // 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' ) ); | |
| 475 | + $nonce = LP_Request::get_param( 'nonce' ); | |
| 476 | + if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) { | |
| 477 | + die( __( 'Nonce check failed', 'learnpress' ) ); | |
| 412 | 478 | } |
| 413 | 479 | |
| 414 | 480 | // 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' ) { | |
| 481 | + $order_id = LP_Request::get_param( 'order_id', 0, 'int' ); | |
| 482 | + if ( learn_press_get_post_type( $order_id ) != 'lp_order' ) { | |
| 417 | 483 | die( __( 'Invalid order', 'learnpress' ) ); |
| 418 | 484 | } |
| 419 | 485 | |
| 420 | 486 | // validate item |
| 421 | - $items = learn_press_get_request( 'items' ); | |
| 487 | + $item_ids_str = LP_Request::get_param( 'items', '' ); | |
| 488 | + if ( empty( $item_ids_str ) ) { | |
| 489 | + die( __( 'Invalid item', 'learnpress' ) ); | |
| 490 | + } | |
| 422 | 491 | |
| 423 | - $order = learn_press_get_order( $order_id ); | |
| 492 | + $item_ids = array_map( 'absint', explode( ',', $item_ids_str ) ); | |
| 493 | + $order = learn_press_get_order( $order_id ); | |
| 424 | 494 | |
| 425 | - global $wpdb; | |
| 426 | - | |
| 427 | - foreach ( $items as $item_id ) { | |
| 495 | + foreach ( $item_ids as $item_id ) { | |
| 428 | 496 | $order->remove_item( $item_id ); |
| 429 | 497 | } |
| 430 | 498 | |
| 431 | 499 | $order_data = learn_press_update_order_items( $order_id ); |
| @@ -432,10 +500,10 @@ | ||
| 432 | 500 | $currency_symbol = learn_press_get_currency_symbol( $order_data['currency'] ); |
| 433 | 501 | $order_data['subtotal_html'] = learn_press_format_price( $order_data['subtotal'], $currency_symbol ); |
| 434 | 502 | $order_data['total_html'] = learn_press_format_price( $order_data['total'], $currency_symbol ); |
| 435 | 503 | $order_items = $order->get_items(); |
| 504 | + $html = ''; | |
| 436 | 505 | if ( $order_items ) { |
| 437 | - $html = ''; | |
| 438 | 506 | foreach ( $order_items as $item ) { |
| 439 | 507 | ob_start(); |
| 440 | 508 | include learn_press_get_admin_view( 'meta-boxes/order/order-item.php' ); |
| 441 | 509 | $html .= ob_get_clean(); |
| @@ -441,15 +509,13 @@ | ||
| 441 | 509 | $html .= ob_get_clean(); |
| 442 | 510 | } |
| 443 | 511 | } |
| 444 | 512 | |
| 445 | - learn_press_send_json( | |
| 446 | - array( | |
| 447 | - 'result' => 'success', | |
| 448 | - 'item_html' => $html, | |
| 449 | - 'order_data' => $order_data, | |
| 450 | - ) | |
| 451 | - ); | |
| 513 | + $response->status = 'success'; | |
| 514 | + $response->data->item_html = $html; | |
| 515 | + $response->data->order_data = $order_data; | |
| 516 | + | |
| 517 | + wp_send_json( $response ); | |
| 452 | 518 | } |
| 453 | 519 | |
| 454 | 520 | /** |
| 455 | 521 | * Add courses to order |
| @@ -456,8 +522,28 @@ | ||
| 456 | 522 | * |
| 457 | 523 | * @note tungnx checked has use |
| 458 | 524 | */ |
| 459 | 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 | + | |
| 460 | 546 | // ensure that user has permission |
| 461 | 547 | if ( ! current_user_can( 'edit_lp_orders' ) ) { |
| 462 | 548 | die( __( 'Permission denied', 'learnpress' ) ); |
| 463 | 549 | } |
| @@ -462,27 +548,25 @@ | ||
| 462 | 548 | die( __( 'Permission denied', 'learnpress' ) ); |
| 463 | 549 | } |
| 464 | 550 | |
| 465 | 551 | // validate order |
| 466 | - $order_id = learn_press_get_request( 'order_id' ); | |
| 552 | + $order_id = LP_Request::get_param( 'order_id', 0 ); | |
| 467 | 553 | if ( ! is_numeric( $order_id ) || learn_press_get_post_type( $order_id ) != 'lp_order' ) { |
| 468 | 554 | die( __( 'Invalid order', 'learnpress' ) ); |
| 469 | 555 | } |
| 470 | 556 | |
| 471 | 557 | // validate item |
| 472 | - $item_ids = learn_press_get_request( 'items' ); | |
| 473 | - $order = learn_press_get_order( $order_id ); | |
| 558 | + $item_ids_str = LP_Request::get_param( 'items', '' ); | |
| 559 | + if ( empty( $item_ids_str ) ) { | |
| 560 | + die( __( 'Invalid item', 'learnpress' ) ); | |
| 561 | + } | |
| 474 | 562 | |
| 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 | - | |
| 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(); | |
| 485 | 569 | $order_data = learn_press_update_order_items( $order_id ); |
| 486 | 570 | $currency_symbol = learn_press_get_currency_symbol( $order_data['currency'] ); |
| 487 | 571 | $order_data['subtotal_html'] = learn_press_format_price( $order_data['subtotal'], $currency_symbol ); |
| 488 | 572 | $order_data['total_html'] = learn_press_format_price( $order_data['total'], $currency_symbol ); |
| @@ -488,79 +572,27 @@ | ||
| 488 | 572 | $order_data['total_html'] = learn_press_format_price( $order_data['total'], $currency_symbol ); |
| 489 | 573 | |
| 490 | 574 | if ( $order_items ) { |
| 491 | 575 | foreach ( $order_items as $item ) { |
| 492 | - | |
| 493 | - if ( ! in_array( $item['id'], $order_item_ids ) ) { | |
| 576 | + if ( ! in_array( $item['id'], $order_item ) ) { | |
| 494 | 577 | continue; |
| 495 | 578 | } |
| 496 | 579 | |
| 497 | 580 | ob_start(); |
| 498 | - include learn_press_get_admin_view( 'meta-boxes/order/order-item.php' ); | |
| 581 | + Template::instance()->get_admin_template( 'meta-boxes/order/order-item.php', compact( 'item', 'order' ) ); | |
| 499 | 582 | $html .= ob_get_clean(); |
| 500 | 583 | } |
| 501 | 584 | } |
| 502 | 585 | |
| 503 | - $response = array( | |
| 504 | - 'result' => 'success', | |
| 505 | - 'item_html' => $html, | |
| 506 | - 'order_data' => $order_data, | |
| 507 | - ); | |
| 586 | + $response->status = 'success'; | |
| 587 | + $response->data->item_html = $html; | |
| 588 | + $response->data->order_data = $order_data; | |
| 508 | 589 | } |
| 509 | 590 | |
| 510 | - learn_press_send_json( $response ); | |
| 591 | + wp_send_json( $response ); | |
| 511 | 592 | } |
| 512 | 593 | |
| 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() { | |
| 594 | + /*public static function load_chart() { | |
| 563 | 595 | if ( ! class_exists( 'LP_Submenu_Statistics' ) ) { |
| 564 | 596 | $statistic = include_once LP_PLUGIN_PATH . '/inc/admin/sub-menus/class-lp-submenu-statistics.php'; |
| 565 | 597 | } else { |
| 566 | 598 | $statistic = new LP_Submenu_Statistics(); |
| @@ -565,9 +597,9 @@ | ||
| 565 | 597 | } else { |
| 566 | 598 | $statistic = new LP_Submenu_Statistics(); |
| 567 | 599 | } |
| 568 | 600 | $statistic->load_chart(); |
| 569 | - } | |
| 601 | + }*/ | |
| 570 | 602 | |
| 571 | 603 | public static function json_search_customer_name( $query ) { |
| 572 | 604 | global $wpdb; |
| 573 | 605 | |
| @@ -587,49 +619,55 @@ | ||
| 587 | 619 | * |
| 588 | 620 | * @note tungnnx checked use |
| 589 | 621 | */ |
| 590 | 622 | public static function create_page() { |
| 591 | - $response = array( | |
| 592 | - 'code' => 0, | |
| 593 | - 'message' => '', | |
| 594 | - ); | |
| 623 | + $response = new Response(); | |
| 595 | 624 | |
| 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 | - } | |
| 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 | + } | |
| 610 | 639 | |
| 611 | - $page_name = LP_Helper::sanitize_params_submitted( $_POST['page_name'] ); | |
| 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 | + } | |
| 612 | 645 | |
| 613 | - if ( $page_name ) { | |
| 614 | - $page_id = LP_Helper::create_page( $page_name ); | |
| 646 | + $page_name = LP_Helper::sanitize_params_submitted( $_POST['page_name'] ); | |
| 647 | + $field_name = LP_Request::get_param( 'field_name' ); | |
| 615 | 648 | |
| 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> '; | |
| 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' ); | |
| 649 | + if ( ! $page_name ) { | |
| 650 | + throw new Exception( __( 'Empty page name!', 'learnpress' ) ); | |
| 627 | 651 | } |
| 628 | - } else { | |
| 629 | - $response['error'] = __( 'Empty page name!', 'learnpress' ); | |
| 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(); | |
| 630 | 667 | } |
| 631 | - learn_press_send_json( $response ); | |
| 668 | + | |
| 669 | + wp_send_json( $response ); | |
| 632 | 670 | } |
| 633 | 671 | |
| 634 | 672 | /** |
| 635 | 673 | * Get edit|view link of a page |
| @@ -638,11 +676,11 @@ | ||
| 638 | 676 | $page_id = (int) $_REQUEST['page_id'] ?? 0; |
| 639 | 677 | ?> |
| 640 | 678 | |
| 641 | 679 | <a href="<?php echo get_edit_post_link( $page_id ); ?>" |
| 642 | - target="_blank"><?php _e( 'Edit Page', 'learnpress' ); ?></a> | |
| 680 | + target="_blank"><?php _e( 'Edit Page', 'learnpress' ); ?></a> | |
| 643 | 681 | <a href="<?php echo get_permalink( $page_id ); ?>" |
| 644 | - target="_blank"><?php _e( 'View Page', 'learnpress' ); ?></a> | |
| 682 | + target="_blank"><?php _e( 'View Page', 'learnpress' ); ?></a> | |
| 645 | 683 | |
| 646 | 684 | <?php |
| 647 | 685 | die(); |
| 648 | 686 | } |
| @@ -648,10 +686,12 @@ | ||
| 648 | 686 | } |
| 649 | 687 | |
| 650 | 688 | /** |
| 651 | 689 | * Get date from, to for static chart |
| 690 | + * | |
| 691 | + * @deprecated 4.2.6.9.3 | |
| 652 | 692 | */ |
| 653 | - public static function custom_stats() { | |
| 693 | + /*public static function custom_stats() { | |
| 654 | 694 | $from = LP_Helper::sanitize_params_submitted( $_REQUEST['from'] ?? 0 ); |
| 655 | 695 | $to = LP_Helper::sanitize_params_submitted( $_REQUEST['to'] ?? 0 ); |
| 656 | 696 | $date_diff = strtotime( $to ) - strtotime( $from ); |
| 657 | 697 | if ( $date_diff <= 0 || $from == 0 || $to == 0 ) { |
| @@ -658,33 +698,11 @@ | ||
| 658 | 698 | die(); |
| 659 | 699 | } |
| 660 | 700 | learn_press_process_chart( learn_press_get_chart_students( $to, 'days', floor( $date_diff / ( 60 * 60 * 24 ) ) + 1 ) ); |
| 661 | 701 | 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 | - } | |
| 702 | + }*/ | |
| 687 | 703 | } |
| 688 | 704 | |
| 689 | 705 | add_action( 'init', array( 'LP_Admin_Ajax', 'init' ) ); |
| 690 | 706 | } |
| 707 | + | |
| 708 | +new LP_Admin_Ajax(); | |