class-wc-rest-v4-controller.php
105 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST Controller for API Version 4 |
| 4 | * |
| 5 | * This is a completely independent base controller for WooCommerce API v4. |
| 6 | * Unlike previous versions, this does not inherit from v3, v2, or v1 controllers. |
| 7 | * |
| 8 | * @class WC_REST_V4_Controller |
| 9 | * @package WooCommerce\RestApi |
| 10 | */ |
| 11 | |
| 12 | declare(strict_types=1); |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * WooCommerce REST API Version 4 Base Controller |
| 20 | * |
| 21 | * @package WooCommerce\RestApi |
| 22 | * @extends WP_REST_Controller |
| 23 | * @version 4.0.0 |
| 24 | */ |
| 25 | abstract class WC_REST_V4_Controller extends WP_REST_Controller { |
| 26 | |
| 27 | /** |
| 28 | * Endpoint namespace. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | protected $namespace = 'wc/v4'; |
| 33 | |
| 34 | /** |
| 35 | * Route base. |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | protected $rest_base = ''; |
| 40 | |
| 41 | /** |
| 42 | * Check permissions for a given request. |
| 43 | * |
| 44 | * @param WP_REST_Request $request Full details about the request. |
| 45 | * @param string $permission The permission to check for. |
| 46 | * @return bool|WP_Error |
| 47 | */ |
| 48 | protected function check_permissions( $request, $permission = 'read' ) { |
| 49 | if ( ! wc_rest_check_manager_permissions( 'settings', $permission ) ) { |
| 50 | return new WP_Error( |
| 51 | 'woocommerce_rest_cannot_' . $permission, |
| 52 | __( 'Sorry, you are not allowed to perform this action.', 'woocommerce' ), |
| 53 | array( 'status' => rest_authorization_required_code() ) |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get the default REST API version. |
| 62 | * |
| 63 | * @return string |
| 64 | */ |
| 65 | protected function get_api_version() { |
| 66 | return 'v4'; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Prepare a response for inserting into a collection. |
| 71 | * |
| 72 | * @param WP_REST_Response $response Response object. |
| 73 | * @return array Response data. |
| 74 | */ |
| 75 | public function prepare_response_for_collection( $response ) { |
| 76 | if ( ! ( $response instanceof WP_REST_Response ) ) { |
| 77 | return $response; |
| 78 | } |
| 79 | |
| 80 | $data = (array) $response->get_data(); |
| 81 | $server = rest_get_server(); |
| 82 | $links = $server->get_compact_response_links( $response ); |
| 83 | |
| 84 | if ( ! empty( $links ) ) { |
| 85 | $data['_links'] = $links; |
| 86 | } |
| 87 | |
| 88 | return $data; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get the base schema for the API. |
| 93 | * |
| 94 | * @return array |
| 95 | */ |
| 96 | protected function get_base_schema() { |
| 97 | return array( |
| 98 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 99 | 'title' => 'base', |
| 100 | 'type' => 'object', |
| 101 | 'properties' => array(), |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 |