Ability Rest API Integration#828
Conversation
There was a problem hiding this comment.
Pull request overview
Adds optional automatic WordPress REST API route registration for Abilities that opt in via a new RestConfig DTO, including optional input/output adapters and schema propagation.
Changes:
- Introduces
RestConfig, REST adapter contracts, andAbilityRestRegistrarto auto-register REST routes for configured abilities. - Hooks REST registration into
rest_api_initand caches provider abilities to avoid rebuilding for multiple init phases. - Adds unit tests for REST registrar/config plus a WP_Error unit-test stub and documentation updates.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
woocommerce/Abilities/Rest/AbilityRestRegistrar.php |
New registrar that maps opt-in abilities to register_rest_route() endpoints with adapters + schema plumbing. |
woocommerce/Abilities/DataObjects/RestConfig.php |
New DTO describing REST exposure (namespace/version/path/method + adapters). |
woocommerce/Abilities/DataObjects/Ability.php |
Adds nullable restConfig to the Ability DTO (constructor + property). |
woocommerce/Abilities/Contracts/RestInputAdapterContract.php |
New contract for request-to-ability input adaptation. |
woocommerce/Abilities/Contracts/RestOutputAdapterContract.php |
New contract for ability-result-to-response adaptation. |
woocommerce/Abilities/AbstractAbilitiesProvider.php |
Caches resolved abilities so they’re built once and reused across init hooks. |
woocommerce/Abilities/AbilitiesHandler.php |
Adds rest_api_init hook to invoke the new registrar (under the existing abilities API guard). |
tests/unit/Abilities/Rest/AbilityRestRegistrarTest.php |
Unit coverage for registrar behaviors (namespace/method inference, adapters, args/schema, callback flow). |
tests/unit/Abilities/DataObjects/RestConfigTest.php |
Unit coverage for RestConfig defaults and full construction. |
tests/unit/Abilities/AbilitiesHandlerTest.php |
Updates hook expectations to include rest_api_init. |
tests/bootstrap.php |
Loads a WP_Error stub when WP core isn’t available under WP_Mock. |
tests/Stubs/WP_Error.php |
Minimal WP_Error stub to support unit tests. |
.agents/guides/ABILITIES-IMPLEMENTATION-GUIDE.md |
Documents optional REST endpoint configuration and adapter patterns. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $config = new RestConfig( | ||
| 'teams', | ||
| 'woocommerce-memberships-for-teams', | ||
| 'v1', | ||
| 'GET', | ||
| null, | ||
| null | ||
| ); |
There was a problem hiding this comment.
This test uses a route path without a leading slash ('teams'), while RestConfig’s docs/tests and the guide examples show paths like '/teams'. Pick one convention and use it consistently; if the intent is WordPress’ typical register_rest_route() usage, update this to include the leading / to match RestConfigTest and avoid confusion.
* Version 6.1.5 * Update abilities guide with examples for other object references (#827) * Protect against Notes_Helper fatal errors during plugin updates (#826) * Ability Rest API Integration (#828) * Ability Rest API Integration * Use wp_get_ability * Test updates * Update namespaces * Tests * More tests * Tests * Finish tests * Update implementation guide * Remove unused param * Correctly set schema * Update changelog * Change permission callback * Release v6.2.0 (#829) * Version 6.2.0 * Adjust changelog --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Ashley Gibson <agibson@godaddy.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Ashley Gibson <99189195+agibson-godaddy@users.noreply.github.com> Co-authored-by: Ashley Gibson <agibson@godaddy.com>
Summary
Adds optional automatic REST API endpoint generation for abilities that opt in via a new
RestConfigdata object. When an ability carries a non-nullRestConfig, the framework automatically registers a WordPress REST API route that executes the ability throughWP_Ability::execute(), letting WP core handle input/output validation, permission checks, and lifecycle hooks.Story: MWC-19480
Release:
Details
New classes
RestConfig— DTO on theAbilitydata object that declares how an ability should be exposed as a REST endpoint. Configures path, namespace prefix, version, HTTP method, and optional input/output adapter class-strings.AbilityRestRegistrar— Core class that bridges abilities to REST routes. Iterates the provider's abilities and callsregister_rest_route()for each one with aRestConfig. Handles:woocommerce-memberships-for-teams/...→wc-memberships-for-teams/v1), or uses an explicit override.readonlyannotation → GET,destructive→ DELETE, otherwise POST. Explicit$methodonRestConfigtakes precedence.RestInputAdapterContract/RestOutputAdapterContractimplementations that transform request data before execution and result data before response.JsonSerializableobjects and arrays of them are serialized automatically when no output adapter is provided.inputSchemaand exposesoutputSchemaas the route's response schema.RestInputAdapterContract/RestOutputAdapterContract— Contracts for adapter classes.Modified classes
Ability— Added nullable$restConfigproperty (last constructor param, backwards compatible).AbilitiesHandler— Hooksrest_api_initinside thecanUseAbilitiesApi()guard to register REST routes.AbstractAbilitiesProvider— CachesgetAbilities()result so it's only built once (both abilities init and REST init consume it).QA
Before merge