[wp-trac] [WordPress Trac] #64407: Abilities API: when using `ability_class`, the `execute_callback` should not be mandatory
WordPress Trac
noreply at wordpress.org
Fri Dec 12 13:31:38 UTC 2025
#64407: Abilities API: when using `ability_class`, the `execute_callback` should
not be mandatory
--------------------------+-----------------------------
Reporter: artpi | Owner: artpi
Type: defect (bug) | Status: assigned
Priority: normal | Milestone: Awaiting Review
Component: AI | Version:
Severity: minor | Keywords:
Focuses: |
--------------------------+-----------------------------
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`
Like so:
{{{#!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,
'execute_callback' => function() {
// We have to provide an execute callback
that will never be called because of checks in wp_ability.
return null;
},
'permission_callback' => function() {
return true;
},
)
);
} );
wp_get_ability('test/custom-class-ability')->execute();
}}}
When using ability_class, this dummy callback should not be necessary.
- It is a horrible developer experience because you would assume its not
necessary,your ability is not registering and you wonder why -the only
trace is doing_it_wrong back where you register the ability, not the place
where your ability fails to execute
- It is assuming and mandating an implementation detail (callback) for a
class that is most likely using an overwrite
- If the callback is also required and missing, the execute will fail
anyway because of the check in do_execute.
- If, by any chance, developer removes the do_execute or execute
overwrite, the code path of the dummy execute_callback is now active which
may lead to unexpected bugs
--
Ticket URL: <https://core.trac.wordpress.org/ticket/64407>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list