PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.5.3
GenerateBlocks v1.5.3
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / extend / dynamic-content / utils / getContent.js
generateblocks / src / extend / dynamic-content / utils Last commit date
applyContext.js 4 years ago getContent.js 4 years ago getContentTypeLabel.js 4 years ago
getContent.js
392 lines
1 import { __, sprintf } from '@wordpress/i18n';
2 import { dateI18n } from '@wordpress/date';
3 import _ from 'lodash';
4
5 /**
6 * The content type selectors map.
7 *
8 * @type {Object}
9 */
10 const contentTypeSelectors = {
11 'post-title': getPostTitle,
12 'post-excerpt': getPostExcerpt,
13 'post-date': getPostDate,
14 'post-meta': getPostMetaValue,
15 'author-meta': getAuthorMetaValue,
16 'author-email': getAuthorEmail,
17 'author-name': getAuthorName,
18 'author-nickname': getAuthorNickname,
19 'author-first-name': getAuthorFirstName,
20 'author-last-name': getAuthorLastName,
21 'comments-number': getPostCommentsNumber,
22 'pagination-numbers': getPaginationNumbers,
23 'featured-image': getPostFeaturedImage,
24 terms: getPostTerms,
25 'author-avatar': getAuthorAvatar,
26 caption: getCaption,
27 'alt-text': getAltText,
28 'image-description': getDescription,
29 };
30
31 /**
32 * Returns the record content by type.
33 *
34 * @param {string} dynamicContentType The content type to select.
35 * @param {Object} record The post object.
36 * @param {Object} attributes The dynamic content attributes.
37 * @param {boolean} emptyNotFoundMessage If the message should be undefined.
38 * @return {string} The selected content.
39 */
40 export default function getContent( dynamicContentType, record, attributes, emptyNotFoundMessage = false ) {
41 const contentSelector = contentTypeSelectors[ dynamicContentType ];
42
43 if ( contentSelector && 'function' === typeof contentSelector ) {
44 return contentSelector( record, attributes, emptyNotFoundMessage );
45 }
46
47 return contentTypeNotSupported( record, attributes, emptyNotFoundMessage );
48 }
49
50 /**
51 * Returns message for not supported content types.
52 *
53 * @param {Object} record The post object.
54 * @param {Object} attributes The dynamic content attributes.
55 * @param {boolean} emptyNotFoundMessage If the message should be undefined.
56 * @return {string} Text for non-supported content.
57 */
58 function contentTypeNotSupported( record, attributes, emptyNotFoundMessage ) {
59 return ! emptyNotFoundMessage ? sprintf(
60 // translators: %s: Content type.
61 __( 'Content type %s is not supported.', 'generateblocks' ),
62 attributes.dynamicContentType
63 ) : undefined;
64 }
65
66 /**
67 * Return the post title.
68 *
69 * @param {Object} record The post object.
70 * @return {string} The post title.
71 */
72 function getPostTitle( record ) {
73 if ( ! record.title ) {
74 return __( 'Post title not supported for this type.', 'generateblocks' );
75 }
76
77 return record.title.raw || __( 'No post title.', 'generateblocks' );
78 }
79
80 /**
81 * Returns the post excerpt.
82 *
83 * @param {Object} record The post object.
84 * @param {Object} attributes The dynamic content attributes.
85 * @return {string} The post excerpt.
86 */
87 function getPostExcerpt( record, attributes ) {
88 if ( ! record.excerpt ) {
89 return __( 'Post except not supported for this type.', 'generateblocks' );
90 }
91
92 const {
93 raw: rawExcerpt,
94 rendered: renderedExcerpt,
95 protected: isProtected,
96 } = record?.excerpt;
97
98 if ( isProtected || ( ! rawExcerpt && ! renderedExcerpt ) ) {
99 return __( 'No post excerpt.', 'generateblocks' );
100 }
101
102 const renderedText = ( text ) => new window.DOMParser().parseFromString( text, 'text/html' );
103
104 const excerptDocument = renderedText( renderedExcerpt );
105 const readMoreDocument = renderedText( generateBlocksInfo.excerptMore );
106
107 // Get stripped except text.
108 const strippedRenderedExcerpt = excerptDocument.body.textContent || excerptDocument.body.innerText || '';
109
110 // Get stripped excerpt more text.
111 let strippedExcerptMore = readMoreDocument.body.textContent || readMoreDocument.body.innerText || '';
112 strippedExcerptMore = strippedExcerptMore.replace( '...', '' );
113
114 let excerpt = rawExcerpt.trim() ? rawExcerpt : strippedRenderedExcerpt;
115 const hasReadMore = excerpt.includes( strippedExcerptMore );
116
117 // Remove more text from excerpt.
118 excerpt = hasReadMore
119 ? excerpt.replace( strippedExcerptMore, '' )
120 : excerpt;
121
122 // Apply excerpt length.
123 excerpt = excerpt.split( ' ' ).splice( 0, attributes.excerptLength ).join( ' ' );
124
125 // Re-add more text to excerpt.
126 if ( ! attributes.useDefaultMoreLink ) {
127 excerpt += ' ... ' + '<a href="#">' + attributes.customMoreLinkText + '</a>';
128 } else if ( hasReadMore ) {
129 excerpt += generateBlocksInfo.excerptMore;
130 }
131
132 return excerpt;
133 }
134
135 /**
136 * Returns the post date.
137 *
138 * @param {Object} record The post object.
139 * @param {Object} attributes The dynamic content attributes.
140 * @return {string} The post date.
141 */
142 function getPostDate( record, attributes ) {
143 let dateType = attributes.dateType;
144
145 if ( 'published' === dateType && attributes.dateReplacePublished ) {
146 dateType = 'updated';
147 }
148
149 if ( ! record.date ) {
150 return __( 'No post date.', 'generateblocks' );
151 }
152
153 const dateContent = dateType === 'updated' ? record.modified : record.date;
154
155 return dateI18n( attributes.dateFormat || 'F j, Y', dateContent, '' );
156 }
157
158 /**
159 * Returns the meta value of given key.
160 *
161 * @param {string} metaField The meta field name.
162 * @param {Object} metaValues The post meta values.
163 * @param {boolean} emptyNotFoundMessage If the message should be undefined.
164 * @return {string} The meta value.
165 */
166 const getMetaValue = ( metaField, metaValues, emptyNotFoundMessage = false ) => {
167 if ( metaValues && metaValues[ metaField ] ) {
168 const value = metaValues[ metaField ];
169 const notSupportedMessage = ! emptyNotFoundMessage
170 ? __( 'Meta value', 'generateblocks' )
171 : undefined;
172
173 return ( _.isString( value ) || _.isNumber( value ) )
174 ? _.toString( value )
175 : notSupportedMessage;
176 }
177
178 return ! emptyNotFoundMessage ? __( 'Meta value', 'generateblocks' ) : undefined;
179 };
180
181 /**
182 * Returns the post meta values.
183 *
184 * @param {Object} record The post object.
185 * @param {Object} attributes The dynamic content attributes.
186 * @param {boolean} emptyNotFoundMessage If the message should be undefined.
187 * @return {string} The post meta value.
188 */
189 function getPostMetaValue( record, attributes, emptyNotFoundMessage = false ) {
190 return getMetaValue( attributes.metaFieldName, record.meta, emptyNotFoundMessage );
191 }
192
193 /**
194 * Returns the author meta values.
195 *
196 * @param {Object} record The post object.
197 * @param {Object} attributes The dynamic content attributes.
198 * @return {string} The author meta value.
199 */
200 function getAuthorMetaValue( record, attributes ) {
201 if ( ! record.author ) {
202 return authorNotFound();
203 }
204
205 return getMetaValue( attributes.metaFieldName, record.author.meta );
206 }
207
208 /**
209 * Returns author not found.
210 *
211 * @return {string} Text when author is not found.
212 */
213 function authorNotFound() {
214 return __( 'Author not found.', 'generateblocks' );
215 }
216
217 /**
218 * Returns the author email.
219 *
220 * @param {Object} record The post object.
221 * @return {string} The author email.
222 */
223 function getAuthorEmail( record ) {
224 if ( ! record.author ) {
225 return authorNotFound();
226 }
227
228 return record.author.email || __( 'No author email.', 'generateblocks' );
229 }
230
231 /**
232 * Returns the author name.
233 *
234 * @param {Object} record The post object.
235 * @return {string} The author name.
236 */
237 function getAuthorName( record ) {
238 if ( ! record.author ) {
239 return authorNotFound();
240 }
241
242 return record.author.name || __( 'No author name.', 'generateblocks' );
243 }
244
245 /**
246 * Returns the author nickname.
247 *
248 * @param {Object} record The post object.
249 * @return {string} The author nickname.
250 */
251 function getAuthorNickname( record ) {
252 if ( ! record.author ) {
253 return authorNotFound();
254 }
255
256 return record.author.nickname || __( 'No author nickname.', 'generateblocks' );
257 }
258
259 /**
260 * Returns the author first name.
261 *
262 * @param {Object} record The post object.
263 * @return {string} The author first name.
264 */
265 function getAuthorFirstName( record ) {
266 if ( ! record.author ) {
267 return authorNotFound();
268 }
269
270 return record.author.first_name || __( 'No author first name.', 'generateblocks' );
271 }
272
273 /**
274 * Returns the author last name.
275 *
276 * @param {Object} record The post object.
277 * @return {string} The author first name.
278 */
279 function getAuthorLastName( record ) {
280 if ( ! record.author ) {
281 return authorNotFound();
282 }
283
284 return record.author.last_name || __( 'No author last name.', 'generateblocks' );
285 }
286
287 /**
288 * Returns the post comments number.
289 *
290 * @param {Object} record The post object.
291 * @param {Object} attributes The dynamic content attributes.
292 * @return {string} The post comments number.
293 */
294 function getPostCommentsNumber( record, attributes ) {
295 const commentsLength = Array.isArray( record.comments ) ? record.comments.length : 0;
296 const {
297 noCommentsText,
298 singleCommentText,
299 multipleCommentsText,
300 } = attributes;
301
302 if ( commentsLength === 0 ) {
303 return noCommentsText;
304 }
305
306 if ( commentsLength === 1 ) {
307 return singleCommentText;
308 }
309
310 return multipleCommentsText.replace( '%', String( commentsLength ) );
311 }
312
313 /**
314 * Returns the post terms list.
315 *
316 * @param {Object} record The post object.
317 * @param {Object} attributes The dynamic content attributes.
318 * @return {string} The post terms list.
319 */
320 function getPostTerms( record, attributes ) {
321 if ( Array.isArray( record.terms ) && record.terms.length > 0 ) {
322 return record.terms
323 .map( ( term ) => ( term.name ) )
324 .join( attributes.termSeparator );
325 }
326
327 return 'No terms';
328 }
329
330 /**
331 * Return the pagination numbers.
332 *
333 * @return {string} The pagination numbers.
334 */
335 function getPaginationNumbers() {
336 return __( '1 … 2 3', 'generateblocks' );
337 }
338
339 /**
340 * Returns post featured image.
341 *
342 * @param {Object} record The post object.
343 * @return {string} The featured image url.
344 */
345 function getPostFeaturedImage( record ) {
346 return record?.featured_media;
347 }
348
349 /**
350 * Returns post author avatar.
351 *
352 * @param {Object} record The post object.
353 * @return {Object} The post author avatar.
354 */
355 function getAuthorAvatar( record ) {
356 return {
357 source_url: record?.author?.avatar_urls && record?.author?.avatar_urls[ 96 ]
358 ? record?.author?.avatar_urls[ 96 ]
359 : '',
360 };
361 }
362
363 /**
364 * Returns the caption.
365 *
366 * @param {Object} record The post object.
367 * @return {string} The image caption.
368 */
369 function getCaption( record ) {
370 return record?.caption?.raw || __( 'Image caption', 'generateblocks' );
371 }
372
373 /**
374 * Returns the alt text.
375 *
376 * @param {Object} record The post object.
377 * @return {string} The image caption.
378 */
379 function getAltText( record ) {
380 return record?.alt_text || __( 'Image alt text', 'generateblocks' );
381 }
382
383 /**
384 * Returns the description.
385 *
386 * @param {Object} record The post object.
387 * @return {string} The image caption.
388 */
389 function getDescription( record ) {
390 return record?.description?.raw || __( 'Image description', 'generateblocks' );
391 }
392