WP_REST_Abilities_V1_Run_Controller::coerce_input_to_schema( mixed $input, WP_Ability $ability ): mixed
- Since
- 7.1.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php:268
Description
GET and DELETE deliver every scalar as a string ("10", "true") and a list as a single comma-separated string, so without coercion an ability receives raw strings where its schema declares integers, booleans, or arrays.
Coercion never changes what validation accepts. Input is coerced only when WP_Ability::validate_input() already accepts it, and any error surfaced while sanitizing falls back to the raw input, so validate_input() stays the single authority on what is rejected.
Compatibility
- WordPress
- since 7.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 1 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$inputmixed- Raw input extracted from the request.
$abilityWP_Ability- The ability being executed.
Return value
mixed- Coerced input, or the raw input when it cannot be safely coerced.
Performance profile
How much work a call to WP_REST_Abilities_V1_Run_Controller::coerce_input_to_schema() 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
- Trivial
- Scaling
- Constant
- Instructions
- 5–27
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 31.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()one call below WP_REST_Abilities_V1_Run_Controller::coerce_input_to_schema()
Further down the call graph this can also reach option, cache, serialize, 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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Abilities_V1_Run_Controller::coerce_input_to_schema() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$input === null | 5 | none |
$input !== null && empty($schema) | 10 | ->get_input_schema() |
$input !== null && !empty($schema) && is_wp_error() | 17 | ->get_input_schema(), ->validate_input(), is_wp_error() |
$input !== null && !empty($schema) && !is_wp_error() | 27 | ->get_input_schema(), ->validate_input(), is_wp_error(), rest_sanitize_value_from_schema(), ->input_contains_error() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 31 instructions, 5–27 executed per call, 4 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 · 3
- is_wp_error()Checks whether the given variable is a WordPress Error.
- rest_sanitize_value_from_schema()Sanitize a value based on a schema.
- WP_REST_Abilities_V1_Run_Controller::input_contains_error()Determines whether a sanitized value is, or contains, a WP_Error.
Used by · 1
- WP_REST_Abilities_V1_Run_Controller::sanitize_input_for_ability()Sanitizes the run input by coercing it to the ability's input schema.
Source code
private function coerce_input_to_schema( $input, WP_Ability $ability ) { if ( null === $input ) { return $input; } $schema = $ability->get_input_schema(); if ( empty( $schema ) ) { return $input; } /* * Only coerce input that already validates. Sanitizing invalid input can silently * change which values are accepted -- `additionalProperties: false` strips unknown * keys, and a non-numeric string casts to 0 -- so leaving invalid input untouched * lets validate_input() reject it exactly as it does without coercion. * * validate_input() is asked rather than rest_validate_value_from_schema() so that the * `wp_ability_validate_input` filter decides what counts as valid here as well. A filter * that overrides a schema failure accepts the input, so the input is coerced; a filter * that rejects otherwise valid input leaves it untouched for validate_input() to report. */ if ( is_wp_error( $ability->validate_input( $input ) ) ) { return $input; } $sanitized = rest_sanitize_value_from_schema( $input, $schema, 'input' ); /* * Sanitizing can still surface an error the lenient validation above did not, such as * items that are unique as strings but collide once cast to integers (`uniqueItems`). * The error may be returned at the top level or nested inside the returned array, so * scan recursively and fall back to the raw input on any error. */ if ( $this->input_contains_error( $sanitized ) ) { return $input; } return $sanitized; }Changelog
Introduced in 7.1.0.
Signature, return type and hooks compared across 1 parsed release.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-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.