Make WordPress Core


Ignore:
Timestamp:
02/09/2026 04:59:52 PM (2 months ago)
Author:
jorgefilipecosta
Message:

Abilities API: Allow nested namespace ability names (2-4 segments).

Expand ability name validation from exactly 2 segments (namespace/ability) to 2-4 segments, enabling names like my-plugin/resource/find and my-plugin/resource/sub/find.
This allows plugins to organize abilities into logical resource groups. The validation regex changes from /^[a-z0-9-]+\/[a-z0-9-]+$/ to /^[a-z0-9-]+(?:\/[a-z0-9-]+){1,3}$/, which accepts the first segment plus 1-3 additional slash-delimited segments.
Updates the validation regex, error messages, docblocks, and adds corresponding unit and REST API tests.

Props jorgefilipecosta, justlevine, jorbin.
Fixes #64596.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/abilities-api/class-wp-abilities-registry.php

    r61086 r61602  
    4444     * @see wp_register_ability()
    4545     *
    46      * @param string               $name The name of the ability. The name must be a string containing a namespace
    47      *                                   prefix, i.e. `my-plugin/my-ability`. It can only contain lowercase
    48      *                                   alphanumeric characters, dashes and the forward slash.
     46     * @param string               $name The name of the ability. Must be the fully-namespaced
     47 *                                       string identifier, e.g. `my-plugin/my-ability` or `my-plugin/resource/my-ability`.
    4948     * @param array<string, mixed> $args {
    5049     *     An associative array of arguments for the ability.
     
    7978     */
    8079    public function register( string $name, array $args ): ?WP_Ability {
    81         if ( ! preg_match( '/^[a-z0-9-]+\/[a-z0-9-]+$/', $name ) ) {
     80        if ( ! preg_match( '/^[a-z0-9-]+(?:\/[a-z0-9-]+){1,3}$/', $name ) ) {
    8281            _doing_it_wrong(
    8382                __METHOD__,
    8483                __(
    85                     'Ability name must be a string containing a namespace prefix, i.e. "my-plugin/my-ability". It can only contain lowercase alphanumeric characters, dashes and the forward slash.'
     84                    'Ability name must contain 2 to 4 segments separated by forward slashes, e.g. "my-plugin/my-ability" or "my-plugin/resource/my-ability". It can only contain lowercase alphanumeric characters, dashes, and forward slashes.'
    8685                ),
    8786                '6.9.0'
Note: See TracChangeset for help on using the changeset viewer.