Skip to content

Commit dbb09f8

Browse files
Add slugs parameter to core/get-settings ability
Addresses review feedback to add support for filtering settings by specific setting slugs. The input schema now supports both `group` and `slugs` parameters with a oneOf relationship making them mutually exclusive. - Add $available_slugs static property - Add get_available_slugs() method to collect setting slugs - Update input_schema with slugs parameter and oneOf constraint - Update execute_get_settings() to filter by slugs when provided
1 parent c226a10 commit dbb09f8

1 file changed

Lines changed: 58 additions & 2 deletions

File tree

src/wp-includes/abilities/class-wp-settings-abilities.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ class WP_Settings_Abilities {
3939
*/
4040
private static $output_schema;
4141

42+
/**
43+
* Available setting slugs with show_in_rest enabled.
44+
*
45+
* @since 7.0.0
46+
* @var array
47+
*/
48+
private static $available_slugs;
49+
4250
/**
4351
* Registers all settings abilities.
4452
*
@@ -60,6 +68,7 @@ public static function register(): void {
6068
*/
6169
private static function init(): void {
6270
self::$available_groups = self::get_available_groups();
71+
self::$available_slugs = self::get_available_slugs();
6372
self::$output_schema = self::build_output_schema();
6473
}
6574

@@ -89,6 +98,34 @@ private static function get_available_groups(): array {
8998
return $groups;
9099
}
91100

101+
/**
102+
* Gets unique setting slugs that have show_in_rest enabled.
103+
*
104+
* @since 7.0.0
105+
*
106+
* @return array List of unique setting slugs.
107+
*/
108+
private static function get_available_slugs(): array {
109+
$slugs = array();
110+
111+
foreach ( get_registered_settings() as $option_name => $args ) {
112+
if ( empty( $args['show_in_rest'] ) ) {
113+
continue;
114+
}
115+
116+
$rest_name = $option_name;
117+
if ( is_array( $args['show_in_rest'] ) && ! empty( $args['show_in_rest']['name'] ) ) {
118+
$rest_name = $args['show_in_rest']['name'];
119+
}
120+
121+
$slugs[] = $rest_name;
122+
}
123+
124+
sort( $slugs );
125+
126+
return $slugs;
127+
}
128+
92129
/**
93130
* Builds a rich output schema from registered settings metadata.
94131
*
@@ -180,9 +217,22 @@ private static function register_get_settings(): void {
180217
'properties' => array(
181218
'group' => array(
182219
'type' => 'string',
183-
'description' => __( 'Filter settings by group name. If omitted, returns all groups.' ),
220+
'description' => __( 'Filter settings by group name. If omitted, returns all groups. Cannot be used with slugs.' ),
184221
'enum' => self::$available_groups,
185222
),
223+
'slugs' => array(
224+
'type' => 'array',
225+
'description' => __( 'Filter settings by specific setting slugs. If omitted, returns all settings. Cannot be used with group.' ),
226+
'items' => array(
227+
'type' => 'string',
228+
'enum' => self::$available_slugs,
229+
),
230+
),
231+
),
232+
'oneOf' => array(
233+
array( 'required' => array( 'group' ) ),
234+
array( 'required' => array( 'slugs' ) ),
235+
array( 'maxProperties' => 0 ),
186236
),
187237
'additionalProperties' => false,
188238
'default' => array(),
@@ -224,13 +274,15 @@ public static function check_manage_options(): bool {
224274
* @param array $input {
225275
* Optional. Input parameters.
226276
*
227-
* @type string $group Optional. Filter settings by group name.
277+
* @type string $group Optional. Filter settings by group name. Cannot be used with slugs.
278+
* @type string[] $slugs Optional. Filter settings by specific setting slugs. Cannot be used with group.
228279
* }
229280
* @return array Settings grouped by registration group.
230281
*/
231282
public static function execute_get_settings( $input = array() ): array {
232283
$input = is_array( $input ) ? $input : array();
233284
$filter_group = ! empty( $input['group'] ) ? $input['group'] : null;
285+
$filter_slugs = ! empty( $input['slugs'] ) ? $input['slugs'] : null;
234286

235287
$registered_settings = get_registered_settings();
236288
$settings_by_group = array();
@@ -251,6 +303,10 @@ public static function execute_get_settings( $input = array() ): array {
251303
$rest_name = $args['show_in_rest']['name'];
252304
}
253305

306+
if ( $filter_slugs && ! in_array( $rest_name, $filter_slugs, true ) ) {
307+
continue;
308+
}
309+
254310
$default = $args['default'] ?? null;
255311
if ( is_array( $args['show_in_rest'] ) && isset( $args['show_in_rest']['schema']['default'] ) ) {
256312
$default = $args['show_in_rest']['schema']['default'];

0 commit comments

Comments
 (0)