| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Settings API |
| 5 |
* |
| 6 |
* @package Disco |
| 7 |
* @subpackage \Rest |
| 8 |
* @since 1.0.0 |
| 9 |
* @category Rest |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Disco\Rest; |
| 13 |
|
| 14 |
use Disco\App\Utility\Settings; |
| 15 |
use WP_REST_Controller; |
| 16 |
use WP_REST_Server; |
| 17 |
|
| 18 |
/** |
| 19 |
* Class SettingsApi |
| 20 |
* |
| 21 |
* @package Disco |
| 22 |
* @subpackage \Rest |
| 23 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 24 |
* @link https://webappick.com |
| 25 |
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
| 26 |
* @category Rest |
| 27 |
*/ |
| 28 |
class SettingsApi extends WP_REST_Controller { |
| 29 |
|
| 30 |
/** |
| 31 |
* Registers the routes for the objects of the controller. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function register_routes() { |
| 36 |
register_rest_route( |
| 37 |
Api::NAMESPACE_NAME . '/' . Api::VERSION, |
| 38 |
'/' . Api::SETTINGS_ROUTE_NAME . '/', |
| 39 |
array( |
| 40 |
array( |
| 41 |
'methods' => WP_REST_Server::READABLE, |
| 42 |
'callback' => array( |
| 43 |
$this, |
| 44 |
'get_item', |
| 45 |
), |
| 46 |
'permission_callback' => array( |
| 47 |
$this, |
| 48 |
'permissions_check', |
| 49 |
), |
| 50 |
'args' => $this->get_collection_params(), |
| 51 |
), |
| 52 |
array( |
| 53 |
'methods' => WP_REST_Server::EDITABLE, |
| 54 |
'callback' => array( |
| 55 |
$this, |
| 56 |
'update_item', |
| 57 |
), |
| 58 |
'permission_callback' => array( |
| 59 |
$this, |
| 60 |
'permissions_check', |
| 61 |
), |
| 62 |
'args' => $this->get_collection_params(), |
| 63 |
), |
| 64 |
'schema' => array( |
| 65 |
$this, |
| 66 |
'get_item_schema', |
| 67 |
), |
| 68 |
) |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Checks if a given request has access to read contacts. |
| 74 |
* |
| 75 |
* @param \WP_REST_Request $request Full data about the request. |
| 76 |
* @return bool |
| 77 |
*/ |
| 78 |
public function permissions_check( $request ) {// phpcs:ignore |
| 79 |
return current_user_can( 'manage_options' ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Retrieves a list of address items. |
| 84 |
* |
| 85 |
* @param \WP_REST_Request $request Full data about the request. |
| 86 |
* @return \WP_REST_Response|\WP_Error |
| 87 |
* @throws \Exception If the settings is invalid. |
| 88 |
*/ |
| 89 |
public function get_item( $request ) { |
| 90 |
$settings = Settings::get(); |
| 91 |
|
| 92 |
if ( is_wp_error( $settings ) ) { |
| 93 |
return $settings; |
| 94 |
} |
| 95 |
|
| 96 |
$response = $this->prepare_item_for_response( $settings, $request ); |
| 97 |
$data = $this->prepare_response_for_collection( $response ); |
| 98 |
|
| 99 |
$total = 0; |
| 100 |
|
| 101 |
if ( is_array( $settings ) ) { |
| 102 |
$total = count( $settings ); |
| 103 |
} |
| 104 |
|
| 105 |
$response = rest_ensure_response( $data ); |
| 106 |
|
| 107 |
$response->header( 'X-WP-Total', (int) $total ); |
| 108 |
$response->header( 'X-WP-TotalPages', (int) $total ); |
| 109 |
|
| 110 |
return $response; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Updates one item from the collection. |
| 115 |
* |
| 116 |
* @param \WP_REST_Request $request Full data about the request. |
| 117 |
* @return \WP_Error|\WP_REST_Response |
| 118 |
*/ |
| 119 |
public function update_item( $request ) { |
| 120 |
$prepared_settings = (array) $this->prepare_item_for_database( $request ); |
| 121 |
|
| 122 |
$updated = Settings::save( $prepared_settings ); |
| 123 |
|
| 124 |
if ( ! $updated ) { |
| 125 |
return new \WP_Error( |
| 126 |
'rest_not_updated', |
| 127 |
__( 'Sorry, settings could not be updated.', 'disco' ), |
| 128 |
array( 'status' => 400 ) |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
$settings = Settings::get(); |
| 133 |
|
| 134 |
if ( is_wp_error( $settings ) ) { |
| 135 |
return $settings; |
| 136 |
} |
| 137 |
|
| 138 |
$response = $this->prepare_item_for_response( $settings, $request ); |
| 139 |
|
| 140 |
return rest_ensure_response( $response ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Prepares the item for the REST response. |
| 145 |
* |
| 146 |
* @param array|string $item WordPress' representation of the item. |
| 147 |
* @param \WP_REST_Request $request Request object. |
| 148 |
* @return \WP_Error|\WP_REST_Response |
| 149 |
*/ |
| 150 |
public function prepare_item_for_response( $item, $request ) { |
| 151 |
$data = array(); |
| 152 |
|
| 153 |
$fields = $this->get_fields_for_response( $request ); |
| 154 |
|
| 155 |
if ( is_array( $fields ) && is_array( $item ) && in_array( 'product_price_type', $fields, true ) ) { |
| 156 |
$data['product_price_type'] = $item['product_price_type']; |
| 157 |
} |
| 158 |
|
| 159 |
if ( is_array( $fields ) && is_array( $item ) && in_array( 'min_max_discount_amount', $fields, true ) ) { |
| 160 |
$data['min_max_discount_amount'] = $item['min_max_discount_amount']; |
| 161 |
} |
| 162 |
|
| 163 |
$context = 'view'; |
| 164 |
|
| 165 |
if ( is_string( $request['context'] ) ) { |
| 166 |
$context = $request['context']; |
| 167 |
} |
| 168 |
|
| 169 |
$data = $this->filter_response_by_context( $data, $context ); |
| 170 |
|
| 171 |
return rest_ensure_response( $data ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Retrieves the contact schema, conforming to JSON Schema. |
| 176 |
* |
| 177 |
* @return array |
| 178 |
*/ |
| 179 |
public function get_item_schema() { |
| 180 |
if ( $this->schema ) { |
| 181 |
return $this->add_additional_fields_schema( $this->schema ); |
| 182 |
} |
| 183 |
|
| 184 |
$schema = array( |
| 185 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 186 |
'title' => 'contact', |
| 187 |
'type' => 'object', |
| 188 |
'properties' => array( |
| 189 |
'product_price_type' => array( |
| 190 |
'description' => __( 'Product Price Type.', 'disco' ), |
| 191 |
'type' => 'string', |
| 192 |
'context' => array( |
| 193 |
'view', |
| 194 |
'edit', |
| 195 |
), |
| 196 |
), |
| 197 |
'min_max_discount_amount' => array( |
| 198 |
'description' => __( 'Minimum, Maximum or Average Discount Amount.', 'disco' ), |
| 199 |
'type' => 'string', |
| 200 |
'context' => array( |
| 201 |
'view', |
| 202 |
'edit', |
| 203 |
), |
| 204 |
), |
| 205 |
), |
| 206 |
); |
| 207 |
|
| 208 |
$this->schema = $schema; |
| 209 |
|
| 210 |
return $this->add_additional_fields_schema( $this->schema ); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Retrieves the query params for collections. |
| 215 |
* |
| 216 |
* @return array |
| 217 |
*/ |
| 218 |
public function get_collection_params() { |
| 219 |
$params = parent::get_collection_params(); |
| 220 |
|
| 221 |
unset( $params['search'], $params['per_page'], $params['page'] ); |
| 222 |
|
| 223 |
return $params; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Get the setting, if the key is valid. |
| 228 |
* |
| 229 |
* @param string $key Supplied ID. |
| 230 |
* @return array|string|\WP_Error |
| 231 |
*/ |
| 232 |
protected function get_settings( $key ) { |
| 233 |
$setting = Settings::get( $key ); |
| 234 |
|
| 235 |
if ( ! $setting ) { |
| 236 |
return new \WP_Error( |
| 237 |
'rest_setting_invalid_key', |
| 238 |
__( 'Invalid Setting Key.', 'disco' ),// phpcs:ignore |
| 239 |
array( 'status' => 404 ) |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
return $setting; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Prepares one item for create or update operation. |
| 248 |
* |
| 249 |
* @param \WP_REST_Request $request Request object. |
| 250 |
* @return object |
| 251 |
*/ |
| 252 |
protected function prepare_item_for_database( $request ) { |
| 253 |
$prepared = array(); |
| 254 |
|
| 255 |
if ( isset( $request['settings_1'] ) ) { |
| 256 |
$prepared['settings_1'] = $request['settings_1']; |
| 257 |
} |
| 258 |
|
| 259 |
if ( isset( $request['settings_2'] ) ) { |
| 260 |
$prepared['settings_2'] = $request['settings_2']; |
| 261 |
} |
| 262 |
|
| 263 |
if ( isset( $request['settings_3'] ) ) { |
| 264 |
$prepared['settings_3'] = $request['settings_3']; |
| 265 |
} |
| 266 |
|
| 267 |
if ( isset( $request['product_price_type'] ) ) { |
| 268 |
$prepared['product_price_type'] = $request['product_price_type']; |
| 269 |
} |
| 270 |
|
| 271 |
if ( isset( $request['min_max_discount_amount'] ) ) { |
| 272 |
$prepared['min_max_discount_amount'] = $request['min_max_discount_amount']; |
| 273 |
} |
| 274 |
|
| 275 |
return (object) $prepared; |
| 276 |
} |
| 277 |
|
| 278 |
} |
| 279 |
|