EST API: Preserve custom status for ability validation errors

he REST run controller for the Abilities API already preserves a custom
HTTP status returned by `wp_ability_normalize_input` filter errors. Apply
the same behavior to validation errors from `wp_ability_validate_input`:
a `WP_Error` is only defaulted to a 400 status when it does not already
include one.

The shared defaulting logic is extracted into a new private
`ensure_error_status()` helper and reused for both normalization and
validation errors.

Follow-up to [62398].
See #64311.


Built from https://develop.svn.wordpress.org/trunk@62399


git-svn-id: http://core.svn.wordpress.org/trunk@61680 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
gziolo
2026-05-21 10:19:41 +00:00
parent 766edbe807
commit 76233303e7
2 changed files with 21 additions and 9 deletions
@@ -161,18 +161,12 @@ class WP_REST_Abilities_V1_Run_Controller extends WP_REST_Controller {
$input = $this->get_input_from_request( $request );
$input = $ability->normalize_input( $input );
if ( is_wp_error( $input ) ) {
$error_data = $input->get_error_data();
if ( ! is_array( $error_data ) || ! isset( $error_data['status'] ) ) {
$input->add_data( array( 'status' => 400 ) );
}
return $input;
return $this->ensure_error_status( $input, 400 );
}
$is_valid = $ability->validate_input( $input );
if ( is_wp_error( $is_valid ) ) {
$is_valid->add_data( array( 'status' => 400 ) );
return $is_valid;
return $this->ensure_error_status( $is_valid, 400 );
}
$result = $ability->check_permissions( $input );
@@ -191,6 +185,24 @@ class WP_REST_Abilities_V1_Run_Controller extends WP_REST_Controller {
return true;
}
/**
* Ensures a WP_Error object carries an HTTP status, adding a default when none is set.
*
* @since 7.1.0
*
* @param WP_Error $error Error object to update.
* @param int $status HTTP status code to add if not already present.
* @return WP_Error The error object, with a default status when needed.
*/
private function ensure_error_status( WP_Error $error, int $status ): WP_Error {
$error_data = $error->get_error_data();
if ( ! is_array( $error_data ) || ! isset( $error_data['status'] ) ) {
$error->add_data( array( 'status' => $status ) );
}
return $error;
}
/**
* Extracts input parameters from the request.
*
+1 -1
View File
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '7.1-alpha-62398';
$wp_version = '7.1-alpha-62399';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.