Skip to content

Conversation

@artpi
Copy link

@artpi artpi commented Dec 12, 2025

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

Abilities API allows for extending WP_Ability by providing ability_class during the ability registration. This is meant to unlock complex abilities holding some sort of state or logic that requires multiple helper methods.
In all of those scenarios you would ovewrite execute or do_execute method.

However, because the check for execute_callback is in constructor, then in order to register an ability with ability_class overwrite, you have to BOTH:

  • provide do_execute
  • Provide a dummy execute_callback

This PR isolates the execute_callback check only to the base WP_Ability class.

Testing instructions

Save this as test.php

<?php

class My_Test_Ability extends WP_Ability {

    protected function do_execute( $input = null ) {
                // Here is some complex logic.
                echo "SUCCESS!!!";
        return array( 'success' => true );
    }
}

add_action( 'wp_abilities_api_init', function() {
    wp_register_ability(
        'test/custom-class-ability',
        array(
            'label'         => 'Test Custom Ability',
            'description'   => 'Test ability with custom class.',
            'category'      => 'site',
            'ability_class' => My_Test_Ability::class,
            'permission_callback' => function() {
                    return true;
            },
        )
    );
} );

wp_get_ability('test/custom-class-ability')->execute();

and then run with CLI:

npm run env:cli -- eval-file test.php

This commit updates the WP_Ability class to validate the execute_callback only when the ability_class parameter is not overridden during instantiation.
@github-actions
Copy link

Hi @artpi! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions
Copy link

github-actions bot commented Dec 12, 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 artpi, swissspidy, jorgefilipecosta, mindctrl.

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

@mindctrl
Copy link

How is ongoing development handled for this API? There's still active dev in the plugin repo here: https://github.com/WordPress/abilities-api

Is dev handled in the plugin and then ported to core?

@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.

@jorgefilipecosta
Copy link
Member

How is ongoing development handled for this API? There's still active dev in the plugin repo here: https://github.com/WordPress/abilities-api

Is dev handled in the plugin and then ported to core?

Right now given that abilities are in core, the source of true is core, and we should do our changes in core. Probably https://github.com/WordPress/abilities-api will be archieved soon.

if ( empty( $args['execute_callback'] ) || ! is_callable( $args['execute_callback'] ) ) {
// If we are not overriding `ability_class` parameter during instantiation, then we need to validate the execute_callback.
if ( get_class( $this ) === self::class && ( empty( $args['execute_callback'] ) || ! is_callable( $args['execute_callback'] ) ) ) {
throw new InvalidArgumentException(
Copy link
Member

@jorgefilipecosta jorgefilipecosta Dec 12, 2025

Choose a reason for hiding this comment

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

I think we should also apply the same logic for permission_callback, as we still have this condition:

		if ( empty( $args['permission_callback'] ) || ! is_callable( $args['permission_callback'] ) ) {
			throw new InvalidArgumentException(
				__( 'The ability properties must provide a valid `permission_callback` function.' )
			);
		}

Copy link
Author

Choose a reason for hiding this comment

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

Done!
5f091d0

Copy link
Member

@jorgefilipecosta jorgefilipecosta left a comment

Choose a reason for hiding this comment

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

Left a comment related to the permissions callback, but other than that it looks good.

@artpi
Copy link
Author

artpi commented Dec 15, 2025

Left a comment related to the permissions callback, but other than that it looks good.

Done
5f091d0

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants