Changeset 61690
- Timestamp:
- 02/19/2026 10:55:41 AM (3 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
-
src/wp-includes/abilities.php (modified) (2 diffs)
-
src/wp-includes/abilities/class-wp-settings-abilities.php (modified) (1 diff)
-
src/wp-includes/option.php (modified) (22 diffs)
-
tests/phpunit/tests/rest-api/wpRestAbilitiesSettingsController.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/abilities.php
r61600 r61690 9 9 10 10 declare( strict_types = 1 ); 11 12 require_once __DIR__ . '/abilities/class-wp-settings-abilities.php';13 11 14 12 /** … … 260 258 ) 261 259 ); 262 263 WP_Settings_Abilities::register();264 260 } -
trunk/src/wp-includes/abilities/class-wp-settings-abilities.php
r61600 r61690 1 <?php2 /**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 WordPress9 * @subpackage Abilities_API10 * @since 7.0.011 *12 * @internal This class is not part of the public API.13 * @access private14 */15 16 declare( strict_types=1 );17 18 /**19 * Registers core settings abilities.20 *21 * @since 7.0.022 * @access private23 */24 class WP_Settings_Abilities {25 26 /**27 * Available setting groups with show_in_abilities enabled.28 *29 * @since 7.0.030 * @var string[]31 */32 private static $available_groups;33 34 /**35 * Dynamic output schema built from registered settings.36 *37 * @since 7.0.038 * @var array39 */40 private static $output_schema;41 42 /**43 * Available setting slugs with show_in_abilities enabled.44 *45 * @since 7.0.046 * @var string[]47 */48 private static $available_slugs;49 50 /**51 * Registers all settings abilities.52 *53 * @since 7.0.054 *55 * @return void56 */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.066 *67 * @return void68 */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.079 *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.098 *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.0120 *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 settings139 * with their types, titles, descriptions, defaults, and any additional140 * schema properties from show_in_rest.141 *142 * @since 7.0.0143 *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.0191 *192 * @return void193 */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.0260 *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.0274 *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.0322 *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 2744 2744 'blogname', 2745 2745 array( 2746 'show_in_rest' => array(2746 'show_in_rest' => array( 2747 2747 'name' => 'title', 2748 2748 ), 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.' ), 2753 2752 ) 2754 2753 ); … … 2758 2757 'blogdescription', 2759 2758 array( 2760 'show_in_rest' => array(2759 'show_in_rest' => array( 2761 2760 'name' => 'description', 2762 2761 ), 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.' ), 2767 2765 ) 2768 2766 ); … … 2773 2771 'siteurl', 2774 2772 array( 2775 'show_in_rest' => array(2773 'show_in_rest' => array( 2776 2774 'name' => 'url', 2777 2775 'schema' => array( … … 2779 2777 ), 2780 2778 ), 2781 'show_in_abilities' => true, 2782 'type' => 'string', 2783 'description' => __( 'Site URL.' ), 2779 'type' => 'string', 2780 'description' => __( 'Site URL.' ), 2784 2781 ) 2785 2782 ); … … 2791 2788 'admin_email', 2792 2789 array( 2793 'show_in_rest' => array(2790 'show_in_rest' => array( 2794 2791 'name' => 'email', 2795 2792 'schema' => array( … … 2797 2794 ), 2798 2795 ), 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.' ), 2802 2798 ) 2803 2799 ); … … 2808 2804 'timezone_string', 2809 2805 array( 2810 'show_in_rest' => array(2806 'show_in_rest' => array( 2811 2807 'name' => 'timezone', 2812 2808 ), 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.' ), 2816 2811 ) 2817 2812 ); … … 2821 2816 'date_format', 2822 2817 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.' ), 2827 2821 ) 2828 2822 ); … … 2832 2826 'time_format', 2833 2827 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.' ), 2838 2831 ) 2839 2832 ); … … 2843 2836 'start_of_week', 2844 2837 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.' ), 2849 2841 ) 2850 2842 ); … … 2854 2846 'WPLANG', 2855 2847 array( 2856 'show_in_rest' => array(2848 'show_in_rest' => array( 2857 2849 'name' => 'language', 2858 2850 ), 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', 2863 2854 ) 2864 2855 ); … … 2868 2859 'use_smilies', 2869 2860 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, 2875 2865 ) 2876 2866 ); … … 2880 2870 'default_category', 2881 2871 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.' ), 2886 2875 ) 2887 2876 ); … … 2891 2880 'default_post_format', 2892 2881 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.' ), 2897 2885 ) 2898 2886 ); … … 2914 2902 'posts_per_page', 2915 2903 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, 2922 2909 ) 2923 2910 ); … … 2927 2914 'show_on_front', 2928 2915 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' ), 2934 2920 ) 2935 2921 ); … … 2939 2925 'page_on_front', 2940 2926 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' ), 2946 2931 ) 2947 2932 ); … … 2951 2936 'page_for_posts', 2952 2937 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' ), 2957 2941 ) 2958 2942 ); … … 2962 2946 'default_ping_status', 2963 2947 array( 2964 'show_in_rest' => array(2948 'show_in_rest' => array( 2965 2949 'schema' => array( 2966 2950 'enum' => array( 'open', 'closed' ), 2967 2951 ), 2968 2952 ), 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.' ), 2972 2955 ) 2973 2956 ); … … 2977 2960 'default_comment_status', 2978 2961 array( 2979 'show_in_rest' => array(2962 'show_in_rest' => array( 2980 2963 'schema' => array( 2981 2964 'enum' => array( 'open', 'closed' ), 2982 2965 ), 2983 2966 ), 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.' ), 2988 2970 ) 2989 2971 ); … … 3016 2998 * @type string $description A description of the data attached to this setting. 3017 2999 * @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()`. 3024 3004 * } 3025 3005 */ … … 3034 3014 3035 3015 $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, 3043 3022 ); 3044 3023 -
trunk/tests/phpunit/tests/rest-api/wpRestAbilitiesSettingsController.php
r61600 r61690 1 <?php2 3 declare( strict_types=1 );4 5 /**6 * Tests for the core/get-settings ability via REST API.7 *8 * @covers WP_Settings_Abilities9 *10 * @group abilities-api11 * @group rest-api12 */13 class Tests_REST_API_WpRestAbilitiesSettingsController extends WP_UnitTestCase {14 15 /**16 * REST Server instance.17 *18 * @var WP_REST_Server19 */20 protected $server;21 22 /**23 * Administrator user ID.24 *25 * @var int26 */27 protected static $admin_id;28 29 /**30 * Subscriber user ID.31 *32 * @var int33 */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 64605106 */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 64605120 */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 64605134 */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 64605146 */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 64605165 */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 64605190 */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 64605217 */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 64605247 */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 64605269 */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 64605294 */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 64605319 */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 64605336 */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.