💥 TRENDING: Changeset/ - Full Archive

Changeset 48402


Ignore:
Timestamp:
07/07/2020 08:45:55 PM (5 years ago)
Author:
TimothyBlynJacobs
Message:

REST API, Meta: Introduce support for default metadata values.

The register_meta() API now officially supports specifying a default metadata value. When get_metadata() is called for a meta key that does not yet exist for the object, this default value will be returned instead of an empty string.

A new function is introduced get_metadata_raw to retrieve the raw metadata value from the database, without applying the registered default.

Props spacedmonkey, flixos90, rmccue, kadamwhite, mnelson4, johnbillion, chrisvanpatten, TimothyBlynJacobs.
Fixes #43941.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/meta.php

    r48214 r48402  
    211211    // Compare existing value to new value if no prev value given and the key exists only once.
    212212    if ( empty( $prev_value ) ) {
    213         $old_value = get_metadata( $meta_type, $object_id, $meta_key );
    214         if ( count( $old_value ) == 1 ) {
     213        $old_value = get_metadata_raw( $meta_type, $object_id, $meta_key );
     214        if ( is_countable( $old_value ) && count( $old_value ) === 1 ) {
    215215            if ( $old_value[0] === $meta_value ) {
    216216                return false;
     
    486486 * If there's a problem with the parameters passed to the function, boolean `false` is returned.
    487487 *
    488  * @since 2.9.0
     488 * @since 5.5.0
    489489 *
    490490 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
     
    497497 * @return mixed The metadata value or array of values. See description above for further details.
    498498 */
    499 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
     499function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) {
    500500    if ( ! $meta_type || ! is_numeric( $object_id ) ) {
    501501        return false;
     
    554554    }
    555555
     556    return null;
     557}
     558
     559/**
     560 * Retrieves raw metadata for the specified object.
     561 *
     562 * @since 2.9.0
     563 * @uses get_metadata_raw()
     564 *
     565 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
     566 *                          or any other object type with an associated meta table.
     567 * @param int    $object_id ID of the object metadata is for.
     568 * @param string $meta_key  Optional. Metadata key. If not specified, retrieve all metadata for
     569 *                          the specified object. Default empty.
     570 * @param bool   $single    Optional. If true, return only the first value of the specified meta_key.
     571 *                          This parameter has no effect if meta_key is not specified. Default false.
     572 * @return mixed Single metadata value, or array of values
     573 */
     574function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
     575    $value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single );
     576    if ( ! is_null( $value ) ) {
     577        return $value;
     578    }
     579
     580    return get_metadata_default( $meta_type, $meta_key, $single, $object_id );
     581}
     582
     583/**
     584 * Retrieve metadata data default for the specified object.
     585 *
     586 * @since 5.5.0
     587 *
     588 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
     589 * @param string $meta_key  Optional. Metadata key. If not specified, retrieve all metadata for
     590 *                          the specified object.
     591 * @param bool   $single    Optional, default is false.
     592 *                          If true, return only the first value of the specified meta_key.
     593 *                          This parameter has no effect if meta_key is not specified.
     594 * @param int    $object_id Optional, default is 0.
     595 *                          ID of the object metadata is for
     596 * @return mixed Single metadata value, or array of values
     597 */
     598function get_metadata_default( $meta_type, $meta_key, $single = false, $object_id = 0 ) {
    556599    if ( $single ) {
    557