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
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
- Scaling
- Scales with input
- Instructions
- 18–81
- Plugin surface
- None
- Called by
- 0
Reads stored settings via get_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 158.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- serializeserialisation
json_decode()called directly - hookthird-party callbacks
do_action()one call below WP_REST_Font_Faces_Controller::validate_create_font_face_settings() - optionoption read or write
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 18 | ->get_create_params(), rest_validate_value_from_schema(), is_wp_error() |
!is_wp_error() && $settings === null | 33 | ->get_create_params(), rest_validate_value_from_schema(), is_wp_error(), json_decode(), __() |
$settings !== null | 42 | ->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 !== null | 58–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]) && !$settings | 62 | ->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() && !$file | 75–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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 158 | 18–81 | 16 | |
| 8.5 | 158 | 18–81 | 16 | |
| 8.4 | 158 | 18–81 | 16 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 161 | 18–81 | 16 | |
| 8.2 | 161 | 18–81 | 16 | |
| 8.1 | 161 | 18–81 | 16 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 163 | 18–82 | 16 |
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
- rest_validate_value_from_schema()Validate a value based on a schema.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- __()Retrieves the translation of $text.
- wp_http_validate_url()Validates a URL as safe for use in the HTTP API.
- WP_REST_Font_Faces_Controller::get_create_params()Get the params used when creating a new font face.
- WP_Error::__construct()Initializes the error.
- WP_REST_Font_Faces_Controller::get_item_schema()Retrieves the post's schema, conforming to JSON Schema.
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.
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.