Skip to content

Conversation

justlevine
Copy link
Contributor

@justlevine justlevine commented Sep 13, 2025

What

This PR makes permission_callback a required argument.

Warning

This is a breaking change.
As a result of this change, Abilities that do not include a permission_callback will now return a WP_Error.

Why

How

Specific implementation notes on diff.

@justlevine justlevine requested a review from Copilot September 13, 2025 12:22
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR makes the permission_callback argument required for ability registration, introducing a breaking change to prevent abilities from running without proper permission checks.

  • Removes support for abilities without permission callbacks
  • Updates validation logic to require permission_callback as a mandatory parameter
  • Removes tests that verify null permission callback behavior

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/unit/rest-api/wpRestAbilitiesRunController.php Removes test for abilities without permission callbacks
tests/unit/abilities-api/wpRegisterAbility.php Minor formatting cleanup
tests/unit/abilities-api/wpAbilitiesRegistry.php Adds test to verify rejection of abilities without permission callbacks
includes/abilities-api/class-wp-ability.php Updates validation logic to require permission_callback and removes nullable type
includes/abilities-api/class-wp-abilities-registry.php Updates documentation to reflect required permission_callback
includes/abilities-api.php Reorders parameter documentation for consistency
docs/3.registering-abilities.md Updates documentation to mark permission_callback as required

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link

codecov bot commented Sep 13, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.91%. Comparing base (59d8ec6) to head (c99eaae).

Additional details and impacted files
@@             Coverage Diff              @@
##              trunk      #73      +/-   ##
============================================
+ Coverage     83.33%   86.91%   +3.58%     
+ Complexity       96       94       -2     
============================================
  Files             8        8              
  Lines           516      512       -4     
============================================
+ Hits            430      445      +15     
+ Misses           86       67      -19     
Flag Coverage Δ
unit 86.91% <100.00%> (+3.58%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment on lines 35 to -37
* description?: string,
* input_schema?: array<string,mixed>,
* output_schema?: array<string,mixed>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Required before optional.

Comment on lines -327 to -329
if ( empty( $this->get_input_schema() ) ) {
return call_user_func( $this->permission_callback );
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is handled by ::validate_input()

Copy link
Member

Choose a reason for hiding this comment

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

The point is that, if there is no schema, then we don't pass any input. What we agreed upon was that if someone wants to explicitly pass null, they need to provide the schema. This builds predictability, and that's why the condition is in place also here.

Copy link
Member

Choose a reason for hiding this comment

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

Previously, I considered adding a method for calling these callbacks, but it felt like more code than necessary. However, having a method could help document this expected behavior better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What we agreed upon was that if someone wants to explicitly pass null, they need to provide the schema.

Can you clarify? If I understood you correctly that behavior is what this change enforces.

Previously diff, if there was no input schema, then any $input would get stripped and the execution callback would be be handled as if nothing was passed. As a result, if the schema allows an "explicit null, then the ability would still execute even if the supplied $input doesn't meet the correct shape.

With this diff, both no $input must be passed and the schema must support a null value for the ability to execute successfully.

(If you're referring to our internal ability to differentiate between null and unset, that was just one one of the concerns about going to mixed that I raised on #58 . Until core's minimum PHP supports named arguments, nothing can compete with either the dx or future-compat of a partially-sealed array, nullable or otherwise. 🤷‍♂️)

Copy link
Member

@gziolo gziolo Sep 15, 2025

Choose a reason for hiding this comment

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

I'm not sure I follow, so let me explain it in a different way:

  • if there is no input schema declared, it means there is no input to pass, so the callback should be executed with no arguments
  • If there is an input schema provided, it means it needs to be validated. This also allows passing an explicit null as the first argument, but also any other type of data: string, boolean, array, etc.

Copy link
Contributor Author

@justlevine justlevine Sep 15, 2025

Choose a reason for hiding this comment

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

if there is no input schema declared, it means there is no input to pass, so the callback should be executed with no arguments

If there is no input schema declared, but a user/mcp/traditional-tool nevertheless passes an $input what should the expected behavior be? E.g.

$post_getter => wp_get_ability( 'my-custom/get-posts ' );

$posts               = $post_getter->execute(); 
$posts_explicit_null = $post_getter->execute( null );
$just_products       = $post_getter->execute( [ 'post_type' => 'product' ] );
  • $posts will return an error if an input_schema is defined, but will proceed if an input schema is not defined. (✔️ both ways)

  • $posts_explicit_null

    • old: will pass if no input_schema is defined, will fail if an input_schema is defined but does not allow for an explicit null.
    • this pr: will fail both if the input_schema is defined but doesn't allow for an explicit null and if no input_schema is defined. (because you're not passing "nothing", you're explicitly passing a null)
  • $just_products : Here's the IRL dx footgun:

    • old: if no input_schema is defined (IRL: filtering isn't supporting for by this specific ability), it will **return the same results as both $posts and $post_explicit_null. Only if there is an input_schema defined will you get a WP_Error saying that e.g. the expected argument is called type not post_type .
      (or worse, that you should have called $get_all_posts->execute( int $amount_of_posts ) and that ability will never be able to support post type filtering without breaking schema changes. This was my argument against Fix $input type hint in WP_Ability #58 and why mixed is arguably the worst thing we could do to this or any public API 🤷)
    • this: will fail both if the input_schema is defined (as it should), but also if there is no input_schema, correctly alerting the user that this is likely not the WP_Ability they're looking for.

I believe the behavior here is still ideal, even if the change to mixed negates a small part of the DX win.

Either way once clarified, I'll add backfill the necessary unit test to guarantee the behavior here doesn't change unintentionally

Copy link
Member

Choose a reason for hiding this comment

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

If there is no input schema, and there is any input provided, then validate_input() kicks in. Reference:

protected function validate_input( $input = null ) {
$input_schema = $this->get_input_schema();
if ( empty( $input_schema ) ) {
if ( null === $input ) {
return true;
}
return new \WP_Error(
'ability_missing_input_schema',
sprintf(
/* translators: %s ability name. */
__( 'Ability "%s" does not define an input schema required to validate the provided input.' ),
$this->name
)
);
}

So there are no concerns that someone passes an input that can't be validated due to a missing input schema. That's why we can safely skip the argument, making a better experience for devs as they don't have to always include ( $input = null ) for the callbacks that don't consume anything.

It's all a minor thing about ensuring we don't pass the default null when it is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay now I'm sure we're not following each other, because that's literally the initial comment I left here explaining why I removed this conditional here, except you're using it to (seemingly) justify the opposite of what I am 😭

I'll add the unit tests for the above edge cases, if that doesn't clear things up (for either of us), i'll revert here and open a separate bug report that follows up #61 .

Copy link
Member

Choose a reason for hiding this comment

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

Works for me.

Copy link
Member

Choose a reason for hiding this comment

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

The key here is that it needs to be impossible to pass unvalidated input through to permission_callback (and similarly execute_callback).

  • @justlevine You're right that technically we can drop the empty( $this->get_input_schema() ) condition because if this is the case and the input isn't null, the validate_input method would return a WP_Error and this code wouldn't be reached.
  • That being said, I do think this is good to keep as a safe guard for good measure. We have the same in do_execute. Having this extra check in there ensures directly before the relevant call that we don't pass any input (i.e. enforce null) when there's no input schema.

Is it necessary? No. Is it a good measure for defensive coding and clarifying the importance of this to someone new working on this code in the future? Yes, I think so.

IMO we should just keep it. It doesn't hurt.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is here because reordering the prepare_properties() by required/not-required (et al) seemed like the least intrusive way to do this, but it's more of an Integration test pattern and not pure PHPUnit.

If wp-core needs vanilla unit tests, we can relocate and refactor these tests to use new WP_Ability()

@justlevine justlevine marked this pull request as ready for review September 13, 2025 12:44
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.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: justlevine <[email protected]>
Co-authored-by: gziolo <[email protected]>
Co-authored-by: johnbillion <[email protected]>

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

Copy link
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.

I had only one minor feedback where I explained the reasoning why to keep extra logic. The rest of the changes look exactly as expected 💯

Comment on lines -327 to -329
if ( empty( $this->get_input_schema() ) ) {
return call_user_func( $this->permission_callback );
}
Copy link
Member

Choose a reason for hiding this comment

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

The point is that, if there is no schema, then we don't pass any input. What we agreed upon was that if someone wants to explicitly pass null, they need to provide the schema. This builds predictability, and that's why the condition is in place also here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make the permission_callback argument required
3 participants