diff --git a/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php b/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php
index 5b7240ddc6..490cf346b8 100644
|
a
|
b
|
abstract class WP_REST_Meta_Fields {
|
| 350 | 350 | // Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false. |
| 351 | 351 | $old_value = get_metadata( $meta_type, $object_id, $meta_key ); |
| 352 | 352 | $subtype = get_object_subtype( $meta_type, $object_id ); |
| | 353 | $args = $this->get_registered_fields()[ $meta_key ]; |
| 353 | 354 | |
| 354 | 355 | if ( 1 === count( $old_value ) ) { |
| 355 | | if ( (string) sanitize_meta( $meta_key, $value, $meta_type, $subtype ) === $old_value[0] ) { |
| | 356 | $sanitized = sanitize_meta( $meta_key, $value, $meta_type, $subtype ); |
| | 357 | |
| | 358 | if ( in_array( $args['type'], array( 'string', 'number', 'integer', 'boolean' ), true ) ) { |
| | 359 | // The return value of get_metadata will always be a string for scalar types. |
| | 360 | $sanitized = (string) $sanitized; |
| | 361 | } |
| | 362 | |
| | 363 | if ( $sanitized === $old_value[0] ) { |
| 356 | 364 | return true; |
| 357 | 365 | } |
| 358 | 366 | } |
diff --git a/tests/phpunit/tests/rest-api/rest-post-meta-fields.php b/tests/phpunit/tests/rest-api/rest-post-meta-fields.php
index 71bcfdcd87..ee1bdab1a7 100644
|
a
|
b
|
class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
|
| 2120 | 2120 | $this->assertEquals( array( 'BuddyPress' ), $meta[1] ); |
| 2121 | 2121 | } |
| 2122 | 2122 | |
| | 2123 | /** |
| | 2124 | * @ticket 47928 |
| | 2125 | */ |
| | 2126 | public function test_update_meta_with_unchanged_values() { |
| | 2127 | register_post_meta( |
| | 2128 | 'post', |
| | 2129 | 'list', |
| | 2130 | array( |
| | 2131 | 'single' => true, |
| | 2132 | 'type' => 'array', |
| | 2133 | 'show_in_rest' => array( |
| | 2134 | 'schema' => array( |
| | 2135 | 'type' => 'array', |
| | 2136 | 'items' => array( |
| | 2137 | 'type' => 'string', |
| | 2138 | ), |
| | 2139 | ), |
| | 2140 | ), |
| | 2141 | ) |
| | 2142 | ); |
| | 2143 | |
| | 2144 | add_post_meta( self::$post_id, 'list', array( 'WordCamp' ) ); |
| | 2145 | |
| | 2146 | $this->grant_write_permission(); |
| | 2147 | |
| | 2148 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 2149 | $request->set_body_params( |
| | 2150 | array( |
| | 2151 | 'meta' => array( |
| | 2152 | 'list' => array( 'WordCamp' ), |
| | 2153 | ), |
| | 2154 | ) |
| | 2155 | ); |
| | 2156 | |
| | 2157 | $response = rest_get_server()->dispatch( $request ); |
| | 2158 | $this->assertEquals( 200, $response->get_status() ); |
| | 2159 | |
| | 2160 | $data = $response->get_data(); |
| | 2161 | $this->assertEquals( array( 'WordCamp' ), $data['meta']['list'] ); |
| | 2162 | } |
| | 2163 | |
| 2123 | 2164 | /** |
| 2124 | 2165 | * Internal function used to disable an insert query which |
| 2125 | 2166 | * will trigger a wpdb error for testing purposes. |