Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/wp-includes/abilities-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
* @since 6.9.0
* @since 7.1.0 Added the `public` meta argument.
* @since 7.2.0 Added the `required_capabilities` argument.
* @since 7.3.0 Added the `open_world` annotation.
*
* @see WP_Abilities_Registry::register()
* @see WP_Ability::execute()
Expand Down Expand Up @@ -341,6 +342,9 @@
* If false, the ability performs only additive updates.
* @type bool|null $idempotent Optional. If true, calling the ability repeatedly with the same arguments
* will have no additional effect on its environment.
* @type bool|null $open_world Optional. If true, the ability may interact with systems outside of the site,
* such as remote HTTP services or email recipients. If false, the ability
* only interacts with the site itself.
* }
* @type bool $public Optional. Whether the ability is meant to be available to
* clients such as the REST API, MCP, or AI agents. Seeds
Expand Down
5 changes: 5 additions & 0 deletions src/wp-includes/abilities-api/class-wp-abilities-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class WP_Abilities_Registry {
* @since 6.9.0
* @since 7.1.0 Added the `public` meta argument.
* @since 7.2.0 Added the `required_capabilities` argument.
* @since 7.3.0 Added the `open_world` annotation.
*
* @see wp_register_ability()
*
Expand Down Expand Up @@ -76,6 +77,9 @@ final class WP_Abilities_Registry {
* If false, the ability performs only additive updates.
* @type bool|null $idempotent Optional. If true, calling the ability repeatedly with the same arguments
* will have no additional effect on its environment.
* @type bool|null $open_world Optional. If true, the ability may interact with systems outside of the site,
* such as remote HTTP services or email recipients. If false, the ability
* only interacts with the site itself.
* }
* @type bool $public Optional. Whether the ability is meant to be available
* to clients such as the REST API, MCP, or AI agents.
Expand Down Expand Up @@ -118,6 +122,7 @@ public function register( string $name, array $args ): ?WP_Ability {
* @since 6.9.0
* @since 7.1.0 Added the `public` meta argument.
* @since 7.2.0 Added the `required_capabilities` argument.
* @since 7.3.0 Added the `open_world` annotation.
*
* @param array<string, mixed> $args {
* An associative array of arguments for the ability.
Expand Down
17 changes: 17 additions & 0 deletions src/wp-includes/abilities-api/class-wp-ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ class WP_Ability {
* on its environment.
*/
'idempotent' => null,
/*
* If true, at least one supported execution path may interact with systems outside of the site,
* such as remote HTTP services or email recipients.
* If false, all supported execution paths only interact with the site itself.
*/
'open_world' => null,
);

/**
Expand Down Expand Up @@ -149,6 +155,7 @@ class WP_Ability {
* @since 6.9.0
* @since 7.1.0 Added the `public` meta argument.
* @since 7.2.0 Added the `required_capabilities` argument.
* @since 7.3.0 Added the `open_world` annotation.
*
* @see wp_register_ability()
*
Expand Down Expand Up @@ -181,6 +188,9 @@ class WP_Ability {
* If false, the ability performs only additive updates.
* @type bool|null $idempotent Optional. If true, calling the ability repeatedly with the same arguments
* will have no additional effect on its environment.
* @type bool|null $open_world Optional. If true, the ability may interact with systems outside of the site,
* such as remote HTTP services or email recipients. If false, the ability
* only interacts with the site itself.
* }
* @type bool $public Optional. Whether the ability is meant to be available
* to clients such as the REST API, MCP, or AI agents.
Expand Down Expand Up @@ -225,6 +235,7 @@ public function __construct( string $name, array $args ) {
* @since 6.9.0
* @since 7.1.0 Added the `public` meta argument.
* @since 7.2.0 Added the `required_capabilities` argument.
* @since 7.3.0 Added the `open_world` annotation.
*
* @see WP_Abilities_Registry::register()
*
Expand Down Expand Up @@ -256,6 +267,9 @@ public function __construct( string $name, array $args ) {
* If false, the ability performs only additive updates.
* @type bool|null $idempotent Optional. If true, calling the ability repeatedly with the same arguments
* will have no additional effect on its environment.
* @type bool|null $open_world Optional. If true, the ability may interact with systems outside of the site,
* such as remote HTTP services or email recipients. If false, the ability
* only interacts with the site itself.
* }
* @type bool $public Optional. Whether the ability is meant to be available
* to clients such as the REST API, MCP, or AI agents.
Expand Down Expand Up @@ -293,6 +307,9 @@ public function __construct( string $name, array $args ) {
* If false, the ability performs only additive updates.
* @type bool|null $idempotent If true, calling the ability repeatedly with the same arguments
* will have no additional effect on its environment.
* @type bool|null $open_world If true, the ability may interact with systems outside of the site,
* such as remote HTTP services or email recipients. If false, the ability
* only interacts with the site itself.
* }
* @type bool $public Whether the ability is meant to be available to clients
* such as the REST API, MCP, or AI agents. Defaults to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public function prepare_item_for_response( $ability, $request ) {
* @since 6.9.0
* @since 7.1.0 Added the `meta.public` property.
* @since 7.2.0 Added the `required_capabilities` property.
* @since 7.3.0 Added the `meta.annotations.open_world` property.
*
* @return array<string, mixed> Item schema data.
*/
Expand Down Expand Up @@ -325,6 +326,10 @@ public function get_item_schema(): array {
'description' => __( 'Whether repeated calls with the same arguments have no additional effect.' ),
'type' => array( 'boolean', 'null' ),
),
'open_world' => array(
'description' => __( 'Whether the ability may interact with systems outside of the site, such as remote HTTP services or email recipients.' ),
'type' => array( 'boolean', 'null' ),
),
),
'additionalProperties' => true,
),
Expand All @@ -348,6 +353,7 @@ public function get_item_schema(): array {
* @since 6.9.0
* @since 7.1.0 Added the `namespace` and `meta` parameters and the `rest_abilities_collection_params` filter.
* @since 7.2.0 Added the `has_required_capabilities` parameter.
* @since 7.3.0 Added the `open_world` annotation filter.
*
* @return array<string, mixed> Collection parameters.
*/
Expand Down Expand Up @@ -400,6 +406,10 @@ public function get_collection_params(): array {
'description' => __( 'Whether repeated calls with the same arguments have no additional effect.' ),
'type' => array( 'boolean', 'null' ),
),
'open_world' => array(
'description' => __( 'Whether the ability may interact with systems outside of the site, such as remote HTTP services or email recipients.' ),
'type' => array( 'boolean', 'null' ),
),
),
'additionalProperties' => true,
),
Expand Down
37 changes: 37 additions & 0 deletions tests/phpunit/tests/abilities-api/wpAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public function test_get_merged_annotations_from_meta() {
self::$test_ability_properties['meta']['annotations'],
array(
'idempotent' => null,
'open_world' => null,
)
),
$ability->get_meta_item( 'annotations' )
Expand All @@ -132,6 +133,7 @@ public function test_get_default_annotations_from_meta() {
'readonly' => null,
'destructive' => null,
'idempotent' => null,
'open_world' => null,
),
$ability->get_meta_item( 'annotations' )
);
Expand All @@ -147,6 +149,7 @@ public function test_get_overridden_annotations_from_meta() {
'readonly' => true,
'destructive' => false,
'idempotent' => false,
'open_world' => true,
);
$args = array_merge(
self::$test_ability_properties,
Expand All @@ -162,6 +165,40 @@ public function test_get_overridden_annotations_from_meta() {
$this->assertSame( $annotations, $ability->get_meta_item( 'annotations' ) );
}

/**
* Tests that the `open_world` annotation preserves explicit values and stays unspecified otherwise.
*
* @ticket 66799
*
* @dataProvider data_open_world_annotation
*
* @param array $annotations Annotations passed at registration.
* @param bool|null $expected Expected `open_world` value.
*/
public function test_open_world_annotation( array $annotations, $expected ) {
$args = self::$test_ability_properties;
$args['meta'] = array( 'annotations' => $annotations );
$ability = new WP_Ability( self::$test_ability_name, $args );
$resolved_annotations = $ability->get_meta_item( 'annotations' );

$this->assertArrayHasKey( 'open_world', $resolved_annotations, 'The annotation should always be present.' );
$this->assertSame( $expected, $resolved_annotations['open_world'] );
}

/**
* Data provider.
*
* @return array<string, array{0: array, 1: bool|null}>
*/
public function data_open_world_annotation(): array {
return array(
'explicit true' => array( array( 'open_world' => true ), true ),
'explicit false' => array( array( 'open_world' => false ), false ),
'explicit null' => array( array( 'open_world' => null ), null ),
'omitted' => array( array( 'readonly' => true ), null ),
);
}

/**
* Tests that invalid `annotations` value throws an exception.
*
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/abilities-api/wpRegisterAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public function test_register_valid_ability(): void {
self::$test_ability_args['meta']['annotations'],
array(
'idempotent' => null,
'open_world' => null,
)
);
$expected_meta = array_merge(
Expand Down
57 changes: 57 additions & 0 deletions tests/phpunit/tests/rest-api/wpRestAbilitiesV1ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,63 @@ public function test_filter_by_non_matching_annotation(): void {
$this->assertEmpty( $response->get_data() );
}

/**
* Test filtering abilities by the `open_world` annotation.
*
* The value is passed as a string, the way it arrives over the query string, so
* this confirms the meta schema coerces it to a boolean before matching. An
* ability that does not declare the annotation matches neither value.
*
* @ticket 66799
*/
public function test_filter_by_open_world_annotation(): void {
$abilities = array(
'test/open-world' => true,
'test/bounded-world' => false,
'test/undeclared' => null,
);

foreach ( $abilities as $name => $open_world ) {
$meta = array( 'show_in_rest' => true );
if ( null !== $open_world ) {
$meta['annotations'] = array( 'open_world' => $open_world );
}

$this->register_test_ability(
$name,
array(
'label' => $name,
'description' => 'Used to test filtering by the open world annotation.',
'category' => 'general',
'execute_callback' => '__return_true',
'permission_callback' => '__return_true',
'meta' => $meta,
)
);
}

$request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities' );
$request->set_param( 'meta', array( 'annotations' => array( 'open_world' => 'true' ) ) );
$request->set_param( 'per_page', 100 );
$response = $this->server->dispatch( $request );

$this->assertSame( 200, $response->get_status() );

$names = wp_list_pluck( $response->get_data(), 'name' );

$this->assertContains( 'test/open-world', $names, 'An open world ability should match "true".' );
$this->assertNotContains( 'test/bounded-world', $names, 'A bounded ability should not match "true".' );
$this->assertNotContains( 'test/undeclared', $names, 'An ability without the annotation should not match "true".' );

$request->set_param( 'meta', array( 'annotations' => array( 'open_world' => 'false' ) ) );
$response = $this->server->dispatch( $request );
$names = wp_list_pluck( $response->get_data(), 'name' );

$this->assertContains( 'test/bounded-world', $names, 'A bounded ability should match "false".' );
$this->assertNotContains( 'test/open-world', $names, 'An open world ability should not match "false".' );
$this->assertNotContains( 'test/undeclared', $names, 'An ability without the annotation should not match "false".' );
}

/**
* Test filtering abilities by several meta conditions at once.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -12787,6 +12787,13 @@ mockedApiResponse.Schema = {
"boolean",
"null"
]
},
"open_world": {
"description": "Whether the ability may interact with systems outside of the site, such as remote HTTP services or email recipients.",
"type": [
"boolean",
"null"
]
}
},
"additionalProperties": true
Expand Down
Loading