rest_handle_multi_type_schema() WordPress Function

The rest_handle_multi_type_schema() function is used to handle multiple types of schemas for a single field. This is useful for fields that can contain multiple types of data, such as an array of strings or an array of objects. This function takes two arguments: $schema - The schema for the field. $request - The request object. The function returns an array of schemas, one for each type that was specified in the $schema argument.

rest_handle_multi_type_schema( mixed $value, array $args, string $param = '' ) #

Handles getting the best type for a multi-type schema.


Description

This is a wrapper for rest_get_best_type_for_value() that handles backward compatibility for schemas that use invalid types.


Top ↑

Parameters

$value

(mixed)(Required)The value to check.

$args

(array)(Required)The schema array to use.

$param

(string)(Optional)The parameter name, used in error messages.

Default value: ''


Top ↑

Return

(string)


Top ↑

Source

File: wp-includes/rest-api.php

function rest_handle_multi_type_schema( $value, $args, $param = '' ) {
	$allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' );
	$invalid_types = array_diff( $args['type'], $allowed_types );

	if ( $invalid_types ) {
		_doing_it_wrong(
			__FUNCTION__,
			/* translators: 1: Parameter, 2: List of allowed types. */
			wp_sprintf( __( 'The "type" schema keyword for %1$s can only contain the built-in types: %2$l.' ), $param, $allowed_types ),
			'5.5.0'
		);
	}

	$best_type = rest_get_best_type_for_value( $value, $args['type'] );

	if ( ! $best_type ) {
		if ( ! $invalid_types ) {
			return '';
		}

		// Backward compatibility for previous behavior which allowed the value if there was an invalid type used.
		$best_type = reset( $invalid_types );
	}

	return $best_type;
}


Top ↑

Changelog

Changelog
VersionDescription
5.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More