WP_REST_Request::has_valid_params(): true|WP_Error
- Since
- 4.4.0
- Source
wp-includes/rest-api/class-wp-rest-request.php:885
Compatibility
- WordPress
- since 4.4.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.
Return value
true|WP_Error- True if there are no parameters to validate or if all pass validation, WP_Error if required parameters are missing.
Performance profile
How much work a call to WP_REST_Request::has_valid_params() 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
- 8–50
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 146.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()one call below WP_REST_Request::has_valid_params()
Further down the call graph this can also reach query, option, cache, serialize 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_Request::has_valid_params() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 8 | ->parse_json_params(), is_wp_error() |
!is_wp_error() && empty($required) && !isset($attributes) | 27–30 | ->parse_json_params(), is_wp_error(), ->get_attributes() |
!is_wp_error() && !empty($required) | 35–37 | ->parse_json_params(), is_wp_error(), ->get_attributes(), __(), sprintf(), status() |
!is_wp_error() && empty($required) && isset($attributes) | 37–42 | ->parse_json_params(), is_wp_error(), ->get_attributes(), call_user_func(), is_wp_error() |
!is_wp_error() && empty($required) | 44–47 | ->parse_json_params(), is_wp_error(), ->get_attributes(), __(), array_keys(), sprintf(), status() |
!is_wp_error() && empty($required) && isset($attributes) && $valid_check === false | 47–50 | ->parse_json_params(), is_wp_error(), ->get_attributes(), call_user_func(), is_wp_error(), __() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 146 | 8–50 | 18 | |
| 8.5 | 146 | 8–50 | 18 | |
| 8.4 | 146 | 8–50 | 18 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 155 | 8–50 | 18 | |
| 8.2 | 155 | 8–50 | 18 | |
| 8.1 | 155 | 8–50 | 18 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 156 | 8–50 | 18 |
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 · 8
- is_wp_error()Checks whether the given variable is a WordPress Error.
- __()Retrieves the translation of $text.
- rest_convert_error_to_response()Converts an error to a response object.
- WP_REST_Request::parse_json_params()Parses the JSON parameters.
- WP_REST_Request::get_attributes()Retrieves the attributes for the request.
- WP_REST_Request::get_param()Retrieves a parameter from the request.
- WP_Error::__construct()Initializes the error.
- rest_convert_error_to_response($valid_check)::get_data()
Source code
public function has_valid_params() { // If JSON data was passed, check for errors. $json_error = $this->parse_json_params(); if ( is_wp_error( $json_error ) ) { return $json_error; } $attributes = $this->get_attributes(); $required = array(); $args = empty( $attributes['args'] ) ? array() : $attributes['args']; foreach ( $args as $key => $arg ) { $param = $this->get_param( $key ); if ( isset( $arg['required'] ) && true === $arg['required'] && null === $param ) { $required[] = $key; } } if ( ! empty( $required ) ) { return new WP_Error( 'rest_missing_callback_param', /* translators: %s: List of required parameters. */ sprintf( __( 'Missing parameter(s): %s' ), implode( ', ', $required ) ), array( 'status' => 400, 'params' => $required, ) ); } /* * Check the validation callbacks for each registered arg. * * This is done after required checking as required checking is cheaper. */ $invalid_params = array(); $invalid_details = array(); foreach ( $args as $key => $arg ) { $param = $this->get_param( $key ); if ( null !== $param && ! empty( $arg['validate_callback'] ) ) { /** @var bool|\WP_Error $valid_check */ $valid_check = call_user_func( $arg['validate_callback'], $param, $this, $key ); if ( false === $valid_check ) { $invalid_params[ $key ] = __( 'Invalid parameter.' ); } if ( is_wp_error( $valid_check ) ) { $invalid_params[ $key ] = implode( ' ', $valid_check->get_error_messages() ); $invalid_details[ $key ] = rest_convert_error_to_response( $valid_check )->get_data(); } } } if ( $invalid_params ) { return new WP_Error( 'rest_invalid_param', /* translators: %s: List of invalid parameters. */ sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ), array( 'status' => 400, 'params' => $invalid_params, 'details' => $invalid_details, ) ); } if ( isset( $attributes['validate_callback'] ) ) { $valid_check = call_user_func( $attributes['validate_callback'], $this ); if ( is_wp_error( $valid_check ) ) { return $valid_check; } if ( false === $valid_check ) { // A WP_Error instance is preferred, but false is supported for parity with the per-arg validate_callback.Changelog
Introduced in 4.4.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.0.4 tag, from
src/wp-includes/rest-api/class-wp-rest-request.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.