wppaste
WordPress

WP_REST_Request::has_valid_params(): true|WP_Error

Since
4.4.0
Source
wp-includes/rest-api/class-wp-rest-request.php:885
Checks whether this request is valid according to its attributes.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
8–50

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

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

  • hookthird-party callbacksdo_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.

WhenInstructionsCalls 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 === false47–50->parse_json_params(), is_wp_error(), ->get_attributes(), call_user_func(), is_wp_error(), __()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1468–5018
8.51468–5018
8.41468–50189 fewer instructions than PHP 8.3
8.31558–5018
8.21558–5018
8.11558–50181 fewer instruction than PHP 7.4
7.41568–5018

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

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.

  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 6.9.7 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.