WP_REST_Font_Families_Controller::validate_font_family_settings( string $value, WP_REST_Request $request ): true|WP_Error
- Since
- 6.5.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php:89
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 family 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_Families_Controller::validate_font_family_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
- Light
- Scaling
- Scales with input
- Instructions
- 21–70
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
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 103.
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_Families_Controller::validate_font_family_settings()
Further down the call graph this can also reach option, 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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Font_Families_Controller::validate_font_family_settings() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 21 | ->get_method(), ->get_endpoint_args_for_item_schema(), rest_validate_value_from_schema(), is_wp_error() |
!is_wp_error() && $settings === null | 40 | ->get_method(), ->get_endpoint_args_for_item_schema(), rest_validate_value_from_schema(), is_wp_error(), json_decode(), __(), sprintf() |
!is_wp_error() && $settings !== null | 47–51 | ->get_method(), ->get_endpoint_args_for_item_schema(), rest_validate_value_from_schema(), is_wp_error(), json_decode(), ->get_item_schema(), rest_validate_value_from_schema(), is_wp_error() |
$settings !== null | 48–51 | ->get_method(), ->get_endpoint_args_for_item_schema(), 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 && isset($request) && isset($settings) | 50 | ->get_method(), ->get_endpoint_args_for_item_schema(), rest_validate_value_from_schema(), is_wp_error(), json_decode(), ->get_item_schema(), __(), sprintf() |
!is_wp_error() && $settings !== null && !$schema && !$required && isset($settings[$key]) && !$settings | 67–70 | ->get_method(), ->get_endpoint_args_for_item_schema(), rest_validate_value_from_schema(), is_wp_error(), json_decode(), ->get_item_schema(), rest_validate_value_from_schema(), is_wp_error(), __(), sprintf() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 103 instructions, 21–70 executed per call, 9 branches. The work does not change between versions.
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 · 6
- 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_REST_Font_Families_Controller::get_endpoint_args_for_item_schema()Get the arguments used when creating or updating a font family.
- WP_Error::__construct()Initializes the error.
- WP_REST_Font_Families_Controller::get_item_schema()Retrieves the post's schema, conforming to JSON Schema.
Source code
public function validate_font_family_settings( $value, $request ) { // Enforce JSON Schema validity for field before applying custom validation logic. $args = $this->get_endpoint_args_for_item_schema( $request->get_method() ); $validity = rest_validate_value_from_schema( $value, $args['font_family_settings'], 'font_family_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', /* 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; }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-families-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.