Skip to content

Commit d2ce8dd

Browse files
committed
Meta: Add label argument to register_meta function
With the introduction of Block Bindings, it became more common to see workflows where users need to see the custom fields that are available or connected. They were relying on the meta key, however it feelt too technical sometimes. The solution is adding a new label argument to include a human-readable name that can be used across the UI. Props santosguillamot, mamaduka, gziolo, timothyblynjacobs, peterwilsoncc. Fixes #61998. git-svn-id: https://develop.svn.wordpress.org/trunk@59023 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6b81c17 commit d2ce8dd

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

src/wp-includes/meta.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,7 @@ function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype =
13691369
* @since 5.3.0 Valid meta types expanded to include "array" and "object".
13701370
* @since 5.5.0 The `$default` argument was added to the arguments array.
13711371
* @since 6.4.0 The `$revisions_enabled` argument was added to the arguments array.
1372+
* @since 6.7.0 The `label` argument was added to the arguments array.
13721373
*
13731374
* @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
13741375
* or any other object type with an associated meta table.
@@ -1380,6 +1381,7 @@ function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype =
13801381
* the meta key will be registered on the entire object type. Default empty.
13811382
* @type string $type The type of data associated with this meta key.
13821383
* Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
1384+
* @type string $label A human-readable label of the data attached to this meta key.
13831385
* @type string $description A description of the data attached to this meta key.
13841386
* @type bool $single Whether the meta key has one value per object, or an array of values per object.
13851387
* @type mixed $default The default value returned from get_metadata() if no value has been set yet.
@@ -1412,6 +1414,7 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
14121414
$defaults = array(
14131415
'object_subtype' => '',
14141416
'type' => 'string',
1417+
'label' => '',
14151418
'description' => '',
14161419
'default' => '',
14171420
'single' => false,

src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ protected function get_registered_fields() {
478478

479479
$default_schema = array(
480480
'type' => $default_args['type'],
481+
'title' => empty( $args['label'] ) ? '' : $args['label'],
481482
'description' => empty( $args['description'] ) ? '' : $args['description'],
482483
'default' => isset( $args['default'] ) ? $args['default'] : null,
483484
);

tests/phpunit/tests/meta/registerMeta.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public function test_register_meta_with_post_object_type_populates_wp_meta_keys(
9292
'' => array(
9393
'flight_number' => array(
9494
'type' => 'string',
95+
'label' => '',
9596
'description' => '',
9697
'single' => false,
9798
'sanitize_callback' => null,
@@ -117,6 +118,7 @@ public function test_register_meta_with_term_object_type_populates_wp_meta_keys(
117118
'' => array(
118119
'category_icon' => array(
119120
'type' => 'string',
121+
'label' => '',
120122
'description' => '',
121123
'single' => false,
122124
'sanitize_callback' => null,
@@ -172,6 +174,7 @@ public function test_register_meta_with_current_sanitize_callback_populates_wp_m
172174
'' => array(
173175
'flight_number' => array(
174176
'type' => 'string',
177+
'label' => '',
175178
'description' => '',
176179
'single' => false,
177180
'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ),
@@ -256,6 +259,19 @@ public function test_get_registered_meta_keys_with_invalid_type_is_empty() {
256259
$this->assertEmpty( $meta_keys );
257260
}
258261

262+
/**
263+
* @ticket 61998
264+
*/
265+
public function test_get_registered_meta_keys_label_arg() {
266+
register_meta( 'post', 'registered_key1', array( 'label' => 'Field label' ) );
267+
268+
$meta_keys = get_registered_meta_keys( 'post' );
269+
270+
unregister_meta_key( 'post', 'registered_key1' );
271+
272+
$this->assertSame( 'Field label', $meta_keys['registered_key1']['label'] );
273+
}
274+
259275
public function test_get_registered_meta_keys_description_arg() {
260276
register_meta( 'post', 'registered_key1', array( 'description' => 'I\'m just a field, take a good look at me' ) );
261277

@@ -340,6 +356,7 @@ public function test_register_meta_with_subtype_populates_wp_meta_keys( $type, $
340356
$subtype => array(
341357
'flight_number' => array(
342358
'type' => 'string',
359+
'label' => '',
343360
'description' => '',
344361
'single' => false,
345362
'sanitize_callback' => null,
@@ -394,6 +411,7 @@ public function test_unregister_meta_without_subtype_keeps_subtype_meta_key( $ty
394411
$subtype => array(
395412
'flight_number' => array(
396413
'type' => 'string',
414+
'label' => '',
397415
'description' => '',
398416
'single' => false,
399417
'sanitize_callback' => null,

tests/phpunit/tests/rest-api/rest-post-meta-fields.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,18 @@ public function set_up() {
243243
)
244244
);
245245

246+
register_post_meta(
247+
'post',
248+
'with_label',
249+
array(
250+
'type' => 'string',
251+
'single' => true,
252+
'show_in_rest' => true,
253+
'label' => 'Meta Label',
254+
'default' => '',
255+
)
256+
);
257+
246258
/** @var WP_REST_Server $wp_rest_server */
247259
global $wp_rest_server;
248260
$wp_rest_server = new Spy_REST_Server();
@@ -3091,8 +3103,21 @@ public function test_default_is_added_to_schema() {
30913103
$response = rest_do_request( $request );
30923104

30933105
$schema = $response->get_data()['schema']['properties']['meta']['properties']['with_default'];
3094-
$this->assertArrayHasKey( 'default', $schema );
3095-
$this->assertSame( 'Goodnight Moon', $schema['default'] );
3106+
$this->assertArrayHasKey( 'default', $schema, 'Schema is expected to have the default property' );
3107+
$this->assertSame( 'Goodnight Moon', $schema['default'], 'Schema default is expected to be defined and contain the value of the meta default argument.' );
3108+
}
3109+
3110+
/**
3111+
* @ticket 61998
3112+
*/
3113+
public function test_title_is_added_to_schema() {
3114+
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
3115+
$response = rest_do_request( $request );
3116+
3117+
$schema = $response->get_data()['schema']['properties']['meta']['properties']['with_label'];
3118+
3119+
$this->assertArrayHasKey( 'title', $schema, 'Schema is expected to have the title property' );
3120+
$this->assertSame( 'Meta Label', $schema['title'], 'Schema title is expected to be defined and contain the value of the meta label argument.' );
30963121
}
30973122

30983123
/**

tests/phpunit/tests/user/wpRegisterPersistedPreferencesMeta.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function test_should_register_persisted_preferences_meta() {
3131
$this->assertSame(
3232
array(
3333
'type' => 'object',
34+
'label' => '',
3435
'description' => '',
3536
'single' => true,
3637
'sanitize_callback' => null,

0 commit comments

Comments
 (0)