This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
dev!: return WP_Error from WP_Ability methods
#23
Merged
gziolo
merged 5 commits into
WordPress:trunk
from
justlevine:dev/wp_ability-return-error
Aug 19, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a3637cc
dev!: return `\WP_Error` from `WP_Ability` methods
justlevine 751a3b6
docs: fix return on validate_output
justlevine b46d6b6
Merge branch 'trunk' into dev/wp_ability-return-error
gziolo f515cc9
Update class-wp-rest-abilities-run-controller.php
gziolo 3885a9e
Update wpRestAbilitiesRunController.php
gziolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -190,32 +190,17 @@ public function get_meta(): array { | |
| * @since 0.1.0 | ||
| * | ||
| * @param array<string,mixed> $input Optional. The input data to validate. | ||
| * @return bool Returns true if valid, false if validation fails. | ||
| * @return true|\WP_Error Returns true if valid or the WP_Error object if validation fails. | ||
| */ | ||
| protected function validate_input( array $input = array() ): bool { | ||
| protected function validate_input( array $input = array() ) { | ||
| $input_schema = $this->get_input_schema(); | ||
| if ( empty( $input_schema ) ) { | ||
| return true; | ||
| } | ||
|
|
||
| $valid_input = rest_validate_value_from_schema( $input, $input_schema ); | ||
| if ( is_wp_error( $valid_input ) ) { | ||
| _doing_it_wrong( | ||
| __METHOD__, | ||
| esc_html( | ||
| sprintf( | ||
| /* translators: %1$s ability name, %2$s error message. */ | ||
| __( 'Invalid input provided for ability "%1$s": %2$s.' ), | ||
| $this->name, | ||
| $valid_input->get_error_message() | ||
| ) | ||
| ), | ||
| '0.1.0' | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| return is_wp_error( $valid_input ) ? $valid_input : true; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -226,11 +211,12 @@ protected function validate_input( array $input = array() ): bool { | |
| * @since 0.1.0 | ||
| * | ||
| * @param array<string,mixed> $input Optional. The input data for permission checking. | ||
| * @return bool Whether the ability has the necessary permission. | ||
| * @return true|\WP_Error Whether the ability has the necessary permission. | ||
| */ | ||
| public function has_permission( array $input = array() ): bool { | ||
| if ( ! $this->validate_input( $input ) ) { | ||
| return false; | ||
| public function has_permission( array $input = array() ) { | ||
| $is_valid = $this->validate_input( $input ); | ||
| if ( is_wp_error( $is_valid ) ) { | ||
| return $is_valid; | ||
| } | ||
|
|
||
| if ( ! is_callable( $this->permission_callback ) ) { | ||
|
|
@@ -250,16 +236,13 @@ public function has_permission( array $input = array() ): bool { | |
| */ | ||
| protected function do_execute( array $input ) { | ||
| if ( ! is_callable( $this->execute_callback ) ) { | ||
| _doing_it_wrong( | ||
| __METHOD__, | ||
| esc_html( | ||
| /* translators: %s ability name. */ | ||
| sprintf( __( 'Ability "%s" does not have a valid execute callback.' ), $this->name ) | ||
| ), | ||
| '0.1.0' | ||
| return new \WP_Error( | ||
| 'ability_invalid_execute_callback', | ||
| /* translators: %s ability name. */ | ||
| sprintf( __( 'Ability "%s" does not have a valid execute callback.' ), $this->name ) | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| return call_user_func( $this->execute_callback, $input ); | ||
| } | ||
|
|
||
|
|
@@ -269,32 +252,17 @@ protected function do_execute( array $input ) { | |
| * @since 0.1.0 | ||
| * | ||
| * @param mixed $output The output data to validate. | ||
| * @return bool Returns true if valid, false if validation fails. | ||
| * @return true|\WP_Error Returns true if valid, or a WP_Error object if validation fails. | ||
| */ | ||
| protected function validate_output( $output ): bool { | ||
| protected function validate_output( $output ) { | ||
| $output_schema = $this->get_output_schema(); | ||
| if ( empty( $output_schema ) ) { | ||
| return true; | ||
| } | ||
|
|
||
| $valid_output = rest_validate_value_from_schema( $output, $output_schema ); | ||
| if ( is_wp_error( $valid_output ) ) { | ||
| _doing_it_wrong( | ||
| __METHOD__, | ||
| esc_html( | ||
| sprintf( | ||
| /* translators: %1$s ability name, %2$s error message. */ | ||
| __( 'Invalid output provided for ability "%1$s": %2$s.' ), | ||
| $this->name, | ||
| $valid_output->get_error_message() | ||
| ) | ||
| ), | ||
| '0.1.0' | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| return is_wp_error( $valid_output ) ? $valid_output : true; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar feedback as for input handling since we use |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -307,28 +275,33 @@ protected function validate_output( $output ): bool { | |
| * @return mixed|\WP_Error The result of the ability execution, or WP_Error on failure. | ||
| */ | ||
| public function execute( array $input = array() ) { | ||
| if ( ! $this->has_permission( $input ) ) { | ||
| _doing_it_wrong( | ||
| __METHOD__, | ||
| esc_html( | ||
| /* translators: %s ability name. */ | ||
| sprintf( __( 'Ability "%s" does not have necessary permission.' ), $this->name ) | ||
| ), | ||
| '0.1.0' | ||
| $has_permissions = $this->has_permission( $input ); | ||
|
|
||
| if ( true !== $has_permissions ) { | ||
| if ( is_wp_error( $has_permissions ) ) { | ||
| // Don't leak the error to someone without the correct perms. | ||
| _doing_it_wrong( | ||
| __METHOD__, | ||
| esc_html( $has_permissions->get_error_message() ), | ||
| '0.1.0' | ||
| ); | ||
| } | ||
|
|
||
| return new \WP_Error( | ||
| 'ability_invalid_permissions', | ||
| /* translators: %s ability name. */ | ||
| sprintf( __( 'Ability "%s" does not have necessary permission.' ), $this->name ) | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| $result = $this->do_execute( $input ); | ||
| if ( is_wp_error( $result ) ) { | ||
| return $result; | ||
| } | ||
|
|
||
| if ( ! $this->validate_output( $result ) ) { | ||
| return null; | ||
| } | ||
| $is_valid = $this->validate_output( $result ); | ||
|
|
||
| return $result; | ||
| return is_wp_error( $is_valid ) ? $is_valid : $result; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -146,19 +146,7 @@ public function run_ability( $request ) { | |
| $result = $ability->execute( $input ); | ||
|
|
||
| if ( is_wp_error( $result ) ) { | ||
| return new \WP_Error( | ||
| 'rest_ability_execution_failed', | ||
| $result->get_error_message(), | ||
| array( 'status' => 500 ) | ||
| ); | ||
| } | ||
|
|
||
| if ( is_null( $result ) ) { | ||
| return new \WP_Error( | ||
| 'rest_ability_execution_failed', | ||
| __( 'Ability execution failed. Please check permissions and input parameters.' ), | ||
| array( 'status' => 500 ) | ||
| ); | ||
| return $result; | ||
| } | ||
|
|
||
| $output_validation = $this->validate_output( $ability, $result ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both |
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like it could return the value here because rest_validate_value_from_schema returns
trueorWP_Error.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking a bit more about it after seeing the updated unit tests, we should remap these WP_Errors so the names are more tied to the abilty, for example:
rest_invalid_type->ability_input_invalid_typeCould we replace the prefix?