/var/www/html_us/wp-content/plugins/woocommerce/src/StoreApi/Schemas/V1/AbstractSchema.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
namespace Automattic\WooCommerce\StoreApi\Schemas\V1;

use 
Automattic\WooCommerce\StoreApi\SchemaController;
use 
Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema;
use 
Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields;
use 
Automattic\WooCommerce\Blocks\Package;

/**
 * AbstractSchema class.
 *
 * For REST Route Schemas
 */
abstract class AbstractSchema {
    
/**
     * The schema item name.
     *
     * @var string
     */
    
protected $title 'Schema';

    
/**
     * Rest extend instance.
     *
     * @var ExtendSchema
     */
    
protected $extend;

    
/**
     * Schema Controller instance.
     *
     * @var SchemaController
     */
    
protected $controller;

    
/**
     * Extending key that gets added to endpoint.
     *
     * @var string
     */
    
const EXTENDING_KEY 'extensions';

    
/**
     * Constructor.
     *
     * @param ExtendSchema     $extend Rest Extending instance.
     * @param SchemaController $controller Schema Controller instance.
     */
    
public function __constructExtendSchema $extendSchemaController $controller ) {
        
$this->extend     $extend;
        
$this->controller $controller;
    }

    
/**
     * Returns the full item schema.
     *
     * @return array
     */
    
public function get_item_schema() {
        return array(
            
'$schema'    => 'http://json-schema.org/draft-04/schema#',
            
'title'      => $this->title,
            
'type'       => 'object',
            
'properties' => $this->get_properties(),
        );
    }

    
/**
     * Returns the full item response.
     *
     * @param mixed $item Item to get response for.
     * @return array|stdClass
     */
    
public function get_item_response$item ) {
        return [];
    }

    
/**
     * Return schema properties.
     *
     * @return array
     */
    
abstract public function get_properties();

    
/**
     * Recursive removal of arg_options.
     *
     * @param array $properties Schema properties.
     */
    
protected function remove_arg_options$properties ) {
        return 
array_map(
            function( 
$property ) {
                if ( isset( 
$property['properties'] ) ) {
                    
$property['properties'] = $this->remove_arg_options$property['properties'] );
                } elseif ( isset( 
$property['items']['properties'] ) ) {
                    
$property['items']['properties'] = $this->remove_arg_options$property['items']['properties'] );
                }
                unset( 
$property['arg_options'] );
                return 
$property;
            },
            (array) 
$properties
        
);
    }

    
/**
     * Returns the public schema.
     *
     * @return array
     */
    
public function get_public_item_schema() {
        
$schema $this->get_item_schema();

        if ( isset( 
$schema['properties'] ) ) {
            
$schema['properties'] = $this->remove_arg_options$schema['properties'] );
        }

        return 
$schema;
    }

    
/**
     * Returns extended data for a specific endpoint.
     *
     * @param string $endpoint The endpoint identifier.
     * @param array  ...$passed_args An array of arguments to be passed to callbacks.
     * @return object the data that will get added.
     */
    
protected function get_extended_data$endpoint, ...$passed_args ) {
        return 
$this->extend->get_endpoint_data$endpoint$passed_args );
    }

    
/**
     * Gets an array of schema defaults recursively.
     *
     * @param array $properties Schema property data.
     * @return array Array of defaults, pulled from arg_options
     */
    
protected function get_recursive_schema_property_defaults$properties ) {
        
$defaults = [];

        foreach ( 
$properties as $property_key => $property_value ) {
            if ( isset( 
$property_value['arg_options']['default'] ) ) {
                
$defaults$property_key ] = $property_value['arg_options']['default'];
            } elseif ( isset( 
$property_value['properties'] ) ) {
                
$defaults$property_key ] = $this->get_recursive_schema_property_defaults$property_value['properties'] );
            }
        }

        return 
$defaults;
    }

    
/**
     * Gets a function that validates recursively.
     *
     * @param array $properties Schema property data.
     * @return function Anonymous validation callback.
     */
    
protected function get_recursive_validate_callback$properties ) {
        
/**
         * Validate a request argument based on details registered to the route.
         *
         * @param mixed            $values
         * @param \WP_REST_Request $request
         * @param string           $param
         * @return true|\WP_Error
         */
        
return function ( $values$request$param ) use ( $properties ) {
            foreach ( 
$properties as $property_key => $property_value ) {
                
$current_value = isset( $values$property_key ] ) ? $values$property_key ] : null;

                
$property_type is_array$property_value['type'] ) ? $property_value['type'] : [ $property_value['type'] ];
                if ( empty( 
$current_value ) && in_array'null'$property_typetrue ) ) {
                    
// If the value is null and the schema allows null, we can skip validation for children.
                    
continue;
                }

                if ( isset( 
$property_value['arg_options']['validate_callback'] ) ) {
                    
$callback $property_value['arg_options']['validate_callback'];
                    
$result   is_callable$callback ) ? $callback$current_value$request$param ) : false;
                } else {
                    
$result rest_validate_value_from_schema$current_value$property_value$param ' > ' $property_key );
                }

                if ( ! 
$result || is_wp_error$result ) ) {
                    
// If schema validation fails, we return here as we don't need to validate any deeper.
                    
return $result;
                }

                if ( isset( 
$property_value['properties'] ) ) {
                    
$validate_callback $this->get_recursive_validate_callback$property_value['properties'] );
                    
$result            $validate_callback$current_value$request$param ' > ' $property_key );

                    if ( ! 
$result || is_wp_error$result ) ) {
                        
// If schema validation fails, we return here as we don't need to validate any deeper.
                        
return $result;
                    }
                }
            }

            return 
true;
        };
    }

    
/**
     * Gets a function that sanitizes recursively.
     *
     * @param array $properties Schema property data.
     * @return function Anonymous validation callback.
     */
    
protected function get_recursive_sanitize_callback$properties ) {
        
/**
         * Validate a request argument based on details registered to the route.
         *
         * @param mixed            $values
         * @param \WP_REST_Request $request
         * @param string           $param
         * @return true|\WP_Error
         */
        
return function ( $values$request$param ) use ( $properties ) {
            
$sanitized_values = [];

            foreach ( 
$properties as $property_key => $property_value ) {
                
$current_value = isset( $values$property_key ] ) ? $values$property_key ] : null;

                if ( isset( 
$property_value['arg_options']['sanitize_callback'] ) ) {
                    
$callback      $property_value['arg_options']['sanitize_callback'];
                    
$current_value is_callable$callback ) ? $callback$current_value$request$param ) : $current_value;
                } else {
                    
$current_value rest_sanitize_value_from_schema$current_value$property_value$param ' > ' $property_key );
                }

                
// If sanitization failed, return the WP_Error object straight away.
                
if ( is_wp_error$current_value ) ) {
                    return 
$current_value;
                }

                if ( isset( 
$property_value['properties'] ) ) {
                    
$sanitize_callback                 $this->get_recursive_sanitize_callback$property_value['properties'] );
                    
$sanitized_values$property_key ] = $sanitize_callback$current_value$request$param ' > ' $property_key );
                } else {
                    
$sanitized_values$property_key ] = $current_value;
                }
            }

            return 
$sanitized_values;
        };
    }

    
/**
     * Returns extended schema for a specific endpoint.
     *
     * @param string $endpoint The endpoint identifer.
     * @param array  ...$passed_args An array of arguments to be passed to callbacks.
     * @return array the data that will get added.
     */
    
protected function get_extended_schema$endpoint, ...$passed_args ) {
        
$extended_schema $this->extend->get_endpoint_schema$endpoint$passed_args );
        
$defaults        $this->get_recursive_schema_property_defaults$extended_schema );

        return [
            
'type'        => 'object',
            
'context'     => [ 'view''edit' ],
            
'arg_options' => [
                
'default'           => $defaults,
                
'validate_callback' => $this->get_recursive_validate_callback$extended_schema ),
                
'sanitize_callback' => $this->get_recursive_sanitize_callback$extended_schema ),
            ],
            
'properties'  => $extended_schema,
        ];
    }

    
/**
     * Apply a schema get_item_response callback to an array of items and return the result.
     *
     * @param AbstractSchema $schema Schema class instance.
     * @param array          $items Array of items.
     * @return array Array of values from the callback function.
     */
    
protected function get_item_responses_from_schemaAbstractSchema $schema$items ) {
        
$items array_filter$items );

        if ( empty( 
$items ) ) {
            return [];
        }

        return 
array_valuesarray_map( [ $schema'get_item_response' ], $items ) );
    }

    
/**
     * Retrieves an array of endpoint arguments from the item schema for the controller.
     *
     * @uses rest_get_endpoint_args_for_schema()
     * @param string $method Optional. HTTP method of the request.
     * @return array Endpoint arguments.
     */
    
public function get_endpoint_args_for_item_schema$method \WP_REST_Server::CREATABLE ) {
        
$schema        $this->get_item_schema();
        
$endpoint_args rest_get_endpoint_args_for_schema$schema$method );
        
$endpoint_args $this->remove_arg_options$endpoint_args );
        return 
$endpoint_args;
    }


    
/**
     * Force all schema properties to be readonly.
     *
     * @param array $properties Schema.
     * @return array Updated schema.
     */
    
protected function force_schema_readonly$properties ) {
        return 
array_map(
            function( 
$property ) {
                
$property['readonly'] = true;
                if ( isset( 
$property['items']['properties'] ) ) {
                    
$property['items']['properties'] = $this->force_schema_readonly$property['items']['properties'] );
                }
                return 
$property;
            },
            (array) 
$properties
        
);
    }

    
/**
     * Returns consistent currency schema used across endpoints for prices.
     *
     * @return array
     */
    
protected function get_store_currency_properties() {
        return [
            
'currency_code'               => [
                
'description' => __'Currency code (in ISO format) for returned prices.''woocommerce' ),
                
'type'        => 'string',
                
'context'     => [ 'view''edit' ],
                
'readonly'    => true,
            ],
            
'currency_symbol'             => [
                
'description' => __'Currency symbol for the currency which can be used to format returned prices.''woocommerce' ),
                
'type'        => 'string',
                
'context'     => [ 'view''edit' ],
                
'readonly'    => true,
            ],
            
'currency_minor_unit'         => [
                
'description' => __'Currency minor unit (number of digits after the decimal separator) for returned prices.''woocommerce' ),
                
'type'        => 'integer',
                
'context'     => [ 'view''edit' ],
                
'readonly'    => true,
            ],
            
'currency_decimal_separator'  => array(
                
'description' => __'Decimal separator for the currency which can be used to format returned prices.''woocommerce' ),
                
'type'        => 'string',
                
'context'     => array( 'view''edit' ),
                
'readonly'    => true,
            ),
            
'currency_thousand_separator' => array(
                
'description' => __'Thousand separator for the currency which can be used to format returned prices.''woocommerce' ),
                
'type'        => 'string',
                
'context'     => array( 'view''edit' ),
                
'readonly'    => true,
            ),
            
'currency_prefix'             => array(
                
'description' => __'Price prefix for the currency which can be used to format returned prices.''woocommerce' ),
                
'type'        => 'string',
                
'context'     => array( 'view''edit' ),
                
'readonly'    => true,
            ),
            
'currency_suffix'             => array(
                
'description' => __'Price prefix for the currency which can be used to format returned prices.''woocommerce' ),
                
'type'        => 'string',
                
'context'     => array( 'view''edit' ),
                
'readonly'    => true,
            ),
        ];
    }

    
/**
     * Adds currency data to an array of monetary values.
     *
     * @param array $values Monetary amounts.
     * @return array Monetary amounts with currency data appended.
     */
    
protected function prepare_currency_response$values ) {
        return 
$this->extend->get_formatter'currency' )->format$values );
    }

    
/**
     * Convert monetary values from WooCommerce to string based integers, using
     * the smallest unit of a currency.
     *
     * @param string|float $amount Monetary amount with decimals.
     * @param int          $decimals Number of decimals the amount is formatted with.
     * @param int          $rounding_mode Defaults to the PHP_ROUND_HALF_UP constant.
     * @return string      The new amount.
     */
    
protected function prepare_money_response$amount$decimals 2$rounding_mode PHP_ROUND_HALF_UP ) {
        return 
$this->extend->get_formatter'money' )->format(
            
$amount,
            [
                
'decimals'      => $decimals,
                
'rounding_mode' => $rounding_mode,
            ]
        );
    }

    
/**
     * Prepares HTML based content, such as post titles and content, for the API response.
     *
     * @param string|array $response Data to format.
     * @return string|array Formatted data.
     */
    
protected function prepare_html_response$response ) {
        return 
$this->extend->get_formatter'html' )->format$response );
    }
}