Skip to content

Commit

Permalink
REST API, Meta: Store updates in database when they are equal to the …
Browse files Browse the repository at this point in the history
…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 WordPress#6782
Discussed in https://core.trac.wordpress.org/ticket/55600

Follow-up to [48402].

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


git-svn-id: https://develop.svn.wordpress.org/trunk@58831 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
dmsnell committed Jul 29, 2024
1 parent 345699c commit 9ed3553
Show file tree
Hide file tree
Showing 2 changed files with 479 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ protected function delete_meta_value( $object_id, $meta_key, $name ) {
* Alters the list of values in the database to match the list of provided values.
*
* @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 string $meta_key Key for the custom field.
Expand All @@ -290,7 +291,7 @@ protected function update_multi_meta_value( $object_id, $meta_key, $name, $value
);
}

$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 );

if ( ! is_array( $current_values ) ) {
Expand Down Expand Up @@ -367,6 +368,7 @@ function ( $stored_value ) use ( $meta_key, $subtype, $value ) {
* Updates a meta value for an object.
*
* @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 string $meta_key Key for the custom field.
Expand All @@ -378,7 +380,7 @@ protected function update_meta_value( $object_id, $meta_key, $name, $value ) {
$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.
$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 );

if ( is_array( $old_value ) && 1 === count( $old_value )
Expand Down
Loading

0 comments on commit 9ed3553

Please sign in to comment.