REST API, Meta: Store updates in database when they are equal to the defaults.

This patch fixes an oversight from when default metadata values were introduced
in #43941 in WordPress 5.5: metadata updates should persist in the database
even if they match the registered default value (because the default values 
can change over time).

Previously, the REST API code was comparing updated values against the value
returned by the default-aware `get_metadata()` method. This meant that if no
value existed in the database, and the default value was supplied to the update,
WordPress would think that the updated value was already persisted and skip
the database call.

Now, the `get_metadata_raw()` method is called for comparing whether or not
a database update is required, fixing the bug.

In this patch both issues are resolved.

Developed in https://github.com/wordpress/wordpress-develop/pull/6782
Discussed in https://core.trac.wordpress.org/ticket/55600

Follow-up to [48402].

Props: dmsnell, kraftner, ramon-fincken.
Fixes #55600.

Built from https://develop.svn.wordpress.org/trunk@58831


git-svn-id: http://core.svn.wordpress.org/trunk@58227 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
dmsnell 2024-07-29 18:49:14 +00:00
parent faacd44911
commit 63c7122ed0
2 changed files with 5 additions and 3 deletions

View File

@ -268,6 +268,7 @@ abstract class WP_REST_Meta_Fields {
* Alters the list of values in the database to match the list of provided values. * Alters the list of values in the database to match the list of provided values.
* *
* @since 4.7.0 * @since 4.7.0
* @since 6.7.0 Stores values into DB even if provided registered default value.
* *
* @param int $object_id Object ID to update. * @param int $object_id Object ID to update.
* @param string $meta_key Key for the custom field. * @param string $meta_key Key for the custom field.
@ -290,7 +291,7 @@ abstract class WP_REST_Meta_Fields {
); );
} }
$current_values = get_metadata( $meta_type, $object_id, $meta_key, false ); $current_values = get_metadata_raw( $meta_type, $object_id, $meta_key, false );
$subtype = get_object_subtype( $meta_type, $object_id ); $subtype = get_object_subtype( $meta_type, $object_id );
if ( ! is_array( $current_values ) ) { if ( ! is_array( $current_values ) ) {
@ -367,6 +368,7 @@ abstract class WP_REST_Meta_Fields {
* Updates a meta value for an object. * Updates a meta value for an object.
* *
* @since 4.7.0 * @since 4.7.0
* @since 6.7.0 Stores values into DB even if provided registered default value.
* *
* @param int $object_id Object ID to update. * @param int $object_id Object ID to update.
* @param string $meta_key Key for the custom field. * @param string $meta_key Key for the custom field.
@ -378,7 +380,7 @@ abstract class WP_REST_Meta_Fields {
$meta_type = $this->get_meta_type(); $meta_type = $this->get_meta_type();
// Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false. // Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false.
$old_value = get_metadata( $meta_type, $object_id, $meta_key ); $old_value = get_metadata_raw( $meta_type, $object_id, $meta_key );
$subtype = get_object_subtype( $meta_type, $object_id ); $subtype = get_object_subtype( $meta_type, $object_id );
if ( is_array( $old_value ) && 1 === count( $old_value ) if ( is_array( $old_value ) && 1 === count( $old_value )

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.7-alpha-58830'; $wp_version = '6.7-alpha-58831';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.