From 6a8460b033e1b99d1ef46e18092a75f937a804c7 Mon Sep 17 00:00:00 2001 From: Ella Date: Wed, 29 May 2024 08:51:04 +0000 Subject: [PATCH] Options: Add 'label' argument to register_setting. The 'label' will displayed to users when editing core or custom settings via block editors. It avoids hardcoding Settings labels and improves extensibility. Backports https://github.com/WordPress/gutenberg/pull/59243. Props mamaduka, timothyblynjacobs, ellatrix. Fixes #61023. git-svn-id: https://develop.svn.wordpress.org/trunk@58230 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/option.php | 9 +++++++++ .../class-wp-rest-settings-controller.php | 1 + .../rest-api/rest-settings-controller.php | 15 ++++++++++++++ tests/qunit/fixtures/wp-api-generated.js | 20 +++++++++++++++++++ 4 files changed, 45 insertions(+) diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php index 83f282d5c059f..7cdf6ca4f6005 100644 --- a/src/wp-includes/option.php +++ b/src/wp-includes/option.php @@ -2659,6 +2659,7 @@ function register_initial_settings() { 'name' => 'title', ), 'type' => 'string', + 'label' => __( 'Title' ), 'description' => __( 'Site title.' ), ) ); @@ -2671,6 +2672,7 @@ function register_initial_settings() { 'name' => 'description', ), 'type' => 'string', + 'label' => __( 'Tagline' ), 'description' => __( 'Site tagline.' ), ) ); @@ -2801,6 +2803,7 @@ function register_initial_settings() { array( 'show_in_rest' => true, 'type' => 'integer', + 'label' => __( 'Maximum posts per page' ), 'description' => __( 'Blog pages show at most.' ), 'default' => 10, ) @@ -2812,6 +2815,7 @@ function register_initial_settings() { array( 'show_in_rest' => true, 'type' => 'string', + 'label' => __( 'Show on front' ), 'description' => __( 'What to show on the front page' ), ) ); @@ -2822,6 +2826,7 @@ function register_initial_settings() { array( 'show_in_rest' => true, 'type' => 'integer', + 'label' => __( 'Page on front' ), 'description' => __( 'The ID of the page that should be displayed on the front page' ), ) ); @@ -2860,6 +2865,7 @@ function register_initial_settings() { ), ), 'type' => 'string', + 'label' => __( 'Allow comments on new posts' ), 'description' => __( 'Allow people to submit comments on new posts.' ), ) ); @@ -2874,6 +2880,7 @@ function register_initial_settings() { * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`. * @since 5.5.0 `$new_whitelist_options` was renamed to `$new_allowed_options`. * Please consider writing more inclusive code. + * @since 6.6.0 Added the `label` argument. * * @global array $new_allowed_options * @global array $wp_registered_settings @@ -2887,6 +2894,7 @@ function register_initial_settings() { * * @type string $type The type of data associated with this setting. * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. + * @type string $label A label of the data attached to this setting. * @type string $description A description of the data attached to this setting. * @type callable $sanitize_callback A callback function that sanitizes the option's value. * @type bool|array $show_in_rest Whether data associated with this setting should be included in the REST API. @@ -2907,6 +2915,7 @@ function register_setting( $option_group, $option_name, $args = array() ) { $defaults = array( 'type' => 'string', 'group' => $option_group, + 'label' => '', 'description' => '', 'sanitize_callback' => null, 'show_in_rest' => false, diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php index 365afd39d2fa8..004f5851a271f 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php @@ -237,6 +237,7 @@ protected function get_registered_options() { $default_schema = array( 'type' => empty( $args['type'] ) ? null : $args['type'], + 'title' => empty( $args['label'] ) ? '' : $args['label'], 'description' => empty( $args['description'] ) ? '' : $args['description'], 'default' => isset( $args['default'] ) ? $args['default'] : null, ); diff --git a/tests/phpunit/tests/rest-api/rest-settings-controller.php b/tests/phpunit/tests/rest-api/rest-settings-controller.php index d9181e1af8c16..e8f90b53f20f1 100644 --- a/tests/phpunit/tests/rest-api/rest-settings-controller.php +++ b/tests/phpunit/tests/rest-api/rest-settings-controller.php @@ -781,4 +781,19 @@ public function test_register_setting_with_custom_additional_properties_value() $this->assertSame( 2, $response->data['mycustomsetting']['test2'] ); $this->assertSame( 3, $response->data['mycustomsetting']['test3'] ); } + + /** + * @ticket 61023 + */ + public function test_provides_setting_metadata_in_schema() { + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/settings' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $title = $data['schema']['properties']['title']; + + $this->assertSame( 'string', $title['type'] ); + $this->assertSame( 'Title', $title['title'] ); + $this->assertSame( 'Site title.', $title['description'] ); + $this->assertSame( null, $title['default'] ); + } } diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index fec23e0f3fa6b..4494ae108891b 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -10738,88 +10738,105 @@ mockedApiResponse.Schema = { ], "args": { "title": { + "title": "Title", "description": "Site title.", "type": "string", "required": false }, "description": { + "title": "Tagline", "description": "Site tagline.", "type": "string", "required": false }, "url": { + "title": "", "description": "Site URL.", "type": "string", "format": "uri", "required": false }, "email": { + "title": "", "description": "This address is used for admin purposes, like new user notification.", "type": "string", "format": "email", "required": false }, "timezone": { + "title": "", "description": "A city in the same timezone as you.", "type": "string", "required": false }, "date_format": { + "title": "", "description": "A date format for all date strings.", "type": "string", "required": false }, "time_format": { + "title": "", "description": "A time format for all time strings.", "type": "string", "required": false }, "start_of_week": { + "title": "", "description": "A day number of the week that the week should start on.", "type": "integer", "required": false }, "language": { + "title": "", "description": "WordPress locale code.", "type": "string", "required": false }, "use_smilies": { + "title": "", "description": "Convert emoticons like :-) and :-P to graphics on display.", "type": "boolean", "required": false }, "default_category": { + "title": "", "description": "Default post category.", "type": "integer", "required": false }, "default_post_format": { + "title": "", "description": "Default post format.", "type": "string", "required": false }, "posts_per_page": { + "title": "Maximum posts per page", "description": "Blog pages show at most.", "type": "integer", "required": false }, "show_on_front": { + "title": "Show on front", "description": "What to show on the front page", "type": "string", "required": false }, "page_on_front": { + "title": "Page on front", "description": "The ID of the page that should be displayed on the front page", "type": "integer", "required": false }, "page_for_posts": { + "title": "", "description": "The ID of the page that should display the latest posts", "type": "integer", "required": false }, "default_ping_status": { + "title": "", "description": "Allow link notifications from other blogs (pingbacks and trackbacks) on new articles.", "type": "string", "enum": [ @@ -10829,6 +10846,7 @@ mockedApiResponse.Schema = { "required": false }, "default_comment_status": { + "title": "Allow comments on new posts", "description": "Allow people to submit comments on new posts.", "type": "string", "enum": [ @@ -10838,11 +10856,13 @@ mockedApiResponse.Schema = { "required": false }, "site_logo": { + "title": "", "description": "Site logo.", "type": "integer", "required": false }, "site_icon": { + "title": "", "description": "Site icon.", "type": "integer", "required": false