Skip to content

Conversation

@priethor
Copy link

Trac ticket: https://core.trac.wordpress.org/ticket/64311


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@priethor priethor self-assigned this Nov 26, 2025
@github-actions
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props priethor.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@priethor priethor changed the title Abilities API: add filters for input and ouput validation Abilities API: add filters for input and output validation Nov 26, 2025
@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment on lines 465 to 470
if ( empty( $input_schema ) ) {
if ( null === $input ) {
return true;
}

return new WP_Error(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that we don't fire the input hook when the $input_schema is empty but we do for the output:

  • For inputs, non-empty inputs REQUIRE a schema by design
  • For outputs, an empty schema always successfully validates even if the output is not empty

Because of this design decision, it might make sense not to allow hooking into the input in these cases?

Comment on lines +506 to +508
* @param true|WP_Error $is_valid The validation result from default validation.
* @param mixed $input The input data being validated.
* @param string $ability_name The name of the ability.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space can be removed:

Suggested change
* @param true|WP_Error $is_valid The validation result from default validation.
* @param mixed $input The input data being validated.
* @param string $ability_name The name of the ability.
* @param true|WP_Error $is_valid The validation result from default validation.
* @param mixed $input The input data being validated.
* @param string $ability_name The name of the ability.

* @param mixed $input The input data being validated.
* @param string $ability_name The name of the ability.
*/
return apply_filters( 'wp_ability_validate_input', $is_valid, $input, $this->name );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wary of plugins doing the right thing here. This would be safer:

Consider the similar logic in the Customizer:

$validity = new WP_Error();
/**
* Validates a Customize setting value.
*
* Plugins should amend the `$validity` object via its `WP_Error::add()` method.
*
* The dynamic portion of the hook name, `$this->ID`, refers to the setting ID.
*
* @since 4.6.0
*
* @param WP_Error $validity Filtered from `true` to `WP_Error` when invalid.
* @param mixed $value Value of the setting.
* @param WP_Customize_Setting $setting WP_Customize_Setting instance.
*/
$validity = apply_filters( "customize_validate_{$this->id}", $validity, $value, $this );
if ( is_wp_error( $validity ) && ! $validity->has_errors() ) {
$validity = true;
}
return $validity;

So think this could rather be, with even more hardening to handle the case where a plugin erroneously returns false:

Suggested change
return apply_filters( 'wp_ability_validate_input', $is_valid, $input, $this->name );
$validity = apply_filters( 'wp_ability_validate_input', $is_valid, $input, $this->name );
if ( ! is_wp_error( $validity ) || ! $validity->has_errors() ) {
$validity = true;
}
return $validity;

Or maybe returning false should result in a generic invalidity WP_Error being created.

Copy link
Author

@priethor priethor Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

Or maybe returning false should result in a generic invalidity WP_Error being created.

That seems more straightforward to me; do you see any downsides to that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any downsides. Ideally a plugin would provide an error message, but better to err on the side of marking a non-true response as being an error than to accidentally let something which a plugin intends to be an error to be considered valid.

sprintf(
/* translators: %1$s ability name, %2$s error message. */
__( 'Ability "%1$s" has invalid output. Reason: %2$s' ),
esc_html( $this->name ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is HTML escaping appropriate here? It could be rendered into JSON in which case the HTML entities would be wrong.

Suggested change
esc_html( $this->name ),
$this->name,

* @param mixed $output The output data being validated.
* @param string $ability_name The name of the ability.
*/
return apply_filters( 'wp_ability_validate_output', $is_valid, $output, $this->name );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto above:

Suggested change
return apply_filters( 'wp_ability_validate_output', $is_valid, $output, $this->name );
$validity = apply_filters( 'wp_ability_validate_output', $is_valid, $output, $this->name );
if ( ! is_wp_error( $validity ) || ! $validity->has_errors() ) {
$validity = true;
}
return $validity;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants