PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.2.18
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.2.18
1.4.15 1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 All 178 releases
disco / rest / CampaignApi.php

CampaignApi.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.2.18, at rest/CampaignApi.php

766 lines 21.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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' );
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['ui'] = array();
440
441 if ( ! empty( $item->ui ) && in_array( 'ui', $fields, true ) ) {
442 $data['ui'] = $item->ui;
443 }
444
445 $context = ! empty( $request['context'] ) && is_string( $request['context'] ) ? $request['context'] : 'view';//phpcs:ignore
446
447 $data = $this->filter_response_by_context( $data, $context );
448
449 $response = rest_ensure_response( $data );
450 $response->add_links( $this->prepare_links( $item ) );
451
452 return $response;
453 }
454
455 /**
456 * Retrieves the campaign schema, conforming to JSON Schema.
457 *
458 * @return array
459 * @throws \Exception If the campaign does not exist.
460 */
461 public function get_item_schema() {//phpcs:ignore
462 $schema = array(
463 '$schema' => 'http://json-schema.org/draft-04/schema#',
464 'title' => 'campaign',
465 'type' => 'object',
466 'properties' => array(
467 'id' => array(
468 'description' => __( 'Unique identifier for the campaign.', 'disco' ),
469 'type' => 'integer',
470 'context' => array( 'view', 'edit' ),
471 'readonly' => true,
472 ),
473 'name' => array(
474 'description' => __( 'Name of the campaign.', 'disco' ),
475 'type' => 'string',
476 'context' => array( 'view', 'edit' ),
477 'required' => true,
478 'arg_options' => array(
479 'sanitize_callback' => 'sanitize_text_field',
480 ),
481 ),
482 'priority' => array(
483 'description' => __( 'Campaign Priority.', 'disco' ),
484 'type' => 'string',
485 'required' => true,
486 'context' => array( 'view', 'edit' ),
487 'arg_options' => array(
488 'sanitize_callback' => 'sanitize_text_field',
489 ),
490 ),
491 'status' => array(
492 'description' => __( 'Campaign Status.', 'disco' ),
493 'type' => 'string',
494 'required' => true,
495 'context' => array( 'view', 'edit' ),
496 'arg_options' => array(
497 'sanitize_callback' => 'sanitize_text_field',
498 ),
499 ),
500 'products' => array(
501 'description' => __( 'Discount applicable products.', 'disco' ),
502 'type' => 'array',
503 'required' => true,
504 'context' => array( 'view', 'edit' ),
505 'arg_options' => array(
506 'sanitize_callback' => 'rest_sanitize_array',
507 ),
508 ),
509 'conditions' => array(
510 'description' => __( 'Conditions to match before get the discount.', 'disco' ),
511 'type' => 'array',
512 'context' => array( 'view', 'edit' ),
513 'arg_options' => array(
514 'sanitize_callback' => 'rest_sanitize_array',
515 ),
516 ),
517 'discount_intent' => array(
518 'description' => __( 'Discount Intention.', 'disco' ),
519 'type' => 'string',
520 'required' => true,
521 'context' => array( 'view', 'edit' ),
522 'arg_options' => array(
523 'sanitize_callback' => 'sanitize_text_field',
524 ),
525 ),
526 'discount_rules' => array(
527 'description' => __( 'Discount Rules.', 'disco' ),
528 'type' => 'array',
529 'context' => array( 'view', 'edit' ),
530 'arg_options' => array(
531 'sanitize_callback' => 'rest_sanitize_array',
532 ),
533 ),
534 'discount_based_on' => array(
535 'description' => __( 'Discount based on', 'disco' ),
536 'type' => 'string',
537 'context' => array( 'view', 'edit' ),
538 'arg_options' => array(
539 'sanitize_callback' => 'sanitize_text_field',
540 ),
541 ),
542 'discount_max_user' => array(
543 'description' => __( 'Maximum Discount Users', 'disco' ),
544 'type' => 'string',
545 'context' => array( 'view', 'edit' ),
546 'arg_options' => array(
547 'sanitize_callback' => 'sanitize_text_field',
548 ),
549 ),
550 'bogo_type' => array(
551 'description' => __( 'BOGO Type', 'disco' ),
552 'type' => 'string',
553 'context' => array( 'view', 'edit' ),
554 'arg_options' => array(
555 'sanitize_callback' => 'sanitize_text_field',
556 ),
557 ),
558 'discount_valid_from' => array(
559 'description' => __( 'Discount Start Date.', 'disco' ),
560 'type' => 'date-time',
561 'context' => array( 'view', 'edit' ),
562 'arg_options' => array(
563 'sanitize_callback' => 'sanitize_text_field',
564 ),
565 ),
566 'discount_valid_to' => array(
567 'description' => __( 'Discount End Date.', 'disco' ),
568 'type' => 'date-time',
569 'context' => array( 'view', 'edit' ),
570 'arg_options' => array(
571 'sanitize_callback' => 'sanitize_text_field',
572 ),
573 ),
574 'discount_method' => array(
575 'description' => __( 'Discount Method: automated or coupon.', 'disco' ),
576 'type' => 'string',
577 'required' => true,
578 'context' => array( 'view', 'edit' ),
579 'arg_options' => array(
580 'sanitize_callback' => 'sanitize_text_field',
581 ),
582 ),
583 'discount_coupon' => array(
584 'description' => __( 'Discount coupon code.', 'disco' ),
585 'type' => 'string',
586 'context' => array( 'view', 'edit' ),
587 'arg_options' => array(
588 'sanitize_callback' => 'sanitize_text_field',
589 ),
590 ),
591 'created_by' => array(
592 'description' => __( 'Created by User ID', 'disco' ),
593 'type' => 'string',
594 'context' => array( 'view', 'edit' ),
595 'arg_options' => array(
596 'sanitize_callback' => 'sanitize_text_field',
597 ),
598 ),
599
600 'modified_by' => array(
601 'description' => __( 'Modified by User ID.', 'disco' ),
602 'type' => 'string',
603 'context' => array( 'view', 'edit' ),
604 'arg_options' => array(
605 'sanitize_callback' => 'sanitize_text_field',
606 ),
607 ),
608 'created_date' => array(
609 'description' => __( "The date the object was published, in the site's timezone.", 'disco' ),
610 'type' => 'string',
611 'format' => 'date-time',
612 'context' => array( 'view' ),
613 'readonly' => true,
614 ),
615 'modified_date' => array(
616 'description' => __( "The date the object was published, in the site's timezone.", 'disco' ),
617 'type' => 'string',
618 'format' => 'date-time',
619 'context' => array( 'view' ),
620 'readonly' => true,
621 ),
622 'ui' => array(
623 'description' => __( 'Interface Settings.', 'disco' ),
624 'type' => 'array',
625 'context' => array( 'view' ),
626 'readonly' => true,
627 ),
628
629 ),
630 );
631
632 $this->schema = $schema;
633
634 return $this->add_additional_fields_schema( $this->schema );
635 }
636
637 /**
638 * Validation callback for `discount_intent` parameter.
639 *
640 * @param int|string $value Value of the my-arg parameter.
641 * @param \WP_REST_Request $request Current request object.
642 * @param string $param The name of the parameter in this case, 'my-arg'.
643 * @return \WP_Error|true True, if the data is valid, WP_Error otherwise.
644 * @throws \Exception If the campaign does not exist.
645 */
646 public function validate_schema_properties( $value, $request, $param ) {
647 $attributes = $request->get_attributes();
648
649 // This code won't execute because we have specified this argument as required.
650 // If we reused this validation callback and did not have required args then this would fire.
651 if ( ! isset( $attributes['args'][ $param ] ) ) {
652 return new \WP_Error( 'rest_invalid_param', sprintf( '%s was not registered as a request argument.', $param ), array( 'status' => 400 ) );
653 }
654
655 // Check to make sure our argument in proper type.
656 $argument = $attributes['args'][ $param ];
657
658 if ( gettype( $value ) !== $argument['type'] ) {
659 return new \WP_Error( 'rest_invalid_param', sprintf( '%1$s is not of type %2$s', $param, 'string' ), array( 'status' => 400 ) );
660 }
661
662 // Make sure required arguments are not empty.
663 if ( isset( $argument['required'] ) && true === $argument['required'] && empty( $value ) ) {
664 return new \WP_Error( 'rest_invalid_param_value', sprintf( 'Value for %1$s is required', $param ), array( 'status' => 400 ) );
665 }
666
667 $drop_down_checks = array(
668 'discount_intent' => DiscountDropDown::discount_intents(),
669 'discount_type' => DiscountDropDown::discount_types(),
670 );
671
672 if ( isset( $drop_down_checks[ $param ] ) && ! array_key_exists( $value, $drop_down_checks[ $param ] ) ) {
673 return new \WP_Error( 'rest_invalid_param_value', sprintf( 'Value for %1$s is not valid', $param ), array( 'status' => 400 ) );
674 }
675
676 return true;
677 }
678
679 /**
680 * Retrieves the query params for collections.
681 *
682 * @return array
683 */
684 public function get_collection_params() {
685 $params = parent::get_collection_params();
686 unset( $params['search'] );
687
688 return $params;
689 }
690
691 /**
692 * Get the campaign if the ID is valid.
693 *
694 * @param int $id Supplied ID.
695 * @return Object|\WP_Error
696 */
697 protected function get_campaign( $id ) {
698 return ( new Campaign )->get_campaign( $id );
699 }
700
701 /**
702 * Prepares one item for create or update operation.
703 *
704 * @param \WP_REST_Request $request Full data about the request.
705 * @return object
706 */
707 protected function prepare_item_for_database( $request ) {
708 $valid_keys = array(
709 'name',
710 'discount_intent',
711 'priority',
712 'ui',
713 'status',
714 'products',
715 'conditions',
716 'discount_rules',
717 'discount_label',
718 'discount_valid_from',
719 'discount_max_user',
720 'discount_based_on',
721 'bogo_type',
722 'discount_valid_to',
723 'discount_method',
724 'discount_coupon',
725 );
726
727 $prepared = array();
728
729 foreach ( $valid_keys as $key ) {
730 if ( ! isset( $request[ $key ] ) ) {
731 continue;
732 }
733
734 $prepared[ $key ] = $request[ $key ];
735 }
736
737 return (object) $prepared;
738 }
739
740 /**
741 * Prepares links for the request.
742 *
743 * @param object $item Item object.
744 * @return array Links for the given post.
745 */
746 protected function prepare_links( $item ) {
747 $id = 0;
748
749 if ( isset( $item->id ) ) {
750 $id = $item->id;
751 }
752
753 $base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
754
755 return array(
756 'self' => array(
757 'href' => rest_url( trailingslashit( $base ) . $id ),
758 ),
759 'collection' => array(
760 'href' => rest_url( $base ),
761 ),
762 );
763 }
764
765 }
766