wppaste
WordPress

WP_REST_Font_Faces_Controller::validate_create_font_face_settings( string $value, WP_REST_Request $request ): true|WP_Error

Since
6.5.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php:163
Validates settings when creating a font face.

Compatibility

WordPress
since 6.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$valuestring
Encoded JSON string of font face settings.
$requestWP_REST_Request
Request object.

Return value

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

Performance profile

How much work a call to WP_REST_Font_Faces_Controller::validate_create_font_face_settings() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
18–81

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 158.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • serializeserialisationjson_decode()called directly
  • hookthird-party callbacksdo_action()one call below WP_REST_Font_Faces_Controller::validate_create_font_face_settings()
  • optionoption read or writeget_option()one call below WP_REST_Font_Faces_Controller::validate_create_font_face_settings()

Further down the call graph this can also reach cache, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Font_Faces_Controller::validate_create_font_face_settings() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
is_wp_error()18->get_create_params(), rest_validate_value_from_schema(), is_wp_error()
!is_wp_error() && $settings === null33->get_create_params(), rest_validate_value_from_schema(), is_wp_error(), json_decode(), __()
$settings !== null42->get_create_params(), rest_validate_value_from_schema(), is_wp_error(), json_decode(), ->get_item_schema(), rest_validate_value_from_schema(), is_wp_error(), ->add_data()
!is_wp_error() && $settings !== null58–61->get_create_params(), rest_validate_value_from_schema(), is_wp_error(), json_decode(), ->get_item_schema(), rest_validate_value_from_schema(), is_wp_error(), ->get_file_params(), array_keys()
!is_wp_error() && $settings !== null && !$schema && !$required && isset($settings[$key]) && !$settings62->get_create_params(), rest_validate_value_from_schema(), is_wp_error(), json_decode(), ->get_item_schema(), rest_validate_value_from_schema(), is_wp_error(), __(), sprintf()
!is_wp_error() && $settings !== null && !$srcs && empty($src)72–73->get_create_params(), rest_validate_value_from_schema(), is_wp_error(), json_decode(), ->get_item_schema(), rest_validate_value_from_schema(), is_wp_error(), ->get_file_params(), ltrim(), __(), sprintf()
!is_wp_error() && $settings !== null && !array_keys() && !$file75–77->get_create_params(), rest_validate_value_from_schema(), is_wp_error(), json_decode(), ->get_item_schema(), rest_validate_value_from_schema(), is_wp_error(), ->get_file_params(), array_keys(), __(), sprintf()
!is_wp_error() && $settings !== null && !$srcs && !empty($src) && !isset($files[$src])80–81->get_create_params(), rest_validate_value_from_schema(), is_wp_error(), json_decode(), ->get_item_schema(), rest_validate_value_from_schema(), is_wp_error(), ->get_file_params(), ltrim(), wp_http_validate_url(), __(), sprintf()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev15818–8116
8.515818–8116
8.415818–81163 fewer instructions than PHP 8.3
8.316118–8116
8.216118–8116
8.116118–81162 fewer instructions than PHP 7.4
7.416318–8216

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 7

Source code

	public function validate_create_font_face_settings( $value, $request ) {		// Enforce JSON Schema validity for field before applying custom validation logic.		$args     = $this->get_create_params();		$validity = rest_validate_value_from_schema( $value, $args['font_face_settings'], 'font_face_settings' ); 		if ( is_wp_error( $validity ) ) {			return $validity;		} 		$settings = json_decode( $value, true ); 		// Check settings string is valid JSON.		if ( null === $settings ) {			return new WP_Error(				'rest_invalid_param',				__( 'font_face_settings parameter must be a valid JSON string.' ),				array( 'status' => 400 )			);		} 		// Check that the font face settings match the theme.json schema.		$schema             = $this->get_item_schema()['properties']['font_face_settings'];		$has_valid_settings = rest_validate_value_from_schema( $settings, $schema, 'font_face_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.		$required = $schema['required'];		foreach ( $required as $key ) {			if ( isset( $settings[ $key ] ) && ! $settings[ $key ] ) {				return new WP_Error(					'rest_invalid_param',					/* translators: %s: Name of the missing font face settings parameter, e.g. "font_face_settings[src]". */					sprintf( __( '%s cannot be empty.' ), "font_face_setting[ $key ]" ),					array( 'status' => 400 )				);			}		} 		$srcs  = is_array( $settings['src'] ) ? $settings['src'] : array( $settings['src'] );		$files = $request->get_file_params(); 		foreach ( $srcs as $src ) {			// Check that each src is a non-empty string.			$src = ltrim( $src );			if ( empty( $src ) ) {				return new WP_Error(					'rest_invalid_param',					/* translators: %s: Font face source parameter name: "font_face_settings[src]". */					sprintf( __( '%s values must be non-empty strings.' ), 'font_face_settings[src]' ),					array( 'status' => 400 )				);			} 			// Check that srcs are valid URLs or file references.			if ( false === wp_http_validate_url( $src ) && ! isset( $files[ $src ] ) ) {				return new WP_Error(					'rest_invalid_param',					/* translators: 1: Font face source parameter name: "font_face_settings[src]", 2: The invalid src value. */					sprintf( __( '%1$s value "%2$s" must be a valid URL or file reference.' ), 'font_face_settings[src]', $src ),					array( 'status' => 400 )				);			}		} 		// Check that each file in the request references a src in the settings.		foreach ( array_keys( $files ) as $file ) {			if ( ! in_array( $file, $srcs, true ) ) {				return new WP_Error(					'rest_invalid_param',					/* translators: 1: File key (e.g. "file-0") in the request data, 2: Font face source parameter name: "font_face_settings[src]". */					sprintf( __( 'File %1$s must be used in %2$s.' ), $file, 'font_face_settings[src]' ),					array( 'status' => 400 )				);			}		}

Changelog

Introduced in 6.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.