Make WordPress Core

Changeset 41678


Ignore:
Timestamp:
10/02/2017 06:39:30 PM (8 years ago)
Author:
adamsilverstein
Message:

WP-API JS Client: Improve support for meta.

  • Add/fix getMeta, getMetas, setMeta and setMetas helpers for models that support meta.
  • Add tests for new helpers, verify meta support for Posts, Comments, Tags and Users.
  • Include meta data in fixture generation and fixture file driving tests.

Fixes #41055.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/wp-api.js

    r41657 r41678  
    502502             */
    503503            MetaMixin = {
    504                 getMeta: function() {
    505                     return buildCollectionGetter( this, 'PostMeta', 'https://api.w.org/meta' );
     504
     505                /**
     506                 * Get meta by key for a post.
     507                 *
     508                 * @param {string} key The meta key.
     509                 *
     510                 * @return {object} The post meta value.
     511                 */
     512                getMeta: function( key ) {
     513                    var metas = this.get( 'meta' );
     514                    return metas[ key ];
     515                },
     516
     517                /**
     518                 * Get all meta key/values for a post.
     519                 *
     520                 * @return {object} The post metas, as a key value pair object.
     521                 */
     522                getMetas: function() {
     523                    return this.get( 'meta' );
     524                },
     525
     526                /**
     527                 * Set a group of meta key/values for a post.
     528                 *
     529                 * @param {object} meta The post meta to set, as key/value pairs.
     530                 */
     531                setMetas: function( meta ) {
     532                    var metas = this.get( 'meta' );
     533                    _.extend( metas, meta );
     534                    this.set( 'meta', metas );
     535                },
     536
     537                /**
     538                 * Set a single meta value for a post, by key.
     539                 *
     540                 * @param {string} key   The meta key.
     541                 * @param {object} value The meta value.
     542                 */
     543                setMeta: function( key, value ) {
     544                    var metas = this.get( 'meta' );
     545                    metas[ key ] = value;
     546                    this.set( 'meta', metas );
    506547                }
    507548            },
     
    735776        }
    736777
    737         // Add the MetaMixin for models that support meta collections.
    738         if ( ! _.isUndefined( loadingObjects.collections[ modelClassName + 'Meta' ] ) ) {
     778        // Add the MetaMixin for models that support meta.
     779        if ( ! _.isUndefined( model.prototype.args.meta ) ) {
    739780            model = model.extend( MetaMixin );
    740781        }
  • trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php

    r41154 r41678  
    146146            'post_author'    => 0,
    147147        ) );
     148
    148149        wp_update_post( array(
    149150            'ID'           => $post_id,
     
    196197            'comment_author_url'   => 'http://lights.example.org/',
    197198        ) );
     199        $meta_args = array(
     200            'sanitize_callback' => 'sanitize_my_meta_key',
     201            'auth_callback'     => '__return_true',
     202            'type'              => 'string',
     203            'description'       => 'Test meta key',
     204            'single'            => true,
     205            'show_in_rest'      => true,
     206        );
     207
     208        // Set up meta.
     209        register_meta( 'user', 'meta_key', $meta_args );
     210        update_user_meta( 1, 'meta_key', 'meta_value' ); // Always use the first user.
     211        register_meta( 'post', 'meta_key', $meta_args );
     212        update_post_meta( $post_id, 'meta_key', 'meta_value' );
     213        register_meta( 'comment', 'meta_key', $meta_args );
     214        update_comment_meta( $comment_id, 'meta_key', 'meta_value' );
     215        register_meta( 'term', 'meta_key', $meta_args );
     216        update_term_meta( $tag_id, 'meta_key', 'meta_value' );
    198217
    199218        // Generate route data for subsequent QUnit tests.
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r41154 r41678  
    35613561        "template": "",
    35623562        "format": "standard",
    3563         "meta": [],
     3563        "meta": {
     3564            "meta_key": "meta_value"
     3565        },
    35643566        "categories": [
    35653567            1
     
    36523654    "template": "",
    36533655    "format": "standard",
    3654     "meta": [],
     3656    "meta": {
     3657        "meta_key": "meta_value"
     3658    },
    36553659    "categories": [
    36563660        1
     
    37463750        "ping_status": "closed",
    37473751        "template": "",
    3748         "meta": [],
     3752        "meta": {
     3753            "meta_key": ""
     3754        },
    37493755        "_links": {
    37503756            "self": [
     
    38213827    "ping_status": "closed",
    38223828    "template": "",
    3823     "meta": []
     3829    "meta": {
     3830        "meta_key": ""
     3831    }
    38243832};
    38253833
     
    39003908        "ping_status": "closed",
    39013909        "template": "",
    3902         "meta": [],
     3910        "meta": {
     3911            "meta_key": ""
     3912        },
    39033913        "description": {
    39043914            "rendered": "<p class=\"attachment\"><!-- <a...><img.../></a> --></p>"
     
    39593969    "ping_status": "closed",
    39603970    "template": "",
    3961     "meta": [],
     3971    "meta": {
     3972        "meta_key": ""
     3973    },
    39623974    "description": {
    39633975        "rendered": "<p class=\"attachment\"><!-- <a...><img.../></a> --></p>"
     
    42434255        "taxonomy": "category",
    42444256        "parent": 0,
    4245         "meta": [],
     4257        "meta": {
     4258            "meta_key": ""
     4259        },
    42464260        "_links": {
    42474261            "self": [
     
    42854299    "taxonomy": "category",
    42864300    "parent": 0,
    4287     "meta": []
     4301    "meta": {
     4302        "meta_key": ""
     4303    }
    42884304};
    42894305
     
    42974313        "slug": "restapi-client-fixture-tag",
    42984314        "taxonomy": "post_tag",
    4299         "meta": [],
     4315        "meta": {
     4316            "meta_key": "meta_value"
     4317        },
    43004318        "_links": {
    43014319            "self": [
     
    43384356    "slug": "restapi-client-fixture-tag",
    43394357    "taxonomy": "post_tag",
    4340     "meta": []
     4358    "meta": {
     4359        "meta_key": "meta_value"
     4360    }
    43414361};
    43424362
     
    43544374            "96": "http://0.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=96&d=mm&r=g"
    43554375        },
    4356         "meta": [],
     4376        "meta": {
     4377            "meta_key": "meta_value"
     4378        },
    43574379        "_links": {
    43584380            "self": [
     
    43804402            "96": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
    43814403        },
    4382         "meta": [],
     4404        "meta": {
     4405            "meta_key": ""
     4406        },
    43834407        "_links": {
    43844408            "self": [
     
    44084432        "96": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
    44094433    },
    4410     "meta": []
     4434    "meta": {
     4435        "meta_key": ""
     4436    }
    44114437};
    44124438
     
    44234449        "96": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
    44244450    },
    4425     "meta": []
     4451    "meta": {
     4452        "meta_key": ""
     4453    }
    44264454};
    44274455
     
    44474475            "96": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=96&d=mm&r=g"
    44484476        },
    4449         "meta": [],
     4477        "meta": {
     4478            "meta_key": "meta_value"
     4479        },
    44504480        "_links": {
    44514481            "self": [
     
    44904520        "96": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=96&d=mm&r=g"
    44914521    },
    4492     "meta": []
     4522    "meta": {
     4523        "meta_key": "meta_value"
     4524    }
    44934525};
    44944526
  • trunk/tests/qunit/wp-includes/js/wp-api.js

    r41657 r41678  
    1 /* global wp */
     1/* global wp, JSON */
    22( function( QUnit ) {
    33    module( 'wpapi' );
     
    382382    } );
    383383
     384
     385    var theModelTypesWithMeta = [
     386        'Posts',
     387        'Comments',
     388        'Tags',
     389        'Users'
     390    ];
     391
     392    _.each( theModelTypesWithMeta, function( modelType ) {
     393
     394        // Test post meta.
     395        wp.api.loadPromise.done( function() {
     396            QUnit.test( 'Check meta support for ' + modelType + '.', function( assert ) {
     397                var theModels = new wp.api.collections[ modelType ]();
     398
     399                theModels.fetch().done( function() {
     400
     401                    // Get the main endpoint.
     402                    var endpoint = theModels.at(0);
     403
     404                    // Verify the meta object returned correctly from `getMetas()`.
     405                    assert.equal( JSON.stringify( endpoint.getMetas() ), '{"meta_key":"meta_value"}', 'Full meta key/values object should be readable.' );
     406
     407                    // Verify single meta returned correctly from `getMeta()`
     408                    assert.equal( endpoint.getMeta( 'meta_key' ), 'meta_value', 'Single meta should be readable by key.' );
     409
     410                    // Verify setting meta values with `setMetas()`.
     411                    endpoint.setMetas( { 'test_key':'test_value' } );
     412                    assert.equal( endpoint.getMeta( 'test_key' ), 'test_value', 'Multiple meta should be writable via setMetas.' );
     413
     414                    // Verify setting a single meta value with `setMeta()`.
     415                    endpoint.setMeta( 'test_key2', 'test_value2' );
     416                    assert.equal( endpoint.getMeta( 'test_key2' ), 'test_value2', 'Single meta should be writable via setMeta.' );
     417
     418                } );
     419            } );
     420        } );
     421    } );
     422
     423
    384424} )( window.QUnit );
Note: See TracChangeset for help on using the changeset viewer.