From 02d86d95e9a1240c0683126630352f770b68f890 Mon Sep 17 00:00:00 2001 From: Oussama Elgoumri <65380769+euvoor@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:19:43 +0000 Subject: [PATCH] Add experimental plugin and theme inventory queries --- CHANGELOG.md | 2 + README.md | 2 + src/Manifest.php | 22 ++ src/Site/Abstract_Inventory_Query.php | 177 ++++++++++++++++ src/Site/Plugins_Query.php | 44 ++++ src/Site/Themes_Query.php | 51 +++++ tests/Abilities/InventoryQueryTest.php | 275 +++++++++++++++++++++++++ 7 files changed, 573 insertions(+) create mode 100644 src/Site/Abstract_Inventory_Query.php create mode 100644 src/Site/Plugins_Query.php create mode 100644 src/Site/Themes_Query.php create mode 100644 tests/Abilities/InventoryQueryTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 55a034d..36ba6c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ the initial snapshot of the prototype. ### Added +- Add experimental read-only plugin and theme inventory queries for the current site. + ### Added - Repository scaffold and tooling: Composer contract for `wordpress/abilities` diff --git a/README.md b/README.md index f0a0a80..2e727d7 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,8 @@ Full rules, the promotion path, and the rename protocol are in | `wp/term-create` | experimental | content | Port of the term create ability from [WordPress/ai PR #1103](https://github.com/WordPress/ai/pull/1103) (`core_since: null` — flips to tier `core` when it lands in WordPress core). Creates a term in a taxonomy exposed to abilities via `show_in_abilities` (the same taxonomies `wp/terms-query` reads — taxonomy opt-in governs the writes too): takes a `taxonomy` and a `name`, plus optional `slug`, `description`, and `parent` (hierarchical taxonomies only). Returns the created term in the `wp/terms-query` shape; `fields` picks the subset. `edit_terms` is the floor in a hierarchical taxonomy, `assign_terms` in a flat one, so a contributor can create tags without touching categories. | | `wp/term-update` | experimental | content | Port of the term update ability from [WordPress/ai PR #1103](https://github.com/WordPress/ai/pull/1103) (`core_since: null` — flips to tier `core` when it lands in WordPress core). Updates a term by `id` with the same fields, which keep their values when left out; `taxonomy` is an optional guard, and a term outside it is denied like a missing one. Returns the updated term in the `wp/terms-query` shape; `fields` picks the subset. Requires `edit_term` for the term. | | `wp/term-delete` | experimental | content | Port of the term delete ability from [WordPress/ai PR #1103](https://github.com/WordPress/ai/pull/1103) (`core_since: null` — flips to tier `core` when it lands in WordPress core). Deletes a term by `id`; terms cannot be trashed, so **`force` must be true** — a non-forced call answers `terms_trash_not_supported` (501). Returns `{ deleted, previous }`, the deleted term in the `wp/terms-query` shape; `fields` picks the subset. Requires `delete_term` for the term; the default category is not deleted (core's capability mapping denies it, and `wp_delete_term()` refuses it), and the `taxonomy` input is an optional guard like on update. | +| `wp/plugins-query` | experimental | site | Lists visible installed plugins with their Core REST identifier, name, version, and activation status. Accepts an exact `plugin` or `status` filter, including `network-active` on multisite. | +| `wp/themes-query` | experimental | site | Lists visible themes with stylesheet, template, name, version, and status. Accepts an exact `stylesheet` or `status` filter. Callers with active-theme access can request the active theme. | ## Development diff --git a/src/Manifest.php b/src/Manifest.php index 2c263d1..fc79411 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -345,6 +345,28 @@ public static function all(): array { 'renamed_from' => null, 'proposal' => 'https://github.com/WordPress/ai/pull/1103', ), + 'wp/plugins-query' => array( + 'name' => 'wp/plugins-query', + 'class' => Site\Plugins_Query::class, + 'category' => 'site', + 'tier' => self::TIER_EXPERIMENTAL, + 'min_wp_version' => '6.9', + 'core_since' => null, + 'requires' => array(), + 'renamed_from' => null, + 'proposal' => 'https://github.com/WordPress/abilities/issues/17', + ), + 'wp/themes-query' => array( + 'name' => 'wp/themes-query', + 'class' => Site\Themes_Query::class, + 'category' => 'site', + 'tier' => self::TIER_EXPERIMENTAL, + 'min_wp_version' => '6.9', + 'core_since' => null, + 'requires' => array(), + 'renamed_from' => null, + 'proposal' => 'https://github.com/WordPress/abilities/issues/17', + ), ); } diff --git a/src/Site/Abstract_Inventory_Query.php b/src/Site/Abstract_Inventory_Query.php new file mode 100644 index 0000000..9791e3e --- /dev/null +++ b/src/Site/Abstract_Inventory_Query.php @@ -0,0 +1,177 @@ + 'object', + 'default' => array(), + 'properties' => array( + static::IDENTIFIER => array( + 'type' => 'string', + 'minLength' => 1, + ), + 'status' => array( + 'type' => 'string', + 'enum' => $this->statuses(), + ), + ), + 'additionalProperties' => false, + ); + } + + /** @inheritDoc */ + protected function output_schema(): array { + $fields = array_fill_keys( static::FIELDS, array( 'type' => 'string' ) ); + $fields['status'] = array( + 'type' => 'string', + 'enum' => $this->statuses(), + ); + + return array( + 'type' => 'object', + 'properties' => array( + static::COLLECTION => array( + 'type' => 'array', + 'items' => array( + 'type' => 'object', + 'properties' => $fields, + 'required' => static::FIELDS, + 'additionalProperties' => false, + ), + ), + 'total' => array( + 'type' => 'integer', + 'minimum' => 0, + ), + ), + 'required' => array( static::COLLECTION, 'total' ), + 'additionalProperties' => false, + ); + } + + /** @inheritDoc */ + protected function ability_meta(): array { + return array( + 'annotations' => array( + 'readonly' => true, + 'destructive' => false, + 'idempotent' => true, + 'open_world' => false, + ), + 'show_in_rest' => true, + 'mcp' => array( 'public' => true ), + ); + } + + /** @inheritDoc */ + protected function required_capabilities(): array { + // The controller selects the capability for the requested inventory. + return array(); + } + + /** + * Build the same read request for the permission and execution callbacks. + * + * @param array $input Inventory filters. + * @return WP_REST_Request + */ + protected function request( array $input ): WP_REST_Request { + $request = new WP_REST_Request( 'GET', '/wp/v2/' . static::COLLECTION ); + $request->set_param( 'context', 'view' ); + $request->set_param( '_fields', implode( ',', static::FIELDS ) ); + if ( isset( $input['status'] ) ) { + $request->set_param( 'status', array( $input['status'] ) ); + } + return $request; + } + + /** @inheritDoc */ + protected function make_permission_callback(): callable { + return function ( $input = array() ) { + // Direct permission checks can receive input before the default is applied. + $input = $input ?? array(); + if ( ! is_array( $input ) || is_wp_error( $this->validate_input( $input ) ) ) { + return false; + } + return true === $this->controller()->get_items_permissions_check( $this->request( $input ) ); + }; + } + + /** + * Select the fields promised by this ability, including on filtered REST responses. + * + * @param array $item Core REST collection item. + * @return array + */ + protected function project_item( array $item ): array { + return array_intersect_key( $item, array_flip( static::FIELDS ) ); + } + + /** @inheritDoc */ + protected function make_execute_callback(): callable { + return function ( $input = array() ) { + $response = $this->controller()->get_items( $this->request( $input ) ); + if ( is_wp_error( $response ) ) { + return $response; + } + $items = array(); + foreach ( $response->get_data() as $item ) { + if ( isset( $input[ static::IDENTIFIER ] ) && $input[ static::IDENTIFIER ] !== $item[ static::IDENTIFIER ] ) { + continue; + } + $items[] = $this->project_item( $item ); + } + return array( + static::COLLECTION => $items, + 'total' => count( $items ), + ); + }; + } +} diff --git a/src/Site/Plugins_Query.php b/src/Site/Plugins_Query.php new file mode 100644 index 0000000..b54e0fe --- /dev/null +++ b/src/Site/Plugins_Query.php @@ -0,0 +1,44 @@ +user_id ) { + if ( is_multisite() ) { + wpmu_delete_user( $this->user_id ); + } else { + require_once ABSPATH . 'wp-admin/includes/user.php'; + wp_delete_user( $this->user_id ); + } + } + if ( '' !== $this->plugin_dir ) { + wp_delete_file( $this->plugin_dir . '/inventory.php' ); + rmdir( $this->plugin_dir ); + wp_clean_plugins_cache( false ); + } + parent::tear_down(); + } + + /** + * Look up an inventory through the package's public registration path. + * + * @param string $collection Collection name. + * @return WP_Ability + */ + private function ability( string $collection ): WP_Ability { + $ability = wp_get_ability( 'wp/' . $collection . '-query' ); + self::assertInstanceOf( WP_Ability::class, $ability ); + return $ability; + } + + /** + * Set the current site user for permission checks. + * + * @param string $role Site role. + */ + private function login_as( string $role ): void { + $id = wp_insert_user( + array( + 'user_login' => 'inventory-' . wp_generate_uuid4(), + 'user_pass' => wp_generate_password(), + 'role' => $role, + ) + ); + self::assertIsInt( $id ); + $this->user_id = $id; + wp_set_current_user( $id ); + } + + /** + * Install an inert local plugin header; Core reads Network from disk on multisite. + * + * @return string The Core REST plugin identifier. + */ + private function create_network_plugin(): string { + $dir = 'inventory-' . wp_generate_uuid4(); + $this->plugin_dir = WP_PLUGIN_DIR . '/' . $dir; + self::assertTrue( wp_mkdir_p( $this->plugin_dir ) ); + file_put_contents( $this->plugin_dir . '/inventory.php', " 'plugin', + 'themes' => 'stylesheet', + ) as $collection => $id ) { + $ability = $this->ability( $collection ); + $input = $ability->get_input_schema(); + $output = $ability->get_output_schema(); + self::assertSame( array( $id, 'status' ), array_keys( $input['properties'] ) ); + self::assertFalse( $input['additionalProperties'] ); + self::assertSame( array( $collection, 'total' ), $output['required'] ); + self::assertTrue( $ability->get_meta()['annotations']['readonly'] ); + self::assertTrue( $ability->get_meta()['mcp']['public'] ); + } + } + + /** A caller without inventory access receives no rows, including on exact lookups. */ + public function test_unprivileged_queries_are_denied(): void { + $this->login_as( 'subscriber' ); + foreach ( array( + 'plugins' => array( 'plugin' => 'example' ), + 'themes' => array( 'stylesheet' => get_stylesheet() ), + ) as $collection => $filter ) { + $ability = $this->ability( $collection ); + self::assertNotTrue( $ability->check_permissions( $filter ) ); + self::assertInstanceOf( \WP_Error::class, $ability->execute( $filter ) ); + self::assertInstanceOf( \WP_Error::class, $ability->execute() ); + } + } + + /** Even an administrator cannot turn an invalid filter into an unrestricted request. */ + public function test_malformed_direct_permission_inputs_fail_closed(): void { + $this->login_as( 'administrator' ); + foreach ( array( + 'plugins' => 'plugin', + 'themes' => 'stylesheet', + ) as $collection => $id ) { + $ability = $this->ability( $collection ); + foreach ( array( 'active', array( 'typo' => 'active' ), array( 'status' => 'unknown' ), array( $id => '' ), array( $id => array() ) ) as $input ) { + self::assertNotTrue( $ability->check_permissions( $input ) ); + self::assertInstanceOf( \WP_Error::class, $ability->execute( $input ) ); + } + } + } + + /** Plugin filters intersect, and a network-only row and its count follow Core visibility. */ + public function test_plugin_visibility_and_activation_status(): void { + $this->login_as( 'administrator' ); + $plugin = $this->create_network_plugin(); + $ability = $this->ability( 'plugins' ); + $menu = static function () { + return array( 'plugins' => 1 ); + }; + $active = static function () use ( $plugin ) { + return array( $plugin . '.php' => 1 ); + }; + add_filter( 'pre_site_option_menu_items', $menu ); + try { + $result = $ability->execute( array( 'plugin' => $plugin ) ); + if ( is_multisite() ) { + self::assertSame( + array( + 'plugins' => array(), + 'total' => 0, + ), + $result + ); + grant_super_admin( $this->user_id ); + try { + $visible = $ability->execute( array( 'plugin' => $plugin ) ); + self::assertSame( 1, $visible['total'] ); + self::assertSame( 'inactive', $visible['plugins'][0]['status'] ); + } finally { + revoke_super_admin( $this->user_id ); + } + add_filter( 'pre_site_option_active_sitewide_plugins', $active ); + $status = 'network-active'; + } else { + $status = 'inactive'; + } + + $input = array( + 'plugin' => $plugin, + 'status' => $status, + ); + $result = $ability->execute( $input ); + self::assertIsArray( $result ); + self::assertSame( 1, $result['total'] ); + self::assertEquals( + array( + 'plugin' => $plugin, + 'name' => 'Inventory Fixture', + 'version' => '1.2.3', + 'status' => $status, + ), + $result['plugins'][0] + ); + self::assertSame( + array( + 'plugins' => array(), + 'total' => 0, + ), + $ability->execute( + array( + 'plugin' => $plugin, + 'status' => 'active', + ) + ) + ); + } finally { + remove_filter( 'pre_site_option_active_sitewide_plugins', $active ); + remove_filter( 'pre_site_option_menu_items', $menu ); + } + } + + /** A theme manager can query the full collection and select an exact visible row. */ + public function test_theme_manager_can_list_themes(): void { + $this->login_as( 'administrator' ); + $ability = $this->ability( 'themes' ); + self::assertTrue( $ability->check_permissions() ); + $result = $ability->execute(); + self::assertIsArray( $result ); + self::assertCount( $result['total'], $result['themes'] ); + self::assertContains( get_stylesheet(), wp_list_pluck( $result['themes'], 'stylesheet' ) ); + self::assertTrue( rest_validate_value_from_schema( $result, $ability->get_output_schema() ) ); + + $inactive = $ability->execute( array( 'status' => 'inactive' ) ); + self::assertIsArray( $inactive ); + self::assertNotContains( get_stylesheet(), wp_list_pluck( $inactive['themes'], 'stylesheet' ) ); + self::assertSame( array(), $ability->execute( array( 'stylesheet' => 'missing-' . wp_generate_uuid4() ) )['themes'] ); + } + + /** Active-theme readers can retrieve the current stylesheet without enumerating others. */ + public function test_contributor_active_theme_and_exact_filters(): void { + $this->login_as( 'contributor' ); + $ability = $this->ability( 'themes' ); + $stylesheet = get_stylesheet(); + $mock = static function ( WP_REST_Response $response ): WP_REST_Response { + $data = $response->get_data(); + $data['name'] = array( + 'raw' => 'Example Child', + 'rendered' => 'Example Child', + ); + $data['template'] = 'example-parent'; + $data['private_note'] = 'Only the inventory fields are public.'; + $response->set_data( $data ); + return $response; + }; + add_filter( 'rest_prepare_theme', $mock ); + try { + $result = $ability->execute( array( 'status' => 'active' ) ); + self::assertIsArray( $result ); + self::assertSame( 1, $result['total'] ); + $row = $result['themes'][0]; + self::assertSame( $stylesheet, $row['stylesheet'] ); + self::assertSame( 'example-parent', $row['template'] ); + self::assertSame( 'Example Child', $row['name'] ); + self::assertSame( 'active', $row['status'] ); + self::assertArrayNotHasKey( 'private_note', $row ); + self::assertSame( $result, $ability->execute( array( 'stylesheet' => $stylesheet ) ) ); + self::assertSame( + array( + 'themes' => array(), + 'total' => 0, + ), + $ability->execute( + array( + 'stylesheet' => 'other', + 'status' => 'active', + ) + ) + ); + self::assertInstanceOf( \WP_Error::class, $ability->execute() ); + self::assertInstanceOf( \WP_Error::class, $ability->execute( array( 'status' => 'inactive' ) ) ); + self::assertInstanceOf( \WP_Error::class, $ability->execute( array( 'stylesheet' => 'other' ) ) ); + } finally { + remove_filter( 'rest_prepare_theme', $mock ); + } + } +}