wppaste
WordPress

WP_Ability::validate_output( mixed $output ): true|WP_Error

Since
6.9.0
Source
wp-includes/abilities-api/class-wp-ability.php:712
Validates output data against the output schema.

Compatibility

WordPress
since 6.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 3 of the 5 tracked releases, added in 6.9.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$outputmixed
The output data to validate.

Return value

true|WP_Error
Returns true if valid, or a WP_Error object if validation fails.

Performance profile

How much work a call to WP_Ability::validate_output() 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
24–52

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

Plugin surface
1 hook

Third-party callbacks on 'wp_ability_validate_output' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

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 · 9 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Ability::validate_output() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($output_schema) && $validity !== false && !is_wp_error()24->get_output_schema(), apply_filters(), is_wp_error()
empty($output_schema) && $validity === false27->get_output_schema(), apply_filters(), __()
empty($output_schema) && $validity !== false && is_wp_error()27->get_output_schema(), apply_filters(), is_wp_error(), ->has_errors()
!empty($output_schema) && !is_wp_error() && $validity !== false33->get_output_schema(), rest_validate_value_from_schema(), is_wp_error(), apply_filters(), is_wp_error()
!empty($output_schema) && !is_wp_error() && $validity === false36->get_output_schema(), rest_validate_value_from_schema(), is_wp_error(), apply_filters(), __()
!empty($output_schema) && $validity !== false36->get_output_schema(), rest_validate_value_from_schema(), is_wp_error(), apply_filters(), is_wp_error(), ->has_errors()
!empty($output_schema) && $validity !== false49->get_output_schema(), rest_validate_value_from_schema(), is_wp_error(), __(), ->get_error_message(), sprintf(), apply_filters(), is_wp_error()
!empty($output_schema) && is_wp_error() && $validity === false52->get_output_schema(), rest_validate_value_from_schema(), is_wp_error(), __(), ->get_error_message(), sprintf(), apply_filters(), __()
!empty($output_schema) && is_wp_error() && $validity !== false52->get_output_schema(), rest_validate_value_from_schema(), is_wp_error(), __(), ->get_error_message(), sprintf(), apply_filters(), is_wp_error(), ->has_errors()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 64 instructions, 24–52 executed per call, 5 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.

Hooks and filters fired · 1

One hook fires while WP_Ability::validate_output() runs, in this order:

  1. apply_filters( wp_ability_validate_output )filterline 748 (+36 into the body)

    Filters the output validation result for an ability.

Uses · 6

Used by · 1

Source code

	protected function validate_output( $output ) {		$output_schema = $this->get_output_schema();		if ( empty( $output_schema ) ) {			$is_valid = true;		} else {			$valid_output = rest_validate_value_from_schema( $output, $output_schema, 'output' );			if ( is_wp_error( $valid_output ) ) {				$is_valid = new WP_Error(					'ability_invalid_output',					sprintf(						/* translators: %1$s ability name, %2$s error message. */						__( 'Ability "%1$s" has invalid output. Reason: %2$s' ),						$this->name,						$valid_output->get_error_message()					)				);			} else {				$is_valid = true;			}		} 		/**		 * Filters the output validation result for an ability.		 *		 * Allows developers to add custom validation logic on top of the default		 * JSON Schema validation. If default validation already failed, the filter		 * receives the WP_Error object and can add additional error information or		 * override it. If default validation passed, the filter can add additional		 * validation checks and return a WP_Error if those checks fail.		 *		 * @since 7.1.0		 *		 * @param true|WP_Error $is_valid     The validation result from default validation.		 * @param mixed         $output       The output data being validated.		 * @param string        $ability_name The name of the ability.		 */		$validity = apply_filters( 'wp_ability_validate_output', $is_valid, $output, $this->name );		if ( false === $validity ) {			return new WP_Error( 'ability_invalid_output', __( 'Invalid output.' ) );		}		if ( is_wp_error( $validity ) && $validity->has_errors() ) {			return $validity;		}		return true;	}

Changelog

Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.

  1. 6.9.7
  2. 7.0.4
  3. 7.1.0

Signature, return type and hooks compared across 3 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/abilities-api/class-wp-ability.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.