wp-api.js 8.1 KB

1
  1. !function(a){"use strict";function b(){this.models={},this.collections={},this.views={}}a.wp=a.wp||{},wp.api=wp.api||new b}(window),function(a,b,c,d){function e(){var a=Date.parse,b=[1,4,5,6,7,10,11];this.parseISO8601=function(c){var e,f,g,h,i=0;if(f=/^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(c)){for(g=0;h=b[g];++g)f[h]=+f[h]||0;f[2]=(+f[2]||1)-1,f[3]=+f[3]||1,"Z"!==f[8]&&f[9]!==d&&(i=60*f[10]+f[11],"+"===f[9]&&(i=0-i)),e=Date.UTC(f[1],f[2],f[3],f[4],f[5]+i,f[6],f[7])}else e=a?a(c):0/0;return e}}if(!Date.prototype.toISOString){var f=function(a){var b=String(a);return 1===b.length&&(b="0"+b),b};Date.prototype.toISOString=function(){return this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"."+String((this.getUTCMilliseconds()/1e3).toFixed(3)).slice(2,5)+"Z"}}c.wp=c.wp||{},wp.api=wp.api||{},wp.api.utils=wp.api.utils||new e}(Backbone,_,window),function(a,b,c,d){"use strict";var e=["date","modified","date_gmt","modified_gmt"],f={toJSON:function(){var a=d.clone(this.attributes);return d.each(e,function(b){b in a&&(a[b]=a[b].toISOString())}),a},parse:function(b){return d.each(e,function(c){if(c in b){var d=a.api.utils.parseISO8601(b[c]);b[c]=new Date(d)}}),"undefined"!==b.author&&(b.author=new a.api.models.User(b.author)),b}},g={parent:function(){var a,b=this.get("parent");if(0===b)return null;var c=this;return"undefined"!=typeof this.parentModel&&(c=new this.parentModel),c.collection?c.collection.get(b):(a=new c.constructor({ID:b}),a.fetch(),a)}},h=c.Model.extend({sync:function(a,d,e){if(e=e||{},"undefined"!=typeof b.nonce){var f=e.beforeSend;e.beforeSend=function(a){return a.setRequestHeader("X-WP-Nonce",b.nonce),f?f.apply(this,arguments):void 0}}return c.sync(a,d,e)}});a.api.models.User=h.extend({idAttribute:"ID",urlRoot:b.root+"/users",defaults:{ID:null,username:"",email:"",password:"",name:"",first_name:"",last_name:"",nickname:"",slug:"",URL:"",avatar:"",meta:{links:{}}},avatar:function(a){return this.get("avatar")+"&s="+a}}),a.api.models.Taxonomy=h.extend({idAttribute:"slug",urlRoot:b.root+"/taxonomies",defaults:{name:"",slug:null,labels:{},types:{},show_cloud:!1,hierarchical:!1,meta:{links:{}}}}),a.api.models.Term=h.extend(d.extend({idAttribute:"ID",taxonomy:"category",initialize:function(a,b){"undefined"!=typeof b&&b.taxonomy&&(this.taxonomy=b.taxonomy)},url:function(){var a=this.get("ID");return a=a||"",b.root+"/taxonomies/"+this.taxonomy+"/terms/"+a},defaults:{ID:null,name:"",slug:"",description:"",parent:null,count:0,link:"",meta:{links:{}}}},f,g)),a.api.models.Post=h.extend(d.extend({idAttribute:"ID",urlRoot:b.root+"/posts",defaults:{ID:null,title:"",status:"draft",type:"post",author:new a.api.models.User,content:"",link:"",parent:0,date:new Date,date_gmt:new Date,modified:new Date,modified_gmt:new Date,format:"standard",slug:"",guid:"",excerpt:"",menu_order:0,comment_status:"open",ping_status:"open",sticky:!1,date_tz:"Etc/UTC",modified_tz:"Etc/UTC",featured_image:null,terms:{},post_meta:{},meta:{links:{}}}},f,g)),a.api.models.Page=h.extend(d.extend({idAttribute:"ID",urlRoot:b.root+"/pages",defaults:{ID:null,title:"",status:"draft",type:"page",author:new a.api.models.User,content:"",parent:0,link:"",date:new Date,modified:new Date,date_gmt:new Date,modified_gmt:new Date,date_tz:"Etc/UTC",modified_tz:"Etc/UTC",format:"standard",slug:"",guid:"",excerpt:"",menu_order:0,comment_status:"closed",ping_status:"open",sticky:!1,password:"",meta:{links:{}},featured_image:null,terms:[]}},f,g)),a.api.models.Revision=a.api.models.Post.extend({url:function(){var a=this.get("parent");a=a||"";var c=this.get("ID");return c=c||"",b.root+"/posts/"+a+"/revisions/"+c},initialize:function(){this.parentModel=a.api.models.Post}}),a.api.models.Media=h.extend(d.extend({idAttribute:"ID",urlRoot:b.root+"/media",defaults:{ID:null,title:"",status:"inherit",type:"attachment",author:new a.api.models.User,content:"",parent:0,link:"",date:new Date,modified:new Date,format:"standard",slug:"",guid:"",excerpt:"",menu_order:0,comment_status:"open",ping_status:"open",sticky:!1,date_tz:"Etc/UTC",modified_tz:"Etc/UTC",date_gmt:new Date,modified_gmt:new Date,meta:{links:{}},terms:[],source:"",is_image:!0,attachment_meta:{},image_meta:{}},initialize:function(){this.parentModel=a.api.models.Post}},f,g)),a.api.models.Comment=h.extend(d.extend({idAttribute:"ID",defaults:{ID:null,post:null,content:"",status:"hold",type:"",parent:0,author:new a.api.models.User,date:new Date,date_gmt:new Date,date_tz:"Etc/UTC",meta:{links:{}}},url:function(){var a=this.get("post");a=a||"";var c=this.get("ID");return c=c||"",b.root+"/posts/"+a+"/comments/"+c}},f,g)),a.api.models.PostType=h.extend({idAttribute:"slug",urlRoot:b.root+"/posts/types",defaults:{slug:null,name:"",description:"",labels:{},queryable:!1,searchable:!1,hierarchical:!1,meta:{links:{}},taxonomies:[]},save:function(){return!1},"delete":function(){return!1}}),a.api.models.PostStatus=h.extend({idAttribute:"slug",urlRoot:b.root+"/posts/statuses",defaults:{slug:null,name:"","public":!0,"protected":!1,"private":!1,queryable:!0,show_in_list:!0,meta:{links:{}}},save:function(){return!1},"delete":function(){return!1}})}(wp,WP_API_Settings,Backbone,_,window),function(a,b,c,d){"use strict";var e=c.Collection.extend({initialize:function(){this.state={data:{},currentPage:null,totalPages:null,totalObjects:null}},sync:function(a,e,f){f=f||{};var g=f.beforeSend;if("undefined"!=typeof b.nonce&&(f.beforeSend=function(a){return a.setRequestHeader("X-WP-Nonce",b.nonce),g?g.apply(this,arguments):void 0}),"read"===a){var h=this;f.data?(h.state.data=d.clone(f.data),delete h.state.data.page):h.state.data=f.data={},"undefined"==typeof f.data.page?(h.state.currentPage=null,h.state.totalPages=null,h.state.totalObjects=null):h.state.currentPage=f.data.page-1;var i=f.success;f.success=function(a,b,c){return h.state.totalPages=parseInt(c.getResponseHeader("X-WP-TotalPages"),10),h.state.totalObjects=parseInt(c.getResponseHeader("X-WP-Total"),10),null===h.state.currentPage?h.state.currentPage=1:h.state.currentPage++,i?i.apply(this,arguments):void 0}}return c.sync(a,e,f)},more:function(a){if(a=a||{},a.data=a.data||{},d.extend(a.data,this.state.data),"undefined"==typeof a.data.page){if(!this.hasMore())return!1;a.data.page=null===this.state.currentPage||this.state.currentPage<=1?2:this.state.currentPage+1}return this.fetch(a)},hasMore:function(){return null===this.state.totalPages||null===this.state.totalObjects||null===this.state.currentPage?null:this.state.currentPage<this.state.totalPages}});a.api.collections.Posts=e.extend({url:b.root+"/posts",model:a.api.models.Post}),a.api.collections.Pages=e.extend({url:b.root+"/pages",model:a.api.models.Page}),a.api.collections.Users=e.extend({url:b.root+"/users",model:a.api.models.User}),a.api.collections.PostStatuses=e.extend({url:b.root+"/posts/statuses",model:a.api.models.PostStatus}),a.api.collections.MediaLibrary=e.extend({url:b.root+"/media",model:a.api.models.Media}),a.api.collections.Taxonomies=e.extend({model:a.api.models.Taxonomy,url:b.root+"/taxonomies"}),a.api.collections.Comments=e.extend({model:a.api.models.Comment,post:null,initialize:function(a,b){this.constructor.__super__.initialize.apply(this,arguments),b&&b.post&&(this.post=b.post)},url:function(){return b.root+"/posts/"+this.post+"/comments"}}),a.api.collections.PostTypes=e.extend({model:a.api.models.PostType,url:b.root+"/posts/types"}),a.api.collections.Terms=e.extend({model:a.api.models.Term,type:"post",taxonomy:"category",initialize:function(a,b){this.constructor.__super__.initialize.apply(this,arguments),"undefined"!=typeof b&&(b.type&&(this.type=b.type),b.taxonomy&&(this.taxonomy=b.taxonomy)),this.on("add",d.bind(this.addModel,this))},addModel:function(a){a.type=this.type,a.taxonomy=this.taxonomy},url:function(){return b.root+"/posts/types/"+this.type+"/taxonomies/"+this.taxonomy+"/terms/"}}),a.api.collections.Revisions=e.extend({model:a.api.models.Revision,parent:null,initialize:function(a,b){this.constructor.__super__.initialize.apply(this,arguments),b&&b.parent&&(this.parent=b.parent)},url:function(){return b.root+"/posts/"+this.parent+"/revisions"}})}(wp,WP_API_Settings,Backbone,_,window);