Make WordPress Core

Changeset 61690


Ignore:
Timestamp:
02/19/2026 10:55:41 AM (3 months ago)
Author:
jorgefilipecosta
Message:

Revert: Abilities API core/get-settings ability.

This reverts [61600]. Other core abilities did not made into 7.0 beta 1 timeline, so adding just a single ability does not adds much value and we are temporarily reverting this addition.
This commit is just a clean execution of the revert command.

Props jorgefilipecosta, jason_the_adams, justlevine.
See #64455.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/abilities.php

    r61600 r61690  
    99
    1010declare( strict_types = 1 );
    11 
    12 require_once __DIR__ . '/abilities/class-wp-settings-abilities.php';
    1311
    1412/**
     
    260258        )
    261259    );
    262 
    263     WP_Settings_Abilities::register();
    264260}
  • trunk/src/wp-includes/abilities/class-wp-settings-abilities.php

    r61600 r61690  
    1 <?php
    2 /**
    3  * Registers core settings abilities.
    4  *
    5  * This is a utility class to encapsulate the registration of settings-related abilities.
    6  * It is not intended to be instantiated or consumed directly by any other code or plugin.
    7  *
    8  * @package WordPress
    9  * @subpackage Abilities_API
    10  * @since 7.0.0
    11  *
    12  * @internal This class is not part of the public API.
    13  * @access private
    14  */
    15 
    16 declare( strict_types=1 );
    17 
    18 /**
    19  * Registers core settings abilities.
    20  *
    21  * @since 7.0.0
    22  * @access private
    23  */
    24 class WP_Settings_Abilities {
    25 
    26     /**
    27      * Available setting groups with show_in_abilities enabled.
    28      *
    29      * @since 7.0.0
    30      * @var string[]
    31      */
    32     private static $available_groups;
    33 
    34     /**
    35      * Dynamic output schema built from registered settings.
    36      *
    37      * @since 7.0.0
    38      * @var array
    39      */
    40     private static $output_schema;
    41 
    42     /**
    43      * Available setting slugs with show_in_abilities enabled.
    44      *
    45      * @since 7.0.0
    46      * @var string[]
    47      */
    48     private static $available_slugs;
    49 
    50     /**
    51      * Registers all settings abilities.
    52      *
    53      * @since 7.0.0
    54      *
    55      * @return void
    56      */
    57     public static function register(): void {
    58         self::init();
    59         self::register_get_settings();
    60     }
    61 
    62     /**
    63      * Initializes shared data for settings abilities.
    64      *
    65      * @since 7.0.0
    66      *
    67      * @return void
    68      */
    69     private static function init(): void {
    70         self::$available_groups = self::get_available_groups();
    71         self::$available_slugs  = self::get_available_slugs();
    72         self::$output_schema    = self::build_output_schema();
    73     }
    74 
    75     /**
    76      * Gets registered settings that have show_in_abilities enabled.
    77      *
    78      * @since 7.0.0
    79      *
    80      * @return array Associative array of option_name => args for allowed settings.
    81      */
    82     private static function get_allowed_settings(): array {
    83         $settings = array();
    84 
    85         foreach ( get_registered_settings() as $option_name => $args ) {
    86             if ( ! empty( $args['show_in_abilities'] ) ) {
    87                 $settings[ $option_name ] = $args;
    88             }
    89         }
    90 
    91         return $settings;
    92     }
    93 
    94     /**
    95      * Gets unique setting groups that have show_in_abilities enabled.
    96      *
    97      * @since 7.0.0
    98      *
    99      * @return string[] List of unique group names.
    100      */
    101     private static function get_available_groups(): array {
    102         $groups = array();
    103 
    104         foreach ( self::get_allowed_settings() as $args ) {
    105             $group = $args['group'] ?? 'general';
    106             if ( ! in_array( $group, $groups, true ) ) {
    107                 $groups[] = $group;
    108             }
    109         }
    110 
    111         sort( $groups );
    112 
    113         return $groups;
    114     }
    115 
    116     /**
    117      * Gets unique setting slugs that have show_in_abilities enabled.
    118      *
    119      * @since 7.0.0
    120      *
    121      * @return string[] List of unique setting slugs.
    122      */
    123     private static function get_available_slugs(): array {
    124         $slugs = array();
    125 
    126         foreach ( self::get_allowed_settings() as $option_name => $args ) {
    127             $slugs[] = $option_name;
    128         }
    129 
    130         sort( $slugs );
    131 
    132         return $slugs;
    133     }
    134 
    135     /**
    136      * Builds a rich output schema from registered settings metadata.
    137      *
    138      * Creates a JSON Schema that documents each setting group and its settings
    139      * with their types, titles, descriptions, defaults, and any additional
    140      * schema properties from show_in_rest.
    141      *
    142      * @since 7.0.0
    143      *
    144      * @return array JSON Schema for the output.
    145      */
    146     private static function build_output_schema(): array {
    147         $group_properties = array();
    148 
    149         foreach ( self::get_allowed_settings() as $option_name => $args ) {
    150             $group = $args['group'] ?? 'general';
    151 
    152             $setting_schema = array(
    153                 'type' => $args['type'] ?? 'string',
    154             );
    155 
    156             if ( ! empty( $args['label'] ) ) {
    157                 $setting_schema['title'] = $args['label'];
    158             }
    159 
    160             if ( ! empty( $args['description'] ) ) {
    161                 $setting_schema['description'] = $args['description'];
    162             } elseif ( ! empty( $args['label'] ) ) {
    163                 $setting_schema['description'] = $args['label'];
    164             }
    165 
    166             if ( ! isset( $group_properties[ $group ] ) ) {
    167                 $group_properties[ $group ] = array(
    168                     'type'                 => 'object',
    169                     'properties'           => array(),
    170                     'additionalProperties' => false,
    171                 );
    172             }
    173 
    174             $group_properties[ $group ]['properties'][ $option_name ] = $setting_schema;
    175         }
    176 
    177         ksort( $group_properties );
    178 
    179         return array(
    180             'type'                 => 'object',
    181             'description'          => __( 'Settings grouped by registration group. Each group contains settings with their current values.' ),
    182             'properties'           => $group_properties,
    183             'additionalProperties' => false,
    184         );
    185     }
    186 
    187     /**
    188      * Registers the core/get-settings ability.
    189      *
    190      * @since 7.0.0
    191      *
    192      * @return void
    193      */
    194     private static function register_get_settings(): void {
    195         wp_register_ability(
    196             'core/get-settings',
    197             array(
    198                 'label'               => __( 'Get Settings' ),
    199                 'description'         => __( 'Returns registered WordPress settings grouped by their registration group. Returns key-value pairs per setting.' ),
    200                 'category'            => 'site',
    201                 'input_schema'        => array(
    202                     'default' => (object) array(),
    203                     'oneOf'   => array(
    204                         // Branch 1: No filter (empty object).
    205                         array(
    206                             'type'                 => 'object',
    207                             'additionalProperties' => false,
    208                             'maxProperties'        => 0,
    209                         ),
    210                         // Branch 2: Filter by group only.
    211                         array(
    212                             'type'                 => 'object',
    213                             'properties'           => array(
    214                                 'group' => array(
    215                                     'type'        => 'string',
    216                                     'description' => __( 'Filter settings by group name.' ),
    217                                     'enum'        => self::$available_groups,
    218                                 ),
    219                             ),
    220                             'required'             => array( 'group' ),
    221                             'additionalProperties' => false,
    222                         ),
    223                         // Branch 3: Filter by slugs only.
    224                         array(
    225                             'type'                 => 'object',
    226                             'properties'           => array(
    227                                 'slugs' => array(
    228                                     'type'        => 'array',
    229                                     'description' => __( 'Filter settings by specific setting slugs.' ),
    230                                     'items'       => array(
    231                                         'type' => 'string',
    232                                         'enum' => self::$available_slugs,
    233                                     ),
    234                                 ),
    235                             ),
    236                             'required'             => array( 'slugs' ),
    237                             'additionalProperties' => false,
    238                         ),
    239                     ),
    240                 ),
    241                 'output_schema'       => self::$output_schema,
    242                 'execute_callback'    => array( __CLASS__, 'execute_get_settings' ),
    243                 'permission_callback' => array( __CLASS__, 'check_manage_options' ),
    244                 'meta'                => array(
    245                     'annotations'  => array(
    246                         'readonly'    => true,
    247                         'destructive' => false,
    248                         'idempotent'  => true,
    249                     ),
    250                     'show_in_rest' => true,
    251                 ),
    252             )
    253         );
    254     }
    255 
    256     /**
    257      * Permission callback for settings abilities.
    258      *
    259      * @since 7.0.0
    260      *
    261      * @return bool True if the current user can manage options, false otherwise.
    262      */
    263     public static function check_manage_options(): bool {
    264         return current_user_can( 'manage_options' );
    265     }
    266 
    267     /**
    268      * Execute callback for core/get-settings ability.
    269      *
    270      * Retrieves all registered settings that are exposed through the Abilities API,
    271      * grouped by their registration group.
    272      *
    273      * @since 7.0.0
    274      *
    275      * @param array $input {
    276      *     Optional. Input parameters.
    277      *
    278      *     @type string   $group Optional. Filter settings by group name. Cannot be used with slugs.
    279      *     @type string[] $slugs Optional. Filter settings by specific setting slugs. Cannot be used with group.
    280      * }
    281      * @return array Settings grouped by registration group.
    282      */
    283     public static function execute_get_settings( $input = array() ): array {
    284         $input        = is_array( $input ) ? $input : array();
    285         $filter_group = ! empty( $input['group'] ) ? $input['group'] : null;
    286         $filter_slugs = ! empty( $input['slugs'] ) ? $input['slugs'] : null;
    287 
    288         $settings_by_group = array();
    289 
    290         foreach ( self::get_allowed_settings() as $option_name => $args ) {
    291             $group = $args['group'] ?? 'general';
    292 
    293             if ( $filter_group && $group !== $filter_group ) {
    294                 continue;
    295             }
    296 
    297             if ( $filter_slugs && ! in_array( $option_name, $filter_slugs, true ) ) {
    298                 continue;
    299             }
    300 
    301             $default = $args['default'] ?? null;
    302 
    303             $value = get_option( $option_name, $default );
    304             $value = self::cast_value( $value, $args['type'] ?? 'string' );
    305 
    306             if ( ! isset( $settings_by_group[ $group ] ) ) {
    307                 $settings_by_group[ $group ] = array();
    308             }
    309 
    310             $settings_by_group[ $group ][ $option_name ] = $value;
    311         }
    312 
    313         ksort( $settings_by_group );
    314 
    315         return $settings_by_group;
    316     }
    317 
    318     /**
    319      * Casts a value to the appropriate type based on the setting's registered type.
    320      *
    321      * @since 7.0.0
    322      *
    323      * @param mixed  $value The value to cast.
    324      * @param string $type  The registered type (string, boolean, integer, number, array, object).
    325      * @return string|bool|int|float|array The cast value.
    326      */
    327     private static function cast_value( $value, string $type ) {
    328         switch ( $type ) {
    329             case 'boolean':
    330                 return (bool) $value;
    331             case 'integer':
    332                 return (int) $value;
    333             case 'number':
    334                 return (float) $value;
    335             case 'array':
    336             case 'object':
    337                 return is_array( $value ) ? $value : array();
    338             case 'string':
    339             default:
    340                 return (string) $value;
    341         }
    342     }
    343 }
  • trunk/src/wp-includes/option.php

    r61689 r61690  
    27442744        'blogname',
    27452745        array(
    2746             'show_in_rest'      => array(
     2746            'show_in_rest' => array(
    27472747                'name' => 'title',
    27482748            ),
    2749             'show_in_abilities' => true,
    2750             'type'              => 'string',
    2751             'label'             => __( 'Title' ),
    2752             'description'       => __( 'Site title.' ),
     2749            'type'         => 'string',
     2750            'label'        => __( 'Title' ),
     2751            'description'  => __( 'Site title.' ),
    27532752        )
    27542753    );
     
    27582757        'blogdescription',
    27592758        array(
    2760             'show_in_rest'      => array(
     2759            'show_in_rest' => array(
    27612760                'name' => 'description',
    27622761            ),
    2763             'show_in_abilities' => true,
    2764             'type'              => 'string',
    2765             'label'             => __( 'Tagline' ),
    2766             'description'       => __( 'Site tagline.' ),
     2762            'type'         => 'string',
     2763            'label'        => __( 'Tagline' ),
     2764            'description'  => __( 'Site tagline.' ),
    27672765        )
    27682766    );
     
    27732771            'siteurl',
    27742772            array(
    2775                 'show_in_rest'      => array(
     2773                'show_in_rest' => array(
    27762774                    'name'   => 'url',
    27772775                    'schema' => array(
     
    27792777                    ),
    27802778                ),
    2781                 'show_in_abilities' => true,
    2782                 'type'              => 'string',
    2783                 'description'       => __( 'Site URL.' ),
     2779                'type'         => 'string',
     2780                'description'  => __( 'Site URL.' ),
    27842781            )
    27852782        );
     
    27912788            'admin_email',
    27922789            array(
    2793                 'show_in_rest'      => array(
     2790                'show_in_rest' => array(
    27942791                    'name'   => 'email',
    27952792                    'schema' => array(
     
    27972794                    ),
    27982795                ),
    2799                 'show_in_abilities' => true,
    2800                 'type'              => 'string',
    2801                 'description'       => __( 'This address is used for admin purposes, like new user notification.' ),
     2796                'type'         => 'string',
     2797                'description'  => __( 'This address is used for admin purposes, like new user notification.' ),
    28022798            )
    28032799        );
     
    28082804        'timezone_string',
    28092805        array(
    2810             'show_in_rest'      => array(
     2806            'show_in_rest' => array(
    28112807                'name' => 'timezone',
    28122808            ),
    2813             'show_in_abilities' => true,
    2814             'type'              => 'string',
    2815             'description'       => __( 'A city in the same timezone as you.' ),
     2809            'type'         => 'string',
     2810            'description'  => __( 'A city in the same timezone as you.' ),
    28162811        )
    28172812    );
     
    28212816        'date_format',
    28222817        array(
    2823             'show_in_rest'      => true,
    2824             'show_in_abilities' => true,
    2825             'type'              => 'string',
    2826             'description'       => __( 'A date format for all date strings.' ),
     2818            'show_in_rest' => true,
     2819            'type'         => 'string',
     2820            'description'  => __( 'A date format for all date strings.' ),
    28272821        )
    28282822    );
     
    28322826        'time_format',
    28332827        array(
    2834             'show_in_rest'      => true,
    2835             'show_in_abilities' => true,
    2836             'type'              => 'string',
    2837             'description'       => __( 'A time format for all time strings.' ),
     2828            'show_in_rest' => true,
     2829            'type'         => 'string',
     2830            'description'  => __( 'A time format for all time strings.' ),
    28382831        )
    28392832    );
     
    28432836        'start_of_week',
    28442837        array(
    2845             'show_in_rest'      => true,
    2846             'show_in_abilities' => true,
    2847             'type'              => 'integer',
    2848             'description'       => __( 'A day number of the week that the week should start on.' ),
     2838            'show_in_rest' => true,
     2839            'type'         => 'integer',
     2840            'description'  => __( 'A day number of the week that the week should start on.' ),
    28492841        )
    28502842    );
     
    28542846        'WPLANG',
    28552847        array(
    2856             'show_in_rest'      => array(
     2848            'show_in_rest' => array(
    28572849                'name' => 'language',
    28582850            ),
    2859             'show_in_abilities' => true,
    2860             'type'              => 'string',
    2861             'description'       => __( 'WordPress locale code.' ),
    2862             'default'           => 'en_US',
     2851            'type'         => 'string',
     2852            'description'  => __( 'WordPress locale code.' ),
     2853            'default'      => 'en_US',
    28632854        )
    28642855    );
     
    28682859        'use_smilies',
    28692860        array(
    2870             'show_in_rest'      => true,
    2871             'show_in_abilities' => true,
    2872             'type'              => 'boolean',
    2873             'description'       => __( 'Convert emoticons like :-) and :-P to graphics on display.' ),
    2874             'default'           => true,
     2861            'show_in_rest' => true,
     2862            'type'         => 'boolean',
     2863            'description'  => __( 'Convert emoticons like :-) and :-P to graphics on display.' ),
     2864            'default'      => true,
    28752865        )
    28762866    );
     
    28802870        'default_category',
    28812871        array(
    2882             'show_in_rest'      => true,
    2883             'show_in_abilities' => true,
    2884             'type'              => 'integer',
    2885             'description'       => __( 'Default post category.' ),
     2872            'show_in_rest' => true,
     2873            'type'         => 'integer',
     2874            'description'  => __( 'Default post category.' ),
    28862875        )
    28872876    );
     
    28912880        'default_post_format',
    28922881        array(
    2893             'show_in_rest'      => true,
    2894             'show_in_abilities' => true,
    2895             'type'              => 'string',
    2896             'description'       => __( 'Default post format.' ),
     2882            'show_in_rest' => true,
     2883            'type'         => 'string',
     2884            'description'  => __( 'Default post format.' ),
    28972885        )
    28982886    );
     
    29142902        'posts_per_page',
    29152903        array(
    2916             'show_in_rest'      => true,
    2917             'show_in_abilities' => true,
    2918             'type'              => 'integer',
    2919             'label'             => __( 'Maximum posts per page' ),
    2920             'description'       => __( 'Blog pages show at most.' ),
    2921             'default'           => 10,
     2904            'show_in_rest' => true,
     2905            'type'         => 'integer',
     2906            'label'        => __( 'Maximum posts per page' ),
     2907            'description'  => __( 'Blog pages show at most.' ),
     2908            'default'      => 10,
    29222909        )
    29232910    );
     
    29272914        'show_on_front',
    29282915        array(
    2929             'show_in_rest'      => true,
    2930             'show_in_abilities' => true,
    2931             'type'              => 'string',
    2932             'label'             => __( 'Show on front' ),
    2933             'description'       => __( 'What to show on the front page' ),
     2916            'show_in_rest' => true,
     2917            'type'         => 'string',
     2918            'label'        => __( 'Show on front' ),
     2919            'description'  => __( 'What to show on the front page' ),
    29342920        )
    29352921    );
     
    29392925        'page_on_front',
    29402926        array(
    2941             'show_in_rest'      => true,
    2942             'show_in_abilities' => true,
    2943             'type'              => 'integer',
    2944             'label'             => __( 'Page on front' ),
    2945             'description'       => __( 'The ID of the page that should be displayed on the front page' ),
     2927            'show_in_rest' => true,
     2928            'type'         => 'integer',
     2929            'label'        => __( 'Page on front' ),
     2930            'description'  => __( 'The ID of the page that should be displayed on the front page' ),
    29462931        )
    29472932    );
     
    29512936        'page_for_posts',
    29522937        array(
    2953             'show_in_rest'      => true,
    2954             'show_in_abilities' => true,
    2955             'type'              => 'integer',
    2956             'description'       => __( 'The ID of the page that should display the latest posts' ),
     2938            'show_in_rest' => true,
     2939            'type'         => 'integer',
     2940            'description'  => __( 'The ID of the page that should display the latest posts' ),
    29572941        )
    29582942    );
     
    29622946        'default_ping_status',
    29632947        array(
    2964             'show_in_rest'      => array(
     2948            'show_in_rest' => array(
    29652949                'schema' => array(
    29662950                    'enum' => array( 'open', 'closed' ),
    29672951                ),
    29682952            ),
    2969             'show_in_abilities' => true,
    2970             'type'              => 'string',
    2971             'description'       => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles.' ),
     2953            'type'         => 'string',
     2954            'description'  => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles.' ),
    29722955        )
    29732956    );
     
    29772960        'default_comment_status',
    29782961        array(
    2979             'show_in_rest'      => array(
     2962            'show_in_rest' => array(
    29802963                'schema' => array(
    29812964                    'enum' => array( 'open', 'closed' ),
    29822965                ),
    29832966            ),
    2984             'show_in_abilities' => true,
    2985             'type'              => 'string',
    2986             'label'             => __( 'Allow comments on new posts' ),
    2987             'description'       => __( 'Allow people to submit comments on new posts.' ),
     2967            'type'         => 'string',
     2968            'label'        => __( 'Allow comments on new posts' ),
     2969            'description'  => __( 'Allow people to submit comments on new posts.' ),
    29882970        )
    29892971    );
     
    30162998 *     @type string     $description       A description of the data attached to this setting.
    30172999 *     @type callable   $sanitize_callback A callback function that sanitizes the option's value.
    3018  *     @type bool|array $show_in_rest        Whether data associated with this setting should be included in the REST API.
    3019  *                                           When registering complex settings, this argument may optionally be an
    3020  *                                           array with a 'schema' key.
    3021  *     @type bool       $show_in_abilities  Whether this setting should be exposed through the Abilities API.
    3022  *                                           Default false.
    3023  *     @type mixed      $default            Default value when calling `get_option()`.
     3000 *     @type bool|array $show_in_rest      Whether data associated with this setting should be included in the REST API.
     3001 *                                         When registering complex settings, this argument may optionally be an
     3002 *                                         array with a 'schema' key.
     3003 *     @type mixed      $default           Default value when calling `get_option()`.
    30243004 * }
    30253005 */
     
    30343014
    30353015    $defaults = array(
    3036         'type'               => 'string',
    3037         'group'              => $option_group,
    3038         'label'              => '',
    3039         'description'        => '',
    3040         'sanitize_callback'  => null,
    3041         'show_in_rest'       => false,
    3042         'show_in_abilities'  => false,
     3016        'type'              => 'string',
     3017        'group'             => $option_group,
     3018        'label'             => '',
     3019        'description'       => '',
     3020        'sanitize_callback' => null,
     3021        'show_in_rest'      => false,
    30433022    );
    30443023
  • trunk/tests/phpunit/tests/rest-api/wpRestAbilitiesSettingsController.php

    r61600 r61690  
    1 <?php
    2 
    3 declare( strict_types=1 );
    4 
    5 /**
    6  * Tests for the core/get-settings ability via REST API.
    7  *
    8  * @covers WP_Settings_Abilities
    9  *
    10  * @group abilities-api
    11  * @group rest-api
    12  */
    13 class Tests_REST_API_WpRestAbilitiesSettingsController extends WP_UnitTestCase {
    14 
    15     /**
    16      * REST Server instance.
    17      *
    18      * @var WP_REST_Server
    19      */
    20     protected $server;
    21 
    22     /**
    23      * Administrator user ID.
    24      *
    25      * @var int
    26      */
    27     protected static $admin_id;
    28 
    29     /**
    30      * Subscriber user ID.
    31      *
    32      * @var int
    33      */
    34     protected static $subscriber_id;
    35 
    36     /**
    37      * Set up before class.
    38      */
    39     public static function set_up_before_class(): void {
    40         parent::set_up_before_class();
    41 
    42         self::$admin_id      = self::factory()->user->create( array( 'role' => 'administrator' ) );
    43         self::$subscriber_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
    44 
    45         // Register initial settings first so abilities can build schemas.
    46         register_initial_settings();
    47 
    48         // Ensure core abilities are registered for these tests.
    49         remove_action( 'wp_abilities_api_categories_init', '_unhook_core_ability_categories_registration', 1 );
    50         remove_action( 'wp_abilities_api_init', '_unhook_core_abilities_registration', 1 );
    51 
    52         add_action( 'wp_abilities_api_categories_init', 'wp_register_core_ability_categories' );
    53         add_action( 'wp_abilities_api_init', 'wp_register_core_abilities' );
    54         do_action( 'wp_abilities_api_categories_init' );
    55         do_action( 'wp_abilities_api_init' );
    56     }
    57 
    58     /**
    59      * Tear down after class.
    60      */
    61     public static function tear_down_after_class(): void {
    62         // Re-add the unhook functions for subsequent tests.
    63         add_action( 'wp_abilities_api_categories_init', '_unhook_core_ability_categories_registration', 1 );
    64         add_action( 'wp_abilities_api_init', '_unhook_core_abilities_registration', 1 );
    65 
    66         // Remove the core abilities and their categories.
    67         foreach ( wp_get_abilities() as $ability ) {
    68             wp_unregister_ability( $ability->get_name() );
    69         }
    70         foreach ( wp_get_ability_categories() as $ability_category ) {
    71             wp_unregister_ability_category( $ability_category->get_slug() );
    72         }
    73 
    74         parent::tear_down_after_class();
    75     }
    76 
    77     /**
    78      * Set up before each test.
    79      */
    80     public function set_up(): void {
    81         parent::set_up();
    82 
    83         global $wp_rest_server;
    84         $wp_rest_server = new WP_REST_Server();
    85         $this->server   = $wp_rest_server;
    86 
    87         do_action( 'rest_api_init' );
    88 
    89         wp_set_current_user( self::$admin_id );
    90     }
    91 
    92     /**
    93      * Tear down after each test.
    94      */
    95     public function tear_down(): void {
    96         global $wp_rest_server;
    97         $wp_rest_server = null;
    98 
    99         parent::tear_down();
    100     }
    101 
    102     /**
    103      * Tests that unauthenticated users cannot access the get-settings ability.
    104      *
    105      * @ticket 64605
    106      */
    107     public function test_core_get_settings_requires_authentication(): void {
    108         wp_set_current_user( 0 );
    109 
    110         $request  = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' );
    111         $response = $this->server->dispatch( $request );
    112 
    113         $this->assertSame( 401, $response->get_status() );
    114     }
    115 
    116     /**
    117      * Tests that subscribers cannot access the get-settings ability.
    118      *
    119      * @ticket 64605
    120      */
    121     public function test_core_get_settings_requires_manage_options_capability(): void {
    122         wp_set_current_user( self::$subscriber_id );
    123 
    124         $request  = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' );
    125         $response = $this->server->dispatch( $request );
    126 
    127         $this->assertSame( 403, $response->get_status() );
    128     }
    129 
    130     /**
    131      * Tests that administrators can access the get-settings ability.
    132      *
    133      * @ticket 64605
    134      */
    135     public function test_core_get_settings_allows_administrators(): void {
    136         $request  = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' );
    137         $response = $this->server->dispatch( $request );
    138 
    139         $this->assertSame( 200, $response->get_status() );
    140     }
    141 
    142     /**
    143      * Tests that the get-settings ability returns settings grouped by registration group.
    144      *
    145      * @ticket 64605
    146      */
    147     public function test_core_get_settings_returns_grouped_settings(): void {
    148         $request  = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' );
    149         $response = $this->server->dispatch( $request );
    150 
    151         $this->assertSame( 200, $response->get_status() );
    152 
    153         $data = $response->get_data();
    154 
    155         $this->assertIsArray( $data );
    156         $this->assertArrayHasKey( 'general', $data );
    157         $this->assertArrayHasKey( 'blogname', $data['general'] );
    158         $this->assertArrayHasKey( 'blogdescription', $data['general'] );
    159     }
    160 
    161     /**
    162      * Tests that the get-settings ability can filter by group.
    163      *
    164      * @ticket 64605
    165      */
    166     public function test_core_get_settings_filters_by_group(): void {
    167         $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' );
    168         $request->set_query_params(
    169             array(
    170                 'input' => array(
    171                     'group' => 'general',
    172                 ),
    173             )
    174         );
    175         $response = $this->server->dispatch( $request );
    176 
    177         $this->assertSame( 200, $response->get_status() );
    178 
    179         $data = $response->get_data();
    180 
    181         $this->assertIsArray( $data );
    182         $this->assertCount( 1, $data );
    183         $this->assertArrayHasKey( 'general', $data );
    184     }
    185 
    186     /**
    187      * Tests that the get-settings ability can filter by specific slugs.
    188      *
    189      * @ticket 64605
    190      */
    191     public function test_core_get_settings_filters_by_slugs(): void {
    192         $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' );
    193         $request->set_query_params(
    194             array(
    195                 'input' => array(
    196                     'slugs' => array( 'blogname', 'blogdescription' ),
    197                 ),
    198             )
    199         );
    200         $response = $this->server->dispatch( $request );
    201 
    202         $this->assertSame( 200, $response->get_status() );
    203 
    204         $data = $response->get_data();
    205 
    206         $this->assertIsArray( $data );
    207         $this->assertArrayHasKey( 'general', $data );
    208         $this->assertCount( 2, $data['general'] );
    209         $this->assertArrayHasKey( 'blogname', $data['general'] );
    210         $this->assertArrayHasKey( 'blogdescription', $data['general'] );
    211     }
    212 
    213     /**
    214      * Tests that settings without show_in_abilities are excluded.
    215      *
    216      * @ticket 64605
    217      */
    218     public function test_core_get_settings_excludes_settings_without_show_in_abilities(): void {
    219         register_setting(
    220             'general',
    221             'test_setting_excluded',
    222             array(
    223                 'type'              => 'string',
    224                 'default'           => 'test_value',
    225                 'show_in_abilities' => false,
    226             )
    227         );
    228         update_option( 'test_setting_excluded', 'test_value' );
    229 
    230         $request  = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' );
    231         $response = $this->server->dispatch( $request );
    232 
    233         $this->assertSame( 200, $response->get_status() );
    234 
    235         $data = $response->get_data();
    236 
    237         $this->assertArrayNotHasKey( 'test_setting_excluded', $data['general'] ?? array() );
    238 
    239         unregister_setting( 'general', 'test_setting_excluded' );
    240         delete_option( 'test_setting_excluded' );
    241     }
    242 
    243     /**
    244      * Tests that core settings with show_in_abilities are included.
    245      *
    246      * @ticket 64605
    247      */
    248     public function test_core_get_settings_includes_settings_with_show_in_abilities(): void {
    249         $request  = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' );
    250         $response = $this->server->dispatch( $request );
    251 
    252         $this->assertSame( 200, $response->get_status() );
    253 
    254         $data = $response->get_data();
    255 
    256         // blogname has show_in_abilities => true in register_initial_settings().
    257         $this->assertArrayHasKey( 'general', $data );
    258         $this->assertArrayHasKey( 'blogname', $data['general'] );
    259 
    260         // use_smilies has show_in_abilities => true.
    261         $this->assertArrayHasKey( 'writing', $data );
    262         $this->assertArrayHasKey( 'use_smilies', $data['writing'] );
    263     }
    264 
    265     /**
    266      * Tests that boolean settings are cast to actual booleans.
    267      *
    268      * @ticket 64605
    269      */
    270     public function test_core_get_settings_casts_boolean_values(): void {
    271         $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' );
    272         $request->set_query_params(
    273             array(
    274                 'input' => array(
    275                     'slugs' => array( 'use_smilies' ),
    276                 ),
    277             )
    278         );
    279         $response = $this->server->dispatch( $request );
    280 
    281         $this->assertSame( 200, $response->get_status() );
    282 
    283         $data = $response->get_data();
    284 
    285         $this->assertArrayHasKey( 'writing', $data );
    286         $this->assertArrayHasKey( 'use_smilies', $data['writing'] );
    287         $this->assertIsBool( $data['writing']['use_smilies'] );
    288     }
    289 
    290     /**
    291      * Tests that integer settings are cast to actual integers.
    292      *
    293      * @ticket 64605
    294      */
    295     public function test_core_get_settings_casts_integer_values(): void {
    296         $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' );
    297         $request->set_query_params(
    298             array(
    299                 'input' => array(
    300                     'slugs' => array( 'start_of_week' ),
    301                 ),
    302             )
    303         );
    304         $response = $this->server->dispatch( $request );
    305 
    306         $this->assertSame( 200, $response->get_status() );
    307 
    308         $data = $response->get_data();
    309 
    310         $this->assertArrayHasKey( 'general', $data );
    311         $this->assertArrayHasKey( 'start_of_week', $data['general'] );
    312         $this->assertIsInt( $data['general']['start_of_week'] );
    313     }
    314 
    315     /**
    316      * Tests that the get-settings ability requires GET method (read-only).
    317      *
    318      * @ticket 64605
    319      */
    320     public function test_core_get_settings_requires_get_method(): void {
    321         $request = new WP_REST_Request( 'POST', '/wp-abilities/v1/abilities/core/get-settings/run' );
    322         $request->set_header( 'Content-Type', 'application/json' );
    323         $request->set_body( wp_json_encode( array( 'input' => array() ) ) );
    324         $response = $this->server->dispatch( $request );
    325 
    326         $this->assertSame( 405, $response->get_status() );
    327 
    328         $data = $response->get_data();
    329         $this->assertSame( 'rest_ability_invalid_method', $data['code'] );
    330     }
    331 
    332     /**
    333      * Tests that the get-settings ability returns correct values.
    334      *
    335      * @ticket 64605
    336      */
    337     public function test_core_get_settings_returns_correct_values(): void {
    338         update_option( 'blogname', 'Test Site Name' );
    339 
    340         $request = new WP_REST_Request( 'GET', '/wp-abilities/v1/abilities/core/get-settings/run' );
    341         $request->set_query_params(
    342             array(
    343                 'input' => array(
    344                     'slugs' => array( 'blogname' ),
    345                 ),
    346             )
    347         );
    348         $response = $this->server->dispatch( $request );
    349 
    350         $this->assertSame( 200, $response->get_status() );
    351 
    352         $data = $response->get_data();
    353 
    354         $this->assertSame( 'Test Site Name', $data['general']['blogname'] );
    355     }
    356 }
Note: See TracChangeset for help on using the changeset viewer.