Automattic\WooCommerce\StoreApi\Schemas

ExtendSchema::register_endpoint_data()publicWC 1.0

Register endpoint data under a specified namespace

Method of the class: ExtendSchema{}

No Hooks.

Return

null. Nothing (null).

Usage

$ExtendSchema = new ExtendSchema();
$ExtendSchema->register_endpoint_data( $args );
$args(array) (required)

An array of elements that make up a post to update or insert.

  • endpoint(string)
    Required. The endpoint to extend.

  • namespace(string)
    Required. Plugin namespace.

  • schema_callback(callable)
    Callback executed to add schema data.

  • data_callback(callable)
    Callback executed to add endpoint data.

  • schema_type(string)
    The type of data, object or array.

ExtendSchema::register_endpoint_data() code WC 8.7.0

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'],
	];
}