Skip to content

Abilities API: Allow abilities to belong to multiple categories. - #14839

Open
robelsust wants to merge 1 commit into
WordPress:trunkfrom
robelsust:feature/66367-abilities-multiple-categories
Open

robelsust wants to merge 1 commit into
WordPress:trunkfrom
robelsust:feature/66367-abilities-multiple-categories

Conversation

@robelsust

Copy link
Copy Markdown

Description

Fixes #66367.
Trac ticket: https://core.trac.wordpress.org/ticket/66367

Summary

This PR explores and implements allowing an ability in the Abilities API to belong to multiple categories while maintaining backwards compatibility with code expecting a single primary category.

Changes

  1. WP_Ability (src/wp-includes/abilities-api/class-wp-ability.php):

    • Added protected $categories = array() property.
    • Added public function get_categories(): array to retrieve all assigned categories.
    • Added public function has_category( string $category ): bool to test category membership.
    • Retained public function get_category(): string returning $this->category (the primary category) for backwards compatibility.
    • In prepare_properties(), accept both categories and category, normalize to a unique list of category slugs, and set $categories[0] as $category.
    • Added validation ensuring categories contains only non-empty strings.
  2. WP_Abilities_Registry (src/wp-includes/abilities-api/class-wp-abilities-registry.php):

    • Updated register() to accept categories and category.
    • Validates every assigned category against wp_has_ability_category().
    • Defaults to array( 'uncategorized' ) if omitted.
  3. Abilities API Functions (src/wp-includes/abilities-api.php):

    • Documented $categories in wp_register_ability() docblock.
    • Updated wp_get_abilities():
      • Filtering by $category now checks $ability->has_category( $category ) so an ability matches whether that category is primary or secondary.
      • Added $categories argument allowing callers to filter for abilities matching any of the specified categories.
  4. REST API List Controller (src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.php):

    • Added categories array to prepare_item_for_response().
    • Added categories array definition to get_item_schema().
    • Added categories array query parameter to get_collection_params() and get_items().
  5. Unit Tests:

    • tests/phpunit/tests/abilities-api/wpAbility.php: Added tests for single category, categories array, merging both, has_category(), and invalid/empty input exceptions.
    • tests/phpunit/tests/abilities-api/wpRegisterAbility.php: Added tests for registering multiple categories, unregistered category failure, and invalid type failure.
    • tests/phpunit/tests/abilities-api/wpGetAbilities.php: Added tests for secondary category matching, multiple categories filter matching, and empty categories filter.
    • tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php: Updated schema and embed assertions for categories, added tests for filtering by categories array, comma-separated string, and multi-category abilities.

Verification

  • vendor/bin/phpcs: Clean (0 errors, 0 warnings across all 8 modified files).
  • vendor/bin/phpunit: All 216 tests passed across test suites (wpAbility.php, wpGetAbilities.php, wpRegisterAbility.php, wpRestAbilitiesV1ListController.php).

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props therssoftware.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

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

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

@robelsust
robelsust force-pushed the feature/66367-abilities-multiple-categories branch from 4ce8bae to 84cb41b Compare October 25, 2026 10:30
Extends WP_Ability and the Abilities API registry to support multiple categories per ability while preserving backwards compatibility:
- Added $categories property, get_categories(), and has_category() to WP_Ability.
- In WP_Ability::prepare_properties() and WP_Abilities_Registry::register(), accept categories and category, normalize to a unique list of valid slugs, validate every category exists via wp_has_ability_category(), and retain the first category as the primary category for get_category().
- Updated wp_get_abilities() to check $ability->has_category( $category ) and added $categories filter argument.
- Updated WP_REST_Abilities_V1_List_Controller to expose categories in item response and schema, and support categories filter argument in collection parameters.
- Added comprehensive unit tests covering getters, has_category, validation, registry checks, filtering, and REST endpoint responses.

Props therssoftware, raftaar1191.
Fixes #66367.
@robelsust
robelsust force-pushed the feature/66367-abilities-multiple-categories branch from 84cb41b to 0068c0b Compare October 25, 2026 11:50
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.

1 participant