Completed
Push — master ( 42ce64...5e069a )
by Claudio
19s
created

WC_REST_Coupons_Controller::update_item()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 14

Duplication

Lines 3
Ratio 10.34 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
nc 3
nop 1
dl 3
loc 29
rs 8.5806
c 1
b 0
f 0
1
<?php
2
/**
3
 * REST API Coupons controller
4
 *
5
 * Handles requests to the /coupons endpoint.
6
 *
7
 * @author   WooThemes
8
 * @category API
9
 * @package  WooCommerce/API
10
 * @since    2.6.0
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * REST API Coupons controller class.
19
 *
20
 * @package WooCommerce/API
21
 * @extends WC_REST_Posts_Controller
22
 */
23
class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
24
25
	/**
26
	 * Endpoint namespace.
27
	 *
28
	 * @var string
29
	 */
30
	protected $namespace = 'wc/v1';
31
32
	/**
33
	 * Route base.
34
	 *
35
	 * @var string
36
	 */
37
	protected $rest_base = 'coupons';
38
39
	/**
40
	 * Post type.
41
	 *
42
	 * @var string
43
	 */
44
	protected $post_type = 'shop_coupon';
45
46
	/**
47
	 * Order refunds actions.
48
	 */
49
	public function __construct() {
50
		add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'query_args' ), 10, 2 );
51
	}
52
53
	/**
54
	 * Register the routes for coupons.
55
	 */
56
	public function register_routes() {
57
		register_rest_route( $this->namespace, '/' . $this->rest_base, array(
58
			array(
59
				'methods'             => WP_REST_Server::READABLE,
60
				'callback'            => array( $this, 'get_items' ),
61
				'permission_callback' => array( $this, 'get_items_permissions_check' ),
62
				'args'                => $this->get_collection_params(),
63
			),
64
			array(
65
				'methods'             => WP_REST_Server::CREATABLE,
66
				'callback'            => array( $this, 'create_item' ),
67
				'permission_callback' => array( $this, 'create_item_permissions_check' ),
68
				'args'                => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
69
					'code' => array(
70
						'required' => true,
71
					),
72
				) ),
73
			),
74
			'schema' => array( $this, 'get_public_item_schema' ),
75
		) );
76
77
		register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
78
			array(
79
				'methods'             => WP_REST_Server::READABLE,
80
				'callback'            => array( $this, 'get_item' ),
81
				'permission_callback' => array( $this, 'get_item_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, 'update_item_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, 'delete_item_permissions_check' ),
96
				'args'                => array(
97
					'force' => array(
98
						'default'     => false,
99
						'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
100
					),
101
				),
102
			),
103
			'schema' => array( $this, 'get_public_item_schema' ),
104
		) );
105
106
		register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
107
			array(
108
				'methods'             => WP_REST_Server::EDITABLE,
109
				'callback'            => array( $this, 'batch_items' ),
110
				'permission_callback' => array( $this, 'batch_items_permissions_check' ),
111
				'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
112
			),
113
			'schema' => array( $this, 'get_public_batch_schema' ),
114
		) );
115
	}
116
117
	/**
118
	 * Query args.
119
	 *
120
	 * @param array $args
121
	 * @param WP_REST_Request $request
122
	 * @return array
123
	 */
124
	public function query_args( $args, $request ) {
125
		global $wpdb;
126
127
		if ( ! empty( $request['code'] ) ) {
128
			$id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $request['code'] ) );
129
130
			$args['post__in'] = array( $id );
131
		}
132
133
		return $args;
134
	}
135
136
	/**
137
	 * Prepare a single coupon output for response.
138
	 *
139
	 * @param WP_Post $post Post object.
140
	 * @param WP_REST_Request $request Request object.
141
	 * @return WP_REST_Response $data
142
	 */
143
	public function prepare_item_for_response( $post, $request ) {
144
		global $wpdb;
145
146
		// Get the coupon code.
147
		$code = $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM $wpdb->posts WHERE id = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $post->ID ) );
148
149
		$coupon = new WC_Coupon( $code );
150
151
		$data = array(
152
			'id'                           => $coupon->get_id(),
153
			'code'                         => $coupon->get_code(),
154
			'date_created'                 => wc_rest_prepare_date_response( $post->post_date_gmt ),
155
			'date_modified'                => wc_rest_prepare_date_response( $post->post_modified_gmt ),
156
			'discount_type'                => $coupon->get_discount_type(),
157
			'description'                  => $coupon->get_description(),
158
			'amount'                       => wc_format_decimal( $coupon->get_amount(), 2 ),
0 ignored issues
show
Bug introduced by
It seems like $coupon->get_amount() targeting WC_Coupon::get_amount() can also be of type array; however, wc_format_decimal() does only seem to accept double|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
159
			'expiry_date'                  => ( ! empty( $coupon->get_expiry_date() ) ) ? wc_rest_prepare_date_response( $coupon->get_expiry_date() ) : null,
160
			'usage_count'                  => (int) $coupon->get_usage_count(),
161
			'individual_use'               => $coupon->get_individual_use(),
162
			'product_ids'                  => array_map( 'absint', (array) $coupon->get_product_ids() ),
163
			'exclude_product_ids'          => array_map( 'absint', (array) $coupon->get_excluded_product_ids() ),
164
			'usage_limit'                  => ( ! empty( $coupon->get_usage_limit() ) ) ? $coupon->get_usage_limit() : null,
165
			'usage_limit_per_user'         => ( ! empty( $coupon->get_usage_limit_per_user() ) ) ? $coupon->get_usage_limit_per_user() : null,
166
			'limit_usage_to_x_items'       => (int) $coupon->get_limit_usage_to_x_items(),
167
			'free_shipping'                => $coupon->get_free_shipping(),
168
			'product_categories'           => array_map( 'absint', (array) $coupon->get_product_categories() ),
169
			'excluded_product_categories'  => array_map( 'absint', (array) $coupon->get_excluded_product_categories() ),
170
			'exclude_sale_items'           => $coupon->get_exclude_sale_items(),
171
			'minimum_amount'               => wc_format_decimal( $coupon->get_minimum_amount(), 2 ),
0 ignored issues
show
Bug introduced by
It seems like $coupon->get_minimum_amount() targeting WC_Coupon::get_minimum_amount() can also be of type array; however, wc_format_decimal() does only seem to accept double|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
172
			'maximum_amount'               => wc_format_decimal( $coupon->get_maximum_amount(), 2 ),
0 ignored issues
show
Bug introduced by
It seems like $coupon->get_maximum_amount() targeting WC_Coupon::get_maximum_amount() can also be of type array; however, wc_format_decimal() does only seem to accept double|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
173
			'email_restrictions'           => $coupon->get_email_restrictions(),
174
			'used_by'                      => $coupon->get_used_by(),
175
		);
176
177
		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
178
		$data    = $this->add_additional_fields_to_object( $data, $request );
179
		$data    = $this->filter_response_by_context( $data, $context );
180
181
		// Wrap the data in a response object.
182
		$response = rest_ensure_response( $data );
183
184
		$response->add_links( $this->prepare_links( $post ) );
185
186
		/**
187
		 * Filter the data for a response.
188
		 *
189
		 * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
190
		 * prepared for the response.
191
		 *
192
		 * @param WP_REST_Response   $response   The response object.
193
		 * @param WP_Post            $post       Post object.
194
		 * @param WP_REST_Request    $request    Request object.
195
		 */
196
		return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request );
197
	}
198
199
	/**
200
	 * Prepare a single coupon for create or update.
201
	 *
202
	 * @param WP_REST_Request $request Request object.
203
	 * @return WP_Error|stdClass $data Post object.
204
	 */
205
	protected function prepare_item_for_database( $request ) {
206
		global $wpdb;
207
208
		// ID.
209
		if ( isset( $request['id'] ) ) {
210
			$code     = $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM $wpdb->posts WHERE id = %d AND post_type = 'shop_coupon' AND post_status = 'publish'", $request['id'] ) );
211
			$coupon   = new WC_Coupon( $code );
212
		} else {
213
			$coupon   = new WC_Coupon();
214
		}
215
216
		$schema = $this->get_item_schema();
217
218
		// Validate required POST fields.
219
		if ( 'POST' === $request->get_method() && empty( $coupon->get_id() ) ) {
220
			if ( empty( $request['code'] ) ) {
221
				return new WP_Error( 'woocommerce_rest_empty_coupon_code', sprintf( __( 'The coupon code cannot be empty.', 'woocommerce' ), 'code' ), array( 'status' => 400 ) );
222
			}
223
		}
224
225
		// Code.
226
		if ( ! empty( $schema['properties']['code'] ) && ! empty( $request['code'] ) ) {
227
			$coupon_code = apply_filters( 'woocommerce_coupon_code', $request['code'] );
228
			$id = $coupon->get_id() ? $coupon->get_id() : 0;
229
230
			// Check for duplicate coupon codes.
231
			$coupon_found = $wpdb->get_var( $wpdb->prepare( "
232
				SELECT $wpdb->posts.ID
233
				FROM $wpdb->posts
234
				WHERE $wpdb->posts.post_type = 'shop_coupon'
235
				AND $wpdb->posts.post_status = 'publish'
236
				AND $wpdb->posts.post_title = '%s'
237
				AND $wpdb->posts.ID != %s
238
			 ", $coupon_code, $id ) );
239
240
			if ( $coupon_found ) {
241
				return new WP_Error( 'woocommerce_rest_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) );
242
			}
243
244
			$coupon->set_code( $coupon_code );
245
		}
246
247
		// Coupon description (excerpt).
248
		if ( ! empty( $schema['properties']['description'] ) && isset( $request['description'] ) ) {
249
			$coupon->set_description( wp_filter_post_kses( $request['description'] ) );
250
		}
251
252
		/**
253
		 * Filter the query_vars used in `get_items` for the constructed query.
254
		 *
255
		 * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
256
		 * prepared for insertion.
257
		 *
258
		 * @param stdClass        $data An object representing a single item prepared
259
		 *                                       for inserting or updating the database.
260
		 * @param WP_REST_Request $request       Request object.
261
		 */
262
		return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $coupon, $request );
263
	}
264
265
	/**
266
	 * Create a single item.
267
	 *
268
	 * @param WP_REST_Request $request Full details about the request.
269
	 * @return WP_Error|WP_REST_Response
270
	 */
271 View Code Duplication
	public function create_item( $request ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
272
		if ( ! empty( $request['id'] ) ) {
273
			return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
274
		}
275
276
		$coupon_id = $this->save_coupon( $request );
277
		if ( is_wp_error( $coupon_id ) ) {
278
			return $coupon_id;
279
		}
280
281
		$post = get_post( $coupon_id );
282
		$this->update_additional_fields_for_object( $post, $request );
283
284
		$this->add_post_meta_fields( $post, $request );
285
286
		/**
287
		 * Fires after a single item is created or updated via the REST API.
288
		 *
289
		 * @param object          $post      Inserted object (not a WP_Post object).
290
		 * @param WP_REST_Request $request   Request object.
291
		 * @param boolean         $creating  True when creating item, false when updating.
292
		 */
293
		do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
294
		$request->set_param( 'context', 'edit' );
295
		$response = $this->prepare_item_for_response( $post, $request );
296
		$response = rest_ensure_response( $response );
297
		$response->set_status( 201 );
298
		$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) );
299
300
		return $response;
301
	}
302
303
	/**
304
	 * Update a single coupon.
305
	 *
306
	 * @param WP_REST_Request $request Full details about the request.
307
	 * @return WP_Error|WP_REST_Response
308
	 */
309
	public function update_item( $request ) {
310
		$post_id = (int) $request['id'];
311
312 View Code Duplication
		if ( empty( $post_id ) || $this->post_type !== get_post_type( $post_id ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
313
			return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
314
		}
315
316
		$coupon_id = $this->save_coupon( $request );
317
		if ( is_wp_error( $coupon_id ) ) {
318
			return $coupon_id;
319
		}
320
321
		$post = get_post( $coupon_id );
322
		$this->update_additional_fields_for_object( $post, $request );
323
324
		$this->update_post_meta_fields( $post, $request );
325
326
		/**
327
		 * Fires after a single item is created or updated via the REST API.
328
		 *
329
		 * @param object          $post      Inserted object (not a WP_Post object).
330
		 * @param WP_REST_Request $request   Request object.
331
		 * @param boolean         $creating  True when creating item, false when updating.
332
		 */
333
		do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );
334
		$request->set_param( 'context', 'edit' );
335
		$response = $this->prepare_item_for_response( $post, $request );
336
		return rest_ensure_response( $response );
337
	}
338
339
	/**
340
	 * Saves a coupon to the database.
341
	 */
342
	public function save_coupon( $request ) {
343
		$coupon = $this->prepare_item_for_database( $request );
344
		$coupon->save();
345
		return $coupon->get_id();
346
	}
347
348
	/**
349
	 * Expiry date format.
350
	 *
351
	 * @param string $expiry_date
352
	 * @return string
353
	 */
354
	protected function get_coupon_expiry_date( $expiry_date ) {
355
		if ( '' != $expiry_date ) {
356
			return date( 'Y-m-d', strtotime( $expiry_date ) );
357
		}
358
359
		return '';
360
	}
361
362
	/**
363
	 * Add post meta fields.
364
	 *
365
	 * @param WP_Post $post
366
	 * @param WP_REST_Request $request
367
	 * @return bool|WP_Error
368
	 */
369
	protected function add_post_meta_fields( $post, $request ) {
370
		$data = array_filter( $request->get_params() );
371
372
		$defaults = array(
373
			'discount_type'                => 'fixed_cart',
374
			'amount'                       => 0,
375
			'individual_use'               => false,
376
			'product_ids'                  => array(),
377
			'exclude_product_ids'          => array(),
378
			'usage_limit'                  => '',
379
			'usage_limit_per_user'         => '',
380
			'limit_usage_to_x_items'       => '',
381
			'usage_count'                  => '',
382
			'expiry_date'                  => '',
383
			'free_shipping'                => false,
384
			'product_categories'           => array(),
385
			'excluded_product_categories'  => array(),
386
			'exclude_sale_items'           => false,
387
			'minimum_amount'               => '',
388
			'maximum_amount'               => '',
389
			'email_restrictions'           => array(),
390
			'description'                  => ''
391
		);
392
393
		$data = wp_parse_args( $data, $defaults );
394
395
		if ( ! empty( $post->post_title ) ) {
396
			$coupon = new WC_Coupon( $post->post_title );
397
			// Set coupon meta.
398
			$coupon->set_discount_type( $data['discount_type'] );
399
			$coupon->set_amount( $data['amount'] );
400
			$coupon->set_individual_use( $data['individual_use'] );
401
			$coupon->set_product_ids( $data['product_ids'] );
402
			$coupon->set_excluded_product_ids( $data['exclude_product_ids'] );
403
			$coupon->set_usage_limit( $data['usage_limit'] );
404
			$coupon->set_usage_limit_per_user( $data['usage_limit_per_user'] );
405
			$coupon->set_limit_usage_to_x_items( $data['limit_usage_to_x_items'] );
406
			$coupon->set_usage_count( $data['usage_count'] );
407
			$coupon->set_expiry_date( $data['expiry_date'] );
408
			$coupon->set_free_shipping( $data['free_shipping'] );
409
			$coupon->set_product_categories( $data['product_categories'] );
410
			$coupon->set_excluded_product_categories( $data['excluded_product_categories'] );
411
			$coupon->set_exclude_sale_items( $data['exclude_sale_items'] );
412
			$coupon->set_minimum_amount( $data['minimum_amount'] );
413
			$coupon->set_maximum_amount( $data['maximum_amount'] );
414
			$coupon->set_email_restrictions( $data['email_restrictions'] );
415
			$coupon->save();
416
		}
417
418
		return true;
419
	}
420
421
	/**
422
	 * Update post meta fields.
423
	 *
424
	 * @param WP_Post $post
425
	 * @param WP_REST_Request $request
426
	 * @return bool|WP_Error
427
	 */
428
	protected function update_post_meta_fields( $post, $request ) {
429
430
		$coupon = new WC_Coupon( $post->post_title );
431
432
		if ( isset( $request['amount'] ) ) {
433
			$coupon->set_amount( $request['amount'] );
434
		}
435
436
		if ( isset( $request['individual_use'] ) ) {
437
			$coupon->set_individual_use( $request['individual_use'] );
438
		}
439
440
		if ( isset( $request['product_ids'] ) ) {
441
			$coupon->set_product_ids( $request['product_ids'] );
442
		}
443
444
		if ( isset( $request['exclude_product_ids'] ) ) {
445
			$coupon->set_excluded_product_ids( $request['exclude_product_ids'] );
446
		}
447
448
		if ( isset( $request['usage_limit'] ) ) {
449
			$coupon->set_usage_limit( $request['usage_limit'] );
450
		}
451
452
		if ( isset( $request['usage_limit_per_user'] ) ) {
453
			$coupon->set_usage_limit_per_user( $request['usage_limit_per_user'] );
454
		}
455
456
		if ( isset( $request['limit_usage_to_x_items'] ) ) {
457
			$coupon->set_limit_usage_to_x_items( $request['limit_usage_to_x_items'] );
458
		}
459
460
		if ( isset( $request['usage_count'] ) ) {
461
			$coupon->set_usage_count( $request['usage_count'] );
462
		}
463
464
		if ( isset( $request['expiry_date'] ) ) {
465
			$coupon->set_expiry_date( $request['expiry_date'] );
466
		}
467
468
		if ( isset( $request['free_shipping'] ) ) {
469
			$coupon->set_free_shipping( $request['free_shipping'] );
470
		}
471
472
		if ( isset( $request['product_categories'] ) ) {
473
			$coupon->set_product_categories( $request['product_categories'] );
474
		}
475
476
		if ( isset( $request['excluded_product_categories'] ) ) {
477
			$coupon->set_excluded_product_categories( $request['excluded_product_categories'] );
478
		}
479
480
		if ( isset( $request['exclude_sale_items'] ) ) {
481
			$coupon->set_exclude_sale_items( $request['exclude_sale_items'] );
482
		}
483
484
		if ( isset( $request['minimum_amount'] ) ) {
485
			$coupon->set_minimum_amount( $request['minimum_amount'] );
486
		}
487
488
		if ( isset( $request['maximum_amount'] ) ) {
489
			$coupon->set_maximum_amount( $request['maximum_amount'] );
490
		}
491
492
		if ( isset( $request['email_restrictions'] ) ) {
493
			$coupon->set_email_restrictions( $request['email_restrictions'] );
494
		}
495
496
		$coupon->save();
497
498
		return true;
499
	}
500
501
	/**
502
	 * Get the Coupon's schema, conforming to JSON Schema.
503
	 *
504
	 * @return array
505
	 */
506
	public function get_item_schema() {
507
508
		$schema = array(
509
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
510
			'title'      => $this->post_type,
511
			'type'       => 'object',
512
			'properties' => array(
513
				'id' => array(
514
					'description' => __( 'Unique identifier for the object.', 'woocommerce' ),
515
					'type'        => 'integer',
516
					'context'     => array( 'view', 'edit' ),
517
					'readonly'    => true,
518
				),
519
				'code' => array(
520
					'description' => __( 'Coupon code.', 'woocommerce' ),
521
					'type'        => 'string',
522
					'context'     => array( 'view', 'edit' ),
523
				),
524
				'date_created' => array(
525
					'description' => __( "The date the coupon was created, in the site's timezone.", 'woocommerce' ),
526
					'type'        => 'date-time',
527
					'context'     => array( 'view', 'edit' ),
528
					'readonly'    => true,
529
				),
530
				'date_modified' => array(
531
					'description' => __( "The date the coupon was last modified, in the site's timezone.", 'woocommerce' ),
532
					'type'        => 'date-time',
533
					'context'     => array( 'view', 'edit' ),
534
					'readonly'    => true,
535
				),
536
				'description' => array(
537
					'description' => __( 'Coupon description.', 'woocommerce' ),
538
					'type'        => 'string',
539
					'context'     => array( 'view', 'edit' ),
540
				),
541
				'discount_type' => array(
542
					'description' => __( 'Determines the type of discount that will be applied.', 'woocommerce' ),
543
					'type'        => 'string',
544
					'default'     => 'fixed_cart',
545
					'enum'        => array_keys( wc_get_coupon_types() ),
546
					'context'     => array( 'view', 'edit' ),
547
				),
548
				'amount' => array(
549
					'description' => __( 'The amount of discount.', 'woocommerce' ),
550
					'type'        => 'string',
551
					'context'     => array( 'view', 'edit' ),
552
				),
553
				'expiry_date' => array(
554
					'description' => __( 'UTC DateTime when the coupon expires.', 'woocommerce' ),
555
					'type'        => 'string',
556
					'context'     => array( 'view', 'edit' ),
557
				),
558
				'usage_count' => array(
559
					'description' => __( 'Number of times the coupon has been used already.', 'woocommerce' ),
560
					'type'        => 'integer',
561
					'context'     => array( 'view', 'edit' ),
562
					'readonly'    => true,
563
				),
564
				'individual_use' => array(
565
					'description' => __( 'Whether coupon can only be used individually.', 'woocommerce' ),
566
					'type'        => 'boolean',
567
					'default'     => false,
568
					'context'     => array( 'view', 'edit' ),
569
				),
570
				'product_ids' => array(
571
					'description' => __( "List of product ID's the coupon can be used on.", 'woocommerce' ),
572
					'type'        => 'array',
573
					'context'     => array( 'view', 'edit' ),
574
				),
575
				'exclude_product_ids' => array(
576
					'description' => __( "List of product ID's the coupon cannot be used on.", 'woocommerce' ),
577
					'type'        => 'array',
578
					'context'     => array( 'view', 'edit' ),
579
				),
580
				'usage_limit' => array(
581
					'description' => __( 'How many times the coupon can be used.', 'woocommerce' ),
582
					'type'        => 'integer',
583
					'context'     => array( 'view', 'edit' ),
584
				),
585
				'usage_limit_per_user' => array(
586
					'description' => __( 'How many times the coupon can be used per customer.', 'woocommerce' ),
587
					'type'        => 'integer',
588
					'context'     => array( 'view', 'edit' ),
589
				),
590
				'limit_usage_to_x_items' => array(
591
					'description' => __( 'Max number of items in the cart the coupon can be applied to.', 'woocommerce' ),
592
					'type'        => 'integer',
593
					'context'     => array( 'view', 'edit' ),
594
				),
595
				'free_shipping' => array(
596
					'description' => __( 'Define if can be applied for free shipping.', 'woocommerce' ),
597
					'type'        => 'boolean',
598
					'default'     => false,
599
					'context'     => array( 'view', 'edit' ),
600
				),
601
				'product_categories' => array(
602
					'description' => __( "List of category ID's the coupon applies to.", 'woocommerce' ),
603
					'type'        => 'array',
604
					'context'     => array( 'view', 'edit' ),
605
				),
606
				'excluded_product_categories' => array(
607
					'description' => __( "List of category ID's the coupon does not apply to.", 'woocommerce' ),
608
					'type'        => 'array',
609
					'context'     => array( 'view', 'edit' ),
610
				),
611
				'exclude_sale_items' => array(
612
					'description' => __( 'Define if should not apply when have sale items.', 'woocommerce' ),
613
					'type'        => 'boolean',
614
					'default'     => false,
615
					'context'     => array( 'view', 'edit' ),
616
				),
617
				'minimum_amount' => array(
618
					'description' => __( 'Minimum order amount that needs to be in the cart before coupon applies.', 'woocommerce' ),
619
					'type'        => 'string',
620
					'context'     => array( 'view', 'edit' ),
621
				),
622
				'maximum_amount' => array(
623
					'description' => __( 'Maximum order amount allowed when using the coupon.', 'woocommerce' ),
624
					'type'        => 'string',
625
					'context'     => array( 'view', 'edit' ),
626
				),
627
				'email_restrictions' => array(
628
					'description' => __( 'List of email addresses that can use this coupon.', 'woocommerce' ),
629
					'type'        => 'array',
630
					'context'     => array( 'view', 'edit' ),
631
				),
632
				'used_by' => array(
633
					'description' => __( 'List of user IDs who have used the coupon.', 'woocommerce' ),
634
					'type'        => 'array',
635
					'context'     => array( 'view', 'edit' ),
636
					'readonly'    => true,
637
				),
638
			),
639
		);
640
641
		return $this->add_additional_fields_schema( $schema );
642
	}
643
644
	/**
645
	 * Get the query params for collections of attachments.
646
	 *
647
	 * @return array
648
	 */
649 View Code Duplication
	public function get_collection_params() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
650
		$params = parent::get_collection_params();
651
652
		$params['code'] = array(
653
			'description'       => __( 'Limit result set to resources with a specific code.', 'woocommerce' ),
654
			'type'              => 'string',
655
			'sanitize_callback' => 'sanitize_text_field',
656
			'validate_callback' => 'rest_validate_request_arg',
657
		);
658
659
		return $params;
660
	}
661
}
662