Make WordPress Core


Ignore:
Timestamp:
07/29/2024 06:47:21 PM (16 months ago)
Author:
dmsnell
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php

    r57611 r58831  
    269269     *
    270270     * @since 4.7.0
     271     * @since 6.7.0 Stores values into DB even if provided registered default value.
    271272     *
    272273     * @param int    $object_id Object ID to update.
     
    291292        }
    292293
    293         $current_values = get_metadata( $meta_type, $object_id, $meta_key, false );
     294        $current_values = get_metadata_raw( $meta_type, $object_id, $meta_key, false );
    294295        $subtype        = get_object_subtype( $meta_type, $object_id );
    295296
     
    368369     *
    369370     * @since 4.7.0
     371     * @since 6.7.0 Stores values into DB even if provided registered default value.
    370372     *
    371373     * @param int    $object_id Object ID to update.
     
    379381
    380382        // Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false.
    381         $old_value = get_metadata( $meta_type, $object_id, $meta_key );
     383        $old_value = get_metadata_raw( $meta_type, $object_id, $meta_key );
    382384        $subtype   = get_object_subtype( $meta_type, $object_id );
    383385
Note: See TracChangeset for help on using the changeset viewer.