1 (function( window, undefined ) {
6 * Initialise the WP_API.
10 this.collections = {};
14 window.wp = window.wp || {};
15 wp.api = wp.api || new WP_API();
16 wp.api.versionString = wp.api.versionString || 'wp/v2/';
18 // Alias _includes to _.contains, ensuring it is available if lodash is used.
19 if ( ! _.isFunction( _.includes ) && _.isFunction( _.contains ) ) {
20 _.includes = _.contains;
25 (function( window, undefined ) {
31 window.wp = window.wp || {};
32 wp.api = wp.api || {};
33 wp.api.utils = wp.api.utils || {};
36 * ECMAScript 5 shim, adapted from MDN.
37 * @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
39 if ( ! Date.prototype.toISOString ) {
40 pad = function( number ) {
42 if ( 1 === r.length ) {
49 Date.prototype.toISOString = function() {
50 return this.getUTCFullYear() +
51 '-' + pad( this.getUTCMonth() + 1 ) +
52 '-' + pad( this.getUTCDate() ) +
53 'T' + pad( this.getUTCHours() ) +
54 ':' + pad( this.getUTCMinutes() ) +
55 ':' + pad( this.getUTCSeconds() ) +
56 '.' + String( ( this.getUTCMilliseconds() / 1000 ).toFixed( 3 ) ).slice( 2, 5 ) +
62 * Parse date into ISO8601 format.
66 wp.api.utils.parseISO8601 = function( date ) {
67 var timestamp, struct, i, k,
69 numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ];
71 // ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
72 // before falling back to any implementation-specific date parsing, so that’s what we do, even if native
73 // implementations could be faster.
74 // 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm
75 if ( ( struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec( date ) ) ) {
77 // Avoid NaN timestamps caused by “undefined” values being passed to Date.UTC.
78 for ( i = 0; ( k = numericKeys[i] ); ++i ) {
79 struct[k] = +struct[k] || 0;
82 // Allow undefined days and months.
83 struct[2] = ( +struct[2] || 1 ) - 1;
84 struct[3] = +struct[3] || 1;
86 if ( 'Z' !== struct[8] && undefined !== struct[9] ) {
87 minutesOffset = struct[10] * 60 + struct[11];
89 if ( '+' === struct[9] ) {
90 minutesOffset = 0 - minutesOffset;
94 timestamp = Date.UTC( struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7] );
96 timestamp = Date.parse ? Date.parse( date ) : NaN;
103 * Helper function for getting the root URL.
104 * @return {[type]} [description]
106 wp.api.utils.getRootUrl = function() {
107 return window.location.origin ?
108 window.location.origin + '/' :
109 window.location.protocol + '/' + window.location.host + '/';
113 * Helper for capitalizing strings.
115 wp.api.utils.capitalize = function( str ) {
116 if ( _.isUndefined( str ) ) {
119 return str.charAt( 0 ).toUpperCase() + str.slice( 1 );
123 * Extract a route part based on negative index.
125 * @param {string} route The endpoint route.
126 * @param {int} part The number of parts from the end of the route to retrieve. Default 1.
127 * Example route `/a/b/c`: part 1 is `c`, part 2 is `b`, part 3 is `a`.
129 wp.api.utils.extractRoutePart = function( route, part ) {
134 // Remove versions string from route to avoid returning it.
135 route = route.replace( wp.api.versionString, '' );
136 routeParts = route.split( '/' ).reverse();
137 if ( _.isUndefined( routeParts[ --part ] ) ) {
140 return routeParts[ part ];
144 * Extract a parent name from a passed route.
146 * @param {string} route The route to extract a name from.
148 wp.api.utils.extractParentName = function( route ) {
150 lastSlash = route.lastIndexOf( '_id>[\\d]+)/' );
152 if ( lastSlash < 0 ) {
155 name = route.substr( 0, lastSlash - 1 );
156 name = name.split( '/' );
163 * Add args and options to a model prototype from a route's endpoints.
165 * @param {array} routeEndpoints Array of route endpoints.
166 * @param {Object} modelInstance An instance of the model (or collection)
167 * to add the args to.
169 wp.api.utils.decorateFromRoute = function( routeEndpoints, modelInstance ) {
172 * Build the args based on route endpoint data.
174 _.each( routeEndpoints, function( routeEndpoint ) {
176 // Add post and edit endpoints as model args.
177 if ( _.includes( routeEndpoint.methods, 'POST' ) || _.includes( routeEndpoint.methods, 'PUT' ) ) {
179 // Add any non empty args, merging them into the args object.
180 if ( ! _.isEmpty( routeEndpoint.args ) ) {
182 // Set as default if no args yet.
183 if ( _.isEmpty( modelInstance.prototype.args ) ) {
184 modelInstance.prototype.args = routeEndpoint.args;
187 // We already have args, merge these new args in.
188 modelInstance.prototype.args = _.union( routeEndpoint.args, modelInstance.prototype.defaults );
193 // Add GET method as model options.
194 if ( _.includes( routeEndpoint.methods, 'GET' ) ) {
196 // Add any non empty args, merging them into the defaults object.
197 if ( ! _.isEmpty( routeEndpoint.args ) ) {
199 // Set as default if no defaults yet.
200 if ( _.isEmpty( modelInstance.prototype.options ) ) {
201 modelInstance.prototype.options = routeEndpoint.args;
204 // We already have options, merge these new args in.
205 modelInstance.prototype.options = _.union( routeEndpoint.args, modelInstance.prototype.options );
217 * Add mixins and helpers to models depending on their defaults.
219 * @param {Backbone Model} model The model to attach helpers and mixins to.
220 * @param {string} modelClassName The classname of the constructed model.
221 * @param {Object} loadingObjects An object containing the models and collections we are building.
223 wp.api.utils.addMixinsAndHelpers = function( model, modelClassName, loadingObjects ) {
228 * Array of parseable dates.
232 parseableDates = [ 'date', 'modified', 'date_gmt', 'modified_gmt' ],
235 * Mixin for all content that is time stamped.
237 * This mixin converts between mysql timestamps and JavaScript Dates when syncing a model
238 * to or from the server. For example, a date stored as `2015-12-27T21:22:24` on the server
239 * gets expanded to `Sun Dec 27 2015 14:22:24 GMT-0700 (MST)` when the model is fetched.
241 * @type {{toJSON: toJSON, parse: parse}}.
246 * Prepare a JavaScript Date for transmitting to the server.
248 * This helper function accepts a field and Date object. It converts the passed Date
249 * to an ISO string and sets that on the model field.
251 * @param {Date} date A JavaScript date object. WordPress expects dates in UTC.
252 * @param {string} field The date field to set. One of 'date', 'date_gmt', 'date_modified'
253 * or 'date_modified_gmt'. Optional, defaults to 'date'.
255 setDate: function( date, field ) {
256 var theField = field || 'date';
258 // Don't alter non parsable date fields.
259 if ( _.indexOf( parseableDates, theField ) < 0 ) {
263 this.set( theField, date.toISOString() );
267 * Get a JavaScript Date from the passed field.
269 * WordPress returns 'date' and 'date_modified' in the timezone of the server as well as
270 * UTC dates as 'date_gmt' and 'date_modified_gmt'. Draft posts do not include UTC dates.
272 * @param {string} field The date field to set. One of 'date', 'date_gmt', 'date_modified'
273 * or 'date_modified_gmt'. Optional, defaults to 'date'.
275 getDate: function( field ) {
276 var theField = field || 'date',
277 theISODate = this.get( theField );
279 // Only get date fields and non null values.
280 if ( _.indexOf( parseableDates, theField ) < 0 || _.isNull( theISODate ) ) {
284 return new Date( wp.api.utils.parseISO8601( theISODate ) );
289 * Build a helper function to retrieve related model.
291 * @param {string} parentModel The parent model.
292 * @param {int} modelId The model ID if the object to request
293 * @param {string} modelName The model name to use when constructing the model.
294 * @param {string} embedSourcePoint Where to check the embedds object for _embed data.
295 * @param {string} embedCheckField Which model field to check to see if the model has data.
297 * @return {Deferred.promise} A promise which resolves to the constructed model.
299 buildModelGetter = function( parentModel, modelId, modelName, embedSourcePoint, embedCheckField ) {
300 var getModel, embeddeds, attributes, deferred;
302 deferred = jQuery.Deferred();
303 embeddeds = parentModel.get( '_embedded' ) || {};
305 // Verify that we have a valid object id.
306 if ( ! _.isNumber( modelId ) || 0 === modelId ) {
311 // If we have embedded object data, use that when constructing the getModel.
312 if ( embeddeds[ embedSourcePoint ] ) {
313 attributes = _.findWhere( embeddeds[ embedSourcePoint ], { id: modelId } );
316 // Otherwise use the modelId.
317 if ( ! attributes ) {
318 attributes = { id: modelId };
321 // Create the new getModel model.
322 getModel = new wp.api.models[ modelName ]( attributes );
324 if ( ! getModel.get( embedCheckField ) ) {
326 success: function( getModel ) {
327 deferred.resolve( getModel );
329 error: function( getModel, response ) {
330 deferred.reject( response );
334 // Resolve with the embedded model.
335 deferred.resolve( getModel );
339 return deferred.promise();
343 * Build a helper to retrieve a collection.
345 * @param {string} parentModel The parent model.
346 * @param {string} collectionName The name to use when constructing the collection.
347 * @param {string} embedSourcePoint Where to check the embedds object for _embed data.
348 * @param {string} embedIndex An addiitonal optional index for the _embed data.
350 * @return {Deferred.promise} A promise which resolves to the constructed collection.
352 buildCollectionGetter = function( parentModel, collectionName, embedSourcePoint, embedIndex ) {
354 * Returns a promise that resolves to the requested collection
356 * Uses the embedded data if available, otherwises fetches the
357 * data from the server.
359 * @return {Deferred.promise} promise Resolves to a wp.api.collections[ collectionName ]
362 var postId, embeddeds, getObjects,
363 classProperties = '',
365 deferred = jQuery.Deferred();
367 postId = parentModel.get( 'id' );
368 embeddeds = parentModel.get( '_embedded' ) || {};
370 // Verify that we have a valid post id.
371 if ( ! _.isNumber( postId ) || 0 === postId ) {
376 // If we have embedded getObjects data, use that when constructing the getObjects.
377 if ( ! _.isUndefined( embedSourcePoint ) && ! _.isUndefined( embeddeds[ embedSourcePoint ] ) ) {
379 // Some embeds also include an index offset, check for that.
380 if ( _.isUndefined( embedIndex ) ) {
382 // Use the embed source point directly.
383 properties = embeddeds[ embedSourcePoint ];
386 // Add the index to the embed source point.
387 properties = embeddeds[ embedSourcePoint ][ embedIndex ];
391 // Otherwise use the postId.
392 classProperties = { parent: postId };
395 // Create the new getObjects collection.
396 getObjects = new wp.api.collections[ collectionName ]( properties, classProperties );
398 // If we didn’t have embedded getObjects, fetch the getObjects data.
399 if ( _.isUndefined( getObjects.models[0] ) ) {
401 success: function( getObjects ) {
403 // Add a helper 'parent_post' attribute onto the model.
404 setHelperParentPost( getObjects, postId );
405 deferred.resolve( getObjects );
407 error: function( getModel, response ) {
408 deferred.reject( response );
413 // Add a helper 'parent_post' attribute onto the model.
414 setHelperParentPost( getObjects, postId );
415 deferred.resolve( getObjects );
419 return deferred.promise();
424 * Set the model post parent.
426 setHelperParentPost = function( collection, postId ) {
428 // Attach post_parent id to the collection.
429 _.each( collection.models, function( model ) {
430 model.set( 'parent_post', postId );
435 * Add a helper function to handle post Meta.
438 getMeta: function() {
439 return buildCollectionGetter( this, 'PostMeta', 'https://api.w.org/meta' );
444 * Add a helper function to handle post Revisions.
447 getRevisions: function() {
448 return buildCollectionGetter( this, 'PostRevisions' );
453 * Add a helper function to handle post Tags.
458 * Get the tags for a post.
460 * @return {Deferred.promise} promise Resolves to an array of tags.
462 getTags: function() {
463 var tagIds = this.get( 'tags' ),
464 tags = new wp.api.collections.Tags();
466 // Resolve with an empty array if no tags.
467 if ( _.isEmpty( tagIds ) ) {
468 return jQuery.Deferred().resolve( [] );
471 return tags.fetch( { data: { include: tagIds } } );
475 * Set the tags for a post.
477 * Accepts an array of tag slugs, or a Tags collection.
479 * @param {array|Backbone.Collection} tags The tags to set on the post.
482 setTags: function( tags ) {
487 if ( _.isString( tags ) ) {
491 // If this is an array of slugs, build a collection.
492 if ( _.isArray( tags ) ) {
495 allTags = new wp.api.collections.Tags();
497 data: { per_page: 100 },
498 success: function( alltags ) {
500 // Find the passed tags and set them up.
501 _.each( tags, function( tag ) {
502 newTag = new wp.api.models.Tag( alltags.findWhere( { slug: tag } ) );
504 // Tie the new tag to the post.
505 newTag.set( 'parent_post', self.get( 'id' ) );
507 // Add the new tag to the collection.
508 newTags.push( newTag );
510 tags = new wp.api.collections.Tags( newTags );
511 self.setTagsWithCollection( tags );
516 this.setTagsWithCollection( tags );
521 * Set the tags for a post.
523 * Accepts a Tags collection.
525 * @param {array|Backbone.Collection} tags The tags to set on the post.
528 setTagsWithCollection: function( tags ) {
530 // Pluck out the category ids.
531 this.set( 'tags', tags.pluck( 'id' ) );
537 * Add a helper function to handle post Categories.
542 * Get a the categories for a post.
544 * @return {Deferred.promise} promise Resolves to an array of categories.
546 getCategories: function() {
547 var categoryIds = this.get( 'categories' ),
548 categories = new wp.api.collections.Categories();
550 // Resolve with an empty array if no categories.
551 if ( _.isEmpty( categoryIds ) ) {
552 return jQuery.Deferred().resolve( [] );
555 return categories.fetch( { data: { include: categoryIds } } );
559 * Set the categories for a post.
561 * Accepts an array of category slugs, or a Categories collection.
563 * @param {array|Backbone.Collection} categories The categories to set on the post.
566 setCategories: function( categories ) {
567 var allCategories, newCategory,
571 if ( _.isString( categories ) ) {
575 // If this is an array of slugs, build a collection.
576 if ( _.isArray( categories ) ) {
578 // Get all the categories.
579 allCategories = new wp.api.collections.Categories();
580 allCategories.fetch( {
581 data: { per_page: 100 },
582 success: function( allcats ) {
584 // Find the passed categories and set them up.
585 _.each( categories, function( category ) {
586 newCategory = new wp.api.models.Category( allcats.findWhere( { slug: category } ) );
588 // Tie the new category to the post.
589 newCategory.set( 'parent_post', self.get( 'id' ) );
591 // Add the new category to the collection.
592 newCategories.push( newCategory );
594 categories = new wp.api.collections.Categories( newCategories );
595 self.setCategoriesWithCollection( categories );
600 this.setCategoriesWithCollection( categories );
606 * Set the categories for a post.
608 * Accepts Categories collection.
610 * @param {array|Backbone.Collection} categories The categories to set on the post.
613 setCategoriesWithCollection: function( categories ) {
615 // Pluck out the category ids.
616 this.set( 'categories', categories.pluck( 'id' ) );
622 * Add a helper function to retrieve the author user model.
625 getAuthorUser: function() {
626 return buildModelGetter( this, this.get( 'author' ), 'User', 'author', 'name' );
631 * Add a helper function to retrieve the featured media.
633 FeaturedMediaMixin = {
634 getFeaturedMedia: function() {
635 return buildModelGetter( this, this.get( 'featured_media' ), 'Media', 'wp:featuredmedia', 'source_url' );
639 // Exit if we don't have valid model defaults.
640 if ( _.isUndefined( model.prototype.args ) ) {
644 // Go thru the parsable date fields, if our model contains any of them it gets the TimeStampedMixin.
645 _.each( parseableDates, function( theDateKey ) {
646 if ( ! _.isUndefined( model.prototype.args[ theDateKey ] ) ) {
651 // Add the TimeStampedMixin for models that contain a date field.
653 model = model.extend( TimeStampedMixin );
656 // Add the AuthorMixin for models that contain an author.
657 if ( ! _.isUndefined( model.prototype.args.author ) ) {
658 model = model.extend( AuthorMixin );
661 // Add the FeaturedMediaMixin for models that contain a featured_media.
662 if ( ! _.isUndefined( model.prototype.args.featured_media ) ) {
663 model = model.extend( FeaturedMediaMixin );
666 // Add the CategoriesMixin for models that support categories collections.
667 if ( ! _.isUndefined( model.prototype.args.categories ) ) {
668 model = model.extend( CategoriesMixin );
671 // Add the MetaMixin for models that support meta collections.
672 if ( ! _.isUndefined( loadingObjects.collections[ modelClassName + 'Meta' ] ) ) {
673 model = model.extend( MetaMixin );
676 // Add the TagsMixin for models that support tags collections.
677 if ( ! _.isUndefined( model.prototype.args.tags ) ) {
678 model = model.extend( TagsMixin );
681 // Add the RevisionsMixin for models that support revisions collections.
682 if ( ! _.isUndefined( loadingObjects.collections[ modelClassName + 'Revisions' ] ) ) {
683 model = model.extend( RevisionsMixin );
691 /* global wpApiSettings:false */
693 // Suppress warning about parse function's unused "options" argument:
694 /* jshint unused:false */
699 var wpApiSettings = window.wpApiSettings || {};
702 * Backbone base model for all models.
704 wp.api.WPApiBaseModel = Backbone.Model.extend(
705 /** @lends WPApiBaseModel.prototype */
708 * Set nonce header before every Backbone sync.
710 * @param {string} method.
711 * @param {Backbone.Model} model.
712 * @param {{beforeSend}, *} options.
715 sync: function( method, model, options ) {
718 options = options || {};
720 // Remove date_gmt if null.
721 if ( _.isNull( model.get( 'date_gmt' ) ) ) {
722 model.unset( 'date_gmt' );
725 // Remove slug if empty.
726 if ( _.isEmpty( model.get( 'slug' ) ) ) {
727 model.unset( 'slug' );
730 if ( ! _.isUndefined( wpApiSettings.nonce ) && ! _.isNull( wpApiSettings.nonce ) ) {
731 beforeSend = options.beforeSend;
733 // @todo enable option for jsonp endpoints
734 // options.dataType = 'jsonp';
736 options.beforeSend = function( xhr ) {
737 xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
740 return beforeSend.apply( this, arguments );
745 // Add '?force=true' to use delete method when required.
746 if ( this.requireForceForDelete && 'delete' === method ) {
747 model.url = model.url() + '?force=true';
749 return Backbone.sync( method, model, options );
753 * Save is only allowed when the PUT OR POST methods are available for the endpoint.
755 save: function( attrs, options ) {
757 // Do we have the put method, then execute the save.
758 if ( _.includes( this.methods, 'PUT' ) || _.includes( this.methods, 'POST' ) ) {
760 // Proxy the call to the original save function.
761 return Backbone.Model.prototype.save.call( this, attrs, options );
764 // Otherwise bail, disallowing action.
770 * Delete is only allowed when the DELETE method is available for the endpoint.
772 destroy: function( options ) {
774 // Do we have the DELETE method, then execute the destroy.
775 if ( _.includes( this.methods, 'DELETE' ) ) {
777 // Proxy the call to the original save function.
778 return Backbone.Model.prototype.destroy.call( this, options );
781 // Otherwise bail, disallowing action.
790 * API Schema model. Contains meta information about the API.
792 wp.api.models.Schema = wp.api.WPApiBaseModel.extend(
793 /** @lends Schema.prototype */
801 initialize: function( attributes, options ) {
803 options = options || {};
805 wp.api.WPApiBaseModel.prototype.initialize.call( model, attributes, options );
807 model.apiRoot = options.apiRoot || wpApiSettings.root;
808 model.versionString = options.versionString || wpApiSettings.versionString;
812 return this.apiRoot + this.versionString;
822 var wpApiSettings = window.wpApiSettings || {};
825 * Contains basic collection functionality such as pagination.
827 wp.api.WPApiBaseCollection = Backbone.Collection.extend(
828 /** @lends BaseCollection.prototype */
832 * Setup default state.
834 initialize: function( models, options ) {
841 if ( _.isUndefined( options ) ) {
844 this.parent = options.parent;
849 * Extend Backbone.Collection.sync to add nince and pagination support.
851 * Set nonce header before every Backbone sync.
853 * @param {string} method.
854 * @param {Backbone.Model} model.
855 * @param {{success}, *} options.
858 sync: function( method, model, options ) {
859 var beforeSend, success,
862 options = options || {};
863 beforeSend = options.beforeSend;
865 // If we have a localized nonce, pass that along with each sync.
866 if ( 'undefined' !== typeof wpApiSettings.nonce ) {
867 options.beforeSend = function( xhr ) {
868 xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
871 return beforeSend.apply( self, arguments );
876 // When reading, add pagination data.
877 if ( 'read' === method ) {
878 if ( options.data ) {
879 self.state.data = _.clone( options.data );
881 delete self.state.data.page;
883 self.state.data = options.data = {};
886 if ( 'undefined' === typeof options.data.page ) {
887 self.state.currentPage = null;
888 self.state.totalPages = null;
889 self.state.totalObjects = null;
891 self.state.currentPage = options.data.page - 1;
894 success = options.success;
895 options.success = function( data, textStatus, request ) {
896 if ( ! _.isUndefined( request ) ) {
897 self.state.totalPages = parseInt( request.getResponseHeader( 'x-wp-totalpages' ), 10 );
898 self.state.totalObjects = parseInt( request.getResponseHeader( 'x-wp-total' ), 10 );
901 if ( null === self.state.currentPage ) {
902 self.state.currentPage = 1;
904 self.state.currentPage++;
908 return success.apply( this, arguments );
913 // Continue by calling Bacckbone's sync.
914 return Backbone.sync( method, model, options );
918 * Fetches the next page of objects if a new page exists.
920 * @param {data: {page}} options.
923 more: function( options ) {
924 options = options || {};
925 options.data = options.data || {};
927 _.extend( options.data, this.state.data );
929 if ( 'undefined' === typeof options.data.page ) {
930 if ( ! this.hasMore() ) {
934 if ( null === this.state.currentPage || this.state.currentPage <= 1 ) {
935 options.data.page = 2;
937 options.data.page = this.state.currentPage + 1;
941 return this.fetch( options );
945 * Returns true if there are more pages of objects available.
947 * @returns null|boolean.
949 hasMore: function() {
950 if ( null === this.state.totalPages ||
951 null === this.state.totalObjects ||
952 null === this.state.currentPage ) {
955 return ( this.state.currentPage < this.state.totalPages );
967 var Endpoint, initializedDeferreds = {},
968 wpApiSettings = window.wpApiSettings || {};
969 window.wp = window.wp || {};
970 wp.api = wp.api || {};
972 // If wpApiSettings is unavailable, try the default.
973 if ( _.isEmpty( wpApiSettings ) ) {
974 wpApiSettings.root = window.location.origin + '/wp-json/';
977 Endpoint = Backbone.Model.extend( {
979 apiRoot: wpApiSettings.root,
980 versionString: wp.api.versionString,
987 * Initialize the Endpoint model.
989 initialize: function() {
990 var model = this, deferred;
992 Backbone.Model.prototype.initialize.apply( model, arguments );
994 deferred = jQuery.Deferred();
995 model.schemaConstructed = deferred.promise();
997 model.schemaModel = new wp.api.models.Schema( null, {
998 apiRoot: model.get( 'apiRoot' ),
999 versionString: model.get( 'versionString' )
1002 // When the model loads, resolve the promise.
1003 model.schemaModel.once( 'change', function() {
1004 model.constructFromSchema();
1005 deferred.resolve( model );
1008 if ( model.get( 'schema' ) ) {
1010 // Use schema supplied as model attribute.
1011 model.schemaModel.set( model.schemaModel.parse( model.get( 'schema' ) ) );
1013 ! _.isUndefined( sessionStorage ) &&
1014 ( _.isUndefined( wpApiSettings.cacheSchema ) || wpApiSettings.cacheSchema ) &&
1015 sessionStorage.getItem( 'wp-api-schema-model' + model.get( 'apiRoot' ) + model.get( 'versionString' ) )
1018 // Used a cached copy of the schema model if available.
1019 model.schemaModel.set( model.schemaModel.parse( JSON.parse( sessionStorage.getItem( 'wp-api-schema-model' + model.get( 'apiRoot' ) + model.get( 'versionString' ) ) ) ) );
1021 model.schemaModel.fetch( {
1023 * When the server returns the schema model data, store the data in a sessionCache so we don't
1024 * have to retrieve it again for this session. Then, construct the models and collections based
1025 * on the schema model data.
1027 success: function( newSchemaModel ) {
1029 // Store a copy of the schema model in the session cache if available.
1030 if ( ! _.isUndefined( sessionStorage ) && ( _.isUndefined( wpApiSettings.cacheSchema ) || wpApiSettings.cacheSchema ) ) {
1032 sessionStorage.setItem( 'wp-api-schema-model' + model.get( 'apiRoot' ) + model.get( 'versionString' ), JSON.stringify( newSchemaModel ) );
1035 // Fail silently, fixes errors in safari private mode.
1040 // Log the error condition.
1041 error: function( err ) {
1042 window.console.log( err );
1048 constructFromSchema: function() {
1049 var routeModel = this, modelRoutes, collectionRoutes, schemaRoot, loadingObjects,
1052 * Set up the model and collection name mapping options. As the schema is built, the
1053 * model and collection names will be adjusted if they are found in the mapping object.
1055 * Localizing a variable wpApiSettings.mapping will over-ride the default mapping options.
1058 mapping = wpApiSettings.mapping || {
1060 'Categories': 'Category',
1061 'Comments': 'Comment',
1063 'PagesMeta': 'PageMeta',
1064 'PagesRevisions': 'PageRevision',
1066 'PostsCategories': 'PostCategory',
1067 'PostsRevisions': 'PostRevision',
1068 'PostsTags': 'PostTag',
1070 'Statuses': 'Status',
1072 'Taxonomies': 'Taxonomy',
1077 'PagesMeta': 'PageMeta',
1078 'PagesRevisions': 'PageRevisions',
1079 'PostsCategories': 'PostCategories',
1080 'PostsMeta': 'PostMeta',
1081 'PostsRevisions': 'PostRevisions',
1082 'PostsTags': 'PostTags'
1087 * Iterate thru the routes, picking up models and collections to build. Builds two arrays,
1088 * one for models and one for collections.
1091 collectionRoutes = [];
1092 schemaRoot = routeModel.get( 'apiRoot' ).replace( wp.api.utils.getRootUrl(), '' );
1093 loadingObjects = {};
1096 * Tracking objects for models and collections.
1098 loadingObjects.models = routeModel.get( 'models' );
1099 loadingObjects.collections = routeModel.get( 'collections' );
1101 _.each( routeModel.schemaModel.get( 'routes' ), function( route, index ) {
1103 // Skip the schema root if included in the schema.
1104 if ( index !== routeModel.get( ' versionString' ) &&
1105 index !== schemaRoot &&
1106 index !== ( '/' + routeModel.get( 'versionString' ).slice( 0, -1 ) )
1109 // Single items end with a regex (or the special case 'me').
1110 if ( /(?:.*[+)]|\/me)$/.test( index ) ) {
1111 modelRoutes.push( { index: index, route: route } );
1114 // Collections end in a name.
1115 collectionRoutes.push( { index: index, route: route } );
1121 * Construct the models.
1123 * Base the class name on the route endpoint.
1125 _.each( modelRoutes, function( modelRoute ) {
1127 // Extract the name and any parent from the route.
1129 routeName = wp.api.utils.extractRoutePart( modelRoute.index, 2 ),
1130 parentName = wp.api.utils.extractRoutePart( modelRoute.index, 4 ),
1131 routeEnd = wp.api.utils.extractRoutePart( modelRoute.index, 1 );
1133 // Handle the special case of the 'me' route.
1134 if ( 'me' === routeEnd ) {
1138 // If the model has a parent in its route, add that to its class name.
1139 if ( '' !== parentName && parentName !== routeName ) {
1140 modelClassName = wp.api.utils.capitalize( parentName ) + wp.api.utils.capitalize( routeName );
1141 modelClassName = mapping.models[ modelClassName ] || modelClassName;
1142 loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( {
1144 // Return a constructed url based on the parent and id.
1146 var url = routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) +
1148 ( ( _.isUndefined( this.get( 'parent' ) ) || 0 === this.get( 'parent' ) ) ?
1149 this.get( 'parent_post' ) :
1150 this.get( 'parent' ) ) + '/' +
1152 if ( ! _.isUndefined( this.get( 'id' ) ) ) {
1153 url += '/' + this.get( 'id' );
1158 // Include a reference to the original route object.
1161 // Include a reference to the original class name.
1162 name: modelClassName,
1164 // Include the array of route methods for easy reference.
1165 methods: modelRoute.route.methods,
1167 initialize: function() {
1170 * Posts and pages support trashing, other types don't support a trash
1171 * and require that you pass ?force=true to actually delete them.
1173 * @todo we should be getting trashability from the Schema, not hard coding types here.
1176 'Posts' !== this.name &&
1177 'Pages' !== this.name &&
1178 _.includes( this.methods, 'DELETE' )
1180 this.requireForceForDelete = true;
1186 // This is a model without a parent in its route
1187 modelClassName = wp.api.utils.capitalize( routeName );
1188 modelClassName = mapping.models[ modelClassName ] || modelClassName;
1189 loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( {
1191 // Function that returns a constructed url based on the id.
1193 var url = routeModel.get( 'apiRoot' ) +
1194 routeModel.get( 'versionString' ) +
1195 ( ( 'me' === routeName ) ? 'users/me' : routeName );
1197 if ( ! _.isUndefined( this.get( 'id' ) ) ) {
1198 url += '/' + this.get( 'id' );
1203 // Include a reference to the original route object.
1206 // Include a reference to the original class name.
1207 name: modelClassName,
1209 // Include the array of route methods for easy reference.
1210 methods: modelRoute.route.methods
1214 // Add defaults to the new model, pulled form the endpoint.
1215 wp.api.utils.decorateFromRoute( modelRoute.route.endpoints, loadingObjects.models[ modelClassName ] );
1220 * Construct the collections.
1222 * Base the class name on the route endpoint.
1224 _.each( collectionRoutes, function( collectionRoute ) {
1226 // Extract the name and any parent from the route.
1227 var collectionClassName, modelClassName,
1228 routeName = collectionRoute.index.slice( collectionRoute.index.lastIndexOf( '/' ) + 1 ),
1229 parentName = wp.api.utils.extractRoutePart( collectionRoute.index, 3 );
1231 // If the collection has a parent in its route, add that to its class name.
1232 if ( '' !== parentName && parentName !== routeName ) {
1234 collectionClassName = wp.api.utils.capitalize( parentName ) + wp.api.utils.capitalize( routeName );
1235 modelClassName = mapping.models[ collectionClassName ] || collectionClassName;
1236 collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName;
1237 loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( {
1239 // Function that returns a constructed url passed on the parent.
1241 return routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) +
1242 parentName + '/' + this.parent + '/' +
1246 // Specify the model that this collection contains.
1247 model: function( attrs, options ) {
1248 return new loadingObjects.models[ modelClassName ]( attrs, options );
1251 // Include a reference to the original class name.
1252 name: collectionClassName,
1254 // Include a reference to the original route object.
1255 route: collectionRoute,
1257 // Include the array of route methods for easy reference.
1258 methods: collectionRoute.route.methods
1262 // This is a collection without a parent in its route.
1263 collectionClassName = wp.api.utils.capitalize( routeName );
1264 modelClassName = mapping.models[ collectionClassName ] || collectionClassName;
1265 collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName;
1266 loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( {
1268 // For the url of a root level collection, use a string.
1269 url: routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) + routeName,
1271 // Specify the model that this collection contains.
1272 model: function( attrs, options ) {
1273 return new loadingObjects.models[ modelClassName ]( attrs, options );
1276 // Include a reference to the original class name.
1277 name: collectionClassName,
1279 // Include a reference to the original route object.
1280 route: collectionRoute,
1282 // Include the array of route methods for easy reference.
1283 methods: collectionRoute.route.methods
1287 // Add defaults to the new model, pulled form the endpoint.
1288 wp.api.utils.decorateFromRoute( collectionRoute.route.endpoints, loadingObjects.collections[ collectionClassName ] );
1291 // Add mixins and helpers for each of the models.
1292 _.each( loadingObjects.models, function( model, index ) {
1293 loadingObjects.models[ index ] = wp.api.utils.addMixinsAndHelpers( model, index, loadingObjects );
1300 wp.api.endpoints = new Backbone.Collection( {
1305 * Initialize the wp-api, optionally passing the API root.
1307 * @param {object} [args]
1308 * @param {string} [args.apiRoot] The api root. Optional, defaults to wpApiSettings.root.
1309 * @param {string} [args.versionString] The version string. Optional, defaults to wpApiSettings.root.
1310 * @param {object} [args.schema] The schema. Optional, will be fetched from API if not provided.
1312 wp.api.init = function( args ) {
1313 var endpoint, attributes = {}, deferred, promise;
1316 attributes.apiRoot = args.apiRoot || wpApiSettings.root;
1317 attributes.versionString = args.versionString || wpApiSettings.versionString;
1318 attributes.schema = args.schema || null;
1319 if ( ! attributes.schema && attributes.apiRoot === wpApiSettings.root && attributes.versionString === wpApiSettings.versionString ) {
1320 attributes.schema = wpApiSettings.schema;
1323 if ( ! initializedDeferreds[ attributes.apiRoot + attributes.versionString ] ) {
1324 endpoint = wp.api.endpoints.findWhere( { apiRoot: attributes.apiRoot, versionString: attributes.versionString } );
1326 endpoint = new Endpoint( attributes );
1327 wp.api.endpoints.add( endpoint );
1329 deferred = jQuery.Deferred();
1330 promise = deferred.promise();
1332 endpoint.schemaConstructed.done( function( endpoint ) {
1334 // Map the default endpoints, extending any already present items (including Schema model).
1335 wp.api.models = _.extend( endpoint.get( 'models' ), wp.api.models );
1336 wp.api.collections = _.extend( endpoint.get( 'collections' ), wp.api.collections );
1337 deferred.resolveWith( wp.api, [ endpoint ] );
1339 initializedDeferreds[ attributes.apiRoot + attributes.versionString ] = promise;
1341 return initializedDeferreds[ attributes.apiRoot + attributes.versionString ];
1345 * Construct the default endpoints and add to an endpoints collection.
1348 // The wp.api.init function returns a promise that will resolve with the endpoint once it is ready.
1349 wp.api.loadPromise = wp.api.init();