Make WordPress Core

Ticket #43941: 43941.3.diff

File 43941.3.diff, 3.7 KB (added by spacedmonkey, 6 years ago)
  • src/wp-includes/meta.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    536536                }
    537537        }
    538538
     539        return get_metadata_default( $meta_type, $meta_key, $single, $object_id );
     540}
     541
     542/**
     543 * Retrieve metadata data default for the specified object.
     544 *
     545 * @since 5.2.0
     546 *
     547 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
     548 * @param string $meta_key  Optional. Metadata key. If not specified, retrieve all metadata for
     549 *                          the specified object.
     550 * @param bool   $single    Optional, default is false.
     551 *                          If true, return only the first value of the specified meta_key.
     552 *                          This parameter has no effect if meta_key is not specified.
     553 * @param int    $object_id Optional, default is 0.
     554 *                          ID of the object metadata is for
     555 * @return mixed Single metadata value, or array of values
     556 */
     557function get_metadata_default( $meta_type, $meta_key, $single = false, $object_id = 0 ) {
    539558        if ( $single ) {
    540                 return '';
     559                $value = '';
    541560        } else {
    542                 return array();
     561                $value = array();
    543562        }
     563
     564        /**
     565         *
     566         * @since 5.2.0
     567         *
     568         * @param array|string      $value     The value should return - a single metadata value,
     569         *                                     or an array of values.
     570         * @param string            $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
     571         * @param string            $meta_key  Meta key.
     572         * @param bool              $single    Whether to return only the first value of the specified $meta_key.
     573         * @param int               $object_id Object ID.
     574         */
     575        $value = apply_filters( "default_{$meta_type}_metadata", $value, $meta_type, $meta_key, $single, $object_id );
     576
     577        return $value;
    544578}
    545579
    546580/**
     
    11411175                'type'              => 'string',
    11421176                'description'       => '',
    11431177                'single'            => false,
     1178                'default'           => null,
    11441179                'sanitize_callback' => null,
    11451180                'auth_callback'     => null,
    11461181                'show_in_rest'      => false,
     
    12061241                }
    12071242        }
    12081243
     1244        if ( false === $args['single'] && ! wp_is_numeric_array( $args['default'] ) ) {
     1245                $args['default'] = null;
     1246        }
     1247
     1248        if ( null !== $args['default'] ) {
     1249                add_filter( "default_{$object_type}_metadata", "filter_default_metadata", 10, 5 );
     1250        }
     1251
    12091252        // Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
    12101253        if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
    12111254                unset( $args['object_subtype'] );
     
    14341477         */
    14351478        return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id );
    14361479}
     1480
     1481/**
     1482 *
     1483 * @param $value
     1484 * @param $meta_type
     1485 * @param $meta_key
     1486 * @param $single
     1487 * @param $object_id
     1488 *
     1489 * @return mixed
     1490 */
     1491function filter_default_metadata( $value, $meta_type, $meta_key, $single, $object_id ) {
     1492        $metadata = get_registered_meta_keys( $meta_type );
     1493        if ( ! isset( $metadata[ $meta_key ] ) ) {
     1494                $sub_type = get_object_subtype( $meta_type, $object_id );
     1495                $metadata = get_registered_meta_keys( $meta_type, $sub_type );
     1496                if ( ! isset( $metadata[ $meta_key ] ) ) {
     1497                        return $value;
     1498                }
     1499        }
     1500
     1501        if ( $metadata[ $meta_key ]['single'] ) {
     1502                if ( $single ) {
     1503                        $value = $metadata[ $meta_key ]['default'];
     1504                } else {
     1505                        $value = array( $metadata[ $meta_key ]['default'] );
     1506                }
     1507        } else {
     1508                if ( $single ) {
     1509                        $value = $metadata[ $meta_key ]['default'][0];
     1510                } else {
     1511                        $value = $metadata[ $meta_key ]['default'];
     1512                }
     1513        }
     1514
     1515        return $value;
     1516}
     1517 No newline at end of file