Skip to content

Commit c9abf12

Browse files
gziolojustlevinejonathanbossenger
authored
Add execute actions to the ability object (#56)
* Add execute actions to the ability object * Update includes/abilities-api/class-wp-ability.php Co-authored-by: Dovid Levine <[email protected]> * Add WordPress actions for ability execution hooks - Add before_execute_ability action that fires before ability execution - Add after_execute_ability action that fires after successful execution - Include comprehensive unit tests for both actions with edge cases - Add extensibility documentation with usage examples and best practices - Update README with new documentation link - Fix PHPDoc formatting for action parameters 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * Improve code quality * Update README.md Co-authored-by: Dovid Levine <[email protected]> * Address feedback from review --------- Co-authored-by: Dovid Levine <[email protected]> Co-authored-by: Claude <[email protected]> Co-authored-by: gziolo <[email protected]> Co-authored-by: justlevine <[email protected]> Co-authored-by: jonathanbossenger <[email protected]>
1 parent e48d165 commit c9abf12

File tree

6 files changed

+371
-10
lines changed

6 files changed

+371
-10
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- [Registering Abilities](docs/3.registering-abilities.md)
2424
- [Using Abilities](docs/4.using-abilities.md)
2525
- [REST API Reference](docs/5.rest-api.md)
26+
- [Hooks](docs/6.hooks.md)
2627
- [Contributing Guidelines](CONTRIBUTING.md)
2728

2829
## Inspiration
@@ -36,16 +37,16 @@
3637
| Milestone | State |
3738
| ----------------------------------- | ----------- |
3839
| Placeholder repository | **created** |
39-
| Spec draft | in progress |
40-
| Prototype plugin & Composer package | in progress |
41-
| Community feedback (#core‑ai Slack) | planned |
40+
| Spec draft | **created** |
41+
| Prototype plugin & Composer package | **created** |
42+
| Community feedback (#core‑ai Slack) | in progress |
4243
| Core proposal | planned |
4344

4445
## How to Get Involved
4546

4647
- **Discuss:** `#core-ai` channel on WordPress Slack.
4748
- **File issues:** suggestions & use‑cases welcome in this repo.
48-
- **Prototype:** experiment with the upcoming Composer package once released.
49+
- **Prototype:** experiment with the [feature plugin](https://github.com/WordPress/abilities-api/releases/latest) or the [`wordpress/abilities-api`](https://packagist.org/packages/wordpress/abilities-api) Composer package.
4950

5051
## License
5152

docs/6.hooks.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 6. Hooks
2+
3+
The Abilities API provides [WordPress Action Hooks](https://developer.wordpress.org/apis/hooks/) that allow developers to monitor and respond to ability execution events.
4+
5+
## Available Actions
6+
7+
### `before_execute_ability`
8+
9+
Fires immediately before an ability gets executed, after permission checks have passed but before the execution callback is called.
10+
11+
```php
12+
/**
13+
* Fires before an ability gets executed.
14+
*
15+
* @since n.e.x.t
16+
*
17+
* @param string $ability_name The name of the ability.
18+
* @param mixed $input The input data for the ability.
19+
*/
20+
do_action( 'before_execute_ability', $ability_name, $input );
21+
```
22+
23+
**Parameters:**
24+
25+
- `$ability_name` (`string`): The namespaced name of the ability being executed (e.g., `my-plugin/get-posts`).
26+
- `$input` (`mixed`): The input data passed to the ability.
27+
28+
### `after_execute_ability`
29+
30+
Fires immediately after an ability has finished executing successfully, after output validation has passed.
31+
32+
```php
33+
/**
34+
* Fires immediately after an ability finished executing.
35+
*
36+
* @since n.e.x.t
37+
*
38+
* @param string $ability_name The name of the ability.
39+
* @param mixed $input The input data for the ability.
40+
* @param mixed $result The result of the ability execution.
41+
*/
42+
do_action( 'after_execute_ability', $ability_name, $input, $result );
43+
```
44+
45+
**Parameters:**
46+
47+
- `$ability_name` (`string`): The namespaced name of the ability that was executed.
48+
- `$input` (`mixed`): The input data that was passed to the ability.
49+
- `$result` (`mixed`): The validated result returned by the ability's execution callback.
50+
51+
## Usage Examples
52+
53+
**Basic Logging**
54+
55+
```php
56+
// Log all ability executions.
57+
add_action( 'before_execute_ability', function( $ability_name, $input ) {
58+
error_log( 'Executing ability: ' . $ability_name );
59+
if ( $input !== null ) {
60+
error_log( 'Input: ' . wp_json_encode( $input ) );
61+
}
62+
}, 10, 2 );
63+
64+
add_action( 'after_execute_ability', function( $ability_name, $input, $result ) {
65+
error_log( 'Completed ability: ' . $ability_name );
66+
error_log( 'Result: ' . wp_json_encode( $result ) );
67+
}, 10, 3 );
68+
```

includes/abilities-api.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
* @package WordPress
88
* @subpackage Abilities_API
99
* @since 0.1.0
10-
*
11-
* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals
1210
*/
1311

1412
declare( strict_types = 1 );

includes/abilities-api/class-wp-ability.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,38 @@ public function execute( $input = null ) {
416416
);
417417
}
418418

419+
/**
420+
* Fires before an ability gets executed.
421+
*
422+
* @since n.e.x.t
423+
*
424+
* @param string $ability_name The name of the ability.
425+
* @param mixed $input The input data for the ability.
426+
*/
427+
do_action( 'before_execute_ability', $this->name, $input );
428+
419429
$result = $this->do_execute( $input );
420430
if ( is_wp_error( $result ) ) {
421431
return $result;
422432
}
423433

424434
$is_valid = $this->validate_output( $result );
435+
if ( is_wp_error( $is_valid ) ) {
436+
return $is_valid;
437+
}
425438

426-
return is_wp_error( $is_valid ) ? $is_valid : $result;
439+
/**
440+
* Fires immediately after an ability finished executing.
441+
*
442+
* @since n.e.x.t
443+
*
444+
* @param string $ability_name The name of the ability.
445+
* @param mixed $input The input data for the ability.
446+
* @param mixed $result The result of the ability execution.
447+
*/
448+
do_action( 'after_execute_ability', $this->name, $input, $result );
449+
450+
return $result;
427451
}
428452

429453
/**

phpcs.xml.dist

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,10 @@
193193

194194
<!-- Plugin-specific rule configs -->
195195
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
196+
<exclude-pattern>includes/abilities-api.php</exclude-pattern>
197+
<exclude-pattern>includes/abilities-api/*</exclude-pattern>
196198
<properties>
197199
<property name="prefixes" type="array">
198-
<element value="abilities_api_" /><!-- Hook prefix -->
199-
<element value="WP_Ability" />
200-
<element value="WP_Abilities" />
201200
<element value="WP_REST_Abilities" />
202201
<element value="WP_ABILITIES_API" /><!-- Constant -->
203202
</property>

0 commit comments

Comments
 (0)