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
9 changes: 7 additions & 2 deletions src/wp-includes/abilities/class-wp-abilities-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ private function get_settings_get_output_schema(): array {
* Reads {@see get_registered_settings()} and keeps only settings flagged with a truthy
* `show_in_abilities` argument, of a type the settings endpoint supports. Each entry is
* keyed by its exposed name and carries the underlying option name, the settings group,
* the registered default (null when there is none), and a JSON Schema describing the value.
* the default (the exposure schema's default when it declares one, otherwise the registered
* default, or null when there is none), and a JSON Schema describing the value.
*
* Two settings exposed under the same name trigger a notice, and the later one is exposed.
*
Expand Down Expand Up @@ -260,10 +261,14 @@ private function get_exposed_settings(): array {
);
}

/*
* As the settings endpoint does, a default declared in the exposure schema
* overrides the registered default.
*/
$settings[ $exposed_name ] = array(
'option' => $option_name,
'group' => $args['group'] ?? '',
'default' => $args['default'] ?? null,
'default' => array_key_exists( 'default', $schema ) ? $schema['default'] : ( $args['default'] ?? null ),
'schema' => $schema,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,71 @@ public static function data_never_saved_settings(): array {
),
'false',
),
'a default in the REST API schema' => array(
array(
'type' => 'string',
'show_in_rest' => array(
'schema' => array( 'default' => 'from-rest-schema' ),
),
),
'"from-rest-schema"',
),
'a REST API schema default overriding the registered default' => array(
array(
'type' => 'string',
'default' => 'registered',
'show_in_rest' => array(
'schema' => array( 'default' => 'from-rest-schema' ),
),
),
'"from-rest-schema"',
),
'a default in the abilities schema' => array(
array(
'type' => 'string',
'show_in_abilities' => array(
'schema' => array( 'default' => 'from-abilities-schema' ),
),
),
'"from-abilities-schema"',
),
);
}

/**
* A setting that was never saved reads the same default through the ability as through
* the settings endpoint, including a default declared only in the REST API schema.
*
* @ticket 64605
*/
public function test_core_settings_get_matches_the_settings_endpoint_for_a_schema_default(): void {
$option = 'core_settings_get_ability_schema_default_test_option';

register_setting(
'general',
$option,
array(
'type' => 'string',
'show_in_rest' => array(
'schema' => array( 'default' => 'from-rest-schema' ),
),
'show_in_abilities' => true,
)
);

try {
$this->register_ability();
$this->become_admin();

$ability_result = wp_get_ability( 'core/settings-get' )->execute( array( 'fields' => array( $option ) ) );
$rest_result = rest_get_server()->dispatch( new WP_REST_Request( 'GET', '/wp/v2/settings' ) )->get_data();
} finally {
unregister_setting( 'general', $option );
$this->register_ability();
}

$this->assertSame( 'from-rest-schema', $rest_result[ $option ] ?? null, 'The settings endpoint should read the REST API schema default.' );
$this->assertSame( $rest_result[ $option ] ?? null, $ability_result[ $option ] ?? null, 'The ability should read the same default as the settings endpoint.' );
}

/**
Expand Down
Loading