From 8c2c0f73ad797b60e703e04779559692f75bca94 Mon Sep 17 00:00:00 2001 From: Seghir Nadir Date: Mon, 30 Nov 2020 15:38:10 +0100 Subject: [PATCH] Change register_endpoint_data to use an array of params instead of individual params. (https://github.com/woocommerce/woocommerce-blocks/pull/3478) * use an array args * allow schema to be empty if nothing was registred --- .../src/Domain/Services/ExtendRestApi.php | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/plugins/woocommerce-blocks/src/Domain/Services/ExtendRestApi.php b/plugins/woocommerce-blocks/src/Domain/Services/ExtendRestApi.php index 54466bedd84..f0d04e71288 100644 --- a/plugins/woocommerce-blocks/src/Domain/Services/ExtendRestApi.php +++ b/plugins/woocommerce-blocks/src/Domain/Services/ExtendRestApi.php @@ -44,36 +44,40 @@ class ExtendRestApi { /** * An endpoint that validates registration method call * - * @param string $endpoint The endpoint to extend. - * @param string $namespace Plugin namespace. - * @param callable $schema_callback Callback executed to add schema data. - * @param callable $data_callback Callback executed to add endpoint data. + * @param array $args { + * An array of elements that make up a post to update or insert. + * + * @type string $endpoint The endpoint to extend. + * @type string $namespace Plugin namespace. + * @type callable $schema_callback Callback executed to add schema data. + * @type callable $data_callback Callback executed to add endpoint data. + * } * * @throws Exception On failure to register. * @return boolean True on success. */ - public function register_endpoint_data( $endpoint, $namespace, $schema_callback, $data_callback ) { - if ( ! is_string( $namespace ) ) { + public function register_endpoint_data( $args ) { + if ( ! is_string( $args['namespace'] ) ) { $this->throw_exception( 'You must provide a plugin namespace when extending a Store REST endpoint.' ); } - if ( ! is_string( $endpoint ) || ! in_array( $endpoint, $this->endpoints, true ) ) { + if ( ! is_string( $args['endpoint'] ) || ! 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 ), $endpoint ) + 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_callable( $schema_callback ) ) { + if ( ! is_callable( $args['schema_callback'] ) ) { $this->throw_exception( '$schema_callback must be a callable function.' ); } - if ( ! is_callable( $data_callback ) ) { + if ( ! is_callable( $args['data_callback'] ) ) { $this->throw_exception( '$data_callback must be a callable function.' ); } - $this->extend_data[ $endpoint ][ $namespace ] = [ - 'schema_callback' => $schema_callback, - 'data_callback' => $data_callback, + $this->extend_data[ $args['endpoint'] ][ $args['namespace'] ] = [ + 'schema_callback' => $args['schema_callback'], + 'data_callback' => $args['data_callback'], ]; return true; @@ -177,7 +181,7 @@ class ExtendRestApi { return [ /* translators: %s: extension namespace */ 'description' => sprintf( __( 'Extension data registered by %s', 'woo-gutenberg-products-block' ), $namespace ), - 'type' => 'object', + 'type' => [ 'object', 'null' ], 'context' => [ 'view', 'edit' ], 'readonly' => true, 'properties' => $schema,