| 1 |
<?php |
| 2 |
|
| 3 |
class LP_REST_Settings_Controller extends LP_Abstract_REST_Controller { |
| 4 |
public function __construct() { |
| 5 |
$this->namespace = 'lp/v1'; |
| 6 |
$this->rest_base = 'settings'; |
| 7 |
parent::__construct(); |
| 8 |
} |
| 9 |
|
| 10 |
public function register_routes() { |
| 11 |
$this->routes = array( |
| 12 |
'' => array( |
| 13 |
array( |
| 14 |
'methods' => WP_REST_Server::READABLE, |
| 15 |
'callback' => array( $this, 'get_items' ), |
| 16 |
'permission_callback' => array( $this, 'check_admin_permission' ), |
| 17 |
), |
| 18 |
), |
| 19 |
|
| 20 |
'(?P<key>[\w]+)' => array( |
| 21 |
'args' => array( |
| 22 |
'id' => array( |
| 23 |
'description' => __( 'A unique identifier for the resource.', 'learnpress' ), |
| 24 |
'type' => 'string', |
| 25 |
), |
| 26 |
), |
| 27 |
array( |
| 28 |
'methods' => WP_REST_Server::READABLE, |
| 29 |
'callback' => array( $this, 'get_item' ), |
| 30 |
'permission_callback' => array( $this, 'check_admin_permission' ), |
| 31 |
), |
| 32 |
array( |
| 33 |
'methods' => WP_REST_Server::EDITABLE, |
| 34 |
'callback' => array( $this, 'update_item' ), |
| 35 |
'permission_callback' => array( $this, 'check_admin_permission' ), |
| 36 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 37 |
), |
| 38 |
array( |
| 39 |
'methods' => WP_REST_Server::DELETABLE, |
| 40 |
'callback' => array( $this, 'delete_item' ), |
| 41 |
'permission_callback' => array( $this, 'check_admin_permission' ), |
| 42 |
), |
| 43 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 44 |
), |
| 45 |
); |
| 46 |
|
| 47 |
parent::register_routes(); |
| 48 |
} |
| 49 |
|
| 50 |
public function check_admin_permission() { |
| 51 |
return LP_Abstract_API::check_admin_permission(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @param WP_REST_Request $request |
| 56 |
* |
| 57 |
* @return WP_REST_Response |
| 58 |
*/ |
| 59 |
public function get_items( $request ) { |
| 60 |
$settings = LP_Settings::instance(); |
| 61 |
$response = array( |
| 62 |
'result' => $settings->get(), |
| 63 |
); |
| 64 |
|
| 65 |
return rest_ensure_response( $response ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @param WP_REST_Request $request |
| 70 |
* |
| 71 |
* @return WP_REST_Response |
| 72 |
*/ |
| 73 |
public function get_item( $request ) { |
| 74 |
$settings = LP_Settings::instance(); |
| 75 |
$response = array( |
| 76 |
'result' => $settings->get( $request['key'] ), |
| 77 |
); |
| 78 |
|
| 79 |
return rest_ensure_response( $response ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param WP_REST_Request $request |
| 84 |
* |
| 85 |
* @return WP_REST_Response |
| 86 |
*/ |
| 87 |
public function update_item( $request ) { |
| 88 |
$response = array(); |
| 89 |
$settings = LP_Settings::instance(); |
| 90 |
$option = $settings->get( $request['key'] ); |
| 91 |
|
| 92 |
$settings->update( $request['key'], $request['data'] ); |
| 93 |
$new_option = $settings->get( $request['key'] ); |
| 94 |
$success = maybe_serialize( $option ) !== maybe_serialize( $new_option ); |
| 95 |
|
| 96 |
$response['success'] = $success; |
| 97 |
$response['result'] = $success ? $new_option : $option; |
| 98 |
|
| 99 |
return rest_ensure_response( $response ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @param WP_REST_Request $request |
| 104 |
* |
| 105 |
* @return WP_REST_Response |
| 106 |
*/ |
| 107 |
public function delete_item( $request ) { |
| 108 |
$response = array(); |
| 109 |
|
| 110 |
return rest_ensure_response( $response ); |
| 111 |
} |
| 112 |
} |
| 113 |
|