Make WordPress Core

Ticket #41055: 41055.2.diff

File 41055.2.diff, 6.0 KB (added by adamsilverstein, 9 years ago)
  • src/wp-includes/js/wp-api.js

    diff --git src/wp-includes/js/wp-api.js src/wp-includes/js/wp-api.js
    index 31886d280b..e3c3dc7a4d 100644
     
    501501                         * Add a helper function to handle post Meta.
    502502                         */
    503503                        MetaMixin = {
    504                                 getMeta: function() {
    505                                         return buildCollectionGetter( this, 'PostMeta', 'https://api.w.org/meta' );
    506                                 }
     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 );
     547                                },
    507548                        },
    508549
    509550                        /**
     
    734775                        model = model.extend( CategoriesMixin );
    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                }
    741782
  • tests/phpunit/tests/rest-api/rest-schema-setup.php

    diff --git tests/phpunit/tests/rest-api/rest-schema-setup.php tests/phpunit/tests/rest-api/rest-schema-setup.php
    index 078232328e..6f652b0d3e 100644
    class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase { 
    145145                        'post_excerpt'   => 'REST API Client Fixture: Post',
    146146                        'post_author'    => 0,
    147147                ) );
     148                $args = array(
     149                        'sanitize_callback' => 'sanitize_my_meta_key',
     150                        'auth_callback'     => '__return_true',
     151                        'type'              => 'string',
     152                        'description'       => 'Test meta key',
     153                        'single'            => true,
     154                        'show_in_rest'      => true,
     155                );
     156                register_meta( 'post', 'meta_key', $args );
     157                update_post_meta( $post_id, 'meta_key', 'meta_value' );
    148158                wp_update_post( array(
    149159                        'ID'           => $post_id,
    150160                        'post_content' => 'Updated post content.',
  • tests/qunit/fixtures/wp-api-generated.js

    diff --git tests/qunit/fixtures/wp-api-generated.js tests/qunit/fixtures/wp-api-generated.js
    index 679d024598..aae96457c1 100644
    mockedApiResponse.PostsCollection = [ 
    35603560        "sticky": false,
    35613561        "template": "",
    35623562        "format": "standard",
    3563         "meta": [],
     3563        "meta": {
     3564            "meta_key": "meta_value"
     3565        },
    35643566        "categories": [
    35653567            1
    35663568        ],
    mockedApiResponse.PostModel = { 
    36513653    "sticky": false,
    36523654    "template": "",
    36533655    "format": "standard",
    3654     "meta": [],
     3656    "meta": {
     3657        "meta_key": "meta_value"
     3658    },
    36553659    "categories": [
    36563660        1
    36573661    ],
    mockedApiResponse.PagesCollection = [ 
    37453749        "comment_status": "closed",
    37463750        "ping_status": "closed",
    37473751        "template": "",
    3748         "meta": [],
     3752        "meta": {
     3753            "meta_key": ""
     3754        },
    37493755        "_links": {
    37503756            "self": [
    37513757                {
    mockedApiResponse.PageModel = { 
    38203826    "comment_status": "closed",
    38213827    "ping_status": "closed",
    38223828    "template": "",
    3823     "meta": []
     3829    "meta": {
     3830        "meta_key": ""
     3831    }
    38243832};
    38253833
    38263834mockedApiResponse.pageRevisions = [
    mockedApiResponse.MediaCollection = [ 
    38993907        "comment_status": "open",
    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>"
    39053915        },
    mockedApiResponse.MediaModel = { 
    39583968    "comment_status": "open",
    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>"
    39643976    },
  • tests/qunit/wp-includes/js/wp-api.js

    diff --git tests/qunit/wp-includes/js/wp-api.js tests/qunit/wp-includes/js/wp-api.js
    index e904ccaec8..5b83dbf1e7 100644
     
    343343                } );
    344344        });
    345345
     346        // Test post meta.
     347        wp.api.loadPromise.done( function() {
     348                QUnit.test( 'Check meta support.' , function( assert ) {
     349                        var theModels = new wp.api.collections.Posts();
     350
     351                        theModels.fetch().done( function() {
     352
     353                                // Get the main endpoint.
     354                                var endpoint = theModels.at(0)
     355
     356                                // Verify the meta object returned correctly from `getMetas()`.
     357                                assert.equal( JSON.stringify( endpoint.getMetas() ), '{"meta_key":"meta_value"}', 'Full meta key/values object should be readable.' );
     358
     359                                // Verify single meta returned correctly from `getMeta()`
     360                                assert.equal( endpoint.getMeta( 'meta_key' ), 'meta_value', 'Single meta should be readable by key.' );
     361
     362                                // Verify setting meta values with `setMetas()`.
     363                                endpoint.setMetas( { 'test_key':'test_value' } );
     364                                assert.equal( endpoint.getMeta( 'test_key' ), 'test_value', 'Multiple meta should be writable via setMetas.' );
     365
     366                                // Verify setting a single meta value with `setMeta()`.
     367                                endpoint.setMeta( 'test_key2', 'test_value2' );
     368                                assert.equal( endpoint.getMeta( 'test_key2' ), 'test_value2', 'Single meta should be writable via setMeta.' );
     369
     370                        } );
     371                } );
     372        } );
     373
     374
    346375} )( window.QUnit );