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
|
<?php namespace Automattic\WooCommerce\StoreApi\Schemas;
use Automattic\WooCommerce\StoreApi\Schemas\V1\CartItemSchema; use Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema; use Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema; use Automattic\WooCommerce\StoreApi\Schemas\V1\ProductSchema; use Automattic\WooCommerce\StoreApi\Formatters;
/** * Provides utility functions to extend Store API schemas. * * Note there are also helpers that map to these methods. * * @see woocommerce_store_api_register_endpoint_data() * @see woocommerce_store_api_register_update_callback() * @see woocommerce_store_api_register_payment_requirements() * @see woocommerce_store_api_get_formatter() */ final class ExtendSchema { /** * List of Store API schema that is allowed to be extended by extensions. * * @var string[] */ private $endpoints = [ CartItemSchema::IDENTIFIER, CartSchema::IDENTIFIER, CheckoutSchema::IDENTIFIER, ProductSchema::IDENTIFIER, ];
/** * Holds the formatters class instance. * * @var Formatters */ private $formatters;
/** * Data to be extended * * @var array */ private $extend_data = [];
/** * Data to be extended * * @var array */ private $callback_methods = [];
/** * Array of payment requirements * * @var array */ private $payment_requirements = [];
/** * Constructor * * @param Formatters $formatters An instance of the formatters class. */ public function __construct( Formatters $formatters ) { $this->formatters = $formatters; }
/** * Register endpoint data under a specified namespace * * @param array $args { * An array of elements that make up a post to update or insert. * * @type string $endpoint Required. The endpoint to extend. * @type string $namespace Required. Plugin namespace. * @type callable $schema_callback Callback executed to add schema data. * @type callable $data_callback Callback executed to add endpoint data. * @type string $schema_type The type of data, object or array. * } * * @throws \Exception On failure to register. */ public function register_endpoint_data( $args ) { $args = wp_parse_args( $args, [ 'endpoint' => '', 'namespace' => '', 'schema_callback' => null, 'data_callback' => null, 'schema_type' => ARRAY_A, ] );
if ( ! is_string( $args['namespace'] ) || empty( $args['namespace'] ) ) { $this->throw_exception( 'You must provide a plugin namespace when extending a Store REST endpoint.' ); }
if ( ! in_array( $args['endpoint'], $this->endpoints, true ) ) { $this->throw_exception( sprintf( 'You must provide a valid Store REST endpoint to extend, valid endpoints are: %1$s. You provided %2$s.', implode( ', ', $this->endpoints ), $args['endpoint'] ) ); }
if ( ! is_null( $args['schema_callback'] ) && ! is_callable( $args['schema_callback'] ) ) { $this->throw_exception( '$schema_callback must be a callable function.' ); }
if ( ! is_null( $args['data_callback'] ) && ! is_callable( $args['data_callback'] ) ) { $this->throw_exception( '$data_callback must be a callable function.' ); }
if ( ! in_array( $args['schema_type'], [ ARRAY_N, ARRAY_A ], true ) ) { $this->throw_exception( sprintf( 'Data type must be either ARRAY_N for a numeric array or ARRAY_A for an object like array. You provided %1$s.', $args['schema_type'] ) ); }
$this->extend_data[ $args['endpoint'] ][ $args['namespace'] ] = [ 'schema_callback' => $args['schema_callback'], 'data_callback' => $args['data_callback'], 'schema_type' => $args['schema_type'], ]; }
/** * Add callback functions that can be executed by the cart/extensions endpoint. * * @param array $args { * An array of elements that make up the callback configuration. * * @type string $namespace Required. Plugin namespace. * @type callable $callback Required. The function/callable to execute. * } * * @throws \Exception On failure to register. */ public function register_update_callback( $args ) { $args = wp_parse_args( $args, [ 'namespace' => '', 'callback' => null, ] );
if ( ! is_string( $args['namespace'] ) || empty( $args['namespace'] ) ) { throw new \Exception( 'You must provide a plugin namespace when extending a Store REST endpoint.' ); }
if ( ! is_callable( $args['callback'] ) ) { throw new \Exception( 'There is no valid callback supplied to register_update_callback.' ); }
$this->callback_methods[ $args['namespace'] ] = $args; }
/** * Registers and validates payment requirements callbacks. * * @param array $args { * Array of registration data. * * @type callable $data_callback Required. Callback executed to add payment requirements data. * } * * @throws \Exception On failure to register. */ public function register_payment_requirements( $args ) { if ( empty( $args['data_callback'] ) || ! is_callable( $args['data_callback'] ) ) { $this->throw_exception( '$data_callback must be a callable function.' ); } $this->payment_requirements[] = $args['data_callback']; }
/** * Returns a formatter instance. * * @param string $name Formatter name. * @return FormatterInterface */ public function get_formatter( $name ) { return $this->formatters->$name; }
/** * Get callback for a specific endpoint and namespace. * * @param string $namespace The namespace to get callbacks for. * * @return callable The callback registered by the extension. * @throws \Exception When callback is not callable or parameters are incorrect. */ public function get_update_callback( $namespace ) { if ( ! is_string( $namespace ) ) { throw new \Exception( 'You must provide a plugin namespace when extending a Store REST endpoint.' ); }
if ( ! array_key_exists( $namespace, $this->callback_methods ) ) { throw new \Exception( sprintf( 'There is no such namespace registered: %1$s.', $namespace ) ); }
if ( ! array_key_exists( 'callback', $this->callback_methods[ $namespace ] ) || ! is_callable( $this->callback_methods[ $namespace ]['callback'] ) ) { throw new \Exception( sprintf( 'There is no valid callback registered for: %1$s.', $namespace ) ); }
return $this->callback_methods[ $namespace ]['callback']; }
/** * Returns the registered endpoint data * * @param string $endpoint A valid identifier. * @param array $passed_args Passed arguments from the Schema class. * @return object Returns an casted object with registered endpoint data. * @throws \Exception If a registered callback throws an error, or silently logs it. */ public function get_endpoint_data( $endpoint, array $passed_args = [] ) { $registered_data = [];
if ( isset( $this->extend_data[ $endpoint ] ) ) { foreach ( $this->extend_data[ $endpoint ] as $namespace => $callbacks ) { if ( is_null( $callbacks['data_callback'] ) ) { continue; } try { $data = $callbacks['data_callback']( ...$passed_args );
if ( ! is_array( $data ) ) { $data = []; throw new \Exception( '$data_callback must return an array.' ); } } catch ( \Throwable $e ) { $this->throw_exception( $e ); }
$registered_data[ $namespace ] = $data; } }
return (object) $registered_data; }
/** * Returns the registered endpoint schema * * @param string $endpoint A valid identifier. * @param array $passed_args Passed arguments from the Schema class. * @return object Returns an array with registered schema data. * @throws \Exception If a registered callback throws an error, or silently logs it. */ public function get_endpoint_schema( $endpoint, array $passed_args = [] ) { $registered_schema = [];
if ( isset( $this->extend_data[ $endpoint ] ) ) { foreach ( $this->extend_data[ $endpoint ] as $namespace => $callbacks ) { if ( is_null( $callbacks['schema_callback'] ) ) { continue; } try { $schema = $callbacks['schema_callback']( ...$passed_args );
if ( ! is_array( $schema ) ) { $schema = []; throw new \Exception( '$schema_callback must return an array.' ); } } catch ( \Throwable $e ) { $this->throw_exception( $e ); }
$registered_schema[ $namespace ] = $this->format_extensions_properties( $namespace, $schema, $callbacks['schema_type'] ); } }
return (object) $registered_schema; }
/** * Returns the additional payment requirements for the cart which are required to make payments. Values listed here * are compared against each Payment Gateways "supports" flag. * * @param array $requirements list of requirements that should be added to the collected requirements. * @return array Returns a list of payment requirements. * @throws \Exception If a registered callback throws an error, or silently logs it. */ public function get_payment_requirements( array $requirements = [ 'products' ] ) { if ( ! empty( $this->payment_requirements ) ) { foreach ( $this->payment_requirements as $callback ) { try { $data = $callback();
if ( ! is_array( $data ) ) { throw new \Exception( '$data_callback must return an array.' ); }
$requirements = array_unique( array_merge( $requirements, $data ) ); } catch ( \Throwable $e ) { $this->throw_exception( $e ); } } } return $requirements; }
/** * Throws error and/or silently logs it. * * @param string|\Throwable $exception_or_error Error message or \Exception. * @throws \Exception An error to throw if we have debug enabled and user is admin. */ private function throw_exception( $exception_or_error ) { $exception = is_string( $exception_or_error ) ? new \Exception( $exception_or_error ) : $exception_or_error;
wc_caught_exception( $exception );
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && current_user_can( 'manage_woocommerce' ) ) { throw $exception; } }
/** * Format schema for an extension. * * @param string $namespace Error message or \Exception. * @param array $schema An error to throw if we have debug enabled and user is admin. * @param string $schema_type How should data be shaped. * @return array Formatted schema. */ private function format_extensions_properties( $namespace, $schema, $schema_type ) { if ( ARRAY_N === $schema_type ) { return [ /* translators: %s: extension namespace */ 'description' => sprintf( __( 'Extension data registered by %s', 'woocommerce' ), $namespace ), 'type' => [ 'array', 'null' ], 'context' => [ 'view', 'edit' ], 'items' => $schema, ]; } return [ /* translators: %s: extension namespace */ 'description' => sprintf( __( 'Extension data registered by %s', 'woocommerce' ), $namespace ), 'type' => [ 'object', 'null' ], 'context' => [ 'view', 'edit' ], 'properties' => $schema, ]; } }
|