Make WordPress Core

Opened 4 months ago

Closed 3 months ago

Last modified 41 hours ago

#65569 closed enhancement (fixed)

Abilities API category requirement is a design question worth settling

Reported by: gziolo Owned by: gziolo
Priority: normal Milestone: 7.2
Component: Abilities API Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Registering an ability requires a category, the category must be registered first on its own hook, and there is no built-in catch-all to fall back on. Building real abilities surfaced how much friction this creates during first-time integration. The requirement is defensible, but the current shape makes a simple ability harder to register than it needs to be, and it is worth deciding deliberately whether that is the experience we want.

Context

The guardrails already exist. Every failure mode below emits a _doing_it_wrong() notice today: registering on the wrong hook, naming a category that is not registered, and omitting the category entirely. The catch is that _doing_it_wrong() surfaces through PHP's error channel, so it is visible in a WP_DEBUG development setup and silent in a default configuration. A developer iterating without WP_DEBUG on sees only a NULL return and has nothing to go on, which is what made the category rules feel like undocumented failures rather than a contract.

One clarification worth recording: the core registered site and user categories are available for general use.

The question

The category requirement is real, but its ergonomics pull in three directions and we should pick one on purpose rather than by default.

Grow the default categories. As core introduces new abilities, ship more built-in categories so authors have a fitting home without registering their own. This keeps categorization meaningful, but it does not help the plugin that still wants its own namespace, and it grows the surface core has to maintain.

Make the category optional. Fall back to a single catch-all when none is given, so a simple ability registers with no ceremony. This removes the sharpest edge, at the cost of weaker categorization and a bucket that tends to collect everything. The fallback name matters here, since general reads as a respectable default while uncategorized signals a junk drawer, and that choice quietly shapes how readily authors reach for it.

Keep it required, improve discoverability. Leave the contract as is and close the gap with documentation, stating plainly that these functions return NULL on failure, that WP_DEBUG surfaces the reason, and that categories register on wp_abilities_api_categories_init before abilities on wp_abilities_api_init. This preserves the design intent and costs the least, but keeps a required field that some will still read as friction.

These are not mutually exclusive. The documentation improvement is worth doing regardless of which way the requirement itself lands.

Working pattern

For reference, the sequence that registers cleanly today:

add_action( 'wp_abilities_api_categories_init', function() {
        wp_register_ability_category( 'myplugin', [
                'label'       => 'My Plugin',
                'description' => 'Abilities for my plugin.',
        ] );
} );

add_action( 'wp_abilities_api_init', function() {
        wp_register_ability( 'myplugin/get-posts', [
                'label'               => 'Get Posts',
                'description'         => 'Retrieve posts with optional filtering.',
                'category'            => 'myplugin',
                'input_schema'        => [ /* ... */ ],
                'output_schema'       => [ /* ... */ ],
                'permission_callback' => fn() => current_user_can( 'edit_posts' ),
                'execute_callback'    => function( $input ) { /* ... */ },
                'meta'                => [ 'mcp' => [ 'public' => true ] ],
        ] );
} );

References

Attachments (1)

65569-optional-category-uncategorized.diff​ (7.8 KB ) - added by sachinrajcp123 4 months ago.

Download all attachments as: .zip

Change History (16)

This ticket was mentioned in ​Slack in #core-ai by gziolo. ​View the logs.


4 months ago

#2 @khokansardar
4 months ago

Taking the 2nd-opinion. I'd reframe: this is two fixable bugs and one real taste call, not three equal options.

1. The silent failure is the actual pain, not the requirement.
A bad category returns NULL, and the only signal is _doing_it_wrong() — invisible without WP_DEBUG. Hard consequence (ability silently doesn't register) behind a dev-only signal. This is a DX bug; fix it independent of the category policy by surfacing which rule failed even without WP_DEBUG.

2. The two-hook ordering is the sharpest edge.
Requiring categories on wp_abilities_api_categories_init before abilities on wp_abilities_api_init is an implicit ordering contract that fails invisibly. Let wp_register_ability() accept a category and resolve it lazily (or auto-create a minimal one) instead of a hard pre-registration gate.

3. Required vs optional — the only real design call.
I lean optional-with-a-catch-all + docs, but name the fallback uncategorized, not general. A respectable-sounding default becomes a dumping ground; uncategorized signals "fix this" and stays an escape hatch, not the default path.

Growing default categories (option 1) is worth doing anyway but doesn't solve this — it ignores the plugin that wants its own namespace.

Suggested order: fix observability first, ship the docs, then decide required-vs-optional. The first two stand on their own and make the third much smaller.

#3 @gziolo
4 months ago

  • Keywords has-patch has-unit-tests added
  • Milestone Awaiting Review → 7.2

@khokansardar, appreciate your feedback. @sachinrajcp123, thank you for providing a working patch that implements these recommendations. It looks good based on a quick scan.

#4 @gziolo
4 months ago

  • Owner set to gziolo
  • Status new → reviewing

This ticket was mentioned in ​Slack in #core-ai by gziolo. ​View the logs.


4 months ago

This ticket was mentioned in ​PR #13157 on ​WordPress/wordpress-develop by ​@gziolo.


3 months ago
#6

This makes the category argument optional when registering an ability. When it is omitted or empty, the registry assigns the built-in uncategorized category.

Previously, the registry only validated a category when one was provided, but WP_Ability still required the argument during instantiation. This left registrations without a category unable to succeed. Applying the fallback before validation keeps explicit invalid category values rejected while allowing simple or transitional registrations to omit a category.

The change:

  • registers the uncategorized category with the other core ability categories;
  • defaults omitted or empty category values to uncategorized;
  • updates the public API documentation; and
  • adds regression coverage for the fallback and invalid category types.

Props @Sachin7907 for the patch and @itzmekhokan for feedback.

## Testing

  • git diff --check passes.
  • PHPCS passes for all six changed PHP files.
  • PHPUnit was attempted with npm run test:php -- tests/phpunit/tests/abilities-api, but Docker was not running. The local fallback could not connect to the configured mysql host.

## Use of AI Tools

AI assistance: Yes
Tool(s): OpenAI Codex
Model(s): GPT-5
Used for: Reviewing the existing local diff, running validation, and preparing the commit and pull request description. The implementation was pre-existing and reviewed by the submitter.

#7 follow-up: @gziolo
3 months ago

  • Resolution → fixed
  • Status reviewing → closed

In 63343:

Make the ability category optional

When the category argument is omitted during ability registration, the registry now assigns the built-in uncategorized category. This category is registered alongside the other core ability categories.

Previously, the registry only validated a category when one was provided, but WP_Ability still required the argument, so a registration without a category could never succeed. The fallback is applied before validation, so invalid values are still rejected. This includes non-string types and empty or unregistered category slugs.

Also updates the public API documentation and adds regression coverage for the fallback and for invalid category values.

Props hokansardar, sachinrajcp123, jorgefilipecosta, gziolo.
Fixes #65569.

#8 @gziolo
3 months ago

  • Keywords needs-dev-note added; 2nd-opinion removed

#9 in reply to: ↑ 7 @khokansardar
3 months ago

Replying to gziolo:

In 63343:

Make the ability category optional

When the category argument is omitted during ability registration, the registry now assigns the built-in uncategorized category. This category is registered alongside the other core ability categories.

Previously, the registry only validated a category when one was provided, but WP_Ability still required the argument, so a registration without a category could never succeed. The fallback is applied before validation, so invalid values are still rejected. This includes non-string types and empty or unregistered category slugs.

Also updates the public API documentation and adds regression coverage for the fallback and for invalid category values.

Props hokansardar, sachinrajcp123, jorgefilipecosta, gziolo.
Fixes #65569.

in props my name is misspelled it should be khokansardar

#10 @gziolo
2 months ago

@khokansardar, I added props in another commit [63391] :)

#11 @gziolo
2 weeks ago

In 64685:

Abilities API: Improve inline documentation

Clarify required capabilities and input-dependent permission checks,
schema handling, callbacks, and execution hooks. Correct registration
examples and naming constraints, and document discovery filtering
and preservation of ability keys.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/14550.

Props gziolo, jorgefilipecosta.
See #66325, #65569.

#12 @jason_the_adams
5 days ago

I apologize for missing this. I'd prefer to have this reverted, here's why:

When we introduced Abilities we knew we'd eventually have moments wherein we need to retrieve them in subsets, given that we'll quite likely have scenarios where there are hundreds of Abilities in a given site. AI tooling is one scenario for this.

If we started with Ability Categories being optional, then it wouldn't be possible to make them required later. And if they're optional, a significant number of Abilities will be uncategorized, which brings us back to the same effective problem.

I grant that it is an inconvenience, but I believe a minor one, and one that wouldn't surface to non-technical users. Unless we have a clear case for a problem outside of inconvenience and confusion, I think it's best to keep it the way it is. For confusion, we should update the docs to help folks understand, not change it.

This ticket was mentioned in ​PR #15462 on ​WordPress/wordpress-develop by ​@gziolo.


5 days ago
#13

Restores the requirement for abilities to specify a registered category, following comment 12. An omitted category now returns null with a _doing_it_wrong() notice instead of registering the ability as uncategorized.

This selectively reverts the optional-category behavior from r63343: it removes the built-in fallback, retains invalid-category validation, and updates the existing tests. The documentation explains why categories are required, how to reuse registered categories, and the registration hook order. The failure guidance and subsequent documentation improvements are preserved.

Testing

  • PHPCS passes for all six changed PHP files.
  • git diff --check passes.
  • npm run test:php -- tests/phpunit/tests/abilities-api was attempted, but Docker stalled before PHPUnit started. Running the suite in the existing PHP container also stalled; both commands were stopped.
  • The native fallback, vendor/bin/phpunit tests/phpunit/tests/abilities-api, could not connect because the configured mysql hostname could not be resolved. PHPUnit results remain unverified.

Use of AI Tools

AI assistance: Yes
Tool(s): OpenAI Codex
Model(s): GPT-6
Used for: Inspecting the original change, implementing the targeted revert, updating tests and inline documentation, running validation checks, and preparing the commit and pull request description. The scope and documentation wording were refined with the submitter.

#14 @gziolo
2 days ago

In 65308:

Abilities API: Require a registered category for abilities

Partially reverts [63343]. Abilities that omit a category are no longer assigned to the built-in uncategorized category, which is removed. Registration fails with a _doing_it_wrong() notice instead. Updates the docs to explain why categories are required and how to reuse existing ones.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/15462.

Props gziolo, jason_the_adams, jorgefilipecosta.
Fixes #65569.

#15 @gziolo
41 hours ago

  • Keywords needs-dev-note removed

No dev note is required. The ticket covers only documentation changes.

Note: See TracTickets for help on using tickets.