PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 1.3.3
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v1.3.3
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.3.3, at src/blocks/latest-posts-list/index.php

597 lines 19.3 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 $unq_class = mt_rand(100000,999999);
7 $blockuniqueclass = '';
8
9 if(!empty($attributes['uniqueClass'])){
10 $blockuniqueclass = $attributes['uniqueClass'];
11 }else{
12 $blockuniqueclass = 'blockspare-list-'.$unq_class;
13 }
14
15
16 $categories = isset($attributes['categories']) ? $attributes['categories'] : '';
17
18 /* Setup the query */
19 $grid_query = new WP_Query(
20 array(
21 'posts_per_page' => $attributes['postsToShow'],
22 'post_status' => 'publish',
23 'order' => $attributes['order'],
24 'orderby' => $attributes['orderBy'],
25 'cat' => $categories,
26 'offset' => $attributes['offset'],
27 'post_type' => $attributes['postType'],
28 'ignore_sticky_posts' => 1,
29 )
30 );
31
32 $blockspare_content_markup = '';
33
34 /* Start the loop */
35 if ($grid_query->have_posts()) {
36
37 while ($grid_query->have_posts()) {
38 $grid_query->the_post();
39
40 /* Setup the post ID */
41 $post_id = get_the_ID();
42
43 /* Setup the featured image ID */
44 $post_thumb_id = get_post_thumbnail_id($post_id);
45
46 $has_img_class = '';
47
48 if(!$post_thumb_id){
49 $has_img_class = "post-has-no-image";
50 }
51
52 /* Setup the post classes */
53 $post_classes = 'blockspare-post-single ';
54
55 /* Add sticky class */
56 if (is_sticky($post_id)) {
57 $post_classes .= ' sticky';
58 } else {
59 $post_classes .= null;
60 }
61
62 /* Join classes together */
63 //$post_classes = join(' ', get_post_class($post_classes, $post_id));
64
65
66 if ($attributes['enableBackgroundColor']) {
67 $post_classes .= 'has-background';
68 }
69
70
71
72 /* Start the markup for the post */
73 $blockspare_content_markup .= sprintf(
74 '<div id="post-%1$s" class="%2$s %3$s">',
75 esc_attr($post_id),
76 esc_attr($post_classes),
77 $has_img_class
78
79
80 );
81
82 /* Get the featured image */
83 if (isset($attributes['displayPostImage']) && $attributes['displayPostImage'] && $post_thumb_id) {
84
85
86 if (!empty($attributes['imageSize'])) {
87 $post_thumb_size = $attributes['imageSize'];
88 }
89
90 if (has_post_thumbnail($post_id)) {
91 /* Output the featured image */
92 $blockspare_content_markup .= sprintf(
93 '<figure class="blockspare-post-img"><a href="%1$s" rel="bookmark" aria-hidden="true" tabindex="-1" >%2$s</a></figure>',
94 esc_url(get_permalink($post_id)),
95 wp_kses_post(wp_get_attachment_image($post_thumb_id, $post_thumb_size))
96
97 );
98 }
99
100 }
101
102
103 /* Wrap the text content */
104 $blockspare_content_markup .= sprintf(
105 '<div class="blockspare-post-content %1$s">',
106 $attributes['contentOrder']
107 );
108
109 $blockspare_content_markup .= sprintf(
110 '<header class="blockspare-block-post-grid-header">'
111 );
112 if ($attributes['displayPostCategory']) {
113 $blockspare_content_markup .= sprintf(
114 '<div class="blockspare-post-category" >%1$s</div>',
115 get_the_category_list(esc_html__(' ', 'blockspare'), '', $post_id)
116
117 );
118 }
119
120
121 /* Get the post title */
122 $title = get_the_title($post_id);
123
124 if (!$title) {
125 $title = __('Untitled', 'blockspare');
126 }
127
128 if (isset($attributes['displayPostTitle']) && $attributes['displayPostTitle']) {
129
130 if (isset($attributes['postTitleTag'])) {
131 $post_title_tag = $attributes['postTitleTag'];
132 } else {
133 $post_title_tag = 'h4';
134 }
135
136 $blockspare_content_markup .= sprintf(
137 '<%3$s class="blockspare-block-post-grid-title"><a href="%1$s" class="blockspare-title-link" rel="bookmark"><span>%2$s</span></a></%3$s>',
138 esc_url(get_permalink($post_id)),
139 esc_html($title),
140 esc_attr($post_title_tag)
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 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
157 );
158 }
159
160 /* Get the post date */
161 if (isset($attributes['displayPostDate']) && $attributes['displayPostDate']) {
162 $blockspare_content_markup .= sprintf(
163 '<time " 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
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">'
184
185
186 );
187
188 /* Get the excerpt */
189
190 $post_id = get_the_ID();
191
192 $excerpt = apply_filters('the_excerpt',
193 get_post_field(
194 'post_excerpt',
195 $post_id,
196 'display'
197 )
198 );
199
200 if(empty($excerpt) && isset($attributes['excerptLength'])) {
201 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound, PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket -- Running the_excerpt directly, Previous rule doesn't take without the_excerpt being moved up a line
202 $excerpt = apply_filters('the_excerpt',
203 wp_trim_words(
204 preg_replace(
205 array(
206 '/\<figcaption>.*\<\/figcaption>/',
207 '/\[caption.*\[\/caption\]/',
208 ),
209 '',
210 get_the_content()
211 ),
212 $attributes['excerptLength']
213 )
214 );
215 }
216
217 if(!$excerpt) {
218 $excerpt = null;
219 }
220
221
222
223 if ((isset($attributes['displayPostExcerpt']) && $excerpt != null) || isset($attributes['displayPostLink'])) {
224
225
226 /* Wrap the excerpt content */
227 $blockspare_content_markup .= sprintf(
228 '<div class="blockspare-block-post-grid-excerpt">'
229
230
231 );
232
233 if (isset($attributes['displayPostExcerpt']) && $attributes['displayPostExcerpt']) {
234 $blockspare_content_markup .= sprintf(
235 '<div class="blockspare-block-post-grid-excerpt-content">%s</div>',
236 wp_kses_post($excerpt)
237 );
238 }
239
240 /* Get the read more link */
241 if (isset($attributes['displayPostLink']) && $attributes['displayPostLink']) {
242 $blockspare_content_markup .= sprintf(
243 '<p><a 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>',
244 esc_url(get_permalink($post_id)),
245 esc_html($attributes['readMoreText']),
246 esc_html($title)
247 );
248 }
249
250 /* Close the excerpt content */
251 $blockspare_content_markup .= sprintf(
252 '</div>'
253 );
254
255 }
256
257 /* Close the text content */
258 $blockspare_content_markup .= sprintf(
259 '</div>'
260 );
261
262 $blockspare_content_markup .= sprintf(
263 '</div>'
264 );
265
266 /* Close the post */
267 $blockspare_content_markup .= "</div>\n";
268 }
269
270 /* Restore original post data */
271 wp_reset_postdata();
272
273 /* Build the block classes */
274 $class = "wp-block-blockspare-block-blockspare-latest-posts blockspare-post-wrap align{$attributes['align']}";
275
276 if (isset($attributes['className'])) {
277 $class .= ' ' . $attributes['className'];
278 }
279 $col_class = '';
280
281 if ($attributes['design'] === 'blockspare-is-grid') {
282 $col_class = 'column-' . $attributes['columns'];
283 }
284
285
286 $list_layout_class = $attributes['design'];
287
288 /* Layout orientation class */
289 $grid_class = $blockuniqueclass .' blockspare-latest-post-wrap blockspare-is-list ' . $list_layout_class;
290
291
292 /* Post grid section title */
293 if (isset($attributes['displaySectionTitle']) && $attributes['displaySectionTitle'] && !empty($attributes['sectionTitle'])) {
294 if (isset($attributes['sectionTitleTag'])) {
295 $section_title_tag = $attributes['sectionTitleTag'];
296 } else {
297 $section_title_tag = 'h4';
298 }
299
300 $section_title = '<' . esc_attr($section_title_tag) . '>' . esc_html($attributes['sectionTitle']) . '</' . esc_attr($section_title_tag) . '>';
301 } else {
302 $section_title = null;
303 }
304
305 /* Post grid section tag */
306 if (isset($attributes['sectionTag'])) {
307 $section_tag = $attributes['sectionTag'];
308 } else {
309 $section_tag = 'section';
310 }
311
312 /* Output the post markup */
313 $block_content = sprintf(
314 '<%1$s class="%2$s"><div class="%4$s">%5$s</div></%1$s>',
315 $section_tag,
316 esc_attr($class),
317 $section_title,
318 esc_attr($grid_class),
319 $blockspare_content_markup
320
321
322 );
323
324 $block_content .= '<style type="text/css">';
325 $block_content .= ' .' . $blockuniqueclass . '.blockspare-latest-post-wrap{
326 margin-top:' . $attributes['marginTop'] . 'px;
327 margin-bottom:' . $attributes['marginBottom'] . 'px;
328 }';
329
330 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-category{
331 color:' . $attributes['linkColor'] . ';
332 }';
333
334 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-author a span{
335 color:' . $attributes['linkColor'] . ';
336 }';
337
338 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-text-link a span{
339 color:' . $attributes['linkColor'] . ';
340 }';
341
342 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a span{
343 color: ' . $attributes['postTitleColor'] . ';
344 font-size: ' . $attributes['postTitleFontSize'] . $attributes['titleFontSizeType'] . ';
345 font-family: ' . $attributes['titleFontFamily'] . ';
346 font-weight: ' . $attributes['titleFontWeight'] . ';
347 }';
348
349 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-date{
350 color:' . $attributes['generalColor'] . ';
351 }';
352
353 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-excerpt{
354 color:' . $attributes['generalColor'] . ';
355 }';
356
357
358
359
360 if ($attributes['design'] != 'blockspare-list-layout-4' && $attributes['design'] != 'blockspare-list-layout-5' && $attributes['design'] != 'blockspare-list-layout-6') {
361 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-single{
362 border: 1px solid ' . $attributes['borderColor'] . ';
363 background-color:' . $attributes['backGroundColor'] . ';
364 border-radius:' . $attributes['borderRadius'] . ';
365 }';
366
367 }
368
369 if ($attributes['enableBoxShadow']) {
370 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-single{
371 box-shadow = ' . $attributes['xOffset'] . 'px ' . $attributes['yOffset'] . 'px ' . $attributes['blur'] . 'px ' . $attributes['spread'] . 'px ' . $attributes['shadowColor'] . ';
372 }';
373 }
374
375
376 $block_content .= '@media (max-width: 1025px) { ';
377 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a{
378 font-size: ' . $attributes['titleFontSizeTablet'] . $attributes['titleFontSizeType'] . ';
379 }';
380 $block_content .= '}';
381
382
383 $block_content .= '@media (max-width: 768px) { ';
384 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a{
385 font-size: ' . $attributes['titleFontSizeMobile'] . $attributes['titleFontSizeType'] . ';
386 }';
387 $block_content .= '}';
388
389
390 $block_content .= '</style>';
391 return $block_content;
392 }
393 }
394
395 /**
396 * Registers the post grid block on server
397 */
398 function blockspare_blocks_register_block_core_latest_posts_list()
399 {
400
401 /* Check if the register function exists */
402 if (!function_exists('register_block_type')) {
403 return;
404 }
405
406 ob_start();
407 include BLOCKSPARE_PLUGIN_DIR . '/src/blocks/latest-posts-list/block.json';
408
409 $metadata = json_decode(ob_get_clean(), true);
410
411 /* Block attributes */
412 register_block_type(
413 'blockspare/blockspare-latest-posts-list',
414 array(
415 'attributes' => $metadata['attributes'],
416 'render_callback' => 'blockspare_blocks_render_block_core_latest_posts_list',
417 )
418 );
419 }
420
421 add_action('init', 'blockspare_blocks_register_block_core_latest_posts_list');
422
423
424 /**
425 * Create API fields for additional info
426 */
427 function blockspare_blocks_post_register_rest_fields()
428 {
429
430 register_rest_field('post', 'featured_image_urls',
431 array(
432 'get_callback' => 'blockspare_featured_image_urls',
433 'update_callback' => null,
434 'schema' => array(
435 'description' => __('Different sized featured images', 'blockspare'),
436 'type' => 'array',
437 ),
438 )
439 );
440
441 // Excer
442
443 /* Add author info */
444 register_rest_field(
445 'post',
446 'author_info',
447 array(
448 'get_callback' => 'blockspare_blocks_get_author_infos',
449 'update_callback' => null,
450 'schema' => null,
451 )
452 );
453
454 /* Add author info */
455 register_rest_field(
456 'post',
457 'category_info',
458 array(
459 'get_callback' => 'blockspare_blocks_get_category_infos',
460 'update_callback' => null,
461 'schema' => null,
462 )
463 );
464
465 /* Add author info */
466 register_rest_field(
467 'post',
468 'tag_info',
469 array(
470 'get_callback' => 'blockspare_blocks_get_tag_infos',
471 'update_callback' => null,
472 'schema' => null,
473 )
474 );
475 }
476
477 add_action('rest_api_init', 'blockspare_blocks_post_register_rest_fields');
478
479
480 /**
481 * Get author info for the rest field
482 *
483 * @param String $object The object type.
484 * @param String $field_name Name of the field to retrieve.
485 * @param String $request The current request object.
486 */
487 function blockspare_blocks_get_author_infos($object, $field_name, $request)
488 {
489 /* Get the author name */
490 $author_data['display_name'] = get_the_author_meta('display_name', $object['author']);
491
492 /* Get the author link */
493 $author_data['author_link'] = get_author_posts_url($object['author']);
494
495 /* Return the author data */
496 return $author_data;
497 }
498
499 function blockspare_blocks_get_category_infos($object, $field_name, $request)
500 {
501
502
503 return get_the_category_list(esc_html__(' ', 'blockspare'), '', $object['id']);
504
505 }
506
507 function blockspare_blocks_get_tag_infos($object, $field_name, $request)
508 {
509
510 ob_start();
511 $cate_name = '';
512 if (!empty($object)) {
513 foreach ($object['categories'] as $cat_id) {
514 $cate_name = get_cat_name($cat_id);
515 }
516 }
517 ob_clean();
518 return $cate_name;
519
520 }
521
522 if (!function_exists('blockspare_featured_image_urls')) {
523 /**
524 * Get the different featured image sizes that the blog will use.
525 * Used in the custom REST API endpoint.
526 *
527 * @since 1.7
528 */
529 function blockspare_featured_image_urls($object, $field_name, $request)
530 {
531 return blockspare_featured_image_urls_from_url(!empty($object['featured_media']) ? $object['featured_media'] : '');
532 }
533 }
534
535
536 if (!function_exists('blockspare_featured_image_urls_from_url')) {
537 /**
538 * Get the different featured image sizes that the blog will use.
539 *
540 * @since 2.0
541 */
542 function blockspare_featured_image_urls_from_url($attachment_id)
543 {
544
545 $image = wp_get_attachment_image_src($attachment_id, 'full', false);
546 $sizes = get_intermediate_image_sizes();
547
548 $imageSizes = array(
549 'full' => is_array($image) ? $image : '',
550 );
551
552 foreach ($sizes as $size) {
553 $imageSizes[$size] = is_array($image) ? wp_get_attachment_image_src($attachment_id, $size, false) : '';
554 }
555
556 return $imageSizes;
557 }
558 }
559
560
561 if (!function_exists('blockspare_post_excerpt')) {
562 /**
563 * Get the post excerpt.
564 *
565 * @since 1.7
566 */
567 function blockspare_post_excerpt($object)
568 {
569 return blockspare_get_excerpt($object['id']);
570 }
571 }
572
573 if (!function_exists('blockspare_get_excerpt')) {
574 /**
575 * Get the excerpt.
576 *
577 * @since 1.7
578 */
579 function blockspare_get_excerpt($post_id, $post = null)
580 {
581 $excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id, 'display'));
582 if (!empty($excerpt)) {
583 return $excerpt;
584 }
585
586 $max_excerpt = 100; // WP default is 55.
587
588 if (!empty($post['post_content'])) {
589 return apply_filters('the_excerpt', wp_trim_words($post['post_content'], $max_excerpt));
590 }
591 $post_content = apply_filters('the_content', get_post_field('post_content', $post_id));
592 return apply_filters('the_excerpt', wp_trim_words($post_content, $max_excerpt));
593 }
594 }
595
596
597