Skip to content

Commit 6419a95

Browse files
Abilities API: make WP_Settings_Abilities instance-based.
Move register() and the ability callbacks (execute_get_settings(), has_permission()) from public static to public methods, and the internal helpers to private methods; the exposed-settings cache becomes an instance property. wp_register_core_abilities() now registers the ability via ( new WP_Settings_Abilities() )->register(), reducing the static surface of this @access private class.
1 parent b2e95b9 commit 6419a95

2 files changed

Lines changed: 20 additions & 20 deletions

File tree

‎src/wp-includes/abilities.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,5 +355,5 @@ function wp_register_core_abilities(): void {
355355
);
356356

357357
// Register the settings abilities (currently the read-only `core/settings`).
358-
WP_Settings_Abilities::register();
358+
( new WP_Settings_Abilities() )->register();
359359
}

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ final class WP_Settings_Abilities {
4343
* @since 7.1.0
4444
* @var array<string, array{option: string, group: string, default: mixed, schema: array<string, mixed>}>|null
4545
*/
46-
private static $exposed_settings = null;
46+
private $exposed_settings = null;
4747

4848
/**
4949
* Registers all settings abilities.
@@ -52,14 +52,14 @@ final class WP_Settings_Abilities {
5252
*
5353
* @since 7.1.0
5454
*/
55-
public static function register(): void {
56-
self::register_get_settings();
55+
public function register(): void {
56+
$this->register_get_settings();
5757

5858
/*
5959
* A future write-oriented ability can be registered here, reusing the shared
6060
* helpers below (get_exposed_settings(), value_schema(), cast_value()):
6161
*
62-
* self::register_manage_settings();
62+
* $this->register_manage_settings();
6363
*/
6464
}
6565

@@ -68,11 +68,11 @@ public static function register(): void {
6868
*
6969
* @since 7.1.0
7070
*/
71-
private static function register_get_settings(): void {
71+
private function register_get_settings(): void {
7272
// Compute once; execute_get_settings() reuses this exact structure.
73-
self::$exposed_settings = self::get_exposed_settings();
73+
$this->exposed_settings = $this->get_exposed_settings();
7474

75-
$settings = self::$exposed_settings;
75+
$settings = $this->exposed_settings;
7676
$field_names = array_keys( $settings );
7777
$groups = array();
7878
$properties = array();
@@ -90,15 +90,15 @@ private static function register_get_settings(): void {
9090
'label' => __( 'Get Settings' ),
9191
'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.' ),
9292
'category' => self::CATEGORY,
93-
'input_schema' => self::get_settings_input_schema( $groups, $field_names ),
93+
'input_schema' => $this->get_settings_input_schema( $groups, $field_names ),
9494
'output_schema' => array(
9595
'type' => 'object',
9696
'description' => __( 'A map of setting name to its current value.' ),
9797
'properties' => $properties,
9898
'additionalProperties' => false,
9999
),
100-
'execute_callback' => array( self::class, 'execute_get_settings' ),
101-
'permission_callback' => array( self::class, 'has_permission' ),
100+
'execute_callback' => array( $this, 'execute_get_settings' ),
101+
'permission_callback' => array( $this, 'has_permission' ),
102102
'meta' => array(
103103
'annotations' => array(
104104
'readonly' => true,
@@ -119,10 +119,10 @@ private static function register_get_settings(): void {
119119
* @param mixed $input Optional. The ability input. Default empty array.
120120
* @return array<string, mixed> Map of exposed setting name to current value.
121121
*/
122-
public static function execute_get_settings( $input = array() ): array {
122+
public function execute_get_settings( $input = array() ): array {
123123
$input = is_array( $input ) ? $input : array();
124124

125-
$settings = self::$exposed_settings;
125+
$settings = $this->exposed_settings;
126126
if ( null === $settings ) {
127127
// The cache is populated in register_get_settings() before the ability is
128128
// registered, so this is unreachable in practice; bail defensively otherwise.
@@ -144,7 +144,7 @@ public static function execute_get_settings( $input = array() ): array {
144144
$type = isset( $setting['schema']['type'] ) && is_string( $setting['schema']['type'] ) ? $setting['schema']['type'] : 'string';
145145
$value = get_option( $setting['option'], $setting['default'] );
146146

147-
$result[ $exposed_name ] = self::cast_value( $value, $type );
147+
$result[ $exposed_name ] = $this->cast_value( $value, $type );
148148
}
149149

150150
return $result;
@@ -157,7 +157,7 @@ public static function execute_get_settings( $input = array() ): array {
157157
*
158158
* @return bool True if the current user can manage options.
159159
*/
160-
public static function has_permission(): bool {
160+
public function has_permission(): bool {
161161
return current_user_can( 'manage_options' );
162162
}
163163

@@ -173,7 +173,7 @@ public static function has_permission(): bool {
173173
* @param list<string> $field_names Available exposed setting names.
174174
* @return array<string, mixed> The input JSON Schema.
175175
*/
176-
private static function get_settings_input_schema( array $groups, array $field_names ): array {
176+
private function get_settings_input_schema( array $groups, array $field_names ): array {
177177
return array(
178178
'type' => 'object',
179179
// Object (not array()) so the serialized schema default is {}, consistent with type:object.
@@ -209,7 +209,7 @@ private static function get_settings_input_schema( array $groups, array $field_n
209209
*
210210
* @return array<string, array{option: string, group: string, default: mixed, schema: array<string, mixed>}> Settings keyed by exposed name.
211211
*/
212-
private static function get_exposed_settings(): array {
212+
private function get_exposed_settings(): array {
213213
$settings = array();
214214

215215
foreach ( get_registered_settings() as $option_name => $args ) {
@@ -225,7 +225,7 @@ private static function get_exposed_settings(): array {
225225
'option' => $option_name,
226226
'group' => isset( $args['group'] ) && is_string( $args['group'] ) ? $args['group'] : '',
227227
'default' => array_key_exists( 'default', $args ) ? $args['default'] : false,
228-
'schema' => self::value_schema( $args, $show ),
228+
'schema' => $this->value_schema( $args, $show ),
229229
);
230230
}
231231

@@ -241,7 +241,7 @@ private static function get_exposed_settings(): array {
241241
* @param bool|array<string, mixed> $show The setting's `show_in_abilities` value.
242242
* @return array<string, mixed> The value JSON Schema.
243243
*/
244-
private static function value_schema( array $args, $show ): array {
244+
private function value_schema( array $args, $show ): array {
245245
$schema = array(
246246
'type' => isset( $args['type'] ) && is_string( $args['type'] ) ? $args['type'] : 'string',
247247
);
@@ -269,7 +269,7 @@ private static function value_schema( array $args, $show ): array {
269269
* @param string $type The registered setting type.
270270
* @return mixed The value cast to the declared type.
271271
*/
272-
private static function cast_value( $value, string $type ) {
272+
private function cast_value( $value, string $type ) {
273273
switch ( $type ) {
274274
case 'boolean':
275275
return (bool) $value;

0 commit comments

Comments
 (0)