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

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