Customers
3 months ago
Fulfillments
1 month ago
OrderNotes
3 months ago
Orders
1 month ago
Products
1 month ago
Refunds
1 month ago
Settings
3 months ago
ShippingZoneMethod
5 months ago
ShippingZones
3 months ago
AbstractCollectionQuery.php
7 months ago
AbstractController.php
7 months ago
AbstractSchema.php
7 months ago
AbstractSchema.php
107 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Abstract REST Schema. |
| 4 | * |
| 5 | * Holds schema for REST API routes. |
| 6 | * |
| 7 | * @package WooCommerce\RestApi |
| 8 | */ |
| 9 | |
| 10 | declare( strict_types=1 ); |
| 11 | |
| 12 | namespace Automattic\WooCommerce\Internal\RestApi\Routes\V4; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | use WP_REST_Request; |
| 17 | |
| 18 | /** |
| 19 | * Abstract REST Schema for WooCommerce REST API V4. |
| 20 | * |
| 21 | * Provides common functionality for all V4 schema controllers including |
| 22 | * property generation, context filtering, and validation. |
| 23 | * |
| 24 | * @since 10.2.0 |
| 25 | */ |
| 26 | abstract class AbstractSchema { |
| 27 | /** |
| 28 | * The schema item identifier. |
| 29 | * |
| 30 | * @var string |
| 31 | * @since 10.2.0 |
| 32 | */ |
| 33 | const IDENTIFIER = ''; |
| 34 | |
| 35 | /** |
| 36 | * Context for the item schema - view, edit, and embed. |
| 37 | * |
| 38 | * @var array |
| 39 | * @since 10.2.0 |
| 40 | */ |
| 41 | const VIEW_EDIT_EMBED_CONTEXT = array( 'view', 'edit', 'embed' ); |
| 42 | |
| 43 | /** |
| 44 | * Context for the item schema - view and edit only. |
| 45 | * |
| 46 | * @var array |
| 47 | * @since 10.2.0 |
| 48 | */ |
| 49 | const VIEW_EDIT_CONTEXT = array( 'view', 'edit' ); |
| 50 | |
| 51 | /** |
| 52 | * Get the item schema. |
| 53 | * |
| 54 | * @return array The item schema. |
| 55 | * @since 10.2.0 |
| 56 | */ |
| 57 | public function get_item_schema(): array { |
| 58 | return array( |
| 59 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 60 | 'title' => static::IDENTIFIER, |
| 61 | 'type' => 'object', |
| 62 | 'properties' => $this->get_item_schema_properties(), |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get the item response. |
| 68 | * |
| 69 | * @param mixed $item WordPress representation of the item. |
| 70 | * @param WP_REST_Request $request Request object. |
| 71 | * @param array $include_fields Fields to include in the response. |
| 72 | * @return array The item response. |
| 73 | */ |
| 74 | abstract public function get_item_response( $item, WP_REST_Request $request, array $include_fields = array() ): array; |
| 75 | |
| 76 | /** |
| 77 | * Return all properties for the item schema. |
| 78 | * |
| 79 | * @return array The schema properties. |
| 80 | * @since 10.2.0 |
| 81 | */ |
| 82 | public function get_item_schema_properties(): array { |
| 83 | return array(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Return all writable properties for the item schema. |
| 88 | * |
| 89 | * @return array The schema properties. |
| 90 | * @since 10.2.0 |
| 91 | */ |
| 92 | public function get_writable_item_schema_properties(): array { |
| 93 | return array_filter( $this->get_item_schema_properties(), array( $this, 'filter_writable_props' ) ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Filter schema properties to only return writable ones. |
| 98 | * |
| 99 | * @param array $schema The schema property to check. |
| 100 | * @return bool True if the property is writable, false otherwise. |
| 101 | * @since 10.2.0 |
| 102 | */ |
| 103 | protected function filter_writable_props( array $schema ): bool { |
| 104 | return empty( $schema['readonly'] ); |
| 105 | } |
| 106 | } |
| 107 |