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
This commit is contained in:
Seghir Nadir 2020-11-30 15:38:10 +01:00 committed by GitHub
parent f4e4bdd1e7
commit 8c2c0f73ad
1 changed files with 18 additions and 14 deletions

View File

@ -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,