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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / StoreApi / Schemas / V1 / AbstractSchema.php
woocommerce / src / StoreApi / Schemas / V1 Last commit date
AI 1 year ago Agentic 7 months ago AbstractAddressSchema.php 1 year ago AbstractSchema.php 1 month ago BatchSchema.php 2 years ago BillingAddressSchema.php 2 years ago CartCouponSchema.php 1 year ago CartExtensionsSchema.php 1 year ago CartFeeSchema.php 2 years ago CartItemSchema.php 1 month ago CartSchema.php 2 months ago CartShippingRateSchema.php 1 month ago CheckoutOrderSchema.php 2 years ago CheckoutSchema.php 1 month ago ErrorSchema.php 2 years ago ImageAttachmentSchema.php 3 months ago ItemSchema.php 11 months ago OrderCouponSchema.php 2 years ago OrderFeeSchema.php 2 years ago OrderItemSchema.php 1 month ago OrderSchema.php 2 months ago PatternsSchema.php 1 year ago ProductAttributeSchema.php 2 years ago ProductAttributeTermSchema.php 1 month ago ProductBrandSchema.php 1 year ago ProductCategorySchema.php 2 years ago ProductCollectionDataSchema.php 11 months ago ProductReviewSchema.php 2 years ago ProductSchema.php 1 month ago ShippingAddressSchema.php 2 years ago ShopperListItemSchema.php 1 month ago ShopperListSchema.php 1 month ago TermSchema.php 2 years ago
AbstractSchema.php
416 lines
1 <?php
2 namespace Automattic\WooCommerce\StoreApi\Schemas\V1;
3
4 use Automattic\WooCommerce\StoreApi\SchemaController;
5 use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema;
6 use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields;
7 use Automattic\WooCommerce\Blocks\Package;
8
9 /**
10 * AbstractSchema class.
11 *
12 * For REST Route Schemas
13 */
14 abstract class AbstractSchema {
15 /**
16 * The schema item name.
17 *
18 * @var string
19 */
20 protected $title = 'Schema';
21
22 /**
23 * Rest extend instance.
24 *
25 * @var ExtendSchema
26 */
27 protected $extend;
28
29 /**
30 * Schema Controller instance.
31 *
32 * @var SchemaController
33 */
34 protected $controller;
35
36 /**
37 * Extending key that gets added to endpoint.
38 *
39 * @var string
40 */
41 const EXTENDING_KEY = 'extensions';
42
43 /**
44 * Constructor.
45 *
46 * @param ExtendSchema $extend Rest Extending instance.
47 * @param SchemaController $controller Schema Controller instance.
48 */
49 public function __construct( ExtendSchema $extend, SchemaController $controller ) {
50 $this->extend = $extend;
51 $this->controller = $controller;
52 }
53
54 /**
55 * Returns the full item schema.
56 *
57 * @return array
58 */
59 public function get_item_schema() {
60 return array(
61 '$schema' => 'http://json-schema.org/draft-04/schema#',
62 'title' => $this->title,
63 'type' => 'object',
64 'properties' => $this->get_properties(),
65 );
66 }
67
68 /**
69 * Returns the full item response.
70 *
71 * @param mixed $item Item to get response for.
72 * @return array|stdClass
73 */
74 public function get_item_response( $item ) {
75 return [];
76 }
77
78 /**
79 * Return schema properties.
80 *
81 * @return array
82 */
83 abstract public function get_properties();
84
85 /**
86 * Recursive removal of arg_options.
87 *
88 * @param array $properties Schema properties.
89 */
90 protected function remove_arg_options( $properties ) {
91 return array_map(
92 function( $property ) {
93 if ( ! is_array( $property ) ) {
94 return $property;
95 }
96 if ( isset( $property['properties'] ) ) {
97 $property['properties'] = $this->remove_arg_options( $property['properties'] );
98 } elseif ( isset( $property['items']['properties'] ) ) {
99 $property['items']['properties'] = $this->remove_arg_options( $property['items']['properties'] );
100 }
101 unset( $property['arg_options'] );
102 return $property;
103 },
104 (array) $properties
105 );
106 }
107
108 /**
109 * Returns the public schema.
110 *
111 * @return array
112 */
113 public function get_public_item_schema() {
114 $schema = $this->get_item_schema();
115
116 if ( isset( $schema['properties'] ) ) {
117 $schema['properties'] = $this->remove_arg_options( $schema['properties'] );
118 }
119
120 return $schema;
121 }
122
123 /**
124 * Returns extended data for a specific endpoint.
125 *
126 * @param string $endpoint The endpoint identifier.
127 * @param array ...$passed_args An array of arguments to be passed to callbacks.
128 * @return object the data that will get added.
129 */
130 protected function get_extended_data( $endpoint, ...$passed_args ) {
131 return $this->extend->get_endpoint_data( $endpoint, $passed_args );
132 }
133
134 /**
135 * Gets an array of schema defaults recursively.
136 *
137 * @param array $properties Schema property data.
138 * @return array Array of defaults, pulled from arg_options
139 */
140 protected function get_recursive_schema_property_defaults( $properties ) {
141 $defaults = [];
142
143 foreach ( $properties as $property_key => $property_value ) {
144 if ( isset( $property_value['arg_options']['default'] ) ) {
145 $defaults[ $property_key ] = $property_value['arg_options']['default'];
146 } elseif ( isset( $property_value['properties'] ) ) {
147 $defaults[ $property_key ] = $this->get_recursive_schema_property_defaults( $property_value['properties'] );
148 }
149 }
150
151 return $defaults;
152 }
153
154 /**
155 * Gets a function that validates recursively.
156 *
157 * @param array $properties Schema property data.
158 * @return function Anonymous validation callback.
159 */
160 protected function get_recursive_validate_callback( $properties ) {
161 /**
162 * Validate a request argument based on details registered to the route.
163 *
164 * @param mixed $values
165 * @param \WP_REST_Request $request
166 * @param string $param
167 * @return true|\WP_Error
168 */
169 return function ( $values, $request, $param ) use ( $properties ) {
170 foreach ( $properties as $property_key => $property_value ) {
171 $current_value = isset( $values[ $property_key ] ) ? $values[ $property_key ] : null;
172
173 $property_type = is_array( $property_value['type'] ) ? $property_value['type'] : [ $property_value['type'] ];
174 if ( empty( $current_value ) && in_array( 'null', $property_type, true ) ) {
175 // If the value is null and the schema allows null, we can skip validation for children.
176 continue;
177 }
178
179 if ( isset( $property_value['arg_options']['validate_callback'] ) ) {
180 $callback = $property_value['arg_options']['validate_callback'];
181 $result = is_callable( $callback ) ? $callback( $current_value, $request, $param ) : false;
182 } else {
183 $result = rest_validate_value_from_schema( $current_value, $property_value, $param . ' > ' . $property_key );
184 }
185
186 if ( ! $result || is_wp_error( $result ) ) {
187 // If schema validation fails, we return here as we don't need to validate any deeper.
188 return $result;
189 }
190
191 if ( isset( $property_value['properties'] ) ) {
192 $validate_callback = $this->get_recursive_validate_callback( $property_value['properties'] );
193 $result = $validate_callback( $current_value, $request, $param . ' > ' . $property_key );
194
195 if ( ! $result || is_wp_error( $result ) ) {
196 // If schema validation fails, we return here as we don't need to validate any deeper.
197 return $result;
198 }
199 }
200 }
201
202 return true;
203 };
204 }
205
206 /**
207 * Gets a function that sanitizes recursively.
208 *
209 * @param array $properties Schema property data.
210 * @return function Anonymous validation callback.
211 */
212 protected function get_recursive_sanitize_callback( $properties ) {
213 /**
214 * Validate a request argument based on details registered to the route.
215 *
216 * @param mixed $values
217 * @param \WP_REST_Request $request
218 * @param string $param
219 * @return true|\WP_Error
220 */
221 return function ( $values, $request, $param ) use ( $properties ) {
222 $sanitized_values = [];
223
224 foreach ( $properties as $property_key => $property_value ) {
225 $current_value = isset( $values[ $property_key ] ) ? $values[ $property_key ] : null;
226
227 if ( isset( $property_value['arg_options']['sanitize_callback'] ) ) {
228 $callback = $property_value['arg_options']['sanitize_callback'];
229 $current_value = is_callable( $callback ) ? $callback( $current_value, $request, $param ) : $current_value;
230 } else {
231 $current_value = rest_sanitize_value_from_schema( $current_value, $property_value, $param . ' > ' . $property_key );
232 }
233
234 // If sanitization failed, return the WP_Error object straight away.
235 if ( is_wp_error( $current_value ) ) {
236 return $current_value;
237 }
238
239 if ( isset( $property_value['properties'] ) ) {
240 $sanitize_callback = $this->get_recursive_sanitize_callback( $property_value['properties'] );
241 $sanitized_values[ $property_key ] = $sanitize_callback( $current_value, $request, $param . ' > ' . $property_key );
242 } else {
243 $sanitized_values[ $property_key ] = $current_value;
244 }
245 }
246
247 return $sanitized_values;
248 };
249 }
250
251 /**
252 * Returns extended schema for a specific endpoint.
253 *
254 * @param string $endpoint The endpoint identifier.
255 * @param array ...$passed_args An array of arguments to be passed to callbacks.
256 * @return array the data that will get added.
257 */
258 protected function get_extended_schema( $endpoint, ...$passed_args ) {
259 $extended_schema = $this->extend->get_endpoint_schema( $endpoint, $passed_args );
260 $defaults = $this->get_recursive_schema_property_defaults( $extended_schema );
261
262 return [
263 'type' => 'object',
264 'context' => [ 'view', 'edit', 'embed' ],
265 'arg_options' => [
266 'default' => $defaults,
267 'validate_callback' => $this->get_recursive_validate_callback( $extended_schema ),
268 'sanitize_callback' => $this->get_recursive_sanitize_callback( $extended_schema ),
269 ],
270 'properties' => $extended_schema,
271 ];
272 }
273
274 /**
275 * Apply a schema get_item_response callback to an array of items and return the result.
276 *
277 * @param AbstractSchema $schema Schema class instance.
278 * @param array $items Array of items.
279 * @return array Array of values from the callback function.
280 */
281 protected function get_item_responses_from_schema( AbstractSchema $schema, $items ) {
282 $items = array_filter( $items );
283
284 if ( empty( $items ) ) {
285 return [];
286 }
287
288 return array_values( array_map( [ $schema, 'get_item_response' ], $items ) );
289 }
290
291 /**
292 * Retrieves an array of endpoint arguments from the item schema for the controller.
293 *
294 * @uses rest_get_endpoint_args_for_schema()
295 * @param string $method Optional. HTTP method of the request.
296 * @return array Endpoint arguments.
297 */
298 public function get_endpoint_args_for_item_schema( $method = \WP_REST_Server::CREATABLE ) {
299 $schema = $this->get_item_schema();
300 $endpoint_args = rest_get_endpoint_args_for_schema( $schema, $method );
301 $endpoint_args = $this->remove_arg_options( $endpoint_args );
302 return $endpoint_args;
303 }
304
305
306 /**
307 * Force all schema properties to be readonly.
308 *
309 * @param array $properties Schema.
310 * @return array Updated schema.
311 */
312 protected function force_schema_readonly( $properties ) {
313 return array_map(
314 function( $property ) {
315 $property['readonly'] = true;
316 if ( isset( $property['items']['properties'] ) ) {
317 $property['items']['properties'] = $this->force_schema_readonly( $property['items']['properties'] );
318 }
319 return $property;
320 },
321 (array) $properties
322 );
323 }
324
325 /**
326 * Returns consistent currency schema used across endpoints for prices.
327 *
328 * @return array
329 */
330 protected function get_store_currency_properties() {
331 return [
332 'currency_code' => [
333 'description' => __( 'Currency code (in ISO format) for returned prices.', 'woocommerce' ),
334 'type' => 'string',
335 'context' => [ 'view', 'edit', 'embed' ],
336 'readonly' => true,
337 ],
338 'currency_symbol' => [
339 'description' => __( 'Currency symbol for the currency which can be used to format returned prices.', 'woocommerce' ),
340 'type' => 'string',
341 'context' => [ 'view', 'edit', 'embed' ],
342 'readonly' => true,
343 ],
344 'currency_minor_unit' => [
345 'description' => __( 'Currency minor unit (number of digits after the decimal separator) for returned prices.', 'woocommerce' ),
346 'type' => 'integer',
347 'context' => [ 'view', 'edit', 'embed' ],
348 'readonly' => true,
349 ],
350 'currency_decimal_separator' => array(
351 'description' => __( 'Decimal separator for the currency which can be used to format returned prices.', 'woocommerce' ),
352 'type' => 'string',
353 'context' => array( 'view', 'edit', 'embed' ),
354 'readonly' => true,
355 ),
356 'currency_thousand_separator' => array(
357 'description' => __( 'Thousand separator for the currency which can be used to format returned prices.', 'woocommerce' ),
358 'type' => 'string',
359 'context' => array( 'view', 'edit', 'embed' ),
360 'readonly' => true,
361 ),
362 'currency_prefix' => array(
363 'description' => __( 'Price prefix for the currency which can be used to format returned prices.', 'woocommerce' ),
364 'type' => 'string',
365 'context' => array( 'view', 'edit', 'embed' ),
366 'readonly' => true,
367 ),
368 'currency_suffix' => array(
369 'description' => __( 'Price prefix for the currency which can be used to format returned prices.', 'woocommerce' ),
370 'type' => 'string',
371 'context' => array( 'view', 'edit', 'embed' ),
372 'readonly' => true,
373 ),
374 ];
375 }
376
377 /**
378 * Adds currency data to an array of monetary values.
379 *
380 * @param array $values Monetary amounts.
381 * @return array Monetary amounts with currency data appended.
382 */
383 protected function prepare_currency_response( $values ) {
384 return $this->extend->get_formatter( 'currency' )->format( $values );
385 }
386
387 /**
388 * Convert monetary values from WooCommerce to string based integers, using
389 * the smallest unit of a currency.
390 *
391 * @param string|float $amount Monetary amount with decimals.
392 * @param int $decimals Number of decimals the amount is formatted with.
393 * @param int $rounding_mode Defaults to the PHP_ROUND_HALF_UP constant.
394 * @return string The new amount.
395 */
396 protected function prepare_money_response( $amount, $decimals = 2, $rounding_mode = PHP_ROUND_HALF_UP ) {
397 return $this->extend->get_formatter( 'money' )->format(
398 $amount,
399 [
400 'decimals' => $decimals,
401 'rounding_mode' => $rounding_mode,
402 ]
403 );
404 }
405
406 /**
407 * Prepares HTML based content, such as post titles and content, for the API response.
408 *
409 * @param string|array $response Data to format.
410 * @return string|array Formatted data.
411 */
412 protected function prepare_html_response( $response ) {
413 return $this->extend->get_formatter( 'html' )->format( $response );
414 }
415 }
416