Automattic\WooCommerce\Internal\Abilities\REST

RestAbilityFactory::get_schema_for_operationprivate staticWC 1.0

Get input schema based on operation type.

Method of the class: RestAbilityFactory{}

No Hooks.

Returns

Array. Input schema array.

Usage

$result = RestAbilityFactory::get_schema_for_operation( $controller, $operation ): array;
$controller(object) (required)
REST controller instance.
$operation(string) (required)
Operation type (list, get, create, update, delete).

RestAbilityFactory::get_schema_for_operation() code WC 10.7.0

private static function get_schema_for_operation( $controller, string $operation ): array {
	switch ( $operation ) {
		case 'list':
			// Use controller's collection parameters.
			if ( method_exists( $controller, 'get_collection_params' ) ) {
				return self::sanitize_args_to_schema( $controller->get_collection_params() );
			}
			break;

		case 'create':
			// Use controller's creatable schema.
			if ( method_exists( $controller, 'get_endpoint_args_for_item_schema' ) ) {
				$args = $controller->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE );
				return self::sanitize_args_to_schema( $args );
			}
			break;

		case 'update':
			// Use controller's editable schema + ID.
			if ( method_exists( $controller, 'get_endpoint_args_for_item_schema' ) ) {
				$args   = $controller->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE );
				$schema = self::sanitize_args_to_schema( $args );

				// Add ID field for update operations.
				$schema['properties']['id'] = array(
					'type'        => 'integer',
					'description' => __( 'Unique identifier for the resource', 'woocommerce' ),
				);

				// Ensure ID is required.
				if ( ! isset( $schema['required'] ) ) {
					$schema['required'] = array();
				}
				if ( ! in_array( 'id', $schema['required'], true ) ) {
					$schema['required'][] = 'id';
				}

				return $schema;
			}
			break;

		case 'get':
		case 'delete':
			// Only need ID.
			return array(
				'type'       => 'object',
				'properties' => array(
					'id' => array(
						'type'        => 'integer',
						'description' => __( 'Unique identifier for the resource', 'woocommerce' ),
					),
				),
				'required'   => array( 'id' ),
			);
	}

	// Fallback.
	return array( 'type' => 'object' );
}