ExtendSchema.php
350 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Schemas; |
| 3 | |
| 4 | use Automattic\WooCommerce\StoreApi\Schemas\V1\CartItemSchema; |
| 5 | use Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema; |
| 6 | use Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema; |
| 7 | use Automattic\WooCommerce\StoreApi\Schemas\V1\ProductSchema; |
| 8 | use Automattic\WooCommerce\StoreApi\Formatters; |
| 9 | |
| 10 | /** |
| 11 | * Provides utility functions to extend Store API schemas. |
| 12 | * |
| 13 | * Note there are also helpers that map to these methods. |
| 14 | * |
| 15 | * @see woocommerce_store_api_register_endpoint_data() |
| 16 | * @see woocommerce_store_api_register_update_callback() |
| 17 | * @see woocommerce_store_api_register_payment_requirements() |
| 18 | * @see woocommerce_store_api_get_formatter() |
| 19 | */ |
| 20 | final class ExtendSchema { |
| 21 | /** |
| 22 | * List of Store API schema that is allowed to be extended by extensions. |
| 23 | * |
| 24 | * @var string[] |
| 25 | */ |
| 26 | private $endpoints = [ |
| 27 | CartItemSchema::IDENTIFIER, |
| 28 | CartSchema::IDENTIFIER, |
| 29 | CheckoutSchema::IDENTIFIER, |
| 30 | ProductSchema::IDENTIFIER, |
| 31 | ]; |
| 32 | |
| 33 | /** |
| 34 | * Holds the formatters class instance. |
| 35 | * |
| 36 | * @var Formatters |
| 37 | */ |
| 38 | private $formatters; |
| 39 | |
| 40 | /** |
| 41 | * Data to be extended |
| 42 | * |
| 43 | * @var array |
| 44 | */ |
| 45 | private $extend_data = []; |
| 46 | |
| 47 | /** |
| 48 | * Data to be extended |
| 49 | * |
| 50 | * @var array |
| 51 | */ |
| 52 | private $callback_methods = []; |
| 53 | |
| 54 | /** |
| 55 | * Array of payment requirements |
| 56 | * |
| 57 | * @var array |
| 58 | */ |
| 59 | private $payment_requirements = []; |
| 60 | |
| 61 | /** |
| 62 | * Constructor |
| 63 | * |
| 64 | * @param Formatters $formatters An instance of the formatters class. |
| 65 | */ |
| 66 | public function __construct( Formatters $formatters ) { |
| 67 | $this->formatters = $formatters; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Register endpoint data under a specified namespace |
| 72 | * |
| 73 | * @param array $args { |
| 74 | * An array of elements that make up a post to update or insert. |
| 75 | * |
| 76 | * @type string $endpoint Required. The endpoint to extend. |
| 77 | * @type string $namespace Required. Plugin namespace. |
| 78 | * @type callable $schema_callback Callback executed to add schema data. |
| 79 | * @type callable $data_callback Callback executed to add endpoint data. |
| 80 | * @type string $schema_type The type of data, object or array. |
| 81 | * } |
| 82 | * |
| 83 | * @throws \Exception On failure to register. |
| 84 | */ |
| 85 | public function register_endpoint_data( $args ) { |
| 86 | $args = wp_parse_args( |
| 87 | $args, |
| 88 | [ |
| 89 | 'endpoint' => '', |
| 90 | 'namespace' => '', |
| 91 | 'schema_callback' => null, |
| 92 | 'data_callback' => null, |
| 93 | 'schema_type' => ARRAY_A, |
| 94 | ] |
| 95 | ); |
| 96 | |
| 97 | if ( ! is_string( $args['namespace'] ) || empty( $args['namespace'] ) ) { |
| 98 | $this->throw_exception( 'You must provide a plugin namespace when extending a Store REST endpoint.' ); |
| 99 | } |
| 100 | |
| 101 | if ( ! in_array( $args['endpoint'], $this->endpoints, true ) ) { |
| 102 | $this->throw_exception( |
| 103 | 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'] ) |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | if ( ! is_null( $args['schema_callback'] ) && ! is_callable( $args['schema_callback'] ) ) { |
| 108 | $this->throw_exception( '$schema_callback must be a callable function.' ); |
| 109 | } |
| 110 | |
| 111 | if ( ! is_null( $args['data_callback'] ) && ! is_callable( $args['data_callback'] ) ) { |
| 112 | $this->throw_exception( '$data_callback must be a callable function.' ); |
| 113 | } |
| 114 | |
| 115 | if ( ! in_array( $args['schema_type'], [ ARRAY_N, ARRAY_A ], true ) ) { |
| 116 | $this->throw_exception( |
| 117 | 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'] ) |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | $this->extend_data[ $args['endpoint'] ][ $args['namespace'] ] = [ |
| 122 | 'schema_callback' => $args['schema_callback'], |
| 123 | 'data_callback' => $args['data_callback'], |
| 124 | 'schema_type' => $args['schema_type'], |
| 125 | ]; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Add callback functions that can be executed by the cart/extensions endpoint. |
| 130 | * |
| 131 | * @param array $args { |
| 132 | * An array of elements that make up the callback configuration. |
| 133 | * |
| 134 | * @type string $namespace Required. Plugin namespace. |
| 135 | * @type callable $callback Required. The function/callable to execute. |
| 136 | * } |
| 137 | * |
| 138 | * @throws \Exception On failure to register. |
| 139 | */ |
| 140 | public function register_update_callback( $args ) { |
| 141 | $args = wp_parse_args( |
| 142 | $args, |
| 143 | [ |
| 144 | 'namespace' => '', |
| 145 | 'callback' => null, |
| 146 | ] |
| 147 | ); |
| 148 | |
| 149 | if ( ! is_string( $args['namespace'] ) || empty( $args['namespace'] ) ) { |
| 150 | throw new \Exception( 'You must provide a plugin namespace when extending a Store REST endpoint.' ); |
| 151 | } |
| 152 | |
| 153 | if ( ! is_callable( $args['callback'] ) ) { |
| 154 | throw new \Exception( 'There is no valid callback supplied to register_update_callback.' ); |
| 155 | } |
| 156 | |
| 157 | $this->callback_methods[ $args['namespace'] ] = $args; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Registers and validates payment requirements callbacks. |
| 162 | * |
| 163 | * @param array $args { |
| 164 | * Array of registration data. |
| 165 | * |
| 166 | * @type callable $data_callback Required. Callback executed to add payment requirements data. |
| 167 | * } |
| 168 | * |
| 169 | * @throws \Exception On failure to register. |
| 170 | */ |
| 171 | public function register_payment_requirements( $args ) { |
| 172 | if ( empty( $args['data_callback'] ) || ! is_callable( $args['data_callback'] ) ) { |
| 173 | $this->throw_exception( '$data_callback must be a callable function.' ); |
| 174 | } |
| 175 | $this->payment_requirements[] = $args['data_callback']; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Returns a formatter instance. |
| 180 | * |
| 181 | * @param string $name Formatter name. |
| 182 | * @return FormatterInterface |
| 183 | */ |
| 184 | public function get_formatter( $name ) { |
| 185 | return $this->formatters->$name; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Get callback for a specific endpoint and namespace. |
| 190 | * |
| 191 | * @param string $namespace The namespace to get callbacks for. |
| 192 | * |
| 193 | * @return callable The callback registered by the extension. |
| 194 | * @throws \Exception When callback is not callable or parameters are incorrect. |
| 195 | */ |
| 196 | public function get_update_callback( $namespace ) { |
| 197 | if ( ! is_string( $namespace ) ) { |
| 198 | throw new \Exception( 'You must provide a plugin namespace when extending a Store REST endpoint.' ); |
| 199 | } |
| 200 | |
| 201 | if ( ! array_key_exists( $namespace, $this->callback_methods ) ) { |
| 202 | throw new \Exception( sprintf( 'There is no such namespace registered: %1$s.', $namespace ) ); |
| 203 | } |
| 204 | |
| 205 | if ( ! array_key_exists( 'callback', $this->callback_methods[ $namespace ] ) || ! is_callable( $this->callback_methods[ $namespace ]['callback'] ) ) { |
| 206 | throw new \Exception( sprintf( 'There is no valid callback registered for: %1$s.', $namespace ) ); |
| 207 | } |
| 208 | |
| 209 | return $this->callback_methods[ $namespace ]['callback']; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Returns the registered endpoint data |
| 214 | * |
| 215 | * @param string $endpoint A valid identifier. |
| 216 | * @param array $passed_args Passed arguments from the Schema class. |
| 217 | * @return object Returns an casted object with registered endpoint data. |
| 218 | * @throws \Exception If a registered callback throws an error, or silently logs it. |
| 219 | */ |
| 220 | public function get_endpoint_data( $endpoint, array $passed_args = [] ) { |
| 221 | $registered_data = []; |
| 222 | |
| 223 | if ( isset( $this->extend_data[ $endpoint ] ) ) { |
| 224 | foreach ( $this->extend_data[ $endpoint ] as $namespace => $callbacks ) { |
| 225 | if ( is_null( $callbacks['data_callback'] ) ) { |
| 226 | continue; |
| 227 | } |
| 228 | try { |
| 229 | $data = $callbacks['data_callback']( ...$passed_args ); |
| 230 | |
| 231 | if ( ! is_array( $data ) ) { |
| 232 | $data = []; |
| 233 | throw new \Exception( '$data_callback must return an array.' ); |
| 234 | } |
| 235 | } catch ( \Throwable $e ) { |
| 236 | $this->throw_exception( $e ); |
| 237 | } |
| 238 | |
| 239 | $registered_data[ $namespace ] = $data; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return (object) $registered_data; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Returns the registered endpoint schema |
| 248 | * |
| 249 | * @param string $endpoint A valid identifier. |
| 250 | * @param array $passed_args Passed arguments from the Schema class. |
| 251 | * @return object Returns an array with registered schema data. |
| 252 | * @throws \Exception If a registered callback throws an error, or silently logs it. |
| 253 | */ |
| 254 | public function get_endpoint_schema( $endpoint, array $passed_args = [] ) { |
| 255 | $registered_schema = []; |
| 256 | |
| 257 | if ( isset( $this->extend_data[ $endpoint ] ) ) { |
| 258 | foreach ( $this->extend_data[ $endpoint ] as $namespace => $callbacks ) { |
| 259 | if ( is_null( $callbacks['schema_callback'] ) ) { |
| 260 | continue; |
| 261 | } |
| 262 | try { |
| 263 | $schema = $callbacks['schema_callback']( ...$passed_args ); |
| 264 | |
| 265 | if ( ! is_array( $schema ) ) { |
| 266 | $schema = []; |
| 267 | throw new \Exception( '$schema_callback must return an array.' ); |
| 268 | } |
| 269 | } catch ( \Throwable $e ) { |
| 270 | $this->throw_exception( $e ); |
| 271 | } |
| 272 | |
| 273 | $registered_schema[ $namespace ] = $this->format_extensions_properties( $namespace, $schema, $callbacks['schema_type'] ); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return (object) $registered_schema; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Returns the additional payment requirements for the cart which are required to make payments. Values listed here |
| 282 | * are compared against each Payment Gateways "supports" flag. |
| 283 | * |
| 284 | * @param array $requirements list of requirements that should be added to the collected requirements. |
| 285 | * @return array Returns a list of payment requirements. |
| 286 | * @throws \Exception If a registered callback throws an error, or silently logs it. |
| 287 | */ |
| 288 | public function get_payment_requirements( array $requirements = [ 'products' ] ) { |
| 289 | if ( ! empty( $this->payment_requirements ) ) { |
| 290 | foreach ( $this->payment_requirements as $callback ) { |
| 291 | try { |
| 292 | $data = $callback(); |
| 293 | |
| 294 | if ( ! is_array( $data ) ) { |
| 295 | throw new \Exception( '$data_callback must return an array.' ); |
| 296 | } |
| 297 | |
| 298 | $requirements = array_unique( array_merge( $requirements, $data ) ); |
| 299 | } catch ( \Throwable $e ) { |
| 300 | $this->throw_exception( $e ); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | return $requirements; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Throws error and/or silently logs it. |
| 309 | * |
| 310 | * @param string|\Throwable $exception_or_error Error message or \Exception. |
| 311 | * @throws \Exception An error to throw if we have debug enabled and user is admin. |
| 312 | */ |
| 313 | private function throw_exception( $exception_or_error ) { |
| 314 | $exception = is_string( $exception_or_error ) ? new \Exception( $exception_or_error ) : $exception_or_error; |
| 315 | |
| 316 | wc_caught_exception( $exception ); |
| 317 | |
| 318 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG && current_user_can( 'manage_woocommerce' ) ) { |
| 319 | throw $exception; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Format schema for an extension. |
| 325 | * |
| 326 | * @param string $namespace Error message or \Exception. |
| 327 | * @param array $schema An error to throw if we have debug enabled and user is admin. |
| 328 | * @param string $schema_type How should data be shaped. |
| 329 | * @return array Formatted schema. |
| 330 | */ |
| 331 | private function format_extensions_properties( $namespace, $schema, $schema_type ) { |
| 332 | if ( ARRAY_N === $schema_type ) { |
| 333 | return [ |
| 334 | /* translators: %s: extension namespace */ |
| 335 | 'description' => sprintf( __( 'Extension data registered by %s', 'woocommerce' ), $namespace ), |
| 336 | 'type' => [ 'array', 'null' ], |
| 337 | 'context' => [ 'view', 'edit' ], |
| 338 | 'items' => $schema, |
| 339 | ]; |
| 340 | } |
| 341 | return [ |
| 342 | /* translators: %s: extension namespace */ |
| 343 | 'description' => sprintf( __( 'Extension data registered by %s', 'woocommerce' ), $namespace ), |
| 344 | 'type' => [ 'object', 'null' ], |
| 345 | 'context' => [ 'view', 'edit' ], |
| 346 | 'properties' => $schema, |
| 347 | ]; |
| 348 | } |
| 349 | } |
| 350 |