| 1 |
<?php |
| 2 |
/** |
| 3 |
* class AjaxBase |
| 4 |
* |
| 5 |
* @since 4.2.7.6 |
| 6 |
* @version 1.0.6 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace LearnPress\Ajax; |
| 10 |
|
| 11 |
use LP_Request; |
| 12 |
|
| 13 |
/** |
| 14 |
* @use LoadContentViaAjax::load_content_via_ajax |
| 15 |
* |
| 16 |
* $action must unique name on all Ajax classes. |
| 17 |
* Because not specify a specific class. |
| 18 |
*/ |
| 19 |
abstract class AbstractAjax { |
| 20 |
public static function catch_lp_ajax() { |
| 21 |
if ( ! empty( $_REQUEST['lp-load-ajax'] ) ) { |
| 22 |
$action = LP_Request::get_param( 'lp-load-ajax' ); |
| 23 |
$nonce = LP_Request::get_param( 'nonce' ); |
| 24 |
$class = new static(); |
| 25 |
|
| 26 |
if ( ! method_exists( $class, $action ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
// For case cache HTML, so cache nonce is not required. |
| 31 |
$class_no_nonce = [ |
| 32 |
LoadContentViaAjax::class, |
| 33 |
]; |
| 34 |
|
| 35 |
// Todo: should write separation check none login and none not login |
| 36 |
if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) { |
| 37 |
if ( ! in_array( get_class( $class ), $class_no_nonce ) ) { |
| 38 |
wp_die( 'Invalid request!', 400 ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
if ( is_callable( [ $class, $action ] ) ) { |
| 43 |
call_user_func( [ $class, $action ] ); |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|