| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_REST_Statuses' ) ) : |
| 7 |
|
| 8 |
final class WPSC_REST_Statuses { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize this class |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public static function init() { |
| 16 |
|
| 17 |
add_action( 'wpsc_rest_register_routes', array( __CLASS__, 'register_routes' ) ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Register routes |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public static function register_routes() { |
| 26 |
|
| 27 |
// list statuses. |
| 28 |
register_rest_route( |
| 29 |
'supportcandy/v2', |
| 30 |
'/statuses', |
| 31 |
array( |
| 32 |
'methods' => 'GET', |
| 33 |
'callback' => array( __CLASS__, 'get_statuses' ), |
| 34 |
'permission_callback' => 'is_user_logged_in', |
| 35 |
), |
| 36 |
); |
| 37 |
|
| 38 |
// list individual status. |
| 39 |
register_rest_route( |
| 40 |
'supportcandy/v2', |
| 41 |
'/statuses/(?P<id>\d+)', |
| 42 |
array( |
| 43 |
'methods' => 'GET', |
| 44 |
'callback' => array( __CLASS__, 'get_individual_status' ), |
| 45 |
'args' => array( |
| 46 |
'id' => array( |
| 47 |
'validate_callback' => array( __CLASS__, 'validate_id' ), |
| 48 |
), |
| 49 |
), |
| 50 |
'permission_callback' => 'is_user_logged_in', |
| 51 |
), |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Statuses collection |
| 57 |
* |
| 58 |
* @param WP_REST_Request $request - request object. |
| 59 |
* @return WP_Error|WP_REST_Response |
| 60 |
*/ |
| 61 |
public static function get_statuses( $request ) { |
| 62 |
|
| 63 |
$statuses = WPSC_Status::find( array( 'items_per_page' => 0 ), false )['results']; |
| 64 |
$data = array(); |
| 65 |
foreach ( $statuses as $status ) { |
| 66 |
$data[] = self::modify_response( $status ); |
| 67 |
} |
| 68 |
return new WP_REST_Response( $data, 200 ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Single status |
| 73 |
* |
| 74 |
* @param WP_REST_Request $request - request object. |
| 75 |
* @return WP_Error|WP_REST_Response |
| 76 |
*/ |
| 77 |
public static function get_individual_status( $request ) { |
| 78 |
|
| 79 |
$status = new WPSC_Status( $request->get_param( 'id' ) ); |
| 80 |
$data = self::modify_response( $status->to_array() ); |
| 81 |
return new WP_REST_Response( $data, 200 ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Modify response data appropreate for client side |
| 86 |
* |
| 87 |
* @param array $category - response array. |
| 88 |
* @return array |
| 89 |
*/ |
| 90 |
public static function modify_response( $category ) { |
| 91 |
|
| 92 |
unset( $category['load_order'] ); |
| 93 |
$category['id'] = intval( $category['id'] ); |
| 94 |
return $category; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Validate id |
| 99 |
* |
| 100 |
* @param string $param - parameter value. |
| 101 |
* @param WP_REST_Request $request - request object. |
| 102 |
* @param string $key - filter key. |
| 103 |
* @return boolean |
| 104 |
*/ |
| 105 |
public static function validate_id( $param, $request, $key ) { |
| 106 |
|
| 107 |
$error = new WP_Error( 'invalid_id', 'Invalid status id', array( 'status' => 400 ) ); |
| 108 |
$status = new WPSC_Status( $param ); |
| 109 |
return $status->id ? true : $error; |
| 110 |
} |
| 111 |
} |
| 112 |
endif; |
| 113 |
|
| 114 |
WPSC_REST_Statuses::init(); |
| 115 |
|