Changeset 41678
- Timestamp:
- 10/02/2017 06:39:30 PM (8 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/js/wp-api.js
r41657 r41678 502 502 */ 503 503 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 ); 506 547 } 507 548 }, … … 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 } -
trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php
r41154 r41678 146 146 'post_author' => 0, 147 147 ) ); 148 148 149 wp_update_post( array( 149 150 'ID' => $post_id, … … 196 197 'comment_author_url' => 'http://lights.example.org/', 197 198 ) ); 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' ); 198 217 199 218 // Generate route data for subsequent QUnit tests. -
trunk/tests/qunit/fixtures/wp-api-generated.js
r41154 r41678 3561 3561 "template": "", 3562 3562 "format": "standard", 3563 "meta": [], 3563 "meta": { 3564 "meta_key": "meta_value" 3565 }, 3564 3566 "categories": [ 3565 3567 1 … … 3652 3654 "template": "", 3653 3655 "format": "standard", 3654 "meta": [], 3656 "meta": { 3657 "meta_key": "meta_value" 3658 }, 3655 3659 "categories": [ 3656 3660 1 … … 3746 3750 "ping_status": "closed", 3747 3751 "template": "", 3748 "meta": [], 3752 "meta": { 3753 "meta_key": "" 3754 }, 3749 3755 "_links": { 3750 3756 "self": [ … … 3821 3827 "ping_status": "closed", 3822 3828 "template": "", 3823 "meta": [] 3829 "meta": { 3830 "meta_key": "" 3831 } 3824 3832 }; 3825 3833 … … 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>" … … 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>" … … 4243 4255 "taxonomy": "category", 4244 4256 "parent": 0, 4245 "meta": [], 4257 "meta": { 4258 "meta_key": "" 4259 }, 4246 4260 "_links": { 4247 4261 "self": [ … … 4285 4299 "taxonomy": "category", 4286 4300 "parent": 0, 4287 "meta": [] 4301 "meta": { 4302 "meta_key": "" 4303 } 4288 4304 }; 4289 4305 … … 4297 4313 "slug": "restapi-client-fixture-tag", 4298 4314 "taxonomy": "post_tag", 4299 "meta": [], 4315 "meta": { 4316 "meta_key": "meta_value" 4317 }, 4300 4318 "_links": { 4301 4319 "self": [ … … 4338 4356 "slug": "restapi-client-fixture-tag", 4339 4357 "taxonomy": "post_tag", 4340 "meta": [] 4358 "meta": { 4359 "meta_key": "meta_value" 4360 } 4341 4361 }; 4342 4362 … … 4354 4374 "96": "http://0.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=96&d=mm&r=g" 4355 4375 }, 4356 "meta": [], 4376 "meta": { 4377 "meta_key": "meta_value" 4378 }, 4357 4379 "_links": { 4358 4380 "self": [ … … 4380 4402 "96": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g" 4381 4403 }, 4382 "meta": [], 4404 "meta": { 4405 "meta_key": "" 4406 }, 4383 4407 "_links": { 4384 4408 "self": [ … … 4408 4432 "96": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g" 4409 4433 }, 4410 "meta": [] 4434 "meta": { 4435 "meta_key": "" 4436 } 4411 4437 }; 4412 4438 … … 4423 4449 "96": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g" 4424 4450 }, 4425 "meta": [] 4451 "meta": { 4452 "meta_key": "" 4453 } 4426 4454 }; 4427 4455 … … 4447 4475 "96": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=96&d=mm&r=g" 4448 4476 }, 4449 "meta": [], 4477 "meta": { 4478 "meta_key": "meta_value" 4479 }, 4450 4480 "_links": { 4451 4481 "self": [ … … 4490 4520 "96": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=96&d=mm&r=g" 4491 4521 }, 4492 "meta": [] 4522 "meta": { 4523 "meta_key": "meta_value" 4524 } 4493 4525 }; 4494 4526 -
trunk/tests/qunit/wp-includes/js/wp-api.js
r41657 r41678 1 /* global wp */1 /* global wp, JSON */ 2 2 ( function( QUnit ) { 3 3 module( 'wpapi' ); … … 382 382 } ); 383 383 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 384 424 } )( window.QUnit );
Note: See TracChangeset
for help on using the changeset viewer.