Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try: Set show_in_rest to true by default when label argument is defined #7302

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/wp-includes/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,7 @@ function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype =
* @since 5.3.0 Valid meta types expanded to include "array" and "object".
* @since 5.5.0 The `$default` argument was added to the arguments array.
* @since 6.4.0 The `$revisions_enabled` argument was added to the arguments array.
* @since 6.7.0 The `label` argument was added to the arguments array.
*
* @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
Expand All @@ -1379,6 +1380,7 @@ function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype =
* the meta key will be registered on the entire object type. Default empty.
* @type string $type The type of data associated with this meta key.
* Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
* @type string $label A human-readable label of the data attached to this meta key.
* @type string $description A description of the data attached to this meta key.
* @type bool $single Whether the meta key has one value per object, or an array of values per object.
* @type mixed $default The default value returned from get_metadata() if no value has been set yet.
Expand Down Expand Up @@ -1411,6 +1413,7 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
$defaults = array(
'object_subtype' => '',
'type' => 'string',
'label' => '',
'description' => '',
'default' => '',
'single' => false,
Expand Down Expand Up @@ -1451,6 +1454,9 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
* @param string $meta_key Meta key.
*/
$args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
if ( isset( $args['label'] ) && ! isset( $args['show_in_rest'] ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should change before the filter so 3rd party code can still change it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when the filter adds a label without setting show_in_rest? Shouldn't that follow the same behavior?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is safer to move it before the filter as you say to ensure people can change this behavior.

$args['show_in_rest'] = true;
}
Comment on lines +1457 to +1459
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ( isset( $args['label'] ) && ! isset( $args['show_in_rest'] ) ) {
$args['show_in_rest'] = true;
}
if ( isset( $args['label'] ) && ! isset( $args['show_in_rest'] ) ) {
$args['show_in_rest'] = true;
}

Just adding some spacing for clarity.

unset( $defaults['default'] );
$args = wp_parse_args( $args, $defaults );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ protected function get_registered_fields() {

$default_schema = array(
'type' => $default_args['type'],
'title' => empty( $args['label'] ) ? '' : $args['label'],
'description' => empty( $args['description'] ) ? '' : $args['description'],
'default' => isset( $args['default'] ) ? $args['default'] : null,
);
Expand Down
15 changes: 15 additions & 0 deletions tests/phpunit/tests/meta/registerMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function test_register_meta_with_post_object_type_populates_wp_meta_keys(
'' => array(
'flight_number' => array(
'type' => 'string',
'label' => '',
'description' => '',
'single' => false,
'sanitize_callback' => null,
Expand All @@ -117,6 +118,7 @@ public function test_register_meta_with_term_object_type_populates_wp_meta_keys(
'' => array(
'category_icon' => array(
'type' => 'string',
'label' => '',
'description' => '',
'single' => false,
'sanitize_callback' => null,
Expand Down Expand Up @@ -172,6 +174,7 @@ public function test_register_meta_with_current_sanitize_callback_populates_wp_m
'' => array(
'flight_number' => array(
'type' => 'string',
'label' => '',
'description' => '',
'single' => false,
'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ),
Expand Down Expand Up @@ -256,6 +259,16 @@ public function test_get_registered_meta_keys_with_invalid_type_is_empty() {
$this->assertEmpty( $meta_keys );
}

public function test_get_registered_meta_keys_label_arg() {
register_meta( 'post', 'registered_key1', array( 'label' => 'Field label' ) );

$meta_keys = get_registered_meta_keys( 'post' );

unregister_meta_key( 'post', 'registered_key1' );

$this->assertSame( 'Field label', $meta_keys['registered_key1']['label'] );
}

public function test_get_registered_meta_keys_description_arg() {
register_meta( 'post', 'registered_key1', array( 'description' => 'I\'m just a field, take a good look at me' ) );

Expand Down Expand Up @@ -340,6 +353,7 @@ public function test_register_meta_with_subtype_populates_wp_meta_keys( $type, $
$subtype => array(
'flight_number' => array(
'type' => 'string',
'label' => '',
'description' => '',
'single' => false,
'sanitize_callback' => null,
Expand Down Expand Up @@ -394,6 +408,7 @@ public function test_unregister_meta_without_subtype_keeps_subtype_meta_key( $ty
$subtype => array(
'flight_number' => array(
'type' => 'string',
'label' => '',
'description' => '',
'single' => false,
'sanitize_callback' => null,
Expand Down
55 changes: 55 additions & 0 deletions tests/phpunit/tests/rest-api/rest-post-meta-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function set_up() {
'post',
'test_rest_disabled',
array(
'label' => 'Test REST Disabled',
'show_in_rest' => false,
'type' => 'string',
)
Expand Down Expand Up @@ -243,6 +244,29 @@ public function set_up() {
)
);

register_post_meta(
'post',
'with_label',
array(
'type' => 'string',
'single' => true,
'show_in_rest' => true,
'label' => 'Meta Label',
'default' => '',
)
);

register_post_meta(
'post',
'test_label_and_no_rest',
array(
'type' => 'string',
'single' => true,
'label' => 'Meta Label',
'default' => '',
)
);

/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$wp_rest_server = new Spy_REST_Server();
Expand Down Expand Up @@ -3095,6 +3119,19 @@ public function test_default_is_added_to_schema() {
$this->assertSame( 'Goodnight Moon', $schema['default'] );
}

/**
* @ticket 61998
*/
public function test_title_is_added_to_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
$response = rest_do_request( $request );

$schema = $response->get_data()['schema']['properties']['meta']['properties']['with_label'];

$this->assertArrayHasKey( 'default', $schema );
$this->assertSame( 'Meta Label', $schema['title'] );
}

/**
* Ensures that REST API calls with post meta containing the default value for the
* registered meta field stores the default value into the database.
Expand Down Expand Up @@ -3950,6 +3987,24 @@ public function test_revisioned_single_post_meta_with_posts_endpoint_page_and_cp
wp_delete_post( $post_id, true );
}

/**
* @ticket 61998
*/
public function test_get_registered_label_without_rest() {
add_post_meta( self::$post_id, 'test_label_and_no_rest', 'val1' );
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );

$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );

$data = $response->get_data();
$this->assertArrayHasKey( 'meta', $data );

$meta = (array) $data['meta'];
$this->assertArrayHasKey( 'test_label_and_no_rest', $meta );
$this->assertSame( 'val1', $meta['test_label_and_no_rest'] );
}

/**
* Provide data for the meta revision checks.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function test_should_register_persisted_preferences_meta() {
$this->assertSame(
array(
'type' => 'object',
'label' => '',
'description' => '',
'single' => true,
'sanitize_callback' => null,
Expand Down
Loading