| 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' ) || current_user_can( 'manage_woocommerce' ); // phpcs:ignore WordPress.WP.Capabilities.Unknown |
| 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 ) { // phpcs:ignore |
| 151 |
$data = array(); |
| 152 |
|
| 153 |
$fields = $this->get_fields_for_response( $request ); |
| 154 |
|
| 155 |
if ( is_array( $fields ) && is_array( $item ) && in_array( 'show_strike_through', $fields, true ) ) { |
| 156 |
$data['show_strike_through'] = $item['show_strike_through'] ?? array(); |
| 157 |
} |
| 158 |
|
| 159 |
if ( is_array( $fields ) && is_array( $item ) && in_array( 'on_sale_badge', $fields, true ) ) { |
| 160 |
$data['on_sale_badge'] = $item['on_sale_badge'] ?? ''; |
| 161 |
} |
| 162 |
|
| 163 |
if ( is_array( $fields ) && is_array( $item ) && in_array( 'product_price_type', $fields, true ) ) { |
| 164 |
$data['product_price_type'] = $item['product_price_type']; |
| 165 |
} |
| 166 |
|
| 167 |
if ( is_array( $fields ) && is_array( $item ) && in_array( 'min_max_discount_amount', $fields, true ) ) { |
| 168 |
$data['min_max_discount_amount'] = $item['min_max_discount_amount']; |
| 169 |
} |
| 170 |
|
| 171 |
if ( is_array( $fields ) && is_array( $item ) && in_array( 'discount_priority_type', $fields, true ) ) { |
| 172 |
$data['discount_priority_type'] = $item['discount_priority_type'] ?? ''; |
| 173 |
} |
| 174 |
|
| 175 |
if ( is_array( $fields ) && is_array( $item ) && in_array( 'live_chat_support', $fields, true ) ) { |
| 176 |
$data['live_chat_support'] = (bool) ( $item['live_chat_support'] ?? false ); |
| 177 |
} |
| 178 |
|
| 179 |
$context = 'view'; |
| 180 |
|
| 181 |
if ( is_string( $request['context'] ) ) { |
| 182 |
$context = $request['context']; |
| 183 |
} |
| 184 |
|
| 185 |
$data = $this->filter_response_by_context( $data, $context ); |
| 186 |
|
| 187 |
return rest_ensure_response( $data ); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Retrieves the contact schema, conforming to JSON Schema. |
| 192 |
* |
| 193 |
* @return array |
| 194 |
*/ |
| 195 |
public function get_item_schema() { // phpcs:ignore |
| 196 |
if ( $this->schema ) { |
| 197 |
return $this->add_additional_fields_schema( $this->schema ); |
| 198 |
} |
| 199 |
|
| 200 |
$schema = array( |
| 201 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 202 |
'title' => 'contact', |
| 203 |
'type' => 'object', |
| 204 |
'properties' => array( |
| 205 |
'product_price_type' => array( |
| 206 |
'description' => __( 'Product Price Type.', 'disco' ), |
| 207 |
'type' => 'string', |
| 208 |
'context' => array( |
| 209 |
'view', |
| 210 |
'edit', |
| 211 |
), |
| 212 |
), |
| 213 |
'min_max_discount_amount' => array( |
| 214 |
'description' => __( 'Minimum, Maximum or Average Discount Amount.', 'disco' ), |
| 215 |
'type' => 'string', |
| 216 |
'context' => array( |
| 217 |
'view', |
| 218 |
'edit', |
| 219 |
), |
| 220 |
), |
| 221 |
'discount_priority_type' => array( |
| 222 |
'description' => __( 'Discount Priority Type.', 'disco' ), |
| 223 |
'type' => 'string', |
| 224 |
'enum' => array( 'both', 'disco_campaign' ), |
| 225 |
'default' => 'both', |
| 226 |
'context' => array( |
| 227 |
'view', |
| 228 |
'edit', |
| 229 |
), |
| 230 |
), |
| 231 |
'on_sale_badge' => array( |
| 232 |
'description' => __( 'On Sale Badge Text.', 'disco' ), |
| 233 |
'type' => 'string', |
| 234 |
'context' => array( |
| 235 |
'view', |
| 236 |
'edit', |
| 237 |
), |
| 238 |
), |
| 239 |
'show_strike_through' => array( |
| 240 |
'description' => __( 'Show Strike Through Price.', 'disco' ), |
| 241 |
'type' => 'array', |
| 242 |
'context' => array( |
| 243 |
'view', |
| 244 |
'edit', |
| 245 |
), |
| 246 |
), |
| 247 |
'live_chat_support' => array( |
| 248 |
'description' => __( 'Enable Live Chat Support.', 'disco' ), |
| 249 |
'type' => 'boolean', |
| 250 |
'default' => false, |
| 251 |
'context' => array( |
| 252 |
'view', |
| 253 |
'edit', |
| 254 |
), |
| 255 |
), |
| 256 |
), |
| 257 |
); |
| 258 |
|
| 259 |
$this->schema = $schema; |
| 260 |
|
| 261 |
return $this->add_additional_fields_schema( $this->schema ); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Retrieves the query params for collections. |
| 266 |
* |
| 267 |
* @return array |
| 268 |
*/ |
| 269 |
public function get_collection_params() { |
| 270 |
$params = parent::get_collection_params(); |
| 271 |
|
| 272 |
unset( $params['search'], $params['per_page'], $params['page'] ); |
| 273 |
|
| 274 |
return $params; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Get the setting, if the key is valid. |
| 279 |
* |
| 280 |
* @param string $key Supplied ID. |
| 281 |
* @return array|string|\WP_Error |
| 282 |
*/ |
| 283 |
protected function get_settings( $key ) { |
| 284 |
$setting = Settings::get( $key ); |
| 285 |
|
| 286 |
if ( ! $setting ) { |
| 287 |
return new \WP_Error( |
| 288 |
'rest_setting_invalid_key', |
| 289 |
__( 'Invalid Setting Key.', 'disco' ),// phpcs:ignore |
| 290 |
array( 'status' => 404 ) |
| 291 |
); |
| 292 |
} |
| 293 |
|
| 294 |
return $setting; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Prepares one item for create or update operation. |
| 299 |
* |
| 300 |
* @param \WP_REST_Request $request Request object. |
| 301 |
* @return object |
| 302 |
*/ |
| 303 |
protected function prepare_item_for_database( $request ) { |
| 304 |
$prepared = array(); |
| 305 |
|
| 306 |
if ( isset( $request['show_strike_through'] ) ) { |
| 307 |
$prepared['show_strike_through'] = $request['show_strike_through']; |
| 308 |
} |
| 309 |
|
| 310 |
if ( isset( $request['on_sale_badge'] ) ) { |
| 311 |
$prepared['on_sale_badge'] = $request['on_sale_badge']; |
| 312 |
} |
| 313 |
|
| 314 |
if ( isset( $request['discount_priority_type'] ) ) { |
| 315 |
$prepared['discount_priority_type'] = $request['discount_priority_type']; |
| 316 |
} |
| 317 |
|
| 318 |
if ( isset( $request['product_price_type'] ) ) { |
| 319 |
$prepared['product_price_type'] = $request['product_price_type']; |
| 320 |
} |
| 321 |
|
| 322 |
if ( isset( $request['min_max_discount_amount'] ) ) { |
| 323 |
$prepared['min_max_discount_amount'] = $request['min_max_discount_amount']; |
| 324 |
} |
| 325 |
|
| 326 |
if ( isset( $request['live_chat_support'] ) ) { |
| 327 |
$prepared['live_chat_support'] = (bool) $request['live_chat_support']; |
| 328 |
} |
| 329 |
|
| 330 |
return (object) $prepared; |
| 331 |
} |
| 332 |
|
| 333 |
} |
| 334 |
|