-
Notifications
You must be signed in to change notification settings - Fork 2k
/
utils.js
303 lines (243 loc) · 6.4 KB
/
utils.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/** @format */
/**
* External dependencies
*/
import url from 'url';
import { moment } from 'i18n-calypso';
/**
* Internal dependencies
*/
import postNormalizer from 'lib/post-normalizer';
export const getEditURL = function( post, site ) {
if ( ! site ) {
return '/post';
}
if ( ! post ) {
return `/post/${ site.slug }`;
}
let path;
const type = post.type || 'post';
switch ( type ) {
case 'post':
path = '/post';
break;
case 'page':
path = '/page';
break;
default:
path = `/edit/${ type }`;
break;
}
path += `/${ site.slug }`;
if ( post.ID ) {
path += `/${ post.ID }`;
}
return path;
};
export const getPreviewURL = function( site, post, autosavePreviewUrl ) {
let parsed, previewUrl;
if ( ! post || ! post.URL || post.status === 'trash' ) {
return '';
}
if ( autosavePreviewUrl ) {
previewUrl = autosavePreviewUrl;
} else if ( post.status === 'publish' ) {
previewUrl = post.URL;
} else {
parsed = url.parse( post.URL, true );
parsed.query.preview = 'true';
delete parsed.search;
previewUrl = url.format( parsed );
}
if ( post.site_ID ) {
if ( ! ( site && site.options ) ) {
// site info is still loading, just use what we already have until it does
return previewUrl;
}
if ( site.options.is_mapped_domain ) {
previewUrl = previewUrl.replace( site.URL, site.options.unmapped_url );
}
if ( site.options.frame_nonce ) {
parsed = url.parse( previewUrl, true );
parsed.query[ 'frame-nonce' ] = site.options.frame_nonce;
delete parsed.search;
previewUrl = url.format( parsed );
}
}
return previewUrl;
};
export const userCan = function( capability, post ) {
const hasCap = post.capabilities && post.capabilities[ capability ];
if ( capability === 'edit_post' ) {
return hasCap && post.status !== 'trash';
}
return hasCap;
};
// Return backdated-published status of a post. Optionally, the `status` can be overridden
// with a custom value: what would the post status be if a `status` edit was applied?
export const isBackDatedPublished = function( post, status ) {
if ( ! post ) {
return false;
}
const effectiveStatus = status || post.status;
return effectiveStatus === 'future' && moment( post.date ).isBefore( moment() );
};
// Return published status of a post. Optionally, the `status` can be overridden
// with a custom value: what would the post status be if a `status` edit was applied?
export const isPublished = function( post, status ) {
if ( ! post ) {
return false;
}
const effectiveStatus = status || post.status;
return (
effectiveStatus === 'publish' ||
effectiveStatus === 'private' ||
isBackDatedPublished( post, status )
);
};
export const isScheduled = function( post ) {
if ( ! post ) {
return false;
}
return post.status === 'future';
};
export const isPrivate = function( post ) {
if ( ! post ) {
return false;
}
return post.status === 'private';
};
export const isPending = function( post ) {
if ( ! post ) {
return false;
}
return post.status === 'pending';
};
export const getEditedTime = function( post ) {
if ( ! post ) {
return;
}
if ( post.status === 'publish' || post.status === 'future' ) {
return post.date;
}
return post.modified;
};
export const isFutureDated = function( post ) {
if ( ! post ) {
return false;
}
const oneMinute = 1000 * 60;
return post && +new Date() + oneMinute < +new Date( post.date );
};
export const isBackDated = function( post ) {
if ( ! post || ! post.date || ! post.modified ) {
return false;
}
return moment( post.date ).isBefore( moment( post.modified ) );
};
export const isPage = function( post ) {
if ( ! post ) {
return false;
}
return post && 'page' === post.type;
};
export const normalizeSync = function( post, callback ) {
const imageWidth = 653;
postNormalizer(
post,
[
postNormalizer.decodeEntities,
postNormalizer.stripHTML,
postNormalizer.safeImageProperties( imageWidth ),
postNormalizer.withContentDOM( [
postNormalizer.content.removeStyles,
postNormalizer.content.makeImagesSafe( imageWidth ),
postNormalizer.content.detectMedia,
] ),
postNormalizer.pickCanonicalImage,
],
callback
);
};
export const getVisibility = function( post ) {
if ( ! post ) {
return null;
}
if ( post.password ) {
return 'password';
}
if ( 'private' === post.status ) {
return 'private';
}
return 'public';
};
export const normalizeAsync = function( post, callback ) {
postNormalizer( post, [ postNormalizer.keepValidImages( 72, 72 ) ], callback );
};
export const removeSlug = function( path ) {
if ( ! path ) {
return;
}
const pathParts = path.slice( 0, -1 ).split( '/' );
pathParts[ pathParts.length - 1 ] = '';
return pathParts.join( '/' );
};
export const getPermalinkBasePath = function( post ) {
if ( ! post ) {
return;
}
let path = post.URL;
// if we have a permalink_URL, utlize that
if ( ! isPublished( post ) && post.other_URLs && post.other_URLs.permalink_URL ) {
path = post.other_URLs.permalink_URL;
}
return removeSlug( path );
};
export const getPagePath = function( post ) {
if ( ! post ) {
return;
}
if ( ! isPublished( post ) ) {
return getPermalinkBasePath( post );
}
return removeSlug( post.URL );
};
/**
* Returns the ID of the featured image assigned to the specified post, or
* `undefined` otherwise. A utility function is useful because the format
* of a post varies between the retrieve and update endpoints. When
* retrieving a post, the thumbnail ID is assigned in `post_thumbnail`, but
* in creating a post, the thumbnail ID is assigned to `featured_image`.
*
* @param {Object} post Post object
* @returns {*} featured image id or undefined
*/
export const getFeaturedImageId = function( post ) {
if ( ! post ) {
return;
}
if ( 'featured_image' in post && ! /^https?:\/\//.test( post.featured_image ) ) {
// Return the `featured_image` property if it does not appear to be
// formatted as a URL
return post.featured_image;
}
if ( post.post_thumbnail ) {
// After the initial load from the REST API, pull the numeric ID
// from the thumbnail object if one exists
return post.post_thumbnail.ID;
}
};
/**
* Return date with timezone offset.
* If `date` is not defined it returns `now`.
*
* @param {String|Date} date - date
* @param {String} tz - timezone
* @return {Moment} moment instance
*/
export const getOffsetDate = function( date, tz ) {
if ( ! tz ) {
return moment( date );
}
return moment( moment.tz( date, tz ) );
};