| Total Complexity | 64 |
| Total Lines | 748 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AbstractObjectsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractObjectsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | abstract class AbstractObjectsController extends AbstractController { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * If object is hierarchical. |
||
| 22 | * |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | protected $hierarchical = false; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Post type. |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $post_type = ''; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Get object. |
||
| 36 | * |
||
| 37 | * @param int $id Object ID. |
||
| 38 | * @return \WC_Data|bool |
||
| 39 | */ |
||
| 40 | abstract protected function get_object( $id ); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Prepares one object for create or update operation. |
||
| 44 | * |
||
| 45 | * @since 3.0.0 |
||
| 46 | * @param \WP_REST_Request $request Request object. |
||
| 47 | * @param bool $creating If is creating a new object. |
||
| 48 | * @return \WC_Data The prepared item, or \WP_Error object on failure. |
||
| 49 | */ |
||
| 50 | abstract protected function prepare_object_for_database( $request, $creating = false ); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Register the routes for products. |
||
| 54 | */ |
||
| 55 | public function register_routes() { |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get a single item. |
||
| 63 | * |
||
| 64 | * @param \WP_REST_Request $request Full details about the request. |
||
| 65 | * @return \WP_Error|\WP_REST_Response |
||
| 66 | */ |
||
| 67 | public function get_item( $request ) { |
||
| 68 | $object = $this->get_object( (int) $request['id'] ); |
||
| 69 | |||
| 70 | if ( ! $object || 0 === $object->get_id() ) { |
||
| 71 | return new \WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
||
| 72 | } |
||
| 73 | |||
| 74 | $data = $this->prepare_item_for_response( $object, $request ); |
||
| 75 | $response = rest_ensure_response( $data ); |
||
| 76 | |||
| 77 | return $response; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Save an object data. |
||
| 82 | * |
||
| 83 | * @since 3.0.0 |
||
| 84 | * @param \WP_REST_Request $request Full details about the request. |
||
| 85 | * @param bool $creating If is creating a new object. |
||
| 86 | * @return \WC_Data|\WP_Error |
||
| 87 | */ |
||
| 88 | protected function save_object( $request, $creating = false ) { |
||
| 89 | try { |
||
| 90 | $object = $this->prepare_object_for_database( $request, $creating ); |
||
| 91 | |||
| 92 | if ( is_wp_error( $object ) ) { |
||
| 93 | return $object; |
||
| 94 | } |
||
| 95 | |||
| 96 | $object->save(); |
||
| 97 | |||
| 98 | return $this->get_object( $object->get_id() ); |
||
| 99 | } catch ( \WC_Data_Exception $e ) { |
||
| 100 | return new \WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); |
||
| 101 | } catch ( \WC_REST_Exception $e ) { |
||
| 102 | return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Create a single item. |
||
| 108 | * |
||
| 109 | * @param \WP_REST_Request $request Full details about the request. |
||
| 110 | * @return \WP_Error|\WP_REST_Response |
||
| 111 | */ |
||
| 112 | public function create_item( $request ) { |
||
| 113 | if ( ! empty( $request['id'] ) ) { |
||
| 114 | /* translators: %s: post type */ |
||
| 115 | return new \WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) ); |
||
| 116 | } |
||
| 117 | |||
| 118 | $object = $this->save_object( $request, true ); |
||
| 119 | |||
| 120 | if ( is_wp_error( $object ) ) { |
||
| 121 | return $object; |
||
| 122 | } |
||
| 123 | |||
| 124 | try { |
||
| 125 | $this->update_additional_fields_for_object( $object, $request ); |
||
|
|
|||
| 126 | } catch ( \WC_Data_Exception $e ) { |
||
| 127 | $object->delete(); |
||
| 128 | return new \WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); |
||
| 129 | } catch ( \WC_REST_Exception $e ) { |
||
| 130 | $object->delete(); |
||
| 131 | return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Fires after a single object is created or updated via the REST API. |
||
| 136 | * |
||
| 137 | * @param \WC_Data $object Inserted object. |
||
| 138 | * @param \WP_REST_Request $request Request object. |
||
| 139 | * @param boolean $creating True when creating object, false when updating. |
||
| 140 | */ |
||
| 141 | do_action( "woocommerce_rest_insert_{$this->post_type}_object", $object, $request, true ); |
||
| 142 | |||
| 143 | $request->set_param( 'context', 'edit' ); |
||
| 144 | $response = $this->prepare_item_for_response( $object, $request ); |
||
| 145 | $response = rest_ensure_response( $response ); |
||
| 146 | $response->set_status( 201 ); |
||
| 147 | $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ) ); |
||
| 148 | |||
| 149 | return $response; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Update a single post. |
||
| 154 | * |
||
| 155 | * @param \WP_REST_Request $request Full details about the request. |
||
| 156 | * @return \WP_Error|\WP_REST_Response |
||
| 157 | */ |
||
| 158 | public function update_item( $request ) { |
||
| 159 | $object = $this->get_object( (int) $request['id'] ); |
||
| 160 | |||
| 161 | if ( ! $object || 0 === $object->get_id() ) { |
||
| 162 | return new \WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
||
| 163 | } |
||
| 164 | |||
| 165 | $object = $this->save_object( $request, false ); |
||
| 166 | |||
| 167 | if ( is_wp_error( $object ) ) { |
||
| 168 | return $object; |
||
| 169 | } |
||
| 170 | |||
| 171 | try { |
||
| 172 | $this->update_additional_fields_for_object( $object, $request ); |
||
| 173 | } catch ( \WC_Data_Exception $e ) { |
||
| 174 | return new \WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); |
||
| 175 | } catch ( \WC_REST_Exception $e ) { |
||
| 176 | return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Fires after a single object is created or updated via the REST API. |
||
| 181 | * |
||
| 182 | * @param \WC_Data $object Inserted object. |
||
| 183 | * @param \WP_REST_Request $request Request object. |
||
| 184 | * @param boolean $creating True when creating object, false when updating. |
||
| 185 | */ |
||
| 186 | do_action( "woocommerce_rest_insert_{$this->post_type}_object", $object, $request, false ); |
||
| 187 | |||
| 188 | $request->set_param( 'context', 'edit' ); |
||
| 189 | $response = $this->prepare_item_for_response( $object, $request ); |
||
| 190 | return rest_ensure_response( $response ); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Prepare objects query. |
||
| 195 | * |
||
| 196 | * @since 3.0.0 |
||
| 197 | * @param \WP_REST_Request $request Full details about the request. |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | protected function prepare_objects_query( $request ) { |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get objects. |
||
| 255 | * |
||
| 256 | * @since 3.0.0 |
||
| 257 | * @param array $query_args Query args. |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | protected function get_objects( $query_args ) { |
||
| 277 | ); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get a collection of posts. |
||
| 282 | * |
||
| 283 | * @param \WP_REST_Request $request Full details about the request. |
||
| 284 | * @return \WP_REST_Response |
||
| 285 | */ |
||
| 286 | public function get_items( $request ) { |
||
| 287 | $query_args = $this->prepare_objects_query( $request ); |
||
| 288 | $query_results = $this->get_objects( $query_args ); |
||
| 289 | |||
| 290 | $objects = array(); |
||
| 291 | foreach ( $query_results['objects'] as $object ) { |
||
| 292 | if ( ! Permissions::user_can_read( $this->post_type, $object->get_id() ) ) { |
||
| 293 | continue; |
||
| 294 | } |
||
| 295 | |||
| 296 | $data = $this->prepare_item_for_response( $object, $request ); |
||
| 297 | $objects[] = $this->prepare_response_for_collection( $data ); |
||
| 298 | } |
||
| 299 | |||
| 300 | $total = $query_results['total']; |
||
| 301 | $max_pages = $query_results['pages']; |
||
| 302 | |||
| 303 | $response = rest_ensure_response( $objects ); |
||
| 304 | $response = Pagination::add_pagination_headers( $response, $request, $total, $max_pages ); |
||
| 305 | |||
| 306 | return $response; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Delete a single item. |
||
| 311 | * |
||
| 312 | * @param \WP_REST_Request $request Full details about the request. |
||
| 313 | * @return \WP_REST_Response|\WP_Error |
||
| 314 | */ |
||
| 315 | public function delete_item( $request ) { |
||
| 316 | $force = (bool) $request['force']; |
||
| 317 | $object = $this->get_object( (int) $request['id'] ); |
||
| 318 | $result = false; |
||
| 319 | |||
| 320 | if ( ! $object || 0 === $object->get_id() ) { |
||
| 321 | return new \WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
||
| 322 | } |
||
| 323 | |||
| 324 | $supports_trash = $this->supports_trash( $object ); |
||
| 325 | |||
| 326 | if ( ! Permissions::user_can_delete( $this->post_type, $object->get_id() ) ) { |
||
| 327 | /* translators: %s: post type */ |
||
| 328 | 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() ) ); |
||
| 329 | } |
||
| 330 | |||
| 331 | $request->set_param( 'context', 'edit' ); |
||
| 332 | $previous = $this->prepare_item_for_response( $object, $request ); |
||
| 333 | |||
| 334 | // If we're forcing, then delete permanently. |
||
| 335 | if ( $force ) { |
||
| 336 | $object->delete( true ); |
||
| 337 | $result = 0 === $object->get_id(); |
||
| 338 | } else { |
||
| 339 | // If we don't support trashing for this type, error out. |
||
| 340 | if ( ! $supports_trash ) { |
||
| 341 | /* translators: %s: post type */ |
||
| 342 | return new \WP_Error( 'woocommerce_rest_trash_not_supported', sprintf( __( 'The %s does not support trashing.', 'woocommerce' ), $this->post_type ), array( 'status' => 501 ) ); |
||
| 343 | } |
||
| 344 | |||
| 345 | // Otherwise, only trash if we haven't already. |
||
| 346 | if ( is_callable( array( $object, 'get_status' ) ) && 'trash' === $object->get_status() ) { |
||
| 347 | /* translators: %s: post type */ |
||
| 348 | return new \WP_Error( 'woocommerce_rest_already_trashed', sprintf( __( 'The %s has already been deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 410 ) ); |
||
| 349 | } else { |
||
| 350 | $object->delete(); |
||
| 351 | $result = is_callable( array( $object, 'get_status' ) ) ? 'trash' === $object->get_status() : true; |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | if ( ! $result ) { |
||
| 356 | /* translators: %s: post type */ |
||
| 357 | return new \WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) ); |
||
| 358 | } |
||
| 359 | |||
| 360 | $response = new \WP_REST_Response(); |
||
| 361 | $response->set_data( |
||
| 362 | array( |
||
| 363 | 'deleted' => true, |
||
| 364 | 'previous' => $previous->get_data(), |
||
| 365 | ) |
||
| 366 | ); |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Fires after a single object is deleted or trashed via the REST API. |
||
| 370 | * |
||
| 371 | * @param \WC_Data $object The deleted or trashed object. |
||
| 372 | * @param \WP_REST_Response $response The response data. |
||
| 373 | * @param \WP_REST_Request $request The request sent to the API. |
||
| 374 | */ |
||
| 375 | do_action( "woocommerce_rest_delete_{$this->post_type}_object", $object, $response, $request ); |
||
| 376 | |||
| 377 | return $response; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Can this object be trashed? |
||
| 382 | * |
||
| 383 | * @param object $object Object to check. |
||
| 384 | * @return boolean |
||
| 385 | */ |
||
| 386 | protected function supports_trash( $object ) { |
||
| 387 | $supports_trash = EMPTY_TRASH_DAYS > 0; |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Filter whether an object is trashable. |
||
| 391 | * |
||
| 392 | * Return false to disable trash support for the object. |
||
| 393 | * |
||
| 394 | * @param boolean $supports_trash Whether the object type support trashing. |
||
| 395 | * @param \WC_Data $object The object being considered for trashing support. |
||
| 396 | */ |
||
| 397 | return apply_filters( "woocommerce_rest_{$this->post_type}_object_trashable", $supports_trash, $object ); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Prepare links for the request. |
||
| 402 | * |
||
| 403 | * @param mixed $item Object to prepare. |
||
| 404 | * @param \WP_REST_Request $request Request object. |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | protected function prepare_links( $item, $request ) { |
||
| 408 | $links = array( |
||
| 409 | 'self' => array( |
||
| 410 | 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $item->get_id() ) ), |
||
| 411 | ), |
||
| 412 | 'collection' => array( |
||
| 413 | 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
||
| 414 | ), |
||
| 415 | ); |
||
| 416 | |||
| 417 | return $links; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Get the query params for collections of attachments. |
||
| 422 | * |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | public function get_collection_params() { |
||
| 426 | $params = array(); |
||
| 427 | $params['context'] = $this->get_context_param(); |
||
| 428 | $params['context']['default'] = 'view'; |
||
| 429 | |||
| 430 | $params['page'] = array( |
||
| 431 | 'description' => __( 'Current page of the collection.', 'woocommerce' ), |
||
| 432 | 'type' => 'integer', |
||
| 433 | 'default' => 1, |
||
| 434 | 'sanitize_callback' => 'absint', |
||
| 435 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 436 | 'minimum' => 1, |
||
| 437 | ); |
||
| 438 | |||
| 439 | $params['per_page'] = array( |
||
| 440 | 'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ), |
||
| 441 | 'type' => 'integer', |
||
| 442 | 'default' => 10, |
||
| 443 | 'minimum' => 1, |
||
| 444 | 'maximum' => 100, |
||
| 445 | 'sanitize_callback' => 'absint', |
||
| 446 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 447 | ); |
||
| 448 | |||
| 449 | $params['search'] = array( |
||
| 450 | 'description' => __( 'Limit results to those matching a string.', 'woocommerce' ), |
||
| 451 | 'type' => 'string', |
||
| 452 | 'sanitize_callback' => 'sanitize_text_field', |
||
| 453 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 454 | ); |
||
| 455 | |||
| 456 | $params['after'] = array( |
||
| 457 | 'description' => __( 'Limit response to resources created after a given ISO8601 compliant date.', 'woocommerce' ), |
||
| 458 | 'type' => 'string', |
||
| 459 | 'format' => 'date-time', |
||
| 460 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 461 | ); |
||
| 462 | |||
| 463 | $params['before'] = array( |
||
| 464 | 'description' => __( 'Limit response to resources created before a given ISO8601 compliant date.', 'woocommerce' ), |
||
| 465 | 'type' => 'string', |
||
| 466 | 'format' => 'date-time', |
||
| 467 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 468 | ); |
||
| 469 | |||
| 470 | $params['date_column'] = array( |
||
| 471 | 'description' => __( 'When limiting response using after/before, which date column to compare against.', 'woocommerce' ), |
||
| 472 | 'type' => 'string', |
||
| 473 | 'default' => 'date', |
||
| 474 | 'enum' => array( |
||
| 475 | 'date', |
||
| 476 | 'date_gmt', |
||
| 477 | 'modified', |
||
| 478 | 'modified_gmt', |
||
| 479 | ), |
||
| 480 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 481 | ); |
||
| 482 | |||
| 483 | $params['modified_before'] = array( |
||
| 484 | 'description' => __( 'Limit response to resources modified before a given ISO8601 compliant date.', 'woocommerce' ), |
||
| 485 | 'type' => 'string', |
||
| 486 | 'format' => 'date-time', |
||
| 487 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 488 | ); |
||
| 489 | |||
| 490 | $params['exclude'] = array( |
||
| 491 | 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ), |
||
| 492 | 'type' => 'array', |
||
| 493 | 'items' => array( |
||
| 494 | 'type' => 'integer', |
||
| 495 | ), |
||
| 496 | 'default' => array(), |
||
| 497 | 'sanitize_callback' => 'wp_parse_id_list', |
||
| 498 | ); |
||
| 499 | |||
| 500 | $params['include'] = array( |
||
| 501 | 'description' => __( 'Limit result set to specific ids.', 'woocommerce' ), |
||
| 502 | 'type' => 'array', |
||
| 503 | 'items' => array( |
||
| 504 | 'type' => 'integer', |
||
| 505 | ), |
||
| 506 | 'default' => array(), |
||
| 507 | 'sanitize_callback' => 'wp_parse_id_list', |
||
| 508 | ); |
||
| 509 | |||
| 510 | $params['offset'] = array( |
||
| 511 | 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ), |
||
| 512 | 'type' => 'integer', |
||
| 513 | 'sanitize_callback' => 'absint', |
||
| 514 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 515 | ); |
||
| 516 | |||
| 517 | $params['order'] = array( |
||
| 518 | 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ), |
||
| 519 | 'type' => 'string', |
||
| 520 | 'default' => 'desc', |
||
| 521 | 'enum' => array( 'asc', 'desc' ), |
||
| 522 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 523 | ); |
||
| 524 | |||
| 525 | $params['orderby'] = array( |
||
| 526 | 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ), |
||
| 527 | 'type' => 'string', |
||
| 528 | 'default' => 'date', |
||
| 529 | 'enum' => array( |
||
| 530 | 'date', |
||
| 531 | 'modified', |
||
| 532 | 'id', |
||
| 533 | 'include', |
||
| 534 | 'title', |
||
| 535 | 'slug', |
||
| 536 | ), |
||
| 537 | 'validate_callback' => 'rest_validate_request_arg', |
||
| 538 | ); |
||
| 539 | |||
| 540 | if ( $this->hierarchical ) { |
||
| 541 | $params['parent'] = array( |
||
| 542 | 'description' => __( 'Limit result set to those of particular parent IDs.', 'woocommerce' ), |
||
| 543 | 'type' => 'array', |
||
| 544 | 'items' => array( |
||
| 545 | 'type' => 'integer', |
||
| 546 | ), |
||
| 547 | 'sanitize_callback' => 'wp_parse_id_list', |
||
| 548 | 'default' => array(), |
||
| 549 | ); |
||
| 550 | |||
| 551 | $params['parent_exclude'] = array( |
||
| 552 | 'description' => __( 'Limit result set to all items except those of a particular parent ID.', 'woocommerce' ), |
||
| 553 | 'type' => 'array', |
||
| 554 | 'items' => array( |
||
| 555 | 'type' => 'integer', |
||
| 556 | ), |
||
| 557 | 'sanitize_callback' => 'wp_parse_id_list', |
||
| 558 | 'default' => array(), |
||
| 559 | ); |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Filter collection parameters for the posts controller. |
||
| 564 | * |
||
| 565 | * The dynamic part of the filter `$this->post_type` refers to the post |
||
| 566 | * type slug for the controller. |
||
| 567 | * |
||
| 568 | * This filter registers the collection parameter, but does not map the |
||
| 569 | * collection parameter to an internal \WP_Query parameter. Use the |
||
| 570 | * `rest_{$this->post_type}_query` filter to set \WP_Query parameters. |
||
| 571 | * |
||
| 572 | * @param array $query_params JSON Schema-formatted collection parameters. |
||
| 573 | * @param \WP_Post_Type $post_type Post type object. |
||
| 574 | */ |
||
| 575 | return apply_filters( "rest_{$this->post_type}_collection_params", $params, $this->post_type ); |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Determine the allowed query_vars for a get_items() response and |
||
| 580 | * prepare for \WP_Query. |
||
| 581 | * |
||
| 582 | * @param array $prepared_args Prepared arguments. |
||
| 583 | * @param \WP_REST_Request $request Request object. |
||
| 584 | * @return array $query_args |
||
| 585 | */ |
||
| 586 | protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
||
| 587 | $valid_vars = array_flip( $this->get_allowed_query_vars() ); |
||
| 588 | $query_args = array(); |
||
| 589 | foreach ( $valid_vars as $var => $index ) { |
||
| 590 | if ( isset( $prepared_args[ $var ] ) ) { |
||
| 591 | /** |
||
| 592 | * Filter the query_vars used in `get_items` for the constructed query. |
||
| 593 | * |
||
| 594 | * The dynamic portion of the hook name, $var, refers to the query_var key. |
||
| 595 | * |
||
| 596 | * @param mixed $prepared_args[ $var ] The query_var value. |
||
| 597 | */ |
||
| 598 | $query_args[ $var ] = apply_filters( "woocommerce_rest_query_var_{$var}", $prepared_args[ $var ] ); |
||
| 599 | } |
||
| 600 | } |
||
| 601 | |||
| 602 | $query_args['ignore_sticky_posts'] = true; |
||
| 603 | |||
| 604 | if ( 'include' === $query_args['orderby'] ) { |
||
| 605 | $query_args['orderby'] = 'post__in'; |
||
| 606 | } elseif ( 'id' === $query_args['orderby'] ) { |
||
| 607 | $query_args['orderby'] = 'ID'; // ID must be capitalized. |
||
| 608 | } elseif ( 'slug' === $query_args['orderby'] ) { |
||
| 609 | $query_args['orderby'] = 'name'; |
||
| 610 | } |
||
| 611 | |||
| 612 | return $query_args; |
||
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Get all the WP Query vars that are allowed for the API request. |
||
| 617 | * |
||
| 618 | * @return array |
||
| 619 | */ |
||
| 620 | protected function get_allowed_query_vars() { |
||
| 686 | } |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Add meta query. |
||
| 690 | * |
||
| 691 | * @since 3.0.0 |
||
| 692 | * @param array $args Query args. |
||
| 693 | * @param array $meta_query Meta query. |
||
| 694 | * @return array |
||
| 695 | */ |
||
| 696 | protected function add_meta_query( $args, $meta_query ) { |
||
| 697 | if ( empty( $args['meta_query'] ) ) { |
||
| 698 | $args['meta_query'] = []; // phpcs:ignore |
||
| 699 | } |
||
| 700 | |||
| 701 | $args['meta_query'][] = $meta_query; |
||
| 702 | |||
| 703 | return $args['meta_query']; |
||
| 704 | } |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Return suffix for item action hooks. |
||
| 708 | * |
||
| 709 | * @return string |
||
| 710 | */ |
||
| 711 | protected function get_hook_suffix() { |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Check if a given request has access to read a webhook. |
||
| 717 | * |
||
| 718 | * @param \WP_REST_Request $request Full details about the request. |
||
| 719 | * @return \WP_Error|boolean |
||
| 720 | */ |
||
| 721 | public function get_item_permissions_check( $request ) { |
||
| 722 | $id = $request->get_param( 'id' ); |
||
| 723 | $object = $this->get_object( $id ); |
||
| 724 | |||
| 725 | if ( ! $object || 0 === $object->get_id() ) { |
||
| 726 | return new \WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
||
| 727 | } |
||
| 728 | |||
| 729 | return parent::get_item_permissions_check( $request ); |
||
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Check if a given request has access update a webhook. |
||
| 734 | * |
||
| 735 | * @param \WP_REST_Request $request Full details about the request. |
||
| 736 | * |
||
| 737 | * @return bool|\WP_Error |
||
| 738 | */ |
||
| 739 | public function update_item_permissions_check( $request ) { |
||
| 748 | } |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Check if a given request has access delete a webhook. |
||
| 752 | * |
||
| 753 | * @param \WP_REST_Request $request Full details about the request. |
||
| 754 | * |
||
| 755 | * @return bool|\WP_Error |
||
| 756 | */ |
||
| 757 | public function delete_item_permissions_check( $request ) { |
||
| 768 |