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

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