Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema

Validation::build_meta_schema │ private static │ WC 1.0

Adds $data references and date comparisons to the draft-07 meta schema.

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

Хуков нет.

Возвращает

string. The meta schema as JSON.

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

$result = Validation::build_meta_schema(): string;

Код Validation::build_meta_schema() WC 11.2.2

private static function build_meta_schema(): string {
	// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
	$draft_07    = (string) file_get_contents( __DIR__ . '/json-schema-draft-07.json' );
	$meta_schema = json_decode( $draft_07, true );

	// Fall back to the pristine meta schema rather than none at all, so rules are still checked.
	if ( ! is_array( $meta_schema ) ) {
		return $draft_07;
	}

	$meta_schema['definitions']['dataRef'] = [
		'type'                 => 'object',
		'required'             => [ '$data' ],
		'properties'           => [
			'$data' => [
				'type'  => 'string',
				'anyOf' => [
					[ 'format' => 'json-pointer' ],
					[ 'format' => 'relative-json-pointer' ],
				],
			],
		],
		'additionalProperties' => false,
	];

	foreach ( self::DATA_REF_KEYWORDS as $keyword ) {
		$meta_schema['properties'][ $keyword ] = [
			'anyOf' => [
				$meta_schema['properties'][ $keyword ],
				[ '$ref' => '#/definitions/dataRef' ],
			],
		];
	}

	foreach ( self::FORMAT_LIMIT_KEYWORDS as $keyword ) {
		$meta_schema['properties'][ $keyword ]   = [
			'anyOf' => [
				[
					'type'   => 'string',
					'format' => 'date',
				],
				[ '$ref' => '#/definitions/dataRef' ],
			],
		];
		$meta_schema['dependencies'][ $keyword ] = [
			'required'   => [ 'format' ],
			'properties' => [ 'format' => [ 'const' => 'date' ] ],
		];
	}

	$widened = wp_json_encode( $meta_schema );

	return is_string( $widened ) ? $widened : $draft_07;
}