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-post-meta-fields.php

    r48190 r48402  
    232232                'type'         => 'string',
    233233                'show_in_rest' => true,
     234            )
     235        );
     236
     237        register_post_meta(
     238            'post',
     239            'with_default',
     240            array(
     241                'type'         => 'string',
     242                'single'       => true,
     243                'show_in_rest' => true,
     244                'default'      => 'Goodnight Moon',
    234245            )
    235246        );
     
    27872798
    27882799    /**
     2800     * @ticket 43941
     2801     * @dataProvider data_get_default_data
     2802     */
     2803    public function test_get_default_value( $args, $expected ) {
     2804        $object_type = 'post';
     2805        $meta_key    = 'registered_key1';
     2806        $registered  = register_meta(
     2807            $object_type,
     2808            $meta_key,
     2809            $args
     2810        );
     2811
     2812        $this->assertTrue( $registered );
     2813
     2814        // Check for default value.
     2815        $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     2816        $response = rest_get_server()->dispatch( $request );
     2817
     2818        $this->assertEquals( 200, $response->get_status() );
     2819
     2820        $data = $response->get_data();
     2821        $this->assertArrayHasKey( 'meta', $data );
     2822
     2823        $meta = (array) $data['meta'];
     2824        $this->assertArrayHasKey( $meta_key, $meta );
     2825        $this->assertEquals( $expected, $meta[ $meta_key ] );
     2826    }
     2827
     2828    public function data_get_default_data() {
     2829        return array(
     2830            array(
     2831                array(
     2832                    'show_in_rest' => true,
     2833                    'single'       => true,
     2834                    'default'      => 'wibble',
     2835                ),
     2836                'wibble',
     2837            ),
     2838            array(
     2839                array(
     2840                    'show_in_rest' => true,
     2841                    'single'       => false,
     2842                    'default'      => 'wibble',
     2843                ),
     2844                array( 'wibble' ),
     2845            ),
     2846            array(
     2847                array(
     2848                    'show_in_rest'   => true,
     2849                    'single'         => true,
     2850                    'object_subtype' => 'post',
     2851                    'default'        => 'wibble',
     2852                ),
     2853                'wibble',
     2854            ),
     2855            array(
     2856                array(
     2857                    'show_in_rest'   => true,
     2858                    'single'         => false,
     2859                    'object_subtype' => 'post',
     2860                    'default'        => 'wibble',
     2861                ),
     2862                array( 'wibble' ),
     2863            ),
     2864            array(
     2865                array(
     2866                    'single'       => true,
     2867                    'show_in_rest' => array(
     2868                        'schema' => array(
     2869                            'type'       => 'object',
     2870                            'properties' => array(
     2871                                'wibble' => array(
     2872                                    'type' => 'string',
     2873                                ),
     2874                            ),
     2875                        ),
     2876                    ),
     2877                    'type'         => 'object',
     2878                    'default'      => array( 'wibble' => 'dibble' ),
     2879                ),
     2880                array( 'wibble' => 'dibble' ),
     2881            ),
     2882            array(
     2883                array(
     2884                    'show_in_rest' => array(
     2885                        'schema' => array(
     2886                            'type'       => 'object',
     2887                            'properties' => array(
     2888                                'wibble' => array(
     2889                                    'type' => 'string',
     2890                                ),
     2891                            ),
     2892                        ),
     2893                    ),
     2894                    'type'         => 'object',
     2895                    'single'       => false,
     2896                    'default'      => array( 'wibble' => 'dibble' ),
     2897                ),
     2898                array( array( 'wibble' => 'dibble' ) ),
     2899            ),
     2900
     2901            array(
     2902                array(
     2903                    'show_in_rest' => array(
     2904                        'schema' => array(
     2905                            'type'  => 'array',
     2906                            'items' => array(
     2907                                'type' => 'string',
     2908                            ),
     2909                        ),
     2910                    ),
     2911                    'single'       => true,
     2912                    'type'         => 'array',
     2913                    'default'      => array( 'dibble' ),
     2914                ),
     2915                array( 'dibble' ),
     2916            ),
     2917            array(
     2918                array(
     2919                    'show_in_rest' => array(
     2920                        'schema' => array(
     2921                            'type'  => 'array',
     2922                            'items' => array(
     2923                                'type' => 'string',
     2924                            ),
     2925                        ),
     2926                    ),
     2927                    'single'       => false,
     2928                    'type'         => 'array',
     2929                    'default'      => array( 'dibble' ),
     2930                ),
     2931                array( array( 'dibble' ) ),
     2932            ),
     2933            'array of objects' => array(
     2934                array(
     2935                    'type'         => 'array',
     2936                    'single'       => true,
     2937                    'show_in_rest' => array(
     2938                        'schema' => array(
     2939                            'type'  => 'array',
     2940                            'items' => array(
     2941                                'type'       => 'object',
     2942                                'properties' => array(
     2943                                    'name' => array(
     2944                                        'type' => 'string',
     2945                                    ),
     2946                                ),
     2947                            ),
     2948                        ),
     2949                    ),
     2950                    'default'      => array(
     2951                        array(
     2952                            'name' => 'Kirk',
     2953                        ),
     2954                    ),
     2955                ),
     2956                array(
     2957                    array(
     2958                        'name' => 'Kirk',
     2959                    ),
     2960                ),
     2961            ),
     2962        );
     2963    }
     2964
     2965    /**
     2966     * @ticket 43941
     2967     */
     2968    public function test_set_default_in_schema() {
     2969        register_post_meta(
     2970            'post',
     2971            'greeting',
     2972            array(
     2973                'type'         => 'string',
     2974                'single'       => true,
     2975                'show_in_rest' => array(
     2976                    'schema' => array(
     2977                        'default' => 'Hello World',
     2978                    ),
     2979                ),
     2980            )
     2981        );
     2982
     2983        $response = rest_do_request( '/wp/v2/posts/' . self::$post_id );
     2984        $this->assertEquals( 'Hello World', $response->get_data()['meta']['greeting'] );
     2985    }
     2986
     2987    /**
     2988     * @ticket 43941
     2989     */
     2990    public function test_default_is_added_to_schema() {
     2991        $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
     2992        $response = rest_do_request( $request );
     2993
     2994        $schema = $response->get_data()['schema']['properties']['meta']['properties']['with_default'];
     2995        $this->assertArrayHasKey( 'default', $schema );
     2996        $this->assertEquals( 'Goodnight Moon', $schema['default'] );
     2997    }
     2998
     2999    /**
    27893000     * Internal function used to disable an insert query which
    27903001     * will trigger a wpdb error for testing purposes.
Note: See TracChangeset for help on using the changeset viewer.