Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a167a57
Merge branch 'develop' into trunk
jeffpaul Feb 9, 2026
ecc6673
Merge branch 'develop' into trunk
jeffpaul Feb 18, 2026
f282114
Merge branch 'develop' into trunk
jeffpaul Mar 5, 2026
f9587d2
Merge branch 'develop' into trunk
jeffpaul Mar 6, 2026
1f896e2
Merge branch 'develop' into trunk
jeffpaul Mar 12, 2026
e012381
Merge branch 'develop' into trunk
jeffpaul Mar 20, 2026
c52171d
Merge branch 'develop' into trunk
jeffpaul Apr 9, 2026
f46690c
Merge branch 'develop' into trunk
jeffpaul Apr 23, 2026
4498f98
Merge branch 'develop' into trunk
jeffpaul May 7, 2026
57846e1
Merge branch 'develop' into trunk
jeffpaul May 19, 2026
a0ed736
Merge branch 'develop' into trunk
jeffpaul May 27, 2026
e2a60fc
Merge branch 'develop' into trunk
jeffpaul Jun 16, 2026
1b4c470
Add a core/settings ability
jorgefilipecosta Jun 9, 2026
556b776
Run the core/settings client-side e2e from the editor with an experim…
jorgefilipecosta Jun 15, 2026
8ddfe6e
Rename the core/settings 'slugs' input to 'settings'
jorgefilipecosta Jun 15, 2026
57d97d6
Test that core/settings exposes settings registered by other plugins
jorgefilipecosta Jun 15, 2026
be52b5a
Address review feedback on the core/settings ability
jorgefilipecosta Jun 17, 2026
fe8c56a
Update includes/Abilities/Settings/Settings.php
jorgefilipecosta Jun 19, 2026
18a4029
Address review feedback: list<string> docblock and e2e plugin consoli…
jorgefilipecosta Jun 19, 2026
399ee9f
Rename the e2e support plugin to e2e-testing
jorgefilipecosta Jun 19, 2026
20f60d4
Address core/settings review follow-ups
jorgefilipecosta Jun 19, 2026
2a32ec8
Update includes/Abilities/Show_In_Abilities.php
jorgefilipecosta Jun 22, 2026
a2caa64
Convert Show_In_Abilities to a final instance class
jorgefilipecosta Jun 22, 2026
b32e034
Scope the core/settings Settings class
jorgefilipecosta Jun 22, 2026
e31a4d9
Make the core/settings Settings class instance-based
jorgefilipecosta Jun 22, 2026
af5681b
Make the Settings ability init() an instance method
jorgefilipecosta Jun 22, 2026
a28f881
Align the core/settings test suite with the core PR
jorgefilipecosta Jun 22, 2026
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: 1 addition & 1 deletion .wp-env.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"core": null,
"plugins": [
".",
"./tests/e2e-request-mocking",
"./tests/e2e-testing",
"https://downloads.wordpress.org/plugin/ai-provider-for-google.zip",
"https://downloads.wordpress.org/plugin/ai-provider-for-openai.zip"
],
Expand Down
2 changes: 1 addition & 1 deletion docs/ARCHITECTURE_OVERVIEW.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ ai/
└── tests/ # Tests
├── Integration/ # Integration tests for WordPress + Plugin
├── e2e/ # Playwright end-to-end tests
├── e2e-request-mocking/ # Mock API calls for e2e tests
├── e2e-testing/ # Support plugin for e2e tests (API mocking, fixtures)
└── bootstrap.php # PHPUnit bootstrap
```
315 changes: 315 additions & 0 deletions includes/Abilities/Settings/Settings.php
Comment thread
justlevine marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
<?php
/**
* The `core/settings` WordPress Ability.
*
* @package WordPress\AI
*
* @since x.x.x
*/

declare( strict_types=1 );

namespace WordPress\AI\Abilities\Settings;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

/**
* Class - Settings
*
* Registers the read-only `core/settings` ability, which returns WordPress settings as a
* flat map of setting name to value. Only settings flagged with `show_in_abilities` are
* exposed. It is structured to also back a future write-oriented `core/manage-settings`
* ability via the shared helpers (get_exposed_settings(), value_schema(), cast_value()).
*
* This class is kept almost identical to the WordPress core class `WP_Settings_Abilities`
* so the two implementations stay in sync. Differences from the core class are marked with
* `// Plugin:` comments. Additionally, all user-facing strings use the 'ai' text domain.
*
* @internal This class should not be used outside the plugin and there is no guarantee of backwards compatibility.
*
* @since x.x.x
*/
final class Settings {

/**
* The ability category used for settings abilities.
*
* @since x.x.x
* @var string
*/
private const CATEGORY = 'site';

/**
* Settings exposed through the Abilities API, computed once at registration.
*
* Plugin: cached so the input/output schema and the executed result derive from the exact
* same structure, and {@see get_registered_settings()} is only walked once per request.
*
* @since x.x.x
* @var array<string, array{option: string, group: string, default: mixed, schema: array<string, mixed>}>|null
*/
private $exposed_settings = null;

/**
* Hooks the ability into the Abilities API.
*
* Plugin: this method has no equivalent in the core class. In core, register() is
* invoked directly from wp_register_core_abilities() (already on the
* `wp_abilities_api_init` hook). The plugin instead hooks register() slightly later
* (priority 11) so it can override any core-provided copy.
*
* @since x.x.x
*/
public function init(): void {
add_action( 'wp_abilities_api_init', array( $this, 'register' ), 11 );
}

/**
* Registers all settings abilities.
*
* Must run on the `wp_abilities_api_init` hook.
*
* @since x.x.x
*/
public function register(): void {
$this->register_get_settings();

/*
* A future write-oriented ability can be registered here, reusing the shared
* helpers below (get_exposed_settings(), value_schema(), cast_value()):
*
* $this->register_manage_settings();
*/
Comment thread
jorgefilipecosta marked this conversation as resolved.
}

/**
* Registers the read-only `core/settings` ability.
*
* @since x.x.x
*/
private function register_get_settings(): void {
// Plugin: unregister any core-provided copy first so the plugin's version wins.
if ( wp_has_ability( 'core/settings' ) ) {
wp_unregister_ability( 'core/settings' );
}

// Compute once; execute_get_settings() reuses this exact structure.
$this->exposed_settings = $this->get_exposed_settings();

$settings = $this->exposed_settings;
$field_names = array_keys( $settings );
$groups = array();
$properties = array();
foreach ( $settings as $exposed_name => $setting ) {
$properties[ $exposed_name ] = $setting['schema'];
if ( '' === $setting['group'] || in_array( $setting['group'], $groups, true ) ) {
continue;
}
$groups[] = $setting['group'];
}

wp_register_ability(
'core/settings',
array(
'label' => __( 'Get Settings', 'ai' ),
'description' => __( 'Returns WordPress settings as a flat map of setting name to value. By default returns all settings exposed to abilities, or optionally a subset filtered by settings group, by setting name, or both.', 'ai' ),
'category' => self::CATEGORY,
'input_schema' => $this->get_settings_input_schema( $groups, $field_names ),
'output_schema' => array(
'type' => 'object',
'description' => __( 'A map of setting name to its current value.', 'ai' ),
'properties' => $properties,
'additionalProperties' => false,
),
'execute_callback' => array( $this, 'execute_get_settings' ),
'permission_callback' => array( $this, 'has_permission' ),
'meta' => array(
'annotations' => array(
'readonly' => true,
'destructive' => false,
'idempotent' => true,
),
'show_in_rest' => true,
),
)
);
}

/**
* Executes the `core/settings` ability.
*
* @since x.x.x
*
* @param mixed $input Optional. The ability input. Default empty array.
* @return array<string, mixed> Map of exposed setting name to current value.
*/
public function execute_get_settings( $input = array() ): array {
$input = is_array( $input ) ? $input : array();

$settings = $this->exposed_settings;
if ( null === $settings ) {
// The cache is populated in register_get_settings() before the ability is
// registered, so this is unreachable in practice; bail defensively otherwise.
return array();
}

$group = isset( $input['group'] ) && is_string( $input['group'] ) ? $input['group'] : '';
$fields = isset( $input['fields'] ) && is_array( $input['fields'] ) ? $input['fields'] : array();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was checking WP CLI and their wp option list command. Options are a lower-level concept than settings, but they have some things in common. I have some second thoughts about using fields in this specific context, as an individual setting has more fields than a value, for example: group.

On one hand, the current approach aligns closely with core/get-site-info and how it operates, but that's a curated set of site settings.

On the other hand, if we look at #739 and core/content, it's evident that fields are used there for individual rows rather than filtering which rows to pick.


$result = array();
foreach ( $settings as $exposed_name => $setting ) {
if ( '' !== $group && $setting['group'] !== $group ) {
continue;
}
if ( ! empty( $fields ) && ! in_array( $exposed_name, $fields, true ) ) {
continue;
}

$type = isset( $setting['schema']['type'] ) && is_string( $setting['schema']['type'] ) ? $setting['schema']['type'] : 'string';
$value = get_option( $setting['option'], $setting['default'] );

$result[ $exposed_name ] = $this->cast_value( $value, $type );
}

return $result;
}

/**
* Checks whether the current user may use the settings abilities.
*
* @since x.x.x
*
* @return bool True if the current user can manage options.
*/
public function has_permission(): bool {
return current_user_can( 'manage_options' );
}

/**
* Builds the input schema for the get ability: optional filters by group and/or name.
*
* Both `group` and `fields` are optional; supplying both narrows the response to their
* intersection, and supplying neither returns every exposed setting.
*
* @since x.x.x
*
* @param list<string> $groups Available settings groups.
* @param list<string> $field_names Available exposed setting names.
* @return array<string, mixed> The input JSON Schema.
*/
private function get_settings_input_schema( array $groups, array $field_names ): array {
return array(
'type' => 'object',
// Object (not array()) so the serialized schema default is {}, consistent with type:object.
'default' => (object) array(),
'properties' => array(
'group' => array(
'type' => 'string',
'enum' => $groups,
'description' => __( 'Return only settings that belong to this settings group.', 'ai' ),
),
'fields' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
'enum' => $field_names,
),
'description' => __( 'Return only the settings with these names.', 'ai' ),
),
),
'additionalProperties' => false,
);
}

/**
* Returns the settings exposed through the Abilities API.
*
* Reads {@see get_registered_settings()} and keeps only settings flagged with a truthy
* `show_in_abilities` argument. Each entry is keyed by its exposed name and carries the
* underlying option name, the settings group, the registration default, and a JSON Schema
* describing the value.
*
* @since x.x.x
*
* @return array<string, array{option: string, group: string, default: mixed, schema: array<string, mixed>}> Settings keyed by exposed name.
*/
private function get_exposed_settings(): array {
$settings = array();

foreach ( get_registered_settings() as $option_name => $args ) {
Comment thread
jorgefilipecosta marked this conversation as resolved.
$show = $args['show_in_abilities'] ?? false;
if ( empty( $show ) ) {
continue;
}

$option_name = (string) $option_name;
$exposed_name = is_array( $show ) && isset( $show['name'] ) && is_string( $show['name'] ) && '' !== $show['name'] ? $show['name'] : $option_name;

$settings[ $exposed_name ] = array(
'option' => $option_name,
'group' => isset( $args['group'] ) && is_string( $args['group'] ) ? $args['group'] : '',
'default' => array_key_exists( 'default', $args ) ? $args['default'] : false,
'schema' => $this->value_schema( $args, $show ),
);
}

return $settings;
}

/**
* Builds the JSON Schema describing a single setting's value.
*
* @since x.x.x
*
* @param array<string, mixed> $args The setting registration arguments.
* @param bool|array<string, mixed> $show The setting's `show_in_abilities` value.
* @return array<string, mixed> The value JSON Schema.
*/
private function value_schema( array $args, $show ): array {
$schema = array(
'type' => isset( $args['type'] ) && is_string( $args['type'] ) ? $args['type'] : 'string',
);
if ( ! empty( $args['label'] ) ) {
$schema['title'] = $args['label'];
}
if ( ! empty( $args['description'] ) ) {
$schema['description'] = $args['description'];
}
if ( is_array( $show ) && isset( $show['schema'] ) && is_array( $show['schema'] ) ) {
/** @var array<string, mixed> $show_schema */
$show_schema = $show['schema'];
$schema = array_merge( $schema, $show_schema );
}

return $schema;
}

/**
* Casts a stored option value to the type declared in its settings registration.
*
* @since x.x.x
*
* @param mixed $value The raw option value.
* @param string $type The registered setting type.
* @return mixed The value cast to the declared type.
*/
private function cast_value( $value, string $type ) {
switch ( $type ) {
case 'boolean':
return (bool) $value;
case 'integer':
return is_scalar( $value ) ? (int) $value : 0;
case 'number':
return is_scalar( $value ) ? (float) $value : 0.0;
case 'array':
return is_array( $value ) ? $value : array();
Comment thread
jorgefilipecosta marked this conversation as resolved.
case 'object':
// Cast to object so an empty/non-array value serializes as {} (not []) and
// satisfies the `object` output schema validated by execute().
return (object) ( is_array( $value ) ? $value : array() );
default:
return is_scalar( $value ) ? (string) $value : $value;
}
}
}
Loading
Loading