Няма описание

class-wc-rest-crud-controller.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. <?php
  2. /**
  3. * Abstract Rest CRUD Controller Class
  4. *
  5. * @class WC_REST_CRUD_Controller
  6. * @package WooCommerce\RestApi
  7. * @version 3.0.0
  8. */
  9. if ( ! defined( 'ABSPATH' ) ) {
  10. exit;
  11. }
  12. /**
  13. * WC_REST_CRUD_Controller class.
  14. *
  15. * @extends WC_REST_Posts_Controller
  16. */
  17. abstract class WC_REST_CRUD_Controller extends WC_REST_Posts_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v2';
  24. /**
  25. * If object is hierarchical.
  26. *
  27. * @var bool
  28. */
  29. protected $hierarchical = false;
  30. /**
  31. * Get object.
  32. *
  33. * @param int $id Object ID.
  34. * @return object WC_Data object or WP_Error object.
  35. */
  36. protected function get_object( $id ) {
  37. // translators: %s: Class method name.
  38. return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass.", 'woocommerce' ), __METHOD__ ), array( 'status' => 405 ) );
  39. }
  40. /**
  41. * Check if a given request has access to read an item.
  42. *
  43. * @param WP_REST_Request $request Full details about the request.
  44. * @return WP_Error|boolean
  45. */
  46. public function get_item_permissions_check( $request ) {
  47. $object = $this->get_object( (int) $request['id'] );
  48. if ( $object && 0 !== $object->get_id() && ! wc_rest_check_post_permissions( $this->post_type, 'read', $object->get_id() ) ) {
  49. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  50. }
  51. return true;
  52. }
  53. /**
  54. * Check if a given request has access to update an item.
  55. *
  56. * @param WP_REST_Request $request Full details about the request.
  57. * @return WP_Error|boolean
  58. */
  59. public function update_item_permissions_check( $request ) {
  60. $object = $this->get_object( (int) $request['id'] );
  61. if ( $object && 0 !== $object->get_id() && ! wc_rest_check_post_permissions( $this->post_type, 'edit', $object->get_id() ) ) {
  62. return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  63. }
  64. return true;
  65. }
  66. /**
  67. * Check if a given request has access to delete an item.
  68. *
  69. * @param WP_REST_Request $request Full details about the request.
  70. * @return bool|WP_Error
  71. */
  72. public function delete_item_permissions_check( $request ) {
  73. $object = $this->get_object( (int) $request['id'] );
  74. if ( $object && 0 !== $object->get_id() && ! wc_rest_check_post_permissions( $this->post_type, 'delete', $object->get_id() ) ) {
  75. return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  76. }
  77. return true;
  78. }
  79. /**
  80. * Get object permalink.
  81. *
  82. * @param object $object Object.
  83. * @return string
  84. */
  85. protected function get_permalink( $object ) {
  86. return '';
  87. }
  88. /**
  89. * Prepares the object for the REST response.
  90. *
  91. * @since 3.0.0
  92. * @param WC_Data $object Object data.
  93. * @param WP_REST_Request $request Request object.
  94. * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
  95. */
  96. protected function prepare_object_for_response( $object, $request ) {
  97. // translators: %s: Class method name.
  98. return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass.", 'woocommerce' ), __METHOD__ ), array( 'status' => 405 ) );
  99. }
  100. /**
  101. * Prepares one object for create or update operation.
  102. *
  103. * @since 3.0.0
  104. * @param WP_REST_Request $request Request object.
  105. * @param bool $creating If is creating a new object.
  106. * @return WP_Error|WC_Data The prepared item, or WP_Error object on failure.
  107. */
  108. protected function prepare_object_for_database( $request, $creating = false ) {
  109. // translators: %s: Class method name.
  110. return new WP_Error( 'invalid-method', sprintf( __( "Method '%s' not implemented. Must be overridden in subclass.", 'woocommerce' ), __METHOD__ ), array( 'status' => 405 ) );
  111. }
  112. /**
  113. * Get a single item.
  114. *
  115. * @param WP_REST_Request $request Full details about the request.
  116. * @return WP_Error|WP_REST_Response
  117. */
  118. public function get_item( $request ) {
  119. $object = $this->get_object( (int) $request['id'] );
  120. if ( ! $object || 0 === $object->get_id() ) {
  121. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
  122. }
  123. $data = $this->prepare_object_for_response( $object, $request );
  124. $response = rest_ensure_response( $data );
  125. if ( $this->public ) {
  126. $response->link_header( 'alternate', $this->get_permalink( $object ), array( 'type' => 'text/html' ) );
  127. }
  128. return $response;
  129. }
  130. /**
  131. * Save an object data.
  132. *
  133. * @since 3.0.0
  134. * @param WP_REST_Request $request Full details about the request.
  135. * @param bool $creating If is creating a new object.
  136. * @return WC_Data|WP_Error
  137. */
  138. protected function save_object( $request, $creating = false ) {
  139. try {
  140. $object = $this->prepare_object_for_database( $request, $creating );
  141. if ( is_wp_error( $object ) ) {
  142. return $object;
  143. }
  144. $object->save();
  145. return $this->get_object( $object->get_id() );
  146. } catch ( WC_Data_Exception $e ) {
  147. return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
  148. } catch ( WC_REST_Exception $e ) {
  149. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
  150. }
  151. }
  152. /**
  153. * Create a single item.
  154. *
  155. * @param WP_REST_Request $request Full details about the request.
  156. * @return WP_Error|WP_REST_Response
  157. */
  158. public function create_item( $request ) {
  159. if ( ! empty( $request['id'] ) ) {
  160. /* translators: %s: post type */
  161. return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
  162. }
  163. $object = $this->save_object( $request, true );
  164. if ( is_wp_error( $object ) ) {
  165. return $object;
  166. }
  167. try {
  168. $this->update_additional_fields_for_object( $object, $request );
  169. /**
  170. * Fires after a single object is created or updated via the REST API.
  171. *
  172. * @param WC_Data $object Inserted object.
  173. * @param WP_REST_Request $request Request object.
  174. * @param boolean $creating True when creating object, false when updating.
  175. */
  176. do_action( "woocommerce_rest_insert_{$this->post_type}_object", $object, $request, true );
  177. } catch ( WC_Data_Exception $e ) {
  178. $object->delete();
  179. return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
  180. } catch ( WC_REST_Exception $e ) {
  181. $object->delete();
  182. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
  183. }
  184. $request->set_param( 'context', 'edit' );
  185. $response = $this->prepare_object_for_response( $object, $request );
  186. $response = rest_ensure_response( $response );
  187. $response->set_status( 201 );
  188. $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ) );
  189. return $response;
  190. }
  191. /**
  192. * Update a single post.
  193. *
  194. * @param WP_REST_Request $request Full details about the request.
  195. * @return WP_Error|WP_REST_Response
  196. */
  197. public function update_item( $request ) {
  198. $object = $this->get_object( (int) $request['id'] );
  199. if ( ! $object || 0 === $object->get_id() ) {
  200. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 400 ) );
  201. }
  202. $object = $this->save_object( $request, false );
  203. if ( is_wp_error( $object ) ) {
  204. return $object;
  205. }
  206. try {
  207. $this->update_additional_fields_for_object( $object, $request );
  208. /**
  209. * Fires after a single object is created or updated via the REST API.
  210. *
  211. * @param WC_Data $object Inserted object.
  212. * @param WP_REST_Request $request Request object.
  213. * @param boolean $creating True when creating object, false when updating.
  214. */
  215. do_action( "woocommerce_rest_insert_{$this->post_type}_object", $object, $request, false );
  216. } catch ( WC_Data_Exception $e ) {
  217. return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
  218. } catch ( WC_REST_Exception $e ) {
  219. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
  220. }
  221. $request->set_param( 'context', 'edit' );
  222. $response = $this->prepare_object_for_response( $object, $request );
  223. return rest_ensure_response( $response );
  224. }
  225. /**
  226. * Prepare objects query.
  227. *
  228. * @since 3.0.0
  229. * @param WP_REST_Request $request Full details about the request.
  230. * @return array
  231. */
  232. protected function prepare_objects_query( $request ) {
  233. $args = array();
  234. $args['offset'] = $request['offset'];
  235. $args['order'] = $request['order'];
  236. $args['orderby'] = $request['orderby'];
  237. $args['paged'] = $request['page'];
  238. $args['post__in'] = $request['include'];
  239. $args['post__not_in'] = $request['exclude'];
  240. $args['posts_per_page'] = $request['per_page'];
  241. $args['name'] = $request['slug'];
  242. $args['post_parent__in'] = $request['parent'];
  243. $args['post_parent__not_in'] = $request['parent_exclude'];
  244. $args['s'] = $request['search'];
  245. $args['fields'] = $this->get_fields_for_response( $request );
  246. if ( 'date' === $args['orderby'] ) {
  247. $args['orderby'] = 'date ID';
  248. }
  249. $args['date_query'] = array();
  250. // Set before into date query. Date query must be specified as an array of an array.
  251. if ( isset( $request['before'] ) ) {
  252. $args['date_query'][0]['before'] = $request['before'];
  253. }
  254. // Set after into date query. Date query must be specified as an array of an array.
  255. if ( isset( $request['after'] ) ) {
  256. $args['date_query'][0]['after'] = $request['after'];
  257. }
  258. // Check flag to use post_date vs post_date_gmt.
  259. if ( true === $request['dates_are_gmt'] ) {
  260. if ( isset( $request['before'] ) || isset( $request['after'] ) ) {
  261. $args['date_query'][0]['column'] = 'post_date_gmt';
  262. }
  263. }
  264. // Force the post_type argument, since it's not a user input variable.
  265. $args['post_type'] = $this->post_type;
  266. /**
  267. * Filter the query arguments for a request.
  268. *
  269. * Enables adding extra arguments or setting defaults for a post
  270. * collection request.
  271. *
  272. * @param array $args Key value array of query var to query value.
  273. * @param WP_REST_Request $request The request used.
  274. */
  275. $args = apply_filters( "woocommerce_rest_{$this->post_type}_object_query", $args, $request );
  276. return $this->prepare_items_query( $args, $request );
  277. }
  278. /**
  279. * Get objects.
  280. *
  281. * @since 3.0.0
  282. * @param array $query_args Query args.
  283. * @return array
  284. */
  285. protected function get_objects( $query_args ) {
  286. $query = new WP_Query();
  287. $result = $query->query( $query_args );
  288. $total_posts = $query->found_posts;
  289. if ( $total_posts < 1 ) {
  290. // Out-of-bounds, run the query again without LIMIT for total count.
  291. unset( $query_args['paged'] );
  292. $count_query = new WP_Query();
  293. $count_query->query( $query_args );
  294. $total_posts = $count_query->found_posts;
  295. }
  296. return array(
  297. 'objects' => array_filter( array_map( array( $this, 'get_object' ), $result ) ),
  298. 'total' => (int) $total_posts,
  299. 'pages' => (int) ceil( $total_posts / (int) $query->query_vars['posts_per_page'] ),
  300. );
  301. }
  302. /**
  303. * Get a collection of posts.
  304. *
  305. * @param WP_REST_Request $request Full details about the request.
  306. * @return WP_Error|WP_REST_Response
  307. */
  308. public function get_items( $request ) {
  309. $query_args = $this->prepare_objects_query( $request );
  310. $query_results = $this->get_objects( $query_args );
  311. $objects = array();
  312. foreach ( $query_results['objects'] as $object ) {
  313. if ( ! wc_rest_check_post_permissions( $this->post_type, 'read', $object->get_id() ) ) {
  314. continue;
  315. }
  316. $data = $this->prepare_object_for_response( $object, $request );
  317. $objects[] = $this->prepare_response_for_collection( $data );
  318. }
  319. $page = (int) $query_args['paged'];
  320. $max_pages = $query_results['pages'];
  321. $response = rest_ensure_response( $objects );
  322. $response->header( 'X-WP-Total', $query_results['total'] );
  323. $response->header( 'X-WP-TotalPages', (int) $max_pages );
  324. $base = $this->rest_base;
  325. $attrib_prefix = '(?P<';
  326. if ( strpos( $base, $attrib_prefix ) !== false ) {
  327. $attrib_names = array();
  328. preg_match( '/\(\?P<[^>]+>.*\)/', $base, $attrib_names, PREG_OFFSET_CAPTURE );
  329. foreach ( $attrib_names as $attrib_name_match ) {
  330. $beginning_offset = strlen( $attrib_prefix );
  331. $attrib_name_end = strpos( $attrib_name_match[0], '>', $attrib_name_match[1] );
  332. $attrib_name = substr( $attrib_name_match[0], $beginning_offset, $attrib_name_end - $beginning_offset );
  333. if ( isset( $request[ $attrib_name ] ) ) {
  334. $base = str_replace( "(?P<$attrib_name>[\d]+)", $request[ $attrib_name ], $base );
  335. }
  336. }
  337. }
  338. $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ) );
  339. if ( $page > 1 ) {
  340. $prev_page = $page - 1;
  341. if ( $prev_page > $max_pages ) {
  342. $prev_page = $max_pages;
  343. }
  344. $prev_link = add_query_arg( 'page', $prev_page, $base );
  345. $response->link_header( 'prev', $prev_link );
  346. }
  347. if ( $max_pages > $page ) {
  348. $next_page = $page + 1;
  349. $next_link = add_query_arg( 'page', $next_page, $base );
  350. $response->link_header( 'next', $next_link );
  351. }
  352. return $response;
  353. }
  354. /**
  355. * Delete a single item.
  356. *
  357. * @param WP_REST_Request $request Full details about the request.
  358. * @return WP_REST_Response|WP_Error
  359. */
  360. public function delete_item( $request ) {
  361. $force = (bool) $request['force'];
  362. $object = $this->get_object( (int) $request['id'] );
  363. $result = false;
  364. if ( ! $object || 0 === $object->get_id() ) {
  365. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
  366. }
  367. $supports_trash = EMPTY_TRASH_DAYS > 0 && is_callable( array( $object, 'get_status' ) );
  368. /**
  369. * Filter whether an object is trashable.
  370. *
  371. * Return false to disable trash support for the object.
  372. *
  373. * @param boolean $supports_trash Whether the object type support trashing.
  374. * @param WC_Data $object The object being considered for trashing support.
  375. */
  376. $supports_trash = apply_filters( "woocommerce_rest_{$this->post_type}_object_trashable", $supports_trash, $object );
  377. if ( ! wc_rest_check_post_permissions( $this->post_type, 'delete', $object->get_id() ) ) {
  378. /* translators: %s: post type */
  379. 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() ) );
  380. }
  381. $request->set_param( 'context', 'edit' );
  382. $response = $this->prepare_object_for_response( $object, $request );
  383. // If we're forcing, then delete permanently.
  384. if ( $force ) {
  385. $object->delete( true );
  386. $result = 0 === $object->get_id();
  387. } else {
  388. // If we don't support trashing for this type, error out.
  389. if ( ! $supports_trash ) {
  390. /* translators: %s: post type */
  391. return new WP_Error( 'woocommerce_rest_trash_not_supported', sprintf( __( 'The %s does not support trashing.', 'woocommerce' ), $this->post_type ), array( 'status' => 501 ) );
  392. }
  393. // Otherwise, only trash if we haven't already.
  394. if ( is_callable( array( $object, 'get_status' ) ) ) {
  395. if ( 'trash' === $object->get_status() ) {
  396. /* translators: %s: post type */
  397. return new WP_Error( 'woocommerce_rest_already_trashed', sprintf( __( 'The %s has already been deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 410 ) );
  398. }
  399. $object->delete();
  400. $result = 'trash' === $object->get_status();
  401. }
  402. }
  403. if ( ! $result ) {
  404. /* translators: %s: post type */
  405. return new WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) );
  406. }
  407. /**
  408. * Fires after a single object is deleted or trashed via the REST API.
  409. *
  410. * @param WC_Data $object The deleted or trashed object.
  411. * @param WP_REST_Response $response The response data.
  412. * @param WP_REST_Request $request The request sent to the API.
  413. */
  414. do_action( "woocommerce_rest_delete_{$this->post_type}_object", $object, $response, $request );
  415. return $response;
  416. }
  417. /**
  418. * Get fields for an object if getter is defined.
  419. *
  420. * @param object $object Object we are fetching response for.
  421. * @param string $context Context of the request. Can be `view` or `edit`.
  422. * @param array $fields List of fields to fetch.
  423. * @return array Data fetched from getters.
  424. */
  425. public function fetch_fields_using_getters( $object, $context, $fields ) {
  426. $data = array();
  427. foreach ( $fields as $field ) {
  428. if ( method_exists( $this, "api_get_$field" ) ) {
  429. $data[ $field ] = $this->{"api_get_$field"}( $object, $context );
  430. }
  431. }
  432. return $data;
  433. }
  434. /**
  435. * Prepare links for the request.
  436. *
  437. * @param WC_Data $object Object data.
  438. * @param WP_REST_Request $request Request object.
  439. * @return array Links for the given post.
  440. */
  441. protected function prepare_links( $object, $request ) {
  442. $links = array(
  443. 'self' => array(
  444. 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ),
  445. ),
  446. 'collection' => array(
  447. 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
  448. ),
  449. );
  450. return $links;
  451. }
  452. /**
  453. * Get the query params for collections of attachments.
  454. *
  455. * @return array
  456. */
  457. public function get_collection_params() {
  458. $params = array();
  459. $params['context'] = $this->get_context_param();
  460. $params['context']['default'] = 'view';
  461. $params['page'] = array(
  462. 'description' => __( 'Current page of the collection.', 'woocommerce' ),
  463. 'type' => 'integer',
  464. 'default' => 1,
  465. 'sanitize_callback' => 'absint',
  466. 'validate_callback' => 'rest_validate_request_arg',
  467. 'minimum' => 1,
  468. );
  469. $params['per_page'] = array(
  470. 'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ),
  471. 'type' => 'integer',
  472. 'default' => 10,
  473. 'minimum' => 1,
  474. 'maximum' => 100,
  475. 'sanitize_callback' => 'absint',
  476. 'validate_callback' => 'rest_validate_request_arg',
  477. );
  478. $params['search'] = array(
  479. 'description' => __( 'Limit results to those matching a string.', 'woocommerce' ),
  480. 'type' => 'string',
  481. 'sanitize_callback' => 'sanitize_text_field',
  482. 'validate_callback' => 'rest_validate_request_arg',
  483. );
  484. $params['after'] = array(
  485. 'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
  486. 'type' => 'string',
  487. 'format' => 'date-time',
  488. 'validate_callback' => 'rest_validate_request_arg',
  489. );
  490. $params['before'] = array(
  491. 'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
  492. 'type' => 'string',
  493. 'format' => 'date-time',
  494. 'validate_callback' => 'rest_validate_request_arg',
  495. );
  496. $params['dates_are_gmt'] = array(
  497. 'description' => __( 'Whether to use GMT post dates.', 'woocommerce' ),
  498. 'type' => 'boolean',
  499. 'default' => false,
  500. 'validate_callback' => 'rest_validate_request_arg',
  501. );
  502. $params['exclude'] = array(
  503. 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ),
  504. 'type' => 'array',
  505. 'items' => array(
  506. 'type' => 'integer',
  507. ),
  508. 'default' => array(),
  509. 'sanitize_callback' => 'wp_parse_id_list',
  510. );
  511. $params['include'] = array(
  512. 'description' => __( 'Limit result set to specific ids.', 'woocommerce' ),
  513. 'type' => 'array',
  514. 'items' => array(
  515. 'type' => 'integer',
  516. ),
  517. 'default' => array(),
  518. 'sanitize_callback' => 'wp_parse_id_list',
  519. );
  520. $params['offset'] = array(
  521. 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ),
  522. 'type' => 'integer',
  523. 'sanitize_callback' => 'absint',
  524. 'validate_callback' => 'rest_validate_request_arg',
  525. );
  526. $params['order'] = array(
  527. 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
  528. 'type' => 'string',
  529. 'default' => 'desc',
  530. 'enum' => array( 'asc', 'desc' ),
  531. 'validate_callback' => 'rest_validate_request_arg',
  532. );
  533. $params['orderby'] = array(
  534. 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
  535. 'type' => 'string',
  536. 'default' => 'date',
  537. 'enum' => array(
  538. 'date',
  539. 'id',
  540. 'include',
  541. 'title',
  542. 'slug',
  543. 'modified',
  544. ),
  545. 'validate_callback' => 'rest_validate_request_arg',
  546. );
  547. if ( $this->hierarchical ) {
  548. $params['parent'] = array(
  549. 'description' => __( 'Limit result set to those of particular parent IDs.', 'woocommerce' ),
  550. 'type' => 'array',
  551. 'items' => array(
  552. 'type' => 'integer',
  553. ),
  554. 'sanitize_callback' => 'wp_parse_id_list',
  555. 'default' => array(),
  556. );
  557. $params['parent_exclude'] = array(
  558. 'description' => __( 'Limit result set to all items except those of a particular parent ID.', 'woocommerce' ),
  559. 'type' => 'array',
  560. 'items' => array(
  561. 'type' => 'integer',
  562. ),
  563. 'sanitize_callback' => 'wp_parse_id_list',
  564. 'default' => array(),
  565. );
  566. }
  567. /**
  568. * Filter collection parameters for the posts controller.
  569. *
  570. * The dynamic part of the filter `$this->post_type` refers to the post
  571. * type slug for the controller.
  572. *
  573. * This filter registers the collection parameter, but does not map the
  574. * collection parameter to an internal WP_Query parameter. Use the
  575. * `rest_{$this->post_type}_query` filter to set WP_Query parameters.
  576. *
  577. * @param array $query_params JSON Schema-formatted collection parameters.
  578. * @param WP_Post_Type $post_type Post type object.
  579. */
  580. return apply_filters( "rest_{$this->post_type}_collection_params", $params, $this->post_type );
  581. }
  582. }