Skip to content
Merged
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
44 changes: 44 additions & 0 deletions .stubs/option.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* PHPStan stubs for the 7.2 Settings API additions.
*
* phpstan-wordpress describes get_registered_settings() as it was before 7.2,
* without the `show_in_abilities` argument that the settings abilities read.
* This file is dev tooling only: referenced from phpstan.neon.dist stubFiles,
* never autoloaded, never shipped or listed in composer.json autoload entries.
* Once phpstan-wordpress describes the 7.2 shape, delete this file and its
* stubFiles entry.
*
* @package WordPress\Abilities
*/

namespace {

/**
* Retrieves an array of registered settings.
*
* Mirrors trunk's wp-includes/option.php docblock. register_setting() fills
* every key from its defaults except `default`, which is only present when
* the setting was registered with one; `show_in_abilities` is a default
* since 7.2 and, on older hosts, present only when the registration passed it.
* `group` is always filled too, but stays optional here because the trunk code
* this package ports verbatim reads it defensively (`$args['group'] ?? ''`).
*
* @since 4.7.0
* @since 7.2.0 Registered setting data includes the `show_in_abilities` argument.
*
* @phpstan-return array<int|string, array{
* type: string,
* group?: string,
* label: string,
* description: string,
* sanitize_callback: callable|null,
* show_in_rest: bool|array<string, mixed>,
* show_in_abilities?: bool|array<string, mixed>,
* default?: mixed,
* }>
*/
function get_registered_settings() {
return array();
}
}
10 changes: 6 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ registration under the old name are **rejected** — no exceptions, ever.
deliberately carries no version field — and must match the newest git tag
when tags exist, else the `0.1.0` seed).
- `verify:coreify` — the coreify round-trip gate (`tools/coreify.php
--check`): reverse of the trunk fixture followed by forward must
reproduce the original core file, and the reverse output must match the
hand-port `src/Content/Content_Query.php`, modulo whitespace and
comments.
--check`), for every tier-core ability with a transform profile
(`core/content-query`, `core/settings-get`): reverse of the trunk fixture
followed by forward must reproduce the original core file, and the
reverse output must match the hand-port (`src/Content/Content_Query.php`,
`src/Settings/Settings_Get.php`), modulo whitespace and comments. A new
tier-core backport adds its profile to `PROFILES` in `tools/coreify.php`.
- PHPUnit on both environments — WordPress trunk and latest stable. Both
ship the Abilities API in core (the 6.9+ support floor; stable pre-7.2
carries the real pre-7.2 API), so core-skip and backport paths are both
Expand Down
12 changes: 12 additions & 0 deletions KNOWN-ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ This file tracks open items only: an entry is removed once fixed. The
closing commit and changelog carry the resolution; git history is the
archive of resolved entries.

## Readiness probe constructs the ability twice

For an ability that overrides `should_register()`, or under `public_only`
selection, the Registrar's Gate 7 reads a probe instance before the registry
builds its own, so an ability with registration-time state computes it
twice per registration. For `core/settings-get` this is the exposed-settings
snapshot: both snapshots are identical, but a duplicate exposed setting name
fires its `_doing_it_wrong()` notice once per snapshot (twice, where core
fires once). The registry's `ability_class` mechanism gives no way to hand
it the probed instance. Abilities that keep the default `should_register()`
are not probed outside `public_only`.

## Ported-as-is PR behaviors (documented, not fixed)

Behaviors inherited verbatim from the upstream PR; intentional for parity
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Full rules, the promotion path, and the rename protocol are in
| Name | Tier | Category | Notes |
|---|---|---|---|
| `core/content-query` | core | content | Backport of the readonly content query ability landed in WordPress core 7.2 (`core_since: 7.2`). On 7.2+ core provides it and the Registrar skips it; on 6.9–7.1 the package serves it under the same name. Requires the `show_in_abilities` support feature (polyfilled where core lacks it). |
| `core/settings-get` | core | site | Backport of the readonly settings get ability landed in WordPress core 7.2 (`core_since: 7.2`). On 7.2+ core provides it and the Registrar skips it; on 6.9–7.1 the package serves it under the same name. Requires the `setting_show_in_abilities` support feature (polyfilled where core lacks it: core's own settings are flagged and registered before the ability reads them, as on 7.2). |
| `core/content-create` | stable | content | Port of the content create ability from [WordPress/ai PR #1025](https://github.com/WordPress/ai/pull/1025) (`core_since: null` — flips to tier `core` when it lands in WordPress core). Creates posts of types exposed to abilities under the field names `core/content-query` returns. |
| `core/content-update` | stable | content | Port of the content update ability from [WordPress/ai PR #1025](https://github.com/WordPress/ai/pull/1025) (`core_since: null` — flips to tier `core` when it lands in WordPress core). Updates a post by ID; fields left out keep their current values. |
| `core/content-delete` | stable | content | Port of the content delete ability from [WordPress/ai PR #1025](https://github.com/WordPress/ai/pull/1025) (`core_since: null` — flips to tier `core` when it lands in WordPress core). Trashes a post by ID, or deletes it permanently with `force`. |
Expand All @@ -93,9 +94,8 @@ Full rules, the promotion path, and the rename protocol are in

Planned, **not** available in the current package: the MCP adapter consuming
this package via `Registrar::register_all()`; further write abilities
(`wp/post-update`, `wp/post-delete`); `core/users-query` and
`core/settings-get` backports; the WordPress AI plugin migration; Packagist
publishing.
beyond the content trio; the `core/users-query` backport; the WordPress AI
plugin migration; Packagist publishing.

## Development

Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ parameters:
stubFiles:
# Abilities API symbols (new in core trunk, not yet in phpstan-wordpress).
- .stubs/abilities-api.php
# get_registered_settings() as of 7.2 (phpstan-wordpress predates `show_in_abilities`).
- .stubs/option.php
110 changes: 95 additions & 15 deletions src/Abstract_Core_Ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ abstract protected function make_execute_callback(): callable;
* Handles row/status-level checks and must fail closed on malformed
* input. Return false or a WP_Error to deny.
*
* @return callable(mixed): (bool|\WP_Error)
* Return null when the required capabilities fully own authorization, as
* core allows since 7.2 for an ability that declares a non-empty
* `required_capabilities` list and no `permission_callback`.
*
* @return (callable(mixed): (bool|\WP_Error))|null
*/
abstract protected function make_permission_callback(): callable;
abstract protected function make_permission_callback(): ?callable;

/**
* The ability meta authored by this ability.
Expand All @@ -111,6 +115,29 @@ abstract protected function ability_meta(): array;
*/
abstract protected function required_capabilities(): array;

/**
* Computes the registration-time state the other authoring methods read.
*
* Called once, first, by the constructor. Mirrors the state core computes
* in its register() entry point before registering the ability, such as
* a snapshot that the input schema, output schema, and execute callback
* must all share. Most abilities have none and keep this no-op.
*/
protected function prepare(): void {}

/**
* Whether the ability should be registered at all.
*
* Mirrors the early return core takes in its register() entry
* point, for example when there is nothing to expose. Most
* abilities always register and keep the default.
*
* @return bool True to register the ability.
*/
public function should_register(): bool {
return true;
}

/**
* Provenance meta merged into every ability of this package.
*
Expand Down Expand Up @@ -160,20 +187,73 @@ final public function __construct( ?string $name = null, array $args = array() )
);
}

parent::__construct(
$ability_name,
array(
'label' => $this->label(),
'description' => $this->description(),
'category' => $this->category_slug(),
'input_schema' => $this->input_schema(),
'output_schema' => $this->output_schema(),
'execute_callback' => $this->make_execute_callback(),
'permission_callback' => $this->make_permission_callback(),
'required_capabilities' => $this->required_capabilities(),
'meta' => $this->merge_meta( $args ),
)
$this->prepare();

$properties = array(
'label' => $this->label(),
'description' => $this->description(),
'category' => $this->category_slug(),
'input_schema' => $this->input_schema(),
'output_schema' => $this->output_schema(),
'execute_callback' => $this->make_execute_callback(),
'meta' => $this->merge_meta( $args ),
);

$permission_callback = $this->make_permission_callback();
$required_capabilities = $this->required_capabilities();

if ( self::host_checks_required_capabilities() ) {
$properties['permission_callback'] = $permission_callback;
$properties['required_capabilities'] = $required_capabilities;
} else {
$properties['permission_callback'] = self::with_capability_floor( $required_capabilities, $permission_callback );
}

parent::__construct( $ability_name, $properties );
}

/**
* Whether the host's WP_Ability checks `required_capabilities` itself.
*
* Core added the argument in 7.2. Older hosts reject it as an unknown
* property and would never check the floor, so the constructor folds the
* floor into the permission callback there instead.
*
* @return bool True when the host declares the `required_capabilities` property.
*/
private static function host_checks_required_capabilities(): bool {
return property_exists( WP_Ability::class, 'required_capabilities' );
}

/**
* Wraps a permission callback with the required capabilities check.
*
* Mirrors WP_Ability::check_permissions() on 7.2+: every required
* capability is checked first, and the permission callback runs only
* when all of them pass. Without a permission callback, the floor alone
* decides; an empty floor without a callback fails closed.
*
* @param list<string> $required_capabilities The capability floor.
* @param (callable(mixed): (bool|\WP_Error))|null $permission_callback The authored permission callback.
* @return callable(mixed): (bool|\WP_Error) The combined permission callback.
*/
private static function with_capability_floor( array $required_capabilities, ?callable $permission_callback ): callable {
if ( array() === $required_capabilities ) {
// No floor to fold in: keep the callback, or fail closed without one.
return $permission_callback ?? static function (): bool {
return false;
};
}

return static function ( ...$args ) use ( $required_capabilities, $permission_callback ) {
foreach ( $required_capabilities as $capability ) {
if ( ! current_user_can( $capability ) ) {
return false;
}
}

return null === $permission_callback ? true : $permission_callback( ...$args );
};
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ final class Categories {
* @var array<string, array{label: string, description: string}>
*/
private const DEFINITIONS = array(
'site' => array(
'label' => 'Site',
'description' => 'Abilities that retrieve or modify site information and settings.',
),
'content' => array(
'label' => 'Content',
'description' => 'Abilities that retrieve or manage posts and other content.',
Expand Down
12 changes: 12 additions & 0 deletions src/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ public static function all(): array {
// the landed core work (wordpress-develop PR 12195) before release.
'proposal' => 'https://github.com/WordPress/wordpress-develop/pull/12195',
),
'core/settings-get' => array(
'name' => 'core/settings-get',
'class' => Settings\Settings_Get::class,
'category' => 'site',
'tier' => self::TIER_CORE,
'min_wp_version' => '6.9',
'core_since' => '7.2',
'requires' => array( 'setting_show_in_abilities' ),
'renamed_from' => null,
// Landed in trunk as b6512b1e8c (Trac #64605), developed in this PR.
'proposal' => 'https://github.com/WordPress/wordpress-develop/pull/12141',
),
/*
* The write trio below is a faithful port of WordPress/ai PR #1025
* (author @jorgefilipecosta), on the package's dotted field paths.
Expand Down
Loading
Loading