Repository navigation
Conversation
|
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: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
4ce8bae to
84cb41b
Compare
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.
84cb41b to
0068c0b
Compare
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
WP_Ability(src/wp-includes/abilities-api/class-wp-ability.php):protected $categories = array()property.public function get_categories(): arrayto retrieve all assigned categories.public function has_category( string $category ): boolto test category membership.public function get_category(): stringreturning$this->category(the primary category) for backwards compatibility.prepare_properties(), accept bothcategoriesandcategory, normalize to a unique list of category slugs, and set$categories[0]as$category.categoriescontains only non-empty strings.WP_Abilities_Registry(src/wp-includes/abilities-api/class-wp-abilities-registry.php):register()to acceptcategoriesandcategory.wp_has_ability_category().array( 'uncategorized' )if omitted.Abilities API Functions (
src/wp-includes/abilities-api.php):$categoriesinwp_register_ability()docblock.wp_get_abilities():$categorynow checks$ability->has_category( $category )so an ability matches whether that category is primary or secondary.$categoriesargument allowing callers to filter for abilities matching any of the specified categories.REST API List Controller (
src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.php):categoriesarray toprepare_item_for_response().categoriesarray definition toget_item_schema().categoriesarray query parameter toget_collection_params()andget_items().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 forcategories, added tests for filtering bycategoriesarray, 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).