diff --git a/src/wp-includes/js/wp-api.js b/src/wp-includes/js/wp-api.js index 3f950a47f5..634db4357e 100644 --- a/src/wp-includes/js/wp-api.js +++ b/src/wp-includes/js/wp-api.js @@ -1112,7 +1112,10 @@ 'PostsRevisions': 'PostRevisions', 'PostsTags': 'PostTags' } - }; + }, + + modelEndpoints = routeModel.get( 'modelEndpoints' ), + modelRegex = new RegExp( '(?:.*[+)]|\/(' + modelEndpoints.join( '|' ) + '))$' ); /** * Iterate thru the routes, picking up models and collections to build. Builds two arrays, @@ -1137,8 +1140,8 @@ index !== ( '/' + routeModel.get( 'versionString' ).slice( 0, -1 ) ) ) { - // Single items end with a regex (or the special case 'me'). - if ( /(?:.*[+)]|\/me)$/.test( index ) ) { + // Single items end with a regex, or a special case word. + if ( modelRegex.test( index ) ) { modelRoutes.push( { index: index, route: route } ); } else { @@ -1360,10 +1363,11 @@ wp.api.init = function( args ) { var endpoint, attributes = {}, deferred, promise; - args = args || {}; - attributes.apiRoot = args.apiRoot || wpApiSettings.root || '/wp-json'; - attributes.versionString = args.versionString || wpApiSettings.versionString || 'wp/v2/'; - attributes.schema = args.schema || null; + args = args || {}; + attributes.apiRoot = args.apiRoot || wpApiSettings.root || '/wp-json'; + attributes.versionString = args.versionString || wpApiSettings.versionString || 'wp/v2/'; + attributes.schema = args.schema || null; + attributes.modelEndpoints = args.modelEndpoints || [ 'me', 'settings' ]; if ( ! attributes.schema && attributes.apiRoot === wpApiSettings.root && attributes.versionString === wpApiSettings.versionString ) { attributes.schema = wpApiSettings.schema; } diff --git a/tests/qunit/fixtures/wp-api.js b/tests/qunit/fixtures/wp-api.js index c28486d421..04c9dd1823 100644 --- a/tests/qunit/fixtures/wp-api.js +++ b/tests/qunit/fixtures/wp-api.js @@ -24,7 +24,9 @@ var pathToData = { 'wp-json/wp/v2/taxonomy': mockedApiResponse.TaxonomyModel, 'wp-json/wp/v2/status': mockedApiResponse.StatusModel, 'wp-json/wp/v2/type': mockedApiResponse.TypeModel, - 'wp-json/js-widgets/v1/': jsWidgetsEndpointSchema + 'wp-json/js-widgets/v1/': jsWidgetsEndpointSchema, + 'wp-json/wp/v2/users/me': mockedApiResponse.me, + 'wp-json/wp/v2/settings': mockedApiResponse.settings }; /** diff --git a/tests/qunit/wp-includes/js/wp-api.js b/tests/qunit/wp-includes/js/wp-api.js index 180942fbce..a64e406a49 100644 --- a/tests/qunit/wp-includes/js/wp-api.js +++ b/tests/qunit/wp-includes/js/wp-api.js @@ -115,7 +115,9 @@ 'Page', 'Post', 'Tag', - 'User' + 'User', + 'UsersMe', + 'Settings' ]; _.each( modelsWithIdsClassNames, function( className ) { @@ -130,12 +132,12 @@ assert.ok( theModel, 'We can instantiate wp.api.models.' + className ); theModel.fetch().done( function( ) { var theModel2 = new wp.api.models[ className ](); - theModel2.set( 'id', theModel.attributes[0].id ); + theModel2.set( 'id', theModel.attributes.id ); theModel2.fetch().done( function() { // We were able to retrieve the model. assert.equal( - theModel.attributes[0].id, + theModel.attributes.id, theModel2.get( 'id' ) , 'We should be able to get a ' + className );