Ticket #41055: 41055.2.diff
| File 41055.2.diff, 6.0 KB (added by , 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
501 501 * Add a helper function to handle post Meta. 502 502 */ 503 503 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 }, 507 548 }, 508 549 509 550 /** … … 734 775 model = model.extend( CategoriesMixin ); 735 776 } 736 777 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 ) ) { 739 780 model = model.extend( MetaMixin ); 740 781 } 741 782 -
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 { 145 145 'post_excerpt' => 'REST API Client Fixture: Post', 146 146 'post_author' => 0, 147 147 ) ); 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' ); 148 158 wp_update_post( array( 149 159 'ID' => $post_id, 150 160 '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 = [ 3560 3560 "sticky": false, 3561 3561 "template": "", 3562 3562 "format": "standard", 3563 "meta": [], 3563 "meta": { 3564 "meta_key": "meta_value" 3565 }, 3564 3566 "categories": [ 3565 3567 1 3566 3568 ], … … mockedApiResponse.PostModel = { 3651 3653 "sticky": false, 3652 3654 "template": "", 3653 3655 "format": "standard", 3654 "meta": [], 3656 "meta": { 3657 "meta_key": "meta_value" 3658 }, 3655 3659 "categories": [ 3656 3660 1 3657 3661 ], … … mockedApiResponse.PagesCollection = [ 3745 3749 "comment_status": "closed", 3746 3750 "ping_status": "closed", 3747 3751 "template": "", 3748 "meta": [], 3752 "meta": { 3753 "meta_key": "" 3754 }, 3749 3755 "_links": { 3750 3756 "self": [ 3751 3757 { … … mockedApiResponse.PageModel = { 3820 3826 "comment_status": "closed", 3821 3827 "ping_status": "closed", 3822 3828 "template": "", 3823 "meta": [] 3829 "meta": { 3830 "meta_key": "" 3831 } 3824 3832 }; 3825 3833 3826 3834 mockedApiResponse.pageRevisions = [ … … mockedApiResponse.MediaCollection = [ 3899 3907 "comment_status": "open", 3900 3908 "ping_status": "closed", 3901 3909 "template": "", 3902 "meta": [], 3910 "meta": { 3911 "meta_key": "" 3912 }, 3903 3913 "description": { 3904 3914 "rendered": "<p class=\"attachment\"><!-- <a...><img.../></a> --></p>" 3905 3915 }, … … mockedApiResponse.MediaModel = { 3958 3968 "comment_status": "open", 3959 3969 "ping_status": "closed", 3960 3970 "template": "", 3961 "meta": [], 3971 "meta": { 3972 "meta_key": "" 3973 }, 3962 3974 "description": { 3963 3975 "rendered": "<p class=\"attachment\"><!-- <a...><img.../></a> --></p>" 3964 3976 }, -
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
343 343 } ); 344 344 }); 345 345 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 346 375 } )( window.QUnit );