PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 1.0.0
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v1.0.0
4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 2.6.1 All 38 releases
blockspare / src / blocks / latest-posts-list / index.php

index.php in BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites 1.0.0, at src/blocks/latest-posts-list/index.php

516 lines 16.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function blockspare_blocks_render_block_core_latest_posts_list($attributes)
4 {
5
6
7 $categories = isset($attributes['categories']) ? $attributes['categories'] : '';
8
9 /* Setup the query */
10 $grid_query = new WP_Query(
11 array(
12 'posts_per_page' => $attributes['postsToShow'],
13 'post_status' => 'publish',
14 'order' => $attributes['order'],
15 'orderby' => $attributes['orderBy'],
16 'cat' => $categories,
17 'offset' => $attributes['offset'],
18 'post_type' => $attributes['postType'],
19 'ignore_sticky_posts' => 1,
20 )
21 );
22
23 $blockspare_content_markup = '';
24
25 /* Start the loop */
26 if ($grid_query->have_posts()) {
27
28 while ($grid_query->have_posts()) {
29 $grid_query->the_post();
30
31 /* Setup the post ID */
32 $post_id = get_the_ID();
33
34 /* Setup the featured image ID */
35 $post_thumb_id = get_post_thumbnail_id($post_id);
36
37 /* Setup the post classes */
38 $post_classes = 'blockspare-post-single';
39
40 /* Add sticky class */
41 if (is_sticky($post_id)) {
42 $post_classes .= ' sticky';
43 } else {
44 $post_classes .= null;
45 }
46
47 /* Join classes together */
48 $post_classes = join(' ', get_post_class($post_classes, $post_id));
49
50
51 if ($attributes['enableBackgroundColor']) {
52 $post_classes .= 'has-background';
53 }
54
55
56 $item_background_color = ($attributes['enableBackgroundColor']) ? esc_attr($attributes['backGroundColor']) : '';
57
58 $bxshadow = 'none';
59
60 if ($attributes['enableBoxShadow']) {
61 $bxshadow = $attributes['xOffset'] . "px " . $attributes['yOffset'] . "px " . $attributes['blur'] . "px " . $attributes['spread'] . "px " . $attributes['shadowColor'];
62 }
63
64 $item_style = '';
65 if ($attributes['design'] != 'blockspare-list-layout-4' && $attributes['design'] != 'blockspare-list-layout-5' && $attributes['design'] != 'blockspare-list-layout-6') {
66 $item_style .= 'background-color:' . esc_attr($item_background_color) . ';';
67 $item_style .= ' border-radius:' . esc_attr($attributes['borderRadius']) . "px;";
68 $item_style .= ' box-shadow:' . esc_attr($bxshadow) . ';';
69 }
70
71 /* Start the markup for the post */
72 $blockspare_content_markup .= sprintf(
73 '<article id="post-%1$s" class="%2$s" style="%3$s">',
74 esc_attr($post_id),
75 esc_attr($post_classes),
76 $item_style
77
78 );
79
80 /* Get the featured image */
81 if (isset($attributes['displayPostImage']) && $attributes['displayPostImage'] && $post_thumb_id) {
82
83
84 if (!empty($attributes['imageSize'])) {
85 $post_thumb_size = $attributes['imageSize'];
86 }
87
88 if (has_post_thumbnail($post_id)) {
89 /* Output the featured image */
90 $blockspare_content_markup .= sprintf(
91 '<figure class="blockspare-post-img"><a href="%1$s" rel="bookmark" aria-hidden="true" tabindex="-1" >%2$s</a></figure>',
92 esc_url(get_permalink($post_id)),
93 wp_kses_post(wp_get_attachment_image($post_thumb_id, $post_thumb_size))
94
95 );
96 }
97
98 }
99
100
101 /* Wrap the text content */
102 $blockspare_content_markup .= sprintf(
103 '<div class="blockspare-post-content %1$s">',
104 $attributes['contentOrder']
105 );
106
107 $blockspare_content_markup .= sprintf(
108 '<header class="blockspare-block-post-grid-header">'
109 );
110 if ($attributes['displayPostCategory']) {
111 $blockspare_content_markup .= sprintf(
112 '<div class="blockspare-post-category" style="color:%2$s;">%1$s</div>',
113 get_the_category_list(esc_html__(' ', 'blockspare'), '', $post_id),
114 esc_attr($attributes['linkColor'])
115 );
116 }
117
118
119 /* Get the post title */
120 $title = get_the_title($post_id);
121
122 if (!$title) {
123 $title = __('Untitled', 'blockspare');
124 }
125
126 if (isset($attributes['displayPostTitle']) && $attributes['displayPostTitle']) {
127
128 if (isset($attributes['postTitleTag'])) {
129 $post_title_tag = $attributes['postTitleTag'];
130 } else {
131 $post_title_tag = 'h4';
132 }
133
134 $blockspare_content_markup .= sprintf(
135 '<%3$s class="blockspare-block-post-grid-title" style="font-size: %5$s;"><a style="color:%4$s;" href="%1$s" rel="bookmark">%2$s</a></%3$s>',
136 esc_url(get_permalink($post_id)),
137 esc_html($title),
138 esc_attr($post_title_tag),
139 esc_attr($attributes['postTitleColor']),
140 esc_attr($attributes['postTitleFontSize']) . 'px'
141 );
142 }
143
144 if (isset($attributes['postType']) && ($attributes['postType'] === 'post') && (isset($attributes['displayPostAuthor']) || isset($attributes['displayPostDate']))) {
145 /* Wrap the byline content */
146 $blockspare_content_markup .= sprintf(
147 '<div class="blockspare-block-post-grid-byline">'
148 );
149
150 /* Get the post author */
151 if (isset($attributes['displayPostAuthor']) && $attributes['displayPostAuthor']) {
152 $blockspare_content_markup .= sprintf(
153 '<div class="blockspare-block-post-grid-author"><a style="color:%3$s;" class="blockspare-text-link" href="%2$s" itemprop="url" rel="author"><span itemprop="name">%1$s</span></a></div>',
154 esc_html(get_the_author_meta('display_name', get_the_author_meta('ID'))),
155 esc_url(get_author_posts_url(get_the_author_meta('ID'))),
156 esc_attr($attributes['linkColor'])
157 );
158 }
159
160 /* Get the post date */
161 if (isset($attributes['displayPostDate']) && $attributes['displayPostDate']) {
162 $blockspare_content_markup .= sprintf(
163 '<time style="color: %3$s" " datetime="%1$s" class="blockspare-block-post-grid-date" itemprop="datePublished">%2$s</time>',
164 esc_attr(get_the_date('c', $post_id)),
165 esc_html(get_the_date('', $post_id)),
166 esc_attr($attributes['generalColor'])
167 );
168 }
169
170 /* Close the byline content */
171 $blockspare_content_markup .= sprintf(
172 '</div>'
173 );
174 }
175
176 /* Close the header content */
177 $blockspare_content_markup .= sprintf(
178 '</header>'
179 );
180
181 /* Wrap the excerpt content */
182 $blockspare_content_markup .= sprintf(
183 '<div class="blockspare-block-post-grid-excerpt" style="color: %1$s">',
184 esc_attr($attributes['generalColor'])
185
186 );
187
188 /* Get the excerpt */
189
190 global $post;
191
192
193 $excerpt = blockspare_post_excerpt($post_id, $post);
194
195 if (!$excerpt) {
196 $excerpt = null;
197 }
198
199 if (!empty($excerpt)) {
200 $excerpt = explode(' ', $excerpt);
201 $trim_to_length = (int)$attributes['excerptLength'] ? (int)$attributes['excerptLength'] : 55;
202 if (count($excerpt) > $trim_to_length) {
203 $excerpt = implode(' ', array_slice($excerpt, 0, $trim_to_length)) . '...';
204 } else {
205 $excerpt = implode(' ', $excerpt);
206 }
207
208 }
209
210
211 if ((isset($attributes['displayPostExcerpt']) && $excerpt != null) || isset($attributes['displayPostLink'])) {
212
213
214 /* Wrap the excerpt content */
215 $blockspare_content_markup .= sprintf(
216 '<div class="blockspare-block-post-grid-excerpt" style="color: %1$s">',
217 esc_attr($attributes['generalColor'])
218
219 );
220
221 if (isset($attributes['displayPostExcerpt']) && $attributes['displayPostExcerpt']) {
222 $blockspare_content_markup .= sprintf(
223 '<div class="blockspare-block-post-grid-excerpt-content">%s</div>',
224 wp_kses_post($excerpt)
225 );
226 }
227
228 /* Get the read more link */
229 if (isset($attributes['displayPostLink']) && $attributes['displayPostLink']) {
230 $blockspare_content_markup .= sprintf(
231 '<p><a style="color:%4$s;" class="blockspare-block-post-grid-more-link blockspare-text-link" href="%1$s" rel="bookmark">%2$s <span class="screen-reader-text">%3$s</span></a></p>',
232 esc_url(get_permalink($post_id)),
233 esc_html($attributes['readMoreText']),
234 esc_html($title),
235 esc_attr($attributes['linkColor'])
236 );
237 }
238
239 /* Close the excerpt content */
240 $blockspare_content_markup .= sprintf(
241 '</div>'
242 );
243
244 }
245
246 /* Close the text content */
247 $blockspare_content_markup .= sprintf(
248 '</div>'
249 );
250
251 /* Close the post */
252 $blockspare_content_markup .= "</article>\n";
253 }
254
255 /* Restore original post data */
256 wp_reset_postdata();
257
258 /* Build the block classes */
259 $class = "wp-block-blockspare-block-blockspare-latest-posts blockspare-post-wrap align{$attributes['align']}";
260
261 if (isset($attributes['className'])) {
262 $class .= ' ' . $attributes['className'];
263 }
264 $col_class = '';
265
266 if ($attributes['design'] === 'blockspare-is-grid') {
267 $col_class = 'column-' . $attributes['columns'];
268 }
269
270
271 $list_layout_class = $attributes['design'];
272
273 /* Layout orientation class */
274 $grid_class = 'blockspare-latest-post-wrap blockspare-is-list ' . $list_layout_class;
275
276
277 /* Post grid section title */
278 if (isset($attributes['displaySectionTitle']) && $attributes['displaySectionTitle'] && !empty($attributes['sectionTitle'])) {
279 if (isset($attributes['sectionTitleTag'])) {
280 $section_title_tag = $attributes['sectionTitleTag'];
281 } else {
282 $section_title_tag = 'h4';
283 }
284
285 $section_title = '<' . esc_attr($section_title_tag) . '>' . esc_html($attributes['sectionTitle']) . '</' . esc_attr($section_title_tag) . '>';
286 } else {
287 $section_title = null;
288 }
289
290 /* Post grid section tag */
291 if (isset($attributes['sectionTag'])) {
292 $section_tag = $attributes['sectionTag'];
293 } else {
294 $section_tag = 'section';
295 }
296
297 /* Output the post markup */
298 $block_content = sprintf(
299 '<%1$s class="%2$s" style="margin-top: %6$s;margin-bottom: %7$s;" ><div class="%4$s">%5$s</div></%1$s>',
300 $section_tag,
301 esc_attr($class),
302 $section_title,
303 esc_attr($grid_class),
304 $blockspare_content_markup,
305 esc_attr($attributes['marginTop']) . 'px',
306 esc_attr($attributes['marginBottom']) . 'px'
307
308
309 );
310 return $block_content;
311 }
312 }
313
314 /**
315 * Registers the post grid block on server
316 */
317 function blockspare_blocks_register_block_core_latest_posts_list()
318 {
319
320 /* Check if the register function exists */
321 if (!function_exists('register_block_type')) {
322 return;
323 }
324
325 ob_start();
326 include BLOCKSPARE_PLUGIN_DIR . '/src/blocks/latest-posts-list/block.json';
327
328 $metadata = json_decode(ob_get_clean(), true);
329
330 /* Block attributes */
331 register_block_type(
332 'blockspare/blockspare-latest-posts-list',
333 array(
334 'attributes' => $metadata['attributes'],
335 'render_callback' => 'blockspare_blocks_render_block_core_latest_posts_list',
336 )
337 );
338 }
339
340 add_action('init', 'blockspare_blocks_register_block_core_latest_posts_list');
341
342
343 /**
344 * Create API fields for additional info
345 */
346 function blockspare_blocks_post_register_rest_fields()
347 {
348
349 register_rest_field('post', 'featured_image_urls',
350 array(
351 'get_callback' => 'blockspare_featured_image_urls',
352 'update_callback' => null,
353 'schema' => array(
354 'description' => __('Different sized featured images', 'blockspare'),
355 'type' => 'array',
356 ),
357 )
358 );
359
360 // Excer
361
362 /* Add author info */
363 register_rest_field(
364 'post',
365 'author_info',
366 array(
367 'get_callback' => 'blockspare_blocks_get_author_infos',
368 'update_callback' => null,
369 'schema' => null,
370 )
371 );
372
373 /* Add author info */
374 register_rest_field(
375 'post',
376 'category_info',
377 array(
378 'get_callback' => 'blockspare_blocks_get_category_infos',
379 'update_callback' => null,
380 'schema' => null,
381 )
382 );
383
384 /* Add author info */
385 register_rest_field(
386 'post',
387 'tag_info',
388 array(
389 'get_callback' => 'blockspare_blocks_get_tag_infos',
390 'update_callback' => null,
391 'schema' => null,
392 )
393 );
394 }
395
396 add_action('rest_api_init', 'blockspare_blocks_post_register_rest_fields');
397
398
399 /**
400 * Get author info for the rest field
401 *
402 * @param String $object The object type.
403 * @param String $field_name Name of the field to retrieve.
404 * @param String $request The current request object.
405 */
406 function blockspare_blocks_get_author_infos($object, $field_name, $request)
407 {
408 /* Get the author name */
409 $author_data['display_name'] = get_the_author_meta('display_name', $object['author']);
410
411 /* Get the author link */
412 $author_data['author_link'] = get_author_posts_url($object['author']);
413
414 /* Return the author data */
415 return $author_data;
416 }
417
418 function blockspare_blocks_get_category_infos($object, $field_name, $request)
419 {
420
421
422 return get_the_category_list(esc_html__(' ', 'blockspare'), '', $object['id']);
423
424 }
425
426 function blockspare_blocks_get_tag_infos($object, $field_name, $request)
427 {
428
429 ob_start();
430 $cate_name = '';
431 if (!empty($object)) {
432 foreach ($object['categories'] as $cat_id) {
433 $cate_name = get_cat_name($cat_id);
434 }
435 }
436 ob_clean();
437 return $cate_name;
438
439 }
440
441 if (!function_exists('blockspare_featured_image_urls')) {
442 /**
443 * Get the different featured image sizes that the blog will use.
444 * Used in the custom REST API endpoint.
445 *
446 * @since 1.7
447 */
448 function blockspare_featured_image_urls($object, $field_name, $request)
449 {
450 return blockspare_featured_image_urls_from_url(!empty($object['featured_media']) ? $object['featured_media'] : '');
451 }
452 }
453
454
455 if (!function_exists('blockspare_featured_image_urls_from_url')) {
456 /**
457 * Get the different featured image sizes that the blog will use.
458 *
459 * @since 2.0
460 */
461 function blockspare_featured_image_urls_from_url($attachment_id)
462 {
463
464 $image = wp_get_attachment_image_src($attachment_id, 'full', false);
465 $sizes = get_intermediate_image_sizes();
466
467 $imageSizes = array(
468 'full' => is_array($image) ? $image : '',
469 );
470
471 foreach ($sizes as $size) {
472 $imageSizes[$size] = is_array($image) ? wp_get_attachment_image_src($attachment_id, $size, false) : '';
473 }
474
475 return $imageSizes;
476 }
477 }
478
479
480 if (!function_exists('blockspare_post_excerpt')) {
481 /**
482 * Get the post excerpt.
483 *
484 * @since 1.7
485 */
486 function blockspare_post_excerpt($object)
487 {
488 return blockspare_get_excerpt($object['id']);
489 }
490 }
491
492 if (!function_exists('blockspare_get_excerpt')) {
493 /**
494 * Get the excerpt.
495 *
496 * @since 1.7
497 */
498 function blockspare_get_excerpt($post_id, $post = null)
499 {
500 $excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id, 'display'));
501 if (!empty($excerpt)) {
502 return $excerpt;
503 }
504
505 $max_excerpt = 100; // WP default is 55.
506
507 if (!empty($post['post_content'])) {
508 return apply_filters('the_excerpt', wp_trim_words($post['post_content'], $max_excerpt));
509 }
510 $post_content = apply_filters('the_content', get_post_field('post_content', $post_id));
511 return apply_filters('the_excerpt', wp_trim_words($post_content, $max_excerpt));
512 }
513 }
514
515
516