forked from WP-API/WP-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-api.js
executable file
·242 lines (208 loc) · 5.86 KB
/
wp-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
(function ($) {
window.wp = window.wp || {};
var api = wp.api = wp.api || {};
// Date parsing code
// From: https://github.com/csnover/js-iso8601
// Copyright 2011 Colin Snover, MIT Licensed
var origParse = Date.parse, numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ];
api.parseISO8601 = function (date) {
var timestamp, struct, minutesOffset = 0;
// ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
// before falling back to any implementation-specific date parsing, so that’s what we do, even if native
// implementations could be faster
// 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm
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))) {
// avoid NaN timestamps caused by “undefined” values being passed to Date.UTC
for (var i = 0, k; (k = numericKeys[i]); ++i) {
struct[k] = +struct[k] || 0;
}
// allow undefined days and months
struct[2] = (+struct[2] || 1) - 1;
struct[3] = +struct[3] || 1;
if (struct[8] !== 'Z' && struct[9] !== undefined) {
minutesOffset = struct[10] * 60 + struct[11];
if (struct[9] === '+') {
minutesOffset = 0 - minutesOffset;
}
}
timestamp = Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]);
}
else {
timestamp = origParse ? origParse(date) : NaN;
}
return timestamp;
};
// ECMAScript 5 shim, from MDN
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
if ( !Date.prototype.toISOString ) {
var pad = function (number) {
var r = String(number);
if ( r.length === 1 ) {
r = '0' + r;
}
return r;
};
Date.prototype.toISOString = function() {
return this.getUTCFullYear()
+ '-' + pad( this.getUTCMonth() + 1 )
+ '-' + pad( this.getUTCDate() )
+ 'T' + pad( this.getUTCHours() )
+ ':' + pad( this.getUTCMinutes() )
+ ':' + pad( this.getUTCSeconds() )
+ '.' + String( (this.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5 )
+ 'Z';
};
}
// Set the root URL from the global variable, if available
api.root = "";
if (wpApiOptions) {
api.root = wpApiOptions.base;
}
/**
* wp.api.models
*
* Model container
*/
api.models = {};
/**
* wp.api.models.User
*
* User Entity model
*/
api.models.User = Backbone.Model.extend({
urlRoot: api.root + "/users",
defaults: {
ID: 0,
name: "",
slug: "",
URL: "",
avatar: "",
meta: {
links: {}
}
},
avatar: function (size) {
return this.get('avatar') + '&s=' + size;
}
});
var parseable_dates = [ 'date', 'modified' ];
/**
* wp.api.models.Post
*
* Post Entity model
*/
api.models.Post = Backbone.Model.extend({
// Technically, these are probably not needed
defaults: function () {
return {
ID: 0,
title: "",
status: "draft",
type: "post",
author: new api.models.User(),
content: "",
parent: 0,
link: "",
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: false,
date_tz: "Etc/UTC",
modified_tz: "Etc/UTC",
terms: {},
post_meta: {},
meta: {
links: {}
}
};
},
idAttribute: "ID",
urlRoot: api.root + "/posts",
/**
* Serialize the entity
*
* Overriden for correct date handling
* @return {!Object} Serializable attributes
*/
toJSON: function () {
var attributes = _.clone( this.attributes );
// Remove GMT dates in favour of our native Date objects
// The API only requires one of `date` and `date_gmt`, so this is
// safe for use.
delete attributes.date_gmt;
delete attributes.modified_gmt;
// Serialize Date objects back into 8601 strings
_.each( parseable_dates, function ( key ) {
attributes[ key ] = attributes[ key ].toISOString();
});
return attributes;
},
/**
* Unserialize the entity
*
* Overriden for correct date handling
* @param {!Object} response Attributes parsed from JSON
* @param {!Object} options Request options
* @return {!Object} Fully parsed attributes
*/
parse: function ( response, options ) {
// Parse dates into native Date objects
_.each( parseable_dates, function ( key ) {
if ( ! ( key in response ) )
return;
var timestamp = api.parseISO8601( response[ key ] );
response[ key ] = new Date( timestamp );
});
// Remove GMT dates in favour of our native Date objects
delete response.date_gmt;
delete response.modified_gmt;
// Parse the author into a User object
response.author = new api.models.User(response.author);
return response;
},
/**
* Get parent post
*
* @return {wp.api.models.Post} Parent post, null if not found
*/
parent: function () {
var parent = this.get('parent');
// Return null if we don't have a parent
if (parent === 0) {
return null;
}
// Can we get this from its collection?
if (this.collection) {
return this.collection.get(parent);
}
else {
// Otherwise, get the post directly
var post = new api.models.Post({
id: parent
});
// Note that this acts asynchronously
api.models.post.fetch();
return post;
}
}
});
/**
* wp.api.collections
*/
api.collections = {};
/**
* wp.api.collections.Posts
*/
api.collections.Posts = Backbone.Collection.extend({
url: api.root + "/posts",
model: api.models.Post
});
})(jQuery);