-
Notifications
You must be signed in to change notification settings - Fork 49
Adding ability_class information and example #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -137,6 +137,7 @@ The `$args` array accepts the following keys: | |||||
| - `show_in_rest` (`boolean`, **Optional**): Whether to expose this ability via the REST API. Default: `false`. | ||||||
| - When `true`, the ability will be listed in REST API responses and can be executed via REST endpoints. | ||||||
| - When `false`, the ability will be hidden from REST API listings and cannot be executed via REST endpoints, but remains available for internal PHP usage. | ||||||
| - `ability_class` (`string`, **Optional**): The fully-qualified class name of a custom ability class that extends `WP_Ability`. This allows you to customize the behavior of an ability by extending the base `WP_Ability` class and overriding its methods. The custom class must extend `WP_Ability`. Default: `WP_Ability`. | ||||||
|
|
||||||
| ### Ability ID Convention | ||||||
|
|
||||||
|
|
@@ -381,6 +382,176 @@ function my_plugin_register_send_email_ability() { | |||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| #### Registering an Ability with a Custom Ability Class | ||||||
|
|
||||||
| The `ability_class` parameter allows you to use a custom class that extends `WP_Ability`. This is useful when you need to add custom methods to your ability or override the default behavior of the base `WP_Ability` class. | ||||||
|
|
||||||
| **When to use a custom ability class:** | ||||||
| - You need to add custom helper methods specific to your ability | ||||||
| - You want to override how the ability executes (e.g., add logging, caching, or custom validation) | ||||||
| - You need to store additional state or configuration beyond what the standard metadata provides | ||||||
| - You want to encapsulate complex business logic within the ability class itself | ||||||
|
|
||||||
| **Example: Creating a custom ability class with additional methods** | ||||||
|
|
||||||
| ```php | ||||||
| /** | ||||||
| * Custom ability class that adds a validation helper method. | ||||||
| * | ||||||
| * This example shows how to extend WP_Ability to add custom behavior | ||||||
| * while still leveraging all the standard ability functionality. | ||||||
| */ | ||||||
| class My_Plugin_Post_Validator_Ability extends WP_Ability { | ||||||
|
|
||||||
| /** | ||||||
| * Custom method to check if a post ID is valid and accessible. | ||||||
| * | ||||||
| * This helper method can be called from the execute callback or permission callback | ||||||
| * to perform validation logic that's specific to this ability. | ||||||
| * | ||||||
| * @param int $post_id The post ID to validate. | ||||||
| * @return true|WP_Error True if valid, WP_Error if invalid. | ||||||
| */ | ||||||
| public function validate_post_id( $post_id ) { | ||||||
jonathanbossenger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| // Check if the post exists | ||||||
| $post = get_post( $post_id ); | ||||||
| if ( ! $post ) { | ||||||
| return new WP_Error( | ||||||
|
||||||
| return new WP_Error( | |
| return new \WP_Error( |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| return new WP_Error( | |
| return new \WP_Error( |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| * @return mixed|WP_Error The result of the ability execution. | |
| * @return mixed|\WP_Error The result of the ability execution. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, this could be a lovely place to demonstrate {@inheritDoc} and leave off the types annotations for better future compat
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They can add any sort of method, not sure if we want to encourage public methods with an explicit callout.
| - You can add public methods to provide additional functionality specific to your ability | |
| - You can add custom methods to provide additional functionality specific to your ability |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.