| 1 |
<?php //phpcs:ignore |
| 2 |
|
| 3 |
/** |
| 4 |
* Campaign 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\Campaign; |
| 15 |
use Disco\App\DropDown\DiscountDropDown; |
| 16 |
use Disco\App\Utility\Config; |
| 17 |
use WP_REST_Controller; |
| 18 |
use WP_REST_Server; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Campaign API |
| 22 |
* |
| 23 |
* @package Disco |
| 24 |
* @subpackage \Rest |
| 25 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 26 |
* @link https://webappick.com |
| 27 |
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
| 28 |
* @category Rest |
| 29 |
*/ |
| 30 |
class CampaignApi extends WP_REST_Controller {//phpcs:ignore |
| 31 |
|
| 32 |
/** |
| 33 |
* CampaignApi constructor. |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
$this->namespace = Api::NAMESPACE_NAME . '/' . Api::VERSION; |
| 37 |
$this->rest_base = Api::CAMPAIGN_ROUTE_NAME; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Registers the routes for the objects of the controller. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
* @throws \Exception If route is not available. |
| 45 |
*/ |
| 46 |
public function register_routes() { //phpcs:ignore |
| 47 |
|
| 48 |
register_rest_route( |
| 49 |
Api::NAMESPACE_NAME . '/' . Api::VERSION, |
| 50 |
'/' . Api::CAMPAIGN_ROUTE_NAME . '/', |
| 51 |
array( |
| 52 |
array( |
| 53 |
'methods' => WP_REST_Server::READABLE, |
| 54 |
'callback' => array( $this, 'get_items' ), |
| 55 |
'permission_callback' => array( $this, 'permissions_check' ), |
| 56 |
'args' => $this->get_collection_params(), |
| 57 |
), |
| 58 |
array( |
| 59 |
'methods' => WP_REST_Server::CREATABLE, |
| 60 |
'callback' => array( $this, 'create_item' ), |
| 61 |
'permission_callback' => array( $this, 'permissions_check' ), |
| 62 |
'args' => $this->get_endpoint_args_for_item_schema(), |
| 63 |
), |
| 64 |
'schema' => array( $this, 'get_item_schema' ), |
| 65 |
) |
| 66 |
); |
| 67 |
|
| 68 |
register_rest_route( |
| 69 |
Api::NAMESPACE_NAME . '/' . Api::VERSION, |
| 70 |
'/' . Api::CAMPAIGN_ROUTE_NAME . '/(?P<id>[\d]+)', |
| 71 |
array( |
| 72 |
'args' => array( |
| 73 |
'id' => array( |
| 74 |
'description' => __( 'Unique identifier for the object.', 'disco' ), |
| 75 |
'type' => 'integer', |
| 76 |
), |
| 77 |
), |
| 78 |
array( |
| 79 |
'methods' => WP_REST_Server::READABLE, |
| 80 |
'callback' => array( $this, 'get_item' ), |
| 81 |
'permission_callback' => array( $this, 'permissions_check' ), |
| 82 |
'args' => array( |
| 83 |
'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
| 84 |
), |
| 85 |
), |
| 86 |
array( |
| 87 |
'methods' => WP_REST_Server::EDITABLE, |
| 88 |
'callback' => array( $this, 'update_item' ), |
| 89 |
'permission_callback' => array( $this, 'permissions_check' ), |
| 90 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 91 |
), |
| 92 |
array( |
| 93 |
'methods' => WP_REST_Server::DELETABLE, |
| 94 |
'callback' => array( $this, 'delete_item' ), |
| 95 |
'permission_callback' => array( $this, 'permissions_check' ), |
| 96 |
), |
| 97 |
'schema' => array( $this, 'get_item_schema' ), |
| 98 |
) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Checks if a given request has access to read campaigns. |
| 104 |
* |
| 105 |
* @param \WP_REST_Request $request Full data about the request. |
| 106 |
* @return bool|\WP_Error |
| 107 |
* @throws \Exception If the campaign does not exist. |
| 108 |
*/ |
| 109 |
public function permissions_check( $request ) {//phpcs:ignore |
| 110 |
$permission = current_user_can( 'manage_options' ) || current_user_can( 'manage_woocommerce' ); // phpcs:ignore WordPress.WP.Capabilities.Unknown |
| 111 |
|
| 112 |
if ( ! $permission ) { |
| 113 |
return new \WP_Error( |
| 114 |
'rest_not_found', |
| 115 |
__( 'Sorry, Permission Denied.', 'disco' ), |
| 116 |
array( 'status' => 400 ) |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
if ( isset( $request['id'] ) ) { |
| 121 |
$campaign = $this->get_campaign( absint( $request['id'] ) ); |
| 122 |
|
| 123 |
if ( is_wp_error( $campaign ) ) { |
| 124 |
return $campaign; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return $permission; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Retrieves a list of campaign items. |
| 133 |
* |
| 134 |
* @param \WP_REST_Request $request Full data about the request. |
| 135 |
* @return \WP_REST_Response|\WP_Error |
| 136 |
* @throws \Exception If the campaign does not exist. |
| 137 |
*/ |
| 138 |
public function get_items( $request ) { |
| 139 |
$params = $this->get_collection_params(); |
| 140 |
$args = array_intersect_key( $request->get_params(), $params ); |
| 141 |
|
| 142 |
// unset others. |
| 143 |
unset( $args['per_page'], $args['page'] ); |
| 144 |
|
| 145 |
$data = array(); |
| 146 |
$campaigns = ( new Campaign )->get_campaigns(); |
| 147 |
|
| 148 |
if ( empty( $campaigns ) ) { |
| 149 |
return new \WP_Error( |
| 150 |
'rest_campaign_not_available', |
| 151 |
__( 'No campaign available. Create a campaign first', 'disco' ), |
| 152 |
array( 'status' => 404 ) |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
foreach ( $campaigns as $campaign ) { |
| 157 |
$response = $this->prepare_item_for_response( $campaign, $request ); |
| 158 |
$data[] = $this->prepare_response_for_collection( $response ); |
| 159 |
} |
| 160 |
|
| 161 |
$total = count( $campaigns ); |
| 162 |
$response = rest_ensure_response( $data ); |
| 163 |
|
| 164 |
$response->header( 'X-WP-Total', (int) $total ); |
| 165 |
$response->header( 'X-WP-TotalPages', (int) $total ); |
| 166 |
|
| 167 |
return $response; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Retrieves one item from the collection. |
| 172 |
* |
| 173 |
* @param \WP_REST_Request $request Full data about the request. |
| 174 |
* @return \WP_Error|\WP_REST_Response |
| 175 |
*/ |
| 176 |
public function get_item( $request ) { |
| 177 |
$campaign = $this->get_campaign( absint( $request['id'] ) ); |
| 178 |
|
| 179 |
if ( is_wp_error( $campaign ) ) { |
| 180 |
return $campaign; |
| 181 |
} |
| 182 |
|
| 183 |
$response = $this->prepare_item_for_response( $campaign, $request ); |
| 184 |
|
| 185 |
return rest_ensure_response( $response ); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Creates one item from the collection. |
| 190 |
* |
| 191 |
* @param \WP_REST_Request $request Full data about the request. |
| 192 |
* @return \WP_Error|\WP_REST_Response |
| 193 |
* @throws \Exception If the campaign does not exist. |
| 194 |
*/ |
| 195 |
public function create_item( $request ) { |
| 196 |
$config = (array) $this->prepare_item_for_database( $request ); |
| 197 |
|
| 198 |
if ( empty( $config ) ) { |
| 199 |
return new \WP_Error( |
| 200 |
'rest_not_added', |
| 201 |
__( 'Sorry, the campaign could not be created with empty value.', 'disco' ), |
| 202 |
array( 'status' => 400 ) |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
$campaign = ( new Campaign )->save_campaign( $config ); |
| 207 |
|
| 208 |
if ( is_wp_error( $campaign ) ) { |
| 209 |
$campaign->add_data( array( 'status' => 400 ) ); |
| 210 |
|
| 211 |
return $campaign; |
| 212 |
} |
| 213 |
|
| 214 |
$response = $this->prepare_item_for_response( $campaign, $request ); |
| 215 |
|
| 216 |
$response->set_status( 201 ); |
| 217 |
$campaign_id = 0; |
| 218 |
|
| 219 |
if ( isset( $campaign->id ) ) { |
| 220 |
$campaign_id = $campaign->id; |
| 221 |
} |
| 222 |
|
| 223 |
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $campaign_id ) ) ); |
| 224 |
|
| 225 |
return rest_ensure_response( $response ); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Updates one item from the collection. |
| 230 |
* |
| 231 |
* @param \WP_REST_Request $request Full data about the request. |
| 232 |
* @return \WP_Error|\WP_REST_Response |
| 233 |
* @throws \Exception If the campaign does not exist. |
| 234 |
*/ |
| 235 |
public function update_item( $request ) { |
| 236 |
$campaign = $this->get_campaign( absint( $request['id'] ) ); |
| 237 |
|
| 238 |
if ( is_wp_error( $campaign ) ) { |
| 239 |
return $campaign; |
| 240 |
} |
| 241 |
|
| 242 |
$prepared = (array) $this->prepare_item_for_database( $request ); |
| 243 |
|
| 244 |
if ( $campaign instanceof Config && method_exists( $campaign, 'get_config' ) ) { |
| 245 |
$config = $campaign->get_config(); |
| 246 |
$prepared = array_merge( $config, $prepared ); |
| 247 |
} |
| 248 |
|
| 249 |
$updated = ( new Campaign )->update_campaign( absint( $request['id'] ), $prepared ); |
| 250 |
|
| 251 |
if ( ! $updated ) { |
| 252 |
return new \WP_Error( |
| 253 |
'rest_not_updated', |
| 254 |
__( 'Sorry, the campaign could not be updated.', 'disco' ), |
| 255 |
array( 'status' => 400 ) |
| 256 |
); |
| 257 |
} |
| 258 |
|
| 259 |
$campaign = $this->get_campaign( absint( $request['id'] ) ); |
| 260 |
|
| 261 |
$response = $this->prepare_item_for_response( $campaign, $request ); |
| 262 |
|
| 263 |
return rest_ensure_response( $response ); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Deletes one item from the collection. |
| 268 |
* |
| 269 |
* @param \WP_REST_Request $request Full data about the request. |
| 270 |
* @return \WP_REST_Response|\WP_Error |
| 271 |
* @throws \Exception If the campaign does not exist. |
| 272 |
*/ |
| 273 |
public function delete_item( $request ) { |
| 274 |
$campaign = $this->get_campaign( absint( $request['id'] ) ); |
| 275 |
|
| 276 |
if ( is_wp_error( $campaign ) ) { |
| 277 |
return $campaign; |
| 278 |
} |
| 279 |
|
| 280 |
$previous = $this->prepare_item_for_response( $campaign, $request ); |
| 281 |
|
| 282 |
$deleted = ( new Campaign )->delete_campaign( absint( $request['id'] ) ); |
| 283 |
|
| 284 |
if ( ! $deleted ) { |
| 285 |
return new \WP_Error( |
| 286 |
'rest_not_deleted', |
| 287 |
__( 'Sorry, the campaign could not be deleted.', 'disco' ), |
| 288 |
array( 'status' => 400 ) |
| 289 |
); |
| 290 |
} |
| 291 |
|
| 292 |
$data = array( |
| 293 |
'deleted' => true, |
| 294 |
'previous' => $previous->get_data(), |
| 295 |
); |
| 296 |
|
| 297 |
return rest_ensure_response( $data ); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Prepares the item for the REST response. |
| 302 |
* |
| 303 |
* @param object $item WordPress's representation of the item. |
| 304 |
* @param \WP_REST_Request $request Full data about the request. |
| 305 |
* @return \WP_Error|\WP_HTTP_Response|\WP_REST_Response |
| 306 |
*/ |
| 307 |
public function prepare_item_for_response( $item, $request ) {//phpcs:ignore |
| 308 |
|
| 309 |
$data = array(); |
| 310 |
$fields = $this->get_fields_for_response( $request ); |
| 311 |
$data['id'] = 0; |
| 312 |
|
| 313 |
if ( ! empty( $item->id ) && in_array( 'id', $fields, true ) ) { |
| 314 |
$data['id'] = $item->id; |
| 315 |
} |
| 316 |
|
| 317 |
$data['name'] = ''; |
| 318 |
|
| 319 |
if ( ! empty( $item->name ) && in_array( 'name', $fields, true ) ) { |
| 320 |
$data['name'] = $item->name; |
| 321 |
} |
| 322 |
|
| 323 |
$data['priority'] = '1'; |
| 324 |
|
| 325 |
if ( ! empty( $item->priority ) && in_array( 'priority', $fields, true ) ) { |
| 326 |
$data['priority'] = $item->priority; |
| 327 |
} |
| 328 |
|
| 329 |
$data['status'] = '1'; |
| 330 |
|
| 331 |
if ( isset( $item->status ) && in_array( 'status', $fields, true ) ) { |
| 332 |
$data['status'] = $item->status; |
| 333 |
} |
| 334 |
|
| 335 |
$data['discount_intent'] = ''; |
| 336 |
|
| 337 |
if ( ! empty( $item->discount_intent ) && in_array( 'discount_intent', $fields, true ) ) { |
| 338 |
$data['discount_intent'] = $item->discount_intent; |
| 339 |
} |
| 340 |
|
| 341 |
$data['products'] = array(); |
| 342 |
|
| 343 |
if ( ! empty( $item->products ) && in_array( 'products', $fields, true ) ) { |
| 344 |
$data['products'] = (array) $item->products; |
| 345 |
} |
| 346 |
|
| 347 |
$data['conditions'] = array(); |
| 348 |
|
| 349 |
if ( ! empty( $item->conditions ) && in_array( 'conditions', $fields, true ) ) { |
| 350 |
$data['conditions'] = $item->conditions; |
| 351 |
} |
| 352 |
|
| 353 |
$data['discount_rules'] = array(); |
| 354 |
|
| 355 |
if ( ! empty( $item->discount_rules ) && in_array( 'discount_rules', $fields, true ) ) { |
| 356 |
$data['discount_rules'] = $item->discount_rules; |
| 357 |
} |
| 358 |
|
| 359 |
$data['created_by'] = ''; |
| 360 |
|
| 361 |
if ( ! empty( $item->created_by ) && in_array( 'created_by', $fields, true ) ) { |
| 362 |
$data['created_by'] = $item->created_by; |
| 363 |
} |
| 364 |
|
| 365 |
$data['created_date'] = ''; |
| 366 |
|
| 367 |
if ( ! empty( $item->created_date ) && in_array( 'created_date', $fields, true ) ) { |
| 368 |
$created_date = gmdate( DATE_W3C, strtotime( $item->created_date ) ); |
| 369 |
|
| 370 |
$data['created_date'] = $created_date; |
| 371 |
} |
| 372 |
|
| 373 |
$data['modified_by'] = ''; |
| 374 |
|
| 375 |
if ( ! empty( $item->modified_by ) && in_array( 'modified_by', $fields, true ) ) { |
| 376 |
$data['modified_by'] = $item->modified_by; |
| 377 |
} |
| 378 |
|
| 379 |
$data['modified_date'] = ''; |
| 380 |
|
| 381 |
if ( ! empty( $item->modified_date ) && in_array( 'modified_date', $fields, true ) ) { |
| 382 |
$modified_date = gmdate( DATE_W3C, strtotime( $item->modified_date ) ); |
| 383 |
|
| 384 |
$data['modified_date'] = $modified_date; |
| 385 |
} |
| 386 |
|
| 387 |
$data['discount_valid_from'] = ''; |
| 388 |
|
| 389 |
if ( ! empty( $item->discount_valid_from ) && in_array( 'discount_valid_from', $fields, true ) ) { |
| 390 |
$discount_valid_from = gmdate( DATE_W3C, strtotime( $item->discount_valid_from ) ); |
| 391 |
|
| 392 |
$data['discount_valid_from'] = $discount_valid_from; |
| 393 |
} |
| 394 |
|
| 395 |
$data['discount_valid_to'] = ''; |
| 396 |
|
| 397 |
if ( ! empty( $item->discount_valid_to ) && in_array( 'discount_valid_to', $fields, true ) ) { |
| 398 |
$discount_valid_to = gmdate( DATE_W3C, strtotime( $item->discount_valid_to ) ); |
| 399 |
|
| 400 |
$data['discount_valid_to'] = $discount_valid_to; |
| 401 |
} |
| 402 |
|
| 403 |
$data['discount_method'] = ''; |
| 404 |
|
| 405 |
if ( ! empty( $item->discount_method ) && in_array( 'discount_method', $fields, true ) ) { |
| 406 |
$data['discount_method'] = $item->discount_method; |
| 407 |
} |
| 408 |
|
| 409 |
$data['discount_coupon'] = ''; |
| 410 |
|
| 411 |
if ( ! empty( $item->discount_coupon ) && in_array( 'discount_coupon', $fields, true ) ) { |
| 412 |
$data['discount_coupon'] = $item->discount_coupon; |
| 413 |
} |
| 414 |
|
| 415 |
$data['discount_label'] = ''; |
| 416 |
|
| 417 |
if ( ! empty( $item->discount_label ) && in_array( 'discount_label', $fields, true ) ) { |
| 418 |
$data['discount_label'] = $item->discount_label; |
| 419 |
} |
| 420 |
|
| 421 |
$data['discount_max_user'] = ''; |
| 422 |
|
| 423 |
if ( ! empty( $item->discount_max_user ) && in_array( 'discount_max_user', $fields, true ) ) { |
| 424 |
$data['discount_max_user'] = $item->discount_max_user; |
| 425 |
} |
| 426 |
|
| 427 |
$data['discount_based_on'] = ''; |
| 428 |
|
| 429 |
if ( ! empty( $item->discount_based_on ) && in_array( 'discount_based_on', $fields, true ) ) { |
| 430 |
$data['discount_based_on'] = $item->discount_based_on; |
| 431 |
} |
| 432 |
|
| 433 |
$data['bogo_type'] = ''; |
| 434 |
|
| 435 |
if ( ! empty( $item->bogo_type ) && in_array( 'bogo_type', $fields, true ) ) { |
| 436 |
$data['bogo_type'] = $item->bogo_type; |
| 437 |
} |
| 438 |
|
| 439 |
$data['count_quantity_as'] = 'separate'; |
| 440 |
|
| 441 |
if ( in_array( 'count_quantity_as', $fields, true ) && $item instanceof \Disco\App\Utility\Config ) { |
| 442 |
$data['count_quantity_as'] = $item->get_count_quantity_as(); |
| 443 |
} |
| 444 |
|
| 445 |
$data['free_item_selection'] = 'cart_order'; |
| 446 |
|
| 447 |
if ( in_array( 'free_item_selection', $fields, true ) && $item instanceof \Disco\App\Utility\Config ) { |
| 448 |
$data['free_item_selection'] = $item->get_free_item_selection(); |
| 449 |
} |
| 450 |
|
| 451 |
$data['ui'] = array(); |
| 452 |
|
| 453 |
if ( ! empty( $item->ui ) && in_array( 'ui', $fields, true ) ) { |
| 454 |
$data['ui'] = $item->ui; |
| 455 |
} |
| 456 |
|
| 457 |
// $data['design_blocks'] = array(); |
| 458 |
|
| 459 |
if ( ! empty( $item->design_blocks ) && in_array( 'design_blocks', $fields, true ) ) { |
| 460 |
$data['design_blocks'] = $item->design_blocks; |
| 461 |
} |
| 462 |
|
| 463 |
$context = ! empty( $request['context'] ) && is_string( $request['context'] ) ? $request['context'] : 'view';//phpcs:ignore |
| 464 |
|
| 465 |
$data = $this->filter_response_by_context( $data, $context ); |
| 466 |
|
| 467 |
$response = rest_ensure_response( $data ); |
| 468 |
$response->add_links( $this->prepare_links( $item ) ); |
| 469 |
|
| 470 |
return $response; |
| 471 |
} |
| 472 |
|
| 473 |
/** |
| 474 |
* Retrieves the campaign schema, conforming to JSON Schema. |
| 475 |
* |
| 476 |
* @return array |
| 477 |
* @throws \Exception If the campaign does not exist. |
| 478 |
*/ |
| 479 |
public function get_item_schema() {//phpcs:ignore |
| 480 |
$schema = array( |
| 481 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 482 |
'title' => 'campaign', |
| 483 |
'type' => 'object', |
| 484 |
'properties' => array( |
| 485 |
'id' => array( |
| 486 |
'description' => __( 'Unique identifier for the campaign.', 'disco' ), |
| 487 |
'type' => 'integer', |
| 488 |
'context' => array( 'view', 'edit' ), |
| 489 |
'readonly' => true, |
| 490 |
), |
| 491 |
'name' => array( |
| 492 |
'description' => __( 'Name of the campaign.', 'disco' ), |
| 493 |
'type' => 'string', |
| 494 |
'context' => array( 'view', 'edit' ), |
| 495 |
'required' => true, |
| 496 |
'arg_options' => array( |
| 497 |
'sanitize_callback' => 'sanitize_text_field', |
| 498 |
), |
| 499 |
), |
| 500 |
'priority' => array( |
| 501 |
'description' => __( 'Campaign Priority.', 'disco' ), |
| 502 |
'type' => 'string', |
| 503 |
'required' => true, |
| 504 |
'context' => array( 'view', 'edit' ), |
| 505 |
'arg_options' => array( |
| 506 |
'sanitize_callback' => 'sanitize_text_field', |
| 507 |
), |
| 508 |
), |
| 509 |
'status' => array( |
| 510 |
'description' => __( 'Campaign Status.', 'disco' ), |
| 511 |
'type' => 'string', |
| 512 |
'required' => true, |
| 513 |
'context' => array( 'view', 'edit' ), |
| 514 |
'arg_options' => array( |
| 515 |
'sanitize_callback' => 'sanitize_text_field', |
| 516 |
), |
| 517 |
), |
| 518 |
'products' => array( |
| 519 |
'description' => __( 'Discount applicable products.', 'disco' ), |
| 520 |
'type' => 'array', |
| 521 |
'required' => true, |
| 522 |
'context' => array( 'view', 'edit' ), |
| 523 |
'arg_options' => array( |
| 524 |
'sanitize_callback' => 'rest_sanitize_array', |
| 525 |
), |
| 526 |
), |
| 527 |
'conditions' => array( |
| 528 |
'description' => __( 'Conditions to match before get the discount.', 'disco' ), |
| 529 |
'type' => 'array', |
| 530 |
'context' => array( 'view', 'edit' ), |
| 531 |
'arg_options' => array( |
| 532 |
'sanitize_callback' => 'rest_sanitize_array', |
| 533 |
), |
| 534 |
), |
| 535 |
'discount_intent' => array( |
| 536 |
'description' => __( 'Discount Intention.', 'disco' ), |
| 537 |
'type' => 'string', |
| 538 |
'required' => true, |
| 539 |
'context' => array( 'view', 'edit' ), |
| 540 |
'arg_options' => array( |
| 541 |
'sanitize_callback' => 'sanitize_text_field', |
| 542 |
), |
| 543 |
), |
| 544 |
'discount_rules' => array( |
| 545 |
'description' => __( 'Discount Rules.', 'disco' ), |
| 546 |
'type' => 'array', |
| 547 |
'context' => array( 'view', 'edit' ), |
| 548 |
'arg_options' => array( |
| 549 |
'sanitize_callback' => 'rest_sanitize_array', |
| 550 |
), |
| 551 |
), |
| 552 |
'discount_based_on' => array( |
| 553 |
'description' => __( 'Discount based on', 'disco' ), |
| 554 |
'type' => 'string', |
| 555 |
'context' => array( 'view', 'edit' ), |
| 556 |
'arg_options' => array( |
| 557 |
'sanitize_callback' => 'sanitize_text_field', |
| 558 |
), |
| 559 |
), |
| 560 |
'discount_max_user' => array( |
| 561 |
'description' => __( 'Maximum Discount Users', 'disco' ), |
| 562 |
'type' => 'string', |
| 563 |
'context' => array( 'view', 'edit' ), |
| 564 |
'arg_options' => array( |
| 565 |
'sanitize_callback' => 'sanitize_text_field', |
| 566 |
), |
| 567 |
), |
| 568 |
'bogo_type' => array( |
| 569 |
'description' => __( 'BOGO Type', 'disco' ), |
| 570 |
'type' => 'string', |
| 571 |
'context' => array( 'view', 'edit' ), |
| 572 |
'arg_options' => array( |
| 573 |
'sanitize_callback' => 'sanitize_text_field', |
| 574 |
), |
| 575 |
), |
| 576 |
'count_quantity_as' => array( |
| 577 |
'description' => __( 'How item quantities aggregate toward the tier: separate, variations, filters.', 'disco' ), |
| 578 |
'type' => 'string', |
| 579 |
'context' => array( 'view', 'edit' ), |
| 580 |
'arg_options' => array( |
| 581 |
'sanitize_callback' => 'sanitize_text_field', |
| 582 |
), |
| 583 |
), |
| 584 |
'free_item_selection' => array( |
| 585 |
'description' => __( 'BOGO free item selection: cart_order, lowest, highest.', 'disco' ), |
| 586 |
'type' => 'string', |
| 587 |
'context' => array( 'view', 'edit' ), |
| 588 |
'arg_options' => array( |
| 589 |
'sanitize_callback' => 'sanitize_text_field', |
| 590 |
), |
| 591 |
), |
| 592 |
'discount_valid_from' => array( |
| 593 |
'description' => __( 'Discount Start Date.', 'disco' ), |
| 594 |
'type' => 'date-time', |
| 595 |
'context' => array( 'view', 'edit' ), |
| 596 |
'arg_options' => array( |
| 597 |
'sanitize_callback' => 'sanitize_text_field', |
| 598 |
), |
| 599 |
), |
| 600 |
'discount_valid_to' => array( |
| 601 |
'description' => __( 'Discount End Date.', 'disco' ), |
| 602 |
'type' => 'date-time', |
| 603 |
'context' => array( 'view', 'edit' ), |
| 604 |
'arg_options' => array( |
| 605 |
'sanitize_callback' => 'sanitize_text_field', |
| 606 |
), |
| 607 |
), |
| 608 |
'discount_method' => array( |
| 609 |
'description' => __( 'Discount Method: automated or coupon.', 'disco' ), |
| 610 |
'type' => 'string', |
| 611 |
'required' => true, |
| 612 |
'context' => array( 'view', 'edit' ), |
| 613 |
'arg_options' => array( |
| 614 |
'sanitize_callback' => 'sanitize_text_field', |
| 615 |
), |
| 616 |
), |
| 617 |
'discount_coupon' => array( |
| 618 |
'description' => __( 'Discount coupon code.', 'disco' ), |
| 619 |
'type' => 'string', |
| 620 |
'context' => array( 'view', 'edit' ), |
| 621 |
'arg_options' => array( |
| 622 |
'sanitize_callback' => 'sanitize_text_field', |
| 623 |
), |
| 624 |
), |
| 625 |
'created_by' => array( |
| 626 |
'description' => __( 'Created by User ID', 'disco' ), |
| 627 |
'type' => 'string', |
| 628 |
'context' => array( 'view', 'edit' ), |
| 629 |
'arg_options' => array( |
| 630 |
'sanitize_callback' => 'sanitize_text_field', |
| 631 |
), |
| 632 |
), |
| 633 |
|
| 634 |
'modified_by' => array( |
| 635 |
'description' => __( 'Modified by User ID.', 'disco' ), |
| 636 |
'type' => 'string', |
| 637 |
'context' => array( 'view', 'edit' ), |
| 638 |
'arg_options' => array( |
| 639 |
'sanitize_callback' => 'sanitize_text_field', |
| 640 |
), |
| 641 |
), |
| 642 |
'created_date' => array( |
| 643 |
'description' => __( "The date the object was published, in the site's timezone.", 'disco' ), |
| 644 |
'type' => 'string', |
| 645 |
'format' => 'date-time', |
| 646 |
'context' => array( 'view' ), |
| 647 |
'readonly' => true, |
| 648 |
), |
| 649 |
'modified_date' => array( |
| 650 |
'description' => __( "The date the object was published, in the site's timezone.", 'disco' ), |
| 651 |
'type' => 'string', |
| 652 |
'format' => 'date-time', |
| 653 |
'context' => array( 'view' ), |
| 654 |
'readonly' => true, |
| 655 |
), |
| 656 |
'ui' => array( |
| 657 |
'description' => __( 'Interface Settings.', 'disco' ), |
| 658 |
'type' => 'array', |
| 659 |
'context' => array( 'view' ), |
| 660 |
'readonly' => true, |
| 661 |
), |
| 662 |
'design_blocks' => array( |
| 663 |
'description' => __( 'Design Blocks.', 'disco' ), |
| 664 |
'type' => 'object', |
| 665 |
'context' => array( 'view', 'edit' ), |
| 666 |
), |
| 667 |
|
| 668 |
), |
| 669 |
); |
| 670 |
|
| 671 |
$this->schema = $schema; |
| 672 |
|
| 673 |
return $this->add_additional_fields_schema( $this->schema ); |
| 674 |
} |
| 675 |
|
| 676 |
/** |
| 677 |
* Validation callback for `discount_intent` parameter. |
| 678 |
* |
| 679 |
* @param int|string $value Value of the my-arg parameter. |
| 680 |
* @param \WP_REST_Request $request Current request object. |
| 681 |
* @param string $param The name of the parameter in this case, 'my-arg'. |
| 682 |
* @return \WP_Error|true True, if the data is valid, WP_Error otherwise. |
| 683 |
* @throws \Exception If the campaign does not exist. |
| 684 |
*/ |
| 685 |
public function validate_schema_properties( $value, $request, $param ) { |
| 686 |
$attributes = $request->get_attributes(); |
| 687 |
|
| 688 |
// This code won't execute because we have specified this argument as required. |
| 689 |
// If we reused this validation callback and did not have required args then this would fire. |
| 690 |
if ( ! isset( $attributes['args'][ $param ] ) ) { |
| 691 |
return new \WP_Error( 'rest_invalid_param', sprintf( '%s was not registered as a request argument.', $param ), array( 'status' => 400 ) ); |
| 692 |
} |
| 693 |
|
| 694 |
// Check to make sure our argument in proper type. |
| 695 |
$argument = $attributes['args'][ $param ]; |
| 696 |
|
| 697 |
if ( gettype( $value ) !== $argument['type'] ) { |
| 698 |
return new \WP_Error( 'rest_invalid_param', sprintf( '%1$s is not of type %2$s', $param, 'string' ), array( 'status' => 400 ) ); |
| 699 |
} |
| 700 |
|
| 701 |
// Make sure required arguments are not empty. |
| 702 |
if ( isset( $argument['required'] ) && true === $argument['required'] && empty( $value ) ) { |
| 703 |
return new \WP_Error( 'rest_invalid_param_value', sprintf( 'Value for %1$s is required', $param ), array( 'status' => 400 ) ); |
| 704 |
} |
| 705 |
|
| 706 |
$drop_down_checks = array( |
| 707 |
'discount_intent' => DiscountDropDown::discount_intents(), |
| 708 |
'discount_type' => DiscountDropDown::discount_types(), |
| 709 |
); |
| 710 |
|
| 711 |
if ( isset( $drop_down_checks[ $param ] ) && ! array_key_exists( $value, $drop_down_checks[ $param ] ) ) { |
| 712 |
return new \WP_Error( 'rest_invalid_param_value', sprintf( 'Value for %1$s is not valid', $param ), array( 'status' => 400 ) ); |
| 713 |
} |
| 714 |
|
| 715 |
return true; |
| 716 |
} |
| 717 |
|
| 718 |
/** |
| 719 |
* Retrieves the query params for collections. |
| 720 |
* |
| 721 |
* @return array |
| 722 |
*/ |
| 723 |
public function get_collection_params() { |
| 724 |
$params = parent::get_collection_params(); |
| 725 |
unset( $params['search'] ); |
| 726 |
|
| 727 |
return $params; |
| 728 |
} |
| 729 |
|
| 730 |
/** |
| 731 |
* Get the campaign if the ID is valid. |
| 732 |
* |
| 733 |
* @param int $id Supplied ID. |
| 734 |
* @return Object|\WP_Error |
| 735 |
*/ |
| 736 |
protected function get_campaign( $id ) { |
| 737 |
return ( new Campaign )->get_campaign( $id ); |
| 738 |
} |
| 739 |
|
| 740 |
/** |
| 741 |
* Prepares one item for create or update operation. |
| 742 |
* |
| 743 |
* @param \WP_REST_Request $request Full data about the request. |
| 744 |
* @return object |
| 745 |
*/ |
| 746 |
protected function prepare_item_for_database( $request ) { |
| 747 |
$valid_keys = array( |
| 748 |
'name', |
| 749 |
'discount_intent', |
| 750 |
'priority', |
| 751 |
'ui', |
| 752 |
'status', |
| 753 |
'products', |
| 754 |
'conditions', |
| 755 |
'discount_rules', |
| 756 |
'discount_label', |
| 757 |
'discount_valid_from', |
| 758 |
'discount_max_user', |
| 759 |
'discount_based_on', |
| 760 |
'bogo_type', |
| 761 |
'count_quantity_as', |
| 762 |
'free_item_selection', |
| 763 |
'discount_valid_to', |
| 764 |
'discount_method', |
| 765 |
'discount_coupon', |
| 766 |
'design_blocks', |
| 767 |
); |
| 768 |
|
| 769 |
$prepared = array(); |
| 770 |
|
| 771 |
foreach ( $valid_keys as $key ) { |
| 772 |
if ( ! isset( $request[ $key ] ) ) { |
| 773 |
continue; |
| 774 |
} |
| 775 |
|
| 776 |
$prepared[ $key ] = $request[ $key ]; |
| 777 |
} |
| 778 |
|
| 779 |
return (object) $prepared; |
| 780 |
} |
| 781 |
|
| 782 |
/** |
| 783 |
* Prepares links for the request. |
| 784 |
* |
| 785 |
* @param object $item Item object. |
| 786 |
* @return array Links for the given post. |
| 787 |
*/ |
| 788 |
protected function prepare_links( $item ) { |
| 789 |
$id = 0; |
| 790 |
|
| 791 |
if ( isset( $item->id ) ) { |
| 792 |
$id = $item->id; |
| 793 |
} |
| 794 |
|
| 795 |
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
| 796 |
|
| 797 |
return array( |
| 798 |
'self' => array( |
| 799 |
'href' => rest_url( trailingslashit( $base ) . $id ), |
| 800 |
), |
| 801 |
'collection' => array( |
| 802 |
'href' => rest_url( $base ), |
| 803 |
), |
| 804 |
); |
| 805 |
} |
| 806 |
|
| 807 |
} |
| 808 |
|