Automattic\WooCommerce\Internal\Abilities\REST

RestAbilityFactory::sanitize_args_to_schemaprivate staticWC 1.0

Sanitize WordPress REST args to valid JSON Schema format.

Converts WordPress REST API argument arrays to JSON Schema by:

  • Removing PHP callbacks (sanitize_callback, validate_callback)
  • Converting 'required' from boolean-per-field to array-of-names
  • Removing WordPress-specific non-schema fields
  • Preserving valid JSON Schema properties

Метод класса: RestAbilityFactory{}

Хуков нет.

Возвращает

Массив. Valid JSON Schema object.

Использование

$result = RestAbilityFactory::sanitize_args_to_schema( $args ): array;
$args(массив) (обязательный)
WordPress REST API arguments array.

Код RestAbilityFactory::sanitize_args_to_schema() WC 10.5.0

private static function sanitize_args_to_schema( array $args ): array {
	$properties = array();
	$required   = array();

	foreach ( $args as $key => $arg ) {
		$property = array();

		// Copy valid JSON Schema fields.
		if ( isset( $arg['type'] ) ) {
			$property['type'] = $arg['type'];
		}
		if ( isset( $arg['description'] ) ) {
			$property['description'] = $arg['description'];
		}
		if ( isset( $arg['default'] ) ) {
			$property['default'] = $arg['default'];
		}
		if ( isset( $arg['enum'] ) ) {
			$property['enum'] = array_values( $arg['enum'] );
		}
		if ( isset( $arg['items'] ) ) {
			$property['items'] = $arg['items'];
		}
		if ( isset( $arg['minimum'] ) ) {
			$property['minimum'] = $arg['minimum'];
		}
		if ( isset( $arg['maximum'] ) ) {
			$property['maximum'] = $arg['maximum'];
		}
		if ( isset( $arg['format'] ) ) {
			$property['format'] = $arg['format'];
		}
		if ( isset( $arg['properties'] ) ) {
			$property['properties'] = $arg['properties'];
		}

		// Convert readonly to readOnly (JSON Schema format).
		if ( isset( $arg['readonly'] ) && $arg['readonly'] ) {
			$property['readOnly'] = true;
		}

		// Collect required fields.
		if ( isset( $arg['required'] ) && true === $arg['required'] ) {
			$required[] = $key;
		}

		$properties[ $key ] = $property;
	}

	$schema = array(
		'type'       => 'object',
		'properties' => $properties,
	);

	if ( ! empty( $required ) ) {
		$schema['required'] = array_unique( $required );
	}

	return $schema;
}