update_item_permissions_check()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
eloc 5
nc 2
nop 1
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
/**
8
 * Abstract Rest Posts Controler Class
9
 *
10
 * @author   WooThemes
11
 * @category API
12
 * @package  WooCommerce/Abstracts
13
 * @version  2.6.0
14
 */
15
abstract class WC_REST_Posts_Controller extends WC_REST_Controller {
16
17
	/**
18
	 * Endpoint namespace.
19
	 *
20
	 * @var string
21
	 */
22
	protected $namespace = 'wc/v1';
23
24
	/**
25
	 * Route base.
26
	 *
27
	 * @var string
28
	 */
29
	protected $rest_base = '';
30
31
	/**
32
	 * Post type.
33
	 *
34
	 * @var string
35
	 */
36
	protected $post_type = '';
37
38
	/**
39
	 * Controls visibility on frontend.
40
	 *
41
	 * @var string
42
	 */
43
	protected $public = false;
44
45
	/**
46
	 * Check if a given request has access to read items.
47
	 *
48
	 * @param  WP_REST_Request $request Full details about the request.
49
	 * @return WP_Error|boolean
50
	 */
51
	public function get_items_permissions_check( $request ) {
52
		if ( ! wc_rest_check_post_permissions( $this->post_type, 'read' ) ) {
53
			return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
54
		}
55
56
		return true;
57
	}
58
59
	/**
60
	 * Check if a given request has access to create an item.
61
	 *
62
	 * @param  WP_REST_Request $request Full details about the request.
63
	 * @return WP_Error|boolean
64
	 */
65
	public function create_item_permissions_check( $request ) {
66
		if ( ! wc_rest_check_post_permissions( $this->post_type, 'create' ) ) {
67
			return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
68
		}
69
70
		return true;
71
	}
72
73
	/**
74
	 * Check if a given request has access to read an item.
75
	 *
76
	 * @param  WP_REST_Request $request Full details about the request.
77
	 * @return WP_Error|boolean
78
	 */
79 View Code Duplication
	public function get_item_permissions_check( $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...
80
		$post = get_post( (int) $request['id'] );
81
82
		if ( $post && ! wc_rest_check_post_permissions( $this->post_type, 'read', $post->ID ) ) {
83
			return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
84
		}
85
86
		return true;
87
	}
88
89
	/**
90
	 * Check if a given request has access to update an item.
91
	 *
92
	 * @param  WP_REST_Request $request Full details about the request.
93
	 * @return WP_Error|boolean
94
	 */
95 View Code Duplication
	public function update_item_permissions_check( $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...
96
		$post = get_post( $request['id'] );
97
98
		if ( $post && ! wc_rest_check_post_permissions( $this->post_type, 'edit', $post->ID ) ) {
99
			return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
100
		}
101
102
		return true;
103
	}
104
105
	/**
106
	 * Check if a given request has access to delete an item.
107
	 *
108
	 * @param  WP_REST_Request $request Full details about the request.
109
	 * @return bool|WP_Error
110
	 */
111 View Code Duplication
	public function delete_item_permissions_check( $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...
112
		$post = get_post( $request['id'] );
113
114
		if ( $post && ! wc_rest_check_post_permissions( $this->post_type, 'delete', $post->ID ) ) {
115
			return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
116
		}
117
118
		return true;
119
	}
120
121
	/**
122
	 * Check if a given request has access batch create, update and delete items.
123
	 *
124
	 * @param  WP_REST_Request $request Full details about the request.
125
	 * @return boolean
126
	 */
127
	public function batch_items_permissions_check( $request ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
		if ( ! wc_rest_check_post_permissions( $this->post_type, 'batch' ) ) {
129
			return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to manipule this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
130
		}
131
132
		return true;
133
	}
134
135
	/**
136
	 * Get a single item.
137
	 *
138
	 * @param WP_REST_Request $request Full details about the request.
139
	 * @return WP_Error|WP_REST_Response
140
	 */
141
	public function get_item( $request ) {
142
		$id   = (int) $request['id'];
143
		$post = get_post( $id );
144
145
		if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
146
			return new WP_Error( "woocommerce_rest_invalid_{$this->post_type}_id", __( 'Invalid id.', 'woocommerce' ), array( 'status' => 404 ) );
147
		}
148
149
		$data = $this->prepare_item_for_response( $post, $request );
150
		$response = rest_ensure_response( $data );
151
152
		if ( $this->public ) {
153
			$response->link_header( 'alternate', get_permalink( $id ), array( 'type' => 'text/html' ) );
154
		}
155
156
		return $response;
157
	}
158
159
	/**
160
	 * Create a single item.
161
	 *
162
	 * @param WP_REST_Request $request Full details about the request.
163
	 * @return WP_Error|WP_REST_Response
164
	 */
165
	public function create_item( $request ) {
166
		if ( ! empty( $request['id'] ) ) {
167
			return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
168
		}
169
170
		$post = $this->prepare_item_for_database( $request );
171
		if ( is_wp_error( $post ) ) {
172
			return $post;
173
		}
174
175
		$post->post_type = $this->post_type;
176
		$post_id         = wp_insert_post( $post, true );
177
178 View Code Duplication
		if ( is_wp_error( $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...
179
180
			if ( in_array( $post_id->get_error_code(), array( 'db_insert_error' ) ) ) {
181
				$post_id->add_data( array( 'status' => 500 ) );
182
			} else {
183
				$post_id->add_data( array( 'status' => 400 ) );
184
			}
185
			return $post_id;
186
		}
187
		$post->ID = $post_id;
188
		$post     = get_post( $post_id );
189
190
		$this->update_additional_fields_for_object( $post, $request );
191
192
		// Add meta fields.
193
		$meta_fields = $this->add_post_meta_fields( $post, $request );
194
		if ( is_wp_error( $meta_fields ) ) {
195
			// Remove post.
196
			$this->delete_post( $post );
197
198
			return $meta_fields;
199
		}
200
201
		/**
202
		 * Fires after a single item is created or updated via the REST API.
203
		 *
204
		 * @param object          $post      Inserted object (not a WP_Post object).
205
		 * @param WP_REST_Request $request   Request object.
206
		 * @param boolean         $creating  True when creating item, false when updating.
207
		 */
208
		do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
209
210
		$request->set_param( 'context', 'edit' );
211
		$response = $this->prepare_item_for_response( $post, $request );
212
		$response = rest_ensure_response( $response );
213
		$response->set_status( 201 );
214
		$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post_id ) ) );
215
216
		return $response;
217
	}
218
219
	/**
220
	 * Add post meta fields.
221
	 *
222
	 * @param WP_Post $post
223
	 * @param WP_REST_Request $request
224
	 * @return bool|WP_Error
225
	 */
226
	protected function add_post_meta_fields( $post, $request ) {
227
		return true;
228
	}
229
230
	/**
231
	 * Delete post.
232
	 *
233
	 * @param WP_Post $post
234
	 */
235
	protected function delete_post( $post ) {
236
		wp_delete_post( $post->ID, true );
237
	}
238
239
	/**
240
	 * Update a single post.
241
	 *
242
	 * @param WP_REST_Request $request Full details about the request.
243
	 * @return WP_Error|WP_REST_Response
244
	 */
245
	public function update_item( $request ) {
246
		$id   = (int) $request['id'];
247
		$post = get_post( $id );
248
249
		if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
250
			return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
251
		}
252
253
		$post = $this->prepare_item_for_database( $request );
254
		if ( is_wp_error( $post ) ) {
255
			return $post;
256
		}
257
		// Convert the post object to an array, otherwise wp_update_post will expect non-escaped input.
258
		$post_id = wp_update_post( (array) $post, true );
259 View Code Duplication
		if ( is_wp_error( $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...
260
			if ( in_array( $post_id->get_error_code(), array( 'db_update_error' ) ) ) {
261
				$post_id->add_data( array( 'status' => 500 ) );
262
			} else {
263
				$post_id->add_data( array( 'status' => 400 ) );
264
			}
265
			return $post_id;
266
		}
267
268
		$post = get_post( $post_id );
269
		$this->update_additional_fields_for_object( $post, $request );
270
271
		// Update meta fields.
272
		$meta_fields = $this->update_post_meta_fields( $post, $request );
273
		if ( is_wp_error( $meta_fields ) ) {
274
			return $meta_fields;
275
		}
276
277
		/**
278
		 * Fires after a single item is created or updated via the REST API.
279
		 *
280
		 * @param object          $post      Inserted object (not a WP_Post object).
281
		 * @param WP_REST_Request $request   Request object.
282
		 * @param boolean         $creating  True when creating item, false when updating.
283
		 */
284
		do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );
285
286
		$request->set_param( 'context', 'edit' );
287
		$response = $this->prepare_item_for_response( $post, $request );
288
		return rest_ensure_response( $response );
289
	}
290
291
	/**
292
	 * Get a collection of posts.
293
	 *
294
	 * @param WP_REST_Request $request Full details about the request.
295
	 * @return WP_Error|WP_REST_Response
296
	 */
297
	public function get_items( $request ) {
298
		$args                         = array();
299
		$args['offset']               = $request['offset'];
300
		$args['order']                = $request['order'];
301
		$args['orderby']              = $request['orderby'];
302
		$args['paged']                = $request['page'];
303
		$args['post__in']             = $request['include'];
304
		$args['post__not_in']         = $request['exclude'];
305
		$args['posts_per_page']       = $request['per_page'];
306
		$args['name']                 = $request['slug'];
307
		$args['post_parent__in']      = $request['parent'];
308
		$args['post_parent__not_in']  = $request['parent_exclude'];
309
		$args['s']                    = $request['search'];
310
311
		$args['date_query'] = array();
312
		// Set before into date query. Date query must be specified as an array of an array.
313
		if ( isset( $request['before'] ) ) {
314
			$args['date_query'][0]['before'] = $request['before'];
315
		}
316
317
		// Set after into date query. Date query must be specified as an array of an array.
318
		if ( isset( $request['after'] ) ) {
319
			$args['date_query'][0]['after'] = $request['after'];
320
		}
321
322
		if ( is_array( $request['filter'] ) ) {
323
			$args = array_merge( $args, $request['filter'] );
324
			unset( $args['filter'] );
325
		}
326
327
		// Force the post_type argument, since it's not a user input variable.
328
		$args['post_type'] = $this->post_type;
329
330
		/**
331
		 * Filter the query arguments for a request.
332
		 *
333
		 * Enables adding extra arguments or setting defaults for a post
334
		 * collection request.
335
		 *
336
		 * @param array           $args    Key value array of query var to query value.
337
		 * @param WP_REST_Request $request The request used.
338
		 */
339
		$args = apply_filters( "woocommerce_rest_{$this->post_type}_query", $args, $request );
340
		$query_args = $this->prepare_items_query( $args, $request );
341
342
		$posts_query = new WP_Query();
343
		$query_result = $posts_query->query( $query_args );
344
345
		$posts = array();
346
		foreach ( $query_result as $post ) {
347
			if ( ! wc_rest_check_post_permissions( $this->post_type, 'read', $post->ID ) ) {
348
				continue;
349
			}
350
351
			$data = $this->prepare_item_for_response( $post, $request );
352
			$posts[] = $this->prepare_response_for_collection( $data );
353
		}
354
355
		$page = (int) $query_args['paged'];
356
		$total_posts = $posts_query->found_posts;
357
358
		if ( $total_posts < 1 ) {
359
			// Out-of-bounds, run the query again without LIMIT for total count
360
			unset( $query_args['paged'] );
361
			$count_query = new WP_Query();
362
			$count_query->query( $query_args );
363
			$total_posts = $count_query->found_posts;
364
		}
365
366
		$max_pages = ceil( $total_posts / (int) $query_args['posts_per_page'] );
367
368
		$response = rest_ensure_response( $posts );
369
		$response->header( 'X-WP-Total', (int) $total_posts );
370
		$response->header( 'X-WP-TotalPages', (int) $max_pages );
371
372
		$request_params = $request->get_query_params();
373
		if ( ! empty( $request_params['filter'] ) ) {
374
			// Normalize the pagination params.
375
			unset( $request_params['filter']['posts_per_page'] );
376
			unset( $request_params['filter']['paged'] );
377
		}
378
		$base = add_query_arg( $request_params, rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
379
380 View Code Duplication
		if ( $page > 1 ) {
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...
381
			$prev_page = $page - 1;
382
			if ( $prev_page > $max_pages ) {
383
				$prev_page = $max_pages;
384
			}
385
			$prev_link = add_query_arg( 'page', $prev_page, $base );
386
			$response->link_header( 'prev', $prev_link );
387
		}
388 View Code Duplication
		if ( $max_pages > $page ) {
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...
389
			$next_page = $page + 1;
390
			$next_link = add_query_arg( 'page', $next_page, $base );
391
			$response->link_header( 'next', $next_link );
392
		}
393
394
		return $response;
395
	}
396
397
	/**
398
	 * Delete a single item.
399
	 *
400
	 * @param WP_REST_Request $request Full details about the request.
401
	 * @return WP_REST_Response|WP_Error
402
	 */
403
	public function delete_item( $request ) {
404
		$id    = (int) $request['id'];
405
		$force = (bool) $request['force'];
406
		$post  = get_post( $id );
407
408
		if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
409
			return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid post id.', 'woocommerce' ), array( 'status' => 404 ) );
410
		}
411
412
		$supports_trash = EMPTY_TRASH_DAYS > 0;
413
414
		/**
415
		 * Filter whether an item is trashable.
416
		 *
417
		 * Return false to disable trash support for the item.
418
		 *
419
		 * @param boolean $supports_trash Whether the item type support trashing.
420
		 * @param WP_Post $post           The Post object being considered for trashing support.
421
		 */
422
		$supports_trash = apply_filters( "woocommerce_rest_{$this->post_type}_trashable", $supports_trash, $post );
423
424
		if ( ! wc_rest_check_post_permissions( $this->post_type, 'delete', $post->ID ) ) {
425
			return new WP_Error( "woocommerce_rest_user_cannot_delete_{$this->post_type}", sprintf( __( 'Sorry, you are not allowed to delete %s.', 'woocommerce' ), $this->post_type ), array( 'status' => rest_authorization_required_code() ) );
426
		}
427
428
		$request->set_param( 'context', 'edit' );
429
		$response = $this->prepare_item_for_response( $post, $request );
430
431
		// If we're forcing, then delete permanently.
432
		if ( $force ) {
433
			$result = wp_delete_post( $id, true );
434
		} else {
435
			// If we don't support trashing for this type, error out.
436
			if ( ! $supports_trash ) {
437
				return new WP_Error( 'woocommerce_rest_trash_not_supported', sprintf( __( 'The %s does not support trashing.', 'woocommerce' ), $this->post_type ), array( 'status' => 501 ) );
438
			}
439
440
			// Otherwise, only trash if we haven't already.
441
			if ( 'trash' === $post->post_status ) {
442
				return new WP_Error( 'woocommerce_rest_already_trashed', sprintf( __( 'The %s has already been deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 410 ) );
443
			}
444
445
			// (Note that internally this falls through to `wp_delete_post` if
446
			// the trash is disabled.)
447
			$result = wp_trash_post( $id );
448
		}
449
450
		if ( ! $result ) {
451
			return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) );
452
		}
453
454
		/**
455
		 * Fires after a single item is deleted or trashed via the REST API.
456
		 *
457
		 * @param object           $post     The deleted or trashed item.
458
		 * @param WP_REST_Response $response The response data.
459
		 * @param WP_REST_Request  $request  The request sent to the API.
460
		 */
461
		do_action( "woocommerce_rest_delete_{$this->post_type}", $post, $response, $request );
462
463
		return $response;
464
	}
465
466
	/**
467
	 * Prepare links for the request.
468
	 *
469
	 * @param WP_Post $post Post object.
470
	 * @return array Links for the given post.
471
	 */
472 View Code Duplication
	protected function prepare_links( $post ) {
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...
473
		$links = array(
474
			'self' => array(
475
				'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ),
476
			),
477
			'collection' => array(
478
				'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
479
			),
480
		);
481
482
		return $links;
483
	}
484
485
	/**
486
	 * Determine the allowed query_vars for a get_items() response and
487
	 * prepare for WP_Query.
488
	 *
489
	 * @param array           $prepared_args
490
	 * @param WP_REST_Request $request
491
	 * @return array          $query_args
492
	 */
493
	protected function prepare_items_query( $prepared_args = array(), $request = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
494
495
		$valid_vars = array_flip( $this->get_allowed_query_vars() );
496
		$query_args = array();
497
		foreach ( $valid_vars as $var => $index ) {
498
			if ( isset( $prepared_args[ $var ] ) ) {
499
				/**
500
				 * Filter the query_vars used in `get_items` for the constructed query.
501
				 *
502
				 * The dynamic portion of the hook name, $var, refers to the query_var key.
503
				 *
504
				 * @param mixed $prepared_args[ $var ] The query_var value.
505
				 *
506
				 */
507
				$query_args[ $var ] = apply_filters( "woocommerce_rest_query_var-{$var}", $prepared_args[ $var ] );
508
			}
509
		}
510
511
		$query_args['ignore_sticky_posts'] = true;
512
513
		if ( 'include' === $query_args['orderby'] ) {
514
			$query_args['orderby'] = 'post__in';
515
		}
516
517
		return $query_args;
518
	}
519
520
	/**
521
	 * Get all the WP Query vars that are allowed for the API request.
522
	 *
523
	 * @return array
524
	 */
525
	protected function get_allowed_query_vars() {
526
		global $wp;
527
528
		/**
529
		 * Filter the publicly allowed query vars.
530
		 *
531
		 * Allows adjusting of the default query vars that are made public.
532
		 *
533
		 * @param array  Array of allowed WP_Query query vars.
534
		 */
535
		$valid_vars = apply_filters( 'query_vars', $wp->public_query_vars );
536
537
		$post_type_obj = get_post_type_object( $this->post_type );
538
		if ( current_user_can( $post_type_obj->cap->edit_posts ) ) {
539
			/**
540
			 * Filter the allowed 'private' query vars for authorized users.
541
			 *
542
			 * If the user has the `edit_posts` capability, we also allow use of
543
			 * private query parameters, which are only undesirable on the
544
			 * frontend, but are safe for use in query strings.
545
			 *
546
			 * To disable anyway, use
547
			 * `add_filter( 'woocommerce_rest_private_query_vars', '__return_empty_array' );`
548
			 *
549
			 * @param array $private_query_vars Array of allowed query vars for authorized users.
550
			 * }
551
			 */
552
			$private = apply_filters( 'woocommerce_rest_private_query_vars', $wp->private_query_vars );
553
			$valid_vars = array_merge( $valid_vars, $private );
554
		}
555
		// Define our own in addition to WP's normal vars.
556
		$rest_valid = array(
557
			'date_query',
558
			'ignore_sticky_posts',
559
			'offset',
560
			'post__in',
561
			'post__not_in',
562
			'post_parent',
563
			'post_parent__in',
564
			'post_parent__not_in',
565
			'posts_per_page',
566
			'meta_query',
567
			'tax_query',
568
		);
569
		$valid_vars = array_merge( $valid_vars, $rest_valid );
570
571
		/**
572
		 * Filter allowed query vars for the REST API.
573
		 *
574
		 * This filter allows you to add or remove query vars from the final allowed
575
		 * list for all requests, including unauthenticated ones. To alter the
576
		 * vars for editors only.
577
		 *
578
		 * @param array {
579
		 *    Array of allowed WP_Query query vars.
580
		 *
581
		 *    @param string $allowed_query_var The query var to allow.
582
		 * }
583
		 */
584
		$valid_vars = apply_filters( 'woocommerce_rest_query_vars', $valid_vars );
585
586
		return $valid_vars;
587
	}
588
589
	/**
590
	 * Get the query params for collections of attachments.
591
	 *
592
	 * @return array
593
	 */
594
	public function get_collection_params() {
595
		$params = parent::get_collection_params();
596
597
		$params['context']['default'] = 'view';
598
599
		$params['after'] = array(
600
			'description'        => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
601
			'type'               => 'string',
602
			'format'             => 'date-time',
603
			'validate_callback'  => 'rest_validate_request_arg',
604
		);
605
		$params['before'] = array(
606
			'description'        => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
607
			'type'               => 'string',
608
			'format'             => 'date-time',
609
			'validate_callback'  => 'rest_validate_request_arg',
610
		);
611
		$params['exclude'] = array(
612
			'description'        => __( 'Ensure result set excludes specific ids.', 'woocommerce' ),
613
			'type'               => 'array',
614
			'default'            => array(),
615
			'sanitize_callback'  => 'wp_parse_id_list',
616
		);
617
		$params['include'] = array(
618
			'description'        => __( 'Limit result set to specific ids.', 'woocommerce' ),
619
			'type'               => 'array',
620
			'default'            => array(),
621
			'sanitize_callback'  => 'wp_parse_id_list',
622
		);
623
		$params['offset'] = array(
624
			'description'        => __( 'Offset the result set by a specific number of items.', 'woocommerce' ),
625
			'type'               => 'integer',
626
			'sanitize_callback'  => 'absint',
627
			'validate_callback'  => 'rest_validate_request_arg',
628
		);
629
		$params['order'] = array(
630
			'description'        => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
631
			'type'               => 'string',
632
			'default'            => 'desc',
633
			'enum'               => array( 'asc', 'desc' ),
634
			'validate_callback'  => 'rest_validate_request_arg',
635
		);
636
		$params['orderby'] = array(
637
			'description'        => __( 'Sort collection by object attribute.', 'woocommerce' ),
638
			'type'               => 'string',
639
			'default'            => 'date',
640
			'enum'               => array(
641
				'date',
642
				'id',
643
				'include',
644
				'title',
645
				'slug',
646
			),
647
			'validate_callback'  => 'rest_validate_request_arg',
648
		);
649
650
		$post_type_obj = get_post_type_object( $this->post_type );
651
		if ( $post_type_obj->hierarchical ) {
652
			$params['parent'] = array(
653
				'description'       => __( 'Limit result set to those of particular parent ids.', 'woocommerce' ),
654
				'type'              => 'array',
655
				'sanitize_callback' => 'wp_parse_id_list',
656
				'default'           => array(),
657
			);
658
			$params['parent_exclude'] = array(
659
				'description'       => __( 'Limit result set to all items except those of a particular parent id.', 'woocommerce' ),
660
				'type'              => 'array',
661
				'sanitize_callback' => 'wp_parse_id_list',
662
				'default'           => array(),
663
			);
664
		}
665
666
		$params['filter'] = array(
667
			'description' => __( 'Use WP Query arguments to modify the response; private query vars require appropriate authorization.', 'woocommerce' ),
668
		);
669
670
		return $params;
671
	}
672
673
	/**
674
	 * Update post meta fields.
675
	 *
676
	 * @param WP_Post $post
677
	 * @param WP_REST_Request $request
678
	 * @return bool|WP_Error
679
	 */
680
	protected function update_post_meta_fields( $post, $request ) {
681
		return true;
682
	}
683
}
684