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/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php

    r61373 r61602  
    380380        );
    381381
     382        // Ability with nested namespace (3 segments).
     383        $this->register_test_ability(
     384            'test/math/add',
     385            array(
     386                'label'               => 'Nested Add',
     387                'description'         => 'Adds numbers with nested namespace',
     388                'category'            => 'math',
     389                'input_schema'        => array(
     390                    'type'                 => 'object',
     391                    'properties'           => array(
     392                        'a' => array(
     393                            'type'        => 'number',
     394                            'description' => 'First number',
     395                        ),
     396                        'b' => array(
     397                            'type'        => 'number',
     398                            'description' => 'Second number',
     399                        ),
     400                    ),
     401                    'required'             => array( 'a', 'b' ),
     402                    'additionalProperties' => false,
     403                ),
     404                'output_schema'       => array(
     405                    'type' => 'number',
     406                ),
     407                'execute_callback'    => static function ( array $input ) {
     408                    return $input['a'] + $input['b'];
     409                },
     410                'permission_callback' => static function () {
     411                    return current_user_can( 'edit_posts' );
     412                },
     413                'meta'                => array(
     414                    'show_in_rest' => true,
     415                ),
     416            )
     417        );
     418
    382419        // Read-only ability for query params testing.
    383420        $this->register_test_ability(
     
    431468        $this->assertEquals( 200, $response->get_status() );
    432469        $this->assertEquals( 8, $response->get_data() );
     470    }
     471
     472    /**
     473     * Test executing an ability with a nested namespace (3 segments) via REST.
     474     *
     475     * @ticket 64098
     476     */
     477    public function test_execute_nested_namespace_ability(): void {
     478        $request = new WP_REST_Request( 'POST', '/wp-abilities/v1/abilities/test/math/add/run' );
     479        $request->set_header( 'Content-Type', 'application/json' );
     480        $request->set_body(
     481            wp_json_encode(
     482                array(
     483                    'input' => array(
     484                        'a' => 10,
     485                        'b' => 7,
     486                    ),
     487                )
     488            )
     489        );
     490
     491        $response = $this->server->dispatch( $request );
     492
     493        $this->assertEquals( 200, $response->get_status() );
     494        $this->assertEquals( 17, $response->get_data() );
    433495    }
    434496
Note: See TracChangeset for help on using the changeset viewer.