Skip to content
Draft
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions src/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
);
}

Expand Down
177 changes: 177 additions & 0 deletions src/Site/Abstract_Inventory_Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php
/**
* Shared collection handling for the experimental site inventory queries.
*
* @package WordPress\Abilities
*/

declare( strict_types = 1 );

namespace WordPress\Abilities\Site;

use WordPress\Abilities\Abstract_Core_Ability;
use WP_REST_Controller;
use WP_REST_Request;

/**
* Projects the current site's visible REST collection into a small inventory.
*/
abstract class Abstract_Inventory_Query extends Abstract_Core_Ability {
protected const COLLECTION = '';
protected const IDENTIFIER = '';
protected const FIELDS = array();

/**
* Returns the Core controller responsible for collection and row visibility.
*
* @return WP_REST_Controller
*/
abstract protected function controller(): WP_REST_Controller;

/** @inheritDoc */
protected function ability_name(): string {
return 'wp/' . static::COLLECTION . '-query';
}

/** @inheritDoc */
protected function category_slug(): string {
return 'site';
}

/**
* Status values available on the current installation.
*
* @return string[]
*/
protected function statuses(): array {
return array( 'active', 'inactive' );
}

/** @inheritDoc */
protected function input_schema(): array {
return array(
'type' => '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 ),
);
};
}
}
44 changes: 44 additions & 0 deletions src/Site/Plugins_Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Experimental read-only plugin inventory.
*
* @package WordPress\Abilities
*/

declare( strict_types = 1 );

namespace WordPress\Abilities\Site;

use WP_REST_Controller;
use WP_REST_Plugins_Controller;

/** Lists installed plugins visible on the current site. */
final class Plugins_Query extends Abstract_Inventory_Query {
protected const COLLECTION = 'plugins';
protected const IDENTIFIER = 'plugin';
protected const FIELDS = array( 'plugin', 'name', 'version', 'status' );

/** @inheritDoc */
protected function label(): string {
return __( 'Query Installed Plugins' );
}

/** @inheritDoc */
protected function description(): string {
return __( 'List visible installed plugins, optionally filtered by activation status or exact plugin identifier (the plugin file relative to the plugins directory, without the final .php).' );
}

/** @inheritDoc */
protected function controller(): WP_REST_Controller {
return new WP_REST_Plugins_Controller();
}

/** @inheritDoc */
protected function statuses(): array {
$statuses = parent::statuses();
if ( is_multisite() ) {
$statuses[] = 'network-active';
}
return $statuses;
}
}
51 changes: 51 additions & 0 deletions src/Site/Themes_Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Experimental read-only theme inventory.
*
* @package WordPress\Abilities
*/

declare( strict_types = 1 );

namespace WordPress\Abilities\Site;

use WP_REST_Controller;
use WP_REST_Request;
use WP_REST_Themes_Controller;

/** Lists installed themes visible on the current site. */
final class Themes_Query extends Abstract_Inventory_Query {
protected const COLLECTION = 'themes';
protected const IDENTIFIER = 'stylesheet';
protected const FIELDS = array( 'stylesheet', 'template', 'name', 'version', 'status' );

/** @inheritDoc */
protected function label(): string {
return __( 'Query Installed Themes' );
}

/** @inheritDoc */
protected function description(): string {
return __( 'List visible installed themes, optionally filtered by status or exact stylesheet. The template identifies the parent of a child theme.' );
}

/** @inheritDoc */
protected function controller(): WP_REST_Controller {
return new WP_REST_Themes_Controller();
}

/** @inheritDoc */
protected function request( array $input ): WP_REST_Request {
// Core permits editors to read the active theme without listing other themes.
if ( ! isset( $input['status'] ) && isset( $input['stylesheet'] ) && get_stylesheet() === $input['stylesheet'] ) {
$input['status'] = 'active';
}
return parent::request( $input );
}

/** @inheritDoc */
protected function project_item( array $item ): array {
$item['name'] = $item['name']['raw'];
return parent::project_item( $item );
}
}
Loading
Loading