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

585 lines 18.7 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 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">'
217
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 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 );
236 }
237
238 /* Close the excerpt content */
239 $blockspare_content_markup .= sprintf(
240 '</div>'
241 );
242
243 }
244
245 /* Close the text content */
246 $blockspare_content_markup .= sprintf(
247 '</div>'
248 );
249
250 $blockspare_content_markup .= sprintf(
251 '</div>'
252 );
253
254 /* Close the post */
255 $blockspare_content_markup .= "</div>\n";
256 }
257
258 /* Restore original post data */
259 wp_reset_postdata();
260
261 /* Build the block classes */
262 $class = "wp-block-blockspare-block-blockspare-latest-posts blockspare-post-wrap align{$attributes['align']}";
263
264 if (isset($attributes['className'])) {
265 $class .= ' ' . $attributes['className'];
266 }
267 $col_class = '';
268
269 if ($attributes['design'] === 'blockspare-is-grid') {
270 $col_class = 'column-' . $attributes['columns'];
271 }
272
273
274 $list_layout_class = $attributes['design'];
275
276 /* Layout orientation class */
277 $grid_class = $blockuniqueclass .' blockspare-latest-post-wrap blockspare-is-list ' . $list_layout_class;
278
279
280 /* Post grid section title */
281 if (isset($attributes['displaySectionTitle']) && $attributes['displaySectionTitle'] && !empty($attributes['sectionTitle'])) {
282 if (isset($attributes['sectionTitleTag'])) {
283 $section_title_tag = $attributes['sectionTitleTag'];
284 } else {
285 $section_title_tag = 'h4';
286 }
287
288 $section_title = '<' . esc_attr($section_title_tag) . '>' . esc_html($attributes['sectionTitle']) . '</' . esc_attr($section_title_tag) . '>';
289 } else {
290 $section_title = null;
291 }
292
293 /* Post grid section tag */
294 if (isset($attributes['sectionTag'])) {
295 $section_tag = $attributes['sectionTag'];
296 } else {
297 $section_tag = 'section';
298 }
299
300 /* Output the post markup */
301 $block_content = sprintf(
302 '<%1$s class="%2$s"><div class="%4$s">%5$s</div></%1$s>',
303 $section_tag,
304 esc_attr($class),
305 $section_title,
306 esc_attr($grid_class),
307 $blockspare_content_markup
308
309
310 );
311
312 $block_content .= '<style type="text/css">';
313 $block_content .= ' .' . $blockuniqueclass . '.blockspare-latest-post-wrap{
314 margin-top:' . $attributes['marginTop'] . 'px;
315 margin-bottom:' . $attributes['marginBottom'] . 'px;
316 }';
317
318 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-category{
319 color:' . $attributes['linkColor'] . ';
320 }';
321
322 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-author a span{
323 color:' . $attributes['linkColor'] . ';
324 }';
325
326 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-text-link a span{
327 color:' . $attributes['linkColor'] . ';
328 }';
329
330 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a span{
331 color: ' . $attributes['postTitleColor'] . ';
332 font-size: ' . $attributes['postTitleFontSize'] . $attributes['titleFontSizeType'] . ';
333 font-family: ' . $attributes['titleFontFamily'] . ';
334 font-weight: ' . $attributes['titleFontWeight'] . ';
335 }';
336
337 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-date{
338 color:' . $attributes['generalColor'] . ';
339 }';
340
341 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-excerpt{
342 color:' . $attributes['generalColor'] . ';
343 }';
344
345
346
347
348 if ($attributes['design'] != 'blockspare-list-layout-4' && $attributes['design'] != 'blockspare-list-layout-5' && $attributes['design'] != 'blockspare-list-layout-6') {
349 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-single{
350 border: 1px solid ' . $attributes['borderColor'] . ';
351 background-color:' . $attributes['backGroundColor'] . ';
352 border-radius:' . $attributes['borderRadius'] . ';
353 }';
354
355 }
356
357 if ($attributes['enableBoxShadow']) {
358 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-single{
359 box-shadow = ' . $attributes['xOffset'] . 'px ' . $attributes['yOffset'] . 'px ' . $attributes['blur'] . 'px ' . $attributes['spread'] . 'px ' . $attributes['shadowColor'] . ';
360 }';
361 }
362
363
364 $block_content .= '@media (max-width: 1025px) { ';
365 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a{
366 font-size: ' . $attributes['titleFontSizeTablet'] . $attributes['titleFontSizeType'] . ';
367 }';
368 $block_content .= '}';
369
370
371 $block_content .= '@media (max-width: 768px) { ';
372 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a{
373 font-size: ' . $attributes['titleFontSizeMobile'] . $attributes['titleFontSizeType'] . ';
374 }';
375 $block_content .= '}';
376
377
378 $block_content .= '</style>';
379 return $block_content;
380 }
381 }
382
383 /**
384 * Registers the post grid block on server
385 */
386 function blockspare_blocks_register_block_core_latest_posts_list()
387 {
388
389 /* Check if the register function exists */
390 if (!function_exists('register_block_type')) {
391 return;
392 }
393
394 ob_start();
395 include BLOCKSPARE_PLUGIN_DIR . '/src/blocks/latest-posts-list/block.json';
396
397 $metadata = json_decode(ob_get_clean(), true);
398
399 /* Block attributes */
400 register_block_type(
401 'blockspare/blockspare-latest-posts-list',
402 array(
403 'attributes' => $metadata['attributes'],
404 'render_callback' => 'blockspare_blocks_render_block_core_latest_posts_list',
405 )
406 );
407 }
408
409 add_action('init', 'blockspare_blocks_register_block_core_latest_posts_list');
410
411
412 /**
413 * Create API fields for additional info
414 */
415 function blockspare_blocks_post_register_rest_fields()
416 {
417
418 register_rest_field('post', 'featured_image_urls',
419 array(
420 'get_callback' => 'blockspare_featured_image_urls',
421 'update_callback' => null,
422 'schema' => array(
423 'description' => __('Different sized featured images', 'blockspare'),
424 'type' => 'array',
425 ),
426 )
427 );
428
429 // Excer
430
431 /* Add author info */
432 register_rest_field(
433 'post',
434 'author_info',
435 array(
436 'get_callback' => 'blockspare_blocks_get_author_infos',
437 'update_callback' => null,
438 'schema' => null,
439 )
440 );
441
442 /* Add author info */
443 register_rest_field(
444 'post',
445 'category_info',
446 array(
447 'get_callback' => 'blockspare_blocks_get_category_infos',
448 'update_callback' => null,
449 'schema' => null,
450 )
451 );
452
453 /* Add author info */
454 register_rest_field(
455 'post',
456 'tag_info',
457 array(
458 'get_callback' => 'blockspare_blocks_get_tag_infos',
459 'update_callback' => null,
460 'schema' => null,
461 )
462 );
463 }
464
465 add_action('rest_api_init', 'blockspare_blocks_post_register_rest_fields');
466
467
468 /**
469 * Get author info for the rest field
470 *
471 * @param String $object The object type.
472 * @param String $field_name Name of the field to retrieve.
473 * @param String $request The current request object.
474 */
475 function blockspare_blocks_get_author_infos($object, $field_name, $request)
476 {
477 /* Get the author name */
478 $author_data['display_name'] = get_the_author_meta('display_name', $object['author']);
479
480 /* Get the author link */
481 $author_data['author_link'] = get_author_posts_url($object['author']);
482
483 /* Return the author data */
484 return $author_data;
485 }
486
487 function blockspare_blocks_get_category_infos($object, $field_name, $request)
488 {
489
490
491 return get_the_category_list(esc_html__(' ', 'blockspare'), '', $object['id']);
492
493 }
494
495 function blockspare_blocks_get_tag_infos($object, $field_name, $request)
496 {
497
498 ob_start();
499 $cate_name = '';
500 if (!empty($object)) {
501 foreach ($object['categories'] as $cat_id) {
502 $cate_name = get_cat_name($cat_id);
503 }
504 }
505 ob_clean();
506 return $cate_name;
507
508 }
509
510 if (!function_exists('blockspare_featured_image_urls')) {
511 /**
512 * Get the different featured image sizes that the blog will use.
513 * Used in the custom REST API endpoint.
514 *
515 * @since 1.7
516 */
517 function blockspare_featured_image_urls($object, $field_name, $request)
518 {
519 return blockspare_featured_image_urls_from_url(!empty($object['featured_media']) ? $object['featured_media'] : '');
520 }
521 }
522
523
524 if (!function_exists('blockspare_featured_image_urls_from_url')) {
525 /**
526 * Get the different featured image sizes that the blog will use.
527 *
528 * @since 2.0
529 */
530 function blockspare_featured_image_urls_from_url($attachment_id)
531 {
532
533 $image = wp_get_attachment_image_src($attachment_id, 'full', false);
534 $sizes = get_intermediate_image_sizes();
535
536 $imageSizes = array(
537 'full' => is_array($image) ? $image : '',
538 );
539
540 foreach ($sizes as $size) {
541 $imageSizes[$size] = is_array($image) ? wp_get_attachment_image_src($attachment_id, $size, false) : '';
542 }
543
544 return $imageSizes;
545 }
546 }
547
548
549 if (!function_exists('blockspare_post_excerpt')) {
550 /**
551 * Get the post excerpt.
552 *
553 * @since 1.7
554 */
555 function blockspare_post_excerpt($object)
556 {
557 return blockspare_get_excerpt($object['id']);
558 }
559 }
560
561 if (!function_exists('blockspare_get_excerpt')) {
562 /**
563 * Get the excerpt.
564 *
565 * @since 1.7
566 */
567 function blockspare_get_excerpt($post_id, $post = null)
568 {
569 $excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id, 'display'));
570 if (!empty($excerpt)) {
571 return $excerpt;
572 }
573
574 $max_excerpt = 100; // WP default is 55.
575
576 if (!empty($post['post_content'])) {
577 return apply_filters('the_excerpt', wp_trim_words($post['post_content'], $max_excerpt));
578 }
579 $post_content = apply_filters('the_content', get_post_field('post_content', $post_id));
580 return apply_filters('the_excerpt', wp_trim_words($post_content, $max_excerpt));
581 }
582 }
583
584
585