WP_REST_Font_Families_Controller::validate_font_family_settings()publicWP 6.5.0

Validates settings when creating or updating a font family.

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

Хуков нет.

Возвращает

true|WP_Error. True if the settings are valid, otherwise a WP_Error object.

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

$WP_REST_Font_Families_Controller = new WP_REST_Font_Families_Controller();
$WP_REST_Font_Families_Controller->validate_font_family_settings( $value, $request );
$value(строка) (обязательный)
Encoded JSON string of font family settings.
$request(WP_REST_Request) (обязательный)
Request object.

Список изменений

С версии 6.5.0 Введена.

Код WP_REST_Font_Families_Controller::validate_font_family_settings() WP 6.7.1

public function validate_font_family_settings( $value, $request ) {
	$settings = json_decode( $value, true );

	// Check settings string is valid JSON.
	if ( null === $settings ) {
		return new WP_Error(
			'rest_invalid_param',
			/* translators: %s: Parameter name: "font_family_settings". */
			sprintf( __( '%s parameter must be a valid JSON string.' ), 'font_family_settings' ),
			array( 'status' => 400 )
		);
	}

	$schema   = $this->get_item_schema()['properties']['font_family_settings'];
	$required = $schema['required'];

	if ( isset( $request['id'] ) ) {
		// Allow sending individual properties if we are updating an existing font family.
		unset( $schema['required'] );

		// But don't allow updating the slug, since it is used as a unique identifier.
		if ( isset( $settings['slug'] ) ) {
			return new WP_Error(
				'rest_invalid_param',
				/* translators: %s: Name of parameter being updated: font_family_settings[slug]". */
				sprintf( __( '%s cannot be updated.' ), 'font_family_settings[slug]' ),
				array( 'status' => 400 )
			);
		}
	}

	// Check that the font face settings match the theme.json schema.
	$has_valid_settings = rest_validate_value_from_schema( $settings, $schema, 'font_family_settings' );

	if ( is_wp_error( $has_valid_settings ) ) {
		$has_valid_settings->add_data( array( 'status' => 400 ) );
		return $has_valid_settings;
	}

	// Check that none of the required settings are empty values.
	foreach ( $required as $key ) {
		if ( isset( $settings[ $key ] ) && ! $settings[ $key ] ) {
			return new WP_Error(
				'rest_invalid_param',
				/* translators: %s: Name of the empty font family setting parameter, e.g. "font_family_settings[slug]". */
				sprintf( __( '%s cannot be empty.' ), "font_family_settings[ $key ]" ),
				array( 'status' => 400 )
			);
		}
	}

	return true;
}