| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_REST_API' ) ) : |
| 7 |
|
| 8 |
final class WPSC_REST_API { |
| 9 |
|
| 10 |
/** |
| 11 |
* REST API Settings |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private static $settings = array(); |
| 16 |
|
| 17 |
/** |
| 18 |
* Initialize this class |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public static function init() { |
| 23 |
|
| 24 |
// initialize rest api. |
| 25 |
add_filter( 'rest_authentication_errors', array( __CLASS__, 'load_current_user' ), 100 ); |
| 26 |
add_action( 'rest_api_init', array( __CLASS__, 'register_routes' ) ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Load current user for supportcandy after user authentication |
| 31 |
* |
| 32 |
* @param mixed $response - response object. |
| 33 |
* @return mixed |
| 34 |
*/ |
| 35 |
public static function load_current_user( $response ) { |
| 36 |
|
| 37 |
WPSC_Current_User::load_current_user(); |
| 38 |
return $response; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Register REST API routes |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public static function register_routes() { |
| 47 |
|
| 48 |
$advanced = get_option( 'wpsc-ms-advanced-settings' ); |
| 49 |
if ( ! $advanced['rest-api'] ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
do_action( 'wpsc_rest_register_routes' ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Validate integer value |
| 58 |
* |
| 59 |
* @param string $param - parameter value. |
| 60 |
* @param WP_REST_Request $request - request object. |
| 61 |
* @param string $key - filter key. |
| 62 |
* @return boolean |
| 63 |
*/ |
| 64 |
public static function validate_integer_value( $param, $request, $key ) { |
| 65 |
|
| 66 |
return is_numeric( $param ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
endif; |
| 71 |
|
| 72 |
WPSC_REST_API::init(); |
| 73 |
|