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