Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r48196 r48402  
    28912891    }
    28922892
     2893    /**
     2894     * @ticket 43941
     2895     * @dataProvider data_get_default_data
     2896     */
     2897    public function test_get_default_value( $args, $expected ) {
     2898        wp_set_current_user( self::$user );
     2899
     2900        $object_type = 'user';
     2901        $meta_key    = 'registered_key1';
     2902        register_meta(
     2903            $object_type,
     2904            $meta_key,
     2905            $args
     2906        );
     2907
     2908        // Check for default value.
     2909        $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$user ) );
     2910        $response = rest_get_server()->dispatch( $request );
     2911
     2912        $this->assertEquals( 200, $response->get_status() );
     2913
     2914        $data = $response->get_data();
     2915        $this->assertArrayHasKey( 'meta', $data );
     2916
     2917        $meta = (array) $data['meta'];
     2918        $this->assertArrayHasKey( $meta_key, $meta );
     2919        $this->assertEquals( $expected, $meta[ $meta_key ] );
     2920    }
     2921
     2922    public function data_get_default_data() {
     2923        return array(
     2924            array(
     2925                array(
     2926                    'show_in_rest' => true,
     2927                    'single'       => true,
     2928                    'default'      => 'wibble',
     2929                ),
     2930                'wibble',
     2931            ),
     2932            array(
     2933                array(
     2934                    'show_in_rest' => true,
     2935                    'single'       => false,
     2936                    'default'      => 'wibble',
     2937                ),
     2938                array( 'wibble' ),
     2939            ),
     2940            array(
     2941                array(
     2942                    'single'       => true,
     2943                    'show_in_rest' => array(
     2944                        'schema' => array(
     2945                            'type'       => 'object',
     2946                            'properties' => array(
     2947                                'wibble' => array(
     2948                                    'type' => 'string',
     2949                                ),
     2950                            ),
     2951                        ),
     2952                    ),
     2953                    'type'         => 'object',
     2954                    'default'      => array( 'wibble' => 'dibble' ),
     2955                ),
     2956                array( 'wibble' => 'dibble' ),
     2957            ),
     2958            array(
     2959                array(
     2960                    'show_in_rest' => array(
     2961                        'schema' => array(
     2962                            'type'       => 'object',
     2963                            'properties' => array(
     2964                                'wibble' => array(
     2965                                    'type' => 'string',
     2966                                ),
     2967                            ),
     2968                        ),
     2969                    ),
     2970                    'type'         => 'object',
     2971                    'single'       => false,
     2972                    'default'      => array( 'wibble' => 'dibble' ),
     2973                ),
     2974                array( array( 'wibble' => 'dibble' ) ),
     2975            ),
     2976
     2977            array(
     2978                array(
     2979                    'show_in_rest' => array(
     2980                        'schema' => array(
     2981                            'type'  => 'array',
     2982                            'items' => array(
     2983                                'type' => 'string',
     2984                            ),
     2985                        ),
     2986                    ),
     2987                    'single'       => true,
     2988                    'type'         => 'array',
     2989                    'default'      => array( 'dibble' ),
     2990                ),
     2991                array( 'dibble' ),
     2992            ),
     2993            array(
     2994                array(
     2995                    'show_in_rest' => array(
     2996                        'schema' => array(
     2997                            'type'  => 'array',
     2998                            'items' => array(
     2999                                'type' => 'string',
     3000                            ),
     3001                        ),
     3002                    ),
     3003                    'single'       => false,
     3004                    'type'         => 'array',
     3005                    'default'      => array( 'dibble' ),
     3006                ),
     3007                array( array( 'dibble' ) ),
     3008            ),
     3009        );
     3010    }
     3011
    28933012    public function additional_field_get_callback( $object ) {
    28943013        return get_user_meta( $object['id'], 'my_custom_int', true );
Note: See TracChangeset for help on using the changeset viewer.