Skip to content

Abilities API: add filters for input and output validation#10557

Open
priethor wants to merge 6 commits intoWordPress:trunkfrom
priethor:add/ability-validation-filters
Open

Abilities API: add filters for input and output validation#10557
priethor wants to merge 6 commits intoWordPress:trunkfrom
priethor:add/ability-validation-filters

Conversation

@priethor
Copy link
Copy Markdown

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
Copy Markdown

github-actions bot commented Nov 26, 2025

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, gziolo, westonruter.

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
Copy Markdown

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
Copy Markdown
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?

Copy link
Copy Markdown
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

This is looking very solid — the filter design follows the Customizer's customize_validate_{$id} precedent well, and the false safety net plus the normalization logic are well thought through.

One thing to address: the add_filter() calls in the new tests use anonymous closures and are never cleaned up with remove_filter(). Since PHPUnit runs tests in the same process, filters added in one test can leak into subsequent tests and silently affect results. Each test should store the closure reference and remove it after assertions, e.g.:

$filter = static function ( $is_valid ) {
    return true;
};
add_filter( 'wp_ability_validate_input', $filter );

// ... test logic ...

remove_filter( 'wp_ability_validate_input', $filter );
// ... assertions ...```

@westonruter
Copy link
Copy Markdown
Member

One thing to address: the add_filter() calls in the new tests use anonymous closures and are never cleaned up with remove_filter(). Since PHPUnit runs tests in the same process, filters added in one test can leak into subsequent tests and silently affect results. Each test should store the closure reference and remove it after assertions, e.g.:

@gziolo Actually, this isn't a concern because the hooks get reset after each test is run, regardless of whether the tests run in a separate processor not.

In set_up:

if ( ! self::$hooks_saved ) {
$this->_backup_hooks();
}

In tear_down:

@gziolo
Copy link
Copy Markdown
Member

gziolo commented Mar 26, 2026

@westonruter, thank you so much for pointing me to that logic. That’s perfect. I’m glad it existed as this is the very well designed default behavior 👍

I intend to land this PR as soon as 7.1 cycle starts.

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.

3 participants