PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 1.3.5
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v1.3.5
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-grid / index.php

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

448 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 //wp_add_inline_style('westore-style', westore_custom_style());
4
5 function blockspare_blocks_render_block_core_latest_posts_grid($attributes)
6 {
7
8
9 $unq_class = mt_rand(100000,999999);
10 $blockuniqueclass = '';
11
12 if(!empty($attributes['uniqueClass'])){
13 $blockuniqueclass = $attributes['uniqueClass'];
14 }else{
15 $blockuniqueclass = 'blockspare-grid-'.$unq_class;
16 }
17
18
19 $categories = isset($attributes['categories']) ? $attributes['categories'] : '';
20
21 /* Setup the query */
22 $grid_query = new WP_Query(
23 array(
24 'posts_per_page' => $attributes['postsToShow'],
25 'post_status' => 'publish',
26 'order' => $attributes['order'],
27 'orderby' => $attributes['orderBy'],
28 'cat' => $categories,
29 'offset' => $attributes['offset'],
30 'post_type' => $attributes['postType'],
31 'ignore_sticky_posts' => 1,
32 )
33 );
34
35 $blockspare_content_markup = '';
36
37 /* Start the loop */
38 if ($grid_query->have_posts()) {
39
40 while ($grid_query->have_posts()) {
41 $grid_query->the_post();
42
43 /* Setup the post ID */
44 $post_id = get_the_ID();
45
46 /* Setup the featured image ID */
47 $post_thumb_id = get_post_thumbnail_id($post_id);
48
49 $has_img_class = '';
50
51 if(!$post_thumb_id){
52 $has_img_class = "post-has-no-image";
53 }
54
55
56 /* Setup the post classes */
57 $post_classes = 'blockspare-post-single';
58
59 /* Add sticky class */
60 if (is_sticky($post_id)) {
61 $post_classes .= ' sticky';
62 } else {
63 $post_classes .= null;
64 }
65
66
67 if ($attributes['enableBackgroundColor']) {
68 $post_classes .= ' has-background';
69 }
70
71
72 if ($attributes['equalImageHeight']) {
73 $post_classes .= ' has-img-equal-height';
74 } else {
75 $post_classes .= '';
76 }
77
78
79 /* Start the markup for the post */
80 $blockspare_content_markup .= sprintf(
81 '<div id="post-%1$s" class="%2$s %3$s">',
82 esc_attr($post_id),
83 esc_attr($post_classes),
84 $has_img_class
85
86
87 );
88
89 /* Get the featured image */
90 if (isset($attributes['displayPostImage']) && $attributes['displayPostImage'] && $post_thumb_id) {
91
92
93 if (!empty($attributes['imageSize'])) {
94 $post_thumb_size = $attributes['imageSize'];
95 }
96
97 if (has_post_thumbnail($post_id)) {
98 /* Output the featured image */
99 $blockspare_content_markup .= sprintf(
100 '<figure class="blockspare-post-img">
101 <a href="%1$s" rel="bookmark" aria-hidden="true" tabindex="-1" > %2$s</a></figure>',
102 esc_url(get_permalink($post_id)),
103 wp_get_attachment_image($post_thumb_id, $post_thumb_size)
104
105
106 );
107 }
108
109 }
110
111
112 /* Wrap the text content */
113 $blockspare_content_markup .= sprintf(
114 '<div class="blockspare-post-content %1$s">',
115 $attributes['contentOrder']
116
117 );
118
119 $blockspare_content_markup .= sprintf(
120 '<div class="blockspare-bg-overlay"></div>'
121
122 );
123
124
125 $blockspare_content_markup .= sprintf(
126 '<header class="blockspare-block-post-grid-header">'
127 );
128 if ($attributes['displayPostCategory']) {
129 $blockspare_content_markup .= sprintf(
130 '<div class="blockspare-post-category" >%1$s</div>',
131 get_the_category_list(esc_html__(' ', 'blockspare'), '', $post_id)
132 );
133 }
134
135 /* Get the post title */
136 $title = get_the_title($post_id);
137
138 if (!$title) {
139 $title = __('Untitled', 'blockspare');
140 }
141
142 if (isset($attributes['displayPostTitle']) && $attributes['displayPostTitle']) {
143
144 if (isset($attributes['postTitleTag'])) {
145 $post_title_tag = $attributes['postTitleTag'];
146 } else {
147 $post_title_tag = 'h4';
148 }
149
150 $blockspare_content_markup .= sprintf(
151 '<%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>',
152 esc_url(get_permalink($post_id)),
153 esc_html($title),
154 esc_attr($post_title_tag)
155 );
156 }
157
158 if (isset($attributes['postType']) && ($attributes['postType'] === 'post') && (isset($attributes['displayPostAuthor']) || isset($attributes['displayPostDate']))) {
159 /* Wrap the byline content */
160
161 $blockspare_content_markup .= sprintf(
162 '<div class="blockspare-block-post-grid-byline">'
163 );
164
165 /* Get the post author */
166 if (isset($attributes['displayPostAuthor']) && $attributes['displayPostAuthor']) {
167 $blockspare_content_markup .= sprintf(
168 '<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>',
169 esc_html(get_the_author_meta('display_name', get_the_author_meta('ID'))),
170 esc_html(get_author_posts_url(get_the_author_meta('ID')))
171
172 );
173 }
174
175 /* Get the post date */
176 if (isset($attributes['displayPostDate']) && $attributes['displayPostDate']) {
177 $blockspare_content_markup .= sprintf(
178 '<time datetime="%1$s" class="blockspare-block-post-grid-date" itemprop="datePublished">%2$s</time>',
179 esc_attr(get_the_date('c', $post_id)),
180 esc_html(get_the_date('', $post_id))
181
182 );
183 }
184
185 /* Close the byline content */
186 $blockspare_content_markup .= sprintf(
187 '</div>'
188 );
189 }
190
191 /* Close the header content */
192 $blockspare_content_markup .= sprintf(
193 '</header>'
194 );
195
196
197 /* Get the excerpt */
198
199 $post_id = get_the_ID();
200
201 $excerpt = apply_filters('the_excerpt',
202 get_post_field(
203 'post_excerpt',
204 $post_id,
205 'display'
206 )
207 );
208
209 if(empty($excerpt) && isset($attributes['excerptLength'])) {
210 // 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
211 $excerpt = apply_filters('the_excerpt',
212 wp_trim_words(
213 preg_replace(
214 array(
215 '/\<figcaption>.*\<\/figcaption>/',
216 '/\[caption.*\[\/caption\]/',
217 ),
218 '',
219 get_the_content()
220 ),
221 $attributes['excerptLength']
222 )
223 );
224 }
225
226 // Trim the excerpt if necessary.
227 if ( isset( $attributes['excerptLength'] ) ) {
228 $excerpt = wp_trim_words(
229 $excerpt,
230 $attributes['excerptLength']
231 );
232 }
233
234 $excerpt = apply_filters( 'the_excerpt', $excerpt );
235
236 if(!$excerpt) {
237 $excerpt = null;
238 }
239
240
241 if ((isset($attributes['displayPostExcerpt']) && $excerpt != null) || isset($attributes['displayPostLink'])) {
242
243
244 /* Wrap the excerpt content */
245 $blockspare_content_markup .= sprintf(
246 '<div class="blockspare-block-post-grid-excerpt">'
247
248
249 );
250
251 if (isset($attributes['displayPostExcerpt']) && $attributes['displayPostExcerpt']) {
252 $blockspare_content_markup .= sprintf(
253 '<div class="blockspare-block-post-grid-excerpt-content">%1$s</div>',
254 wp_kses_post($excerpt)
255 );
256 }
257
258
259 /* Get the read more link */
260 if (isset($attributes['displayPostLink']) && $attributes['displayPostLink']) {
261 $blockspare_content_markup .= sprintf(
262 '<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>',
263 esc_url(get_permalink($post_id)),
264 esc_html($attributes['readMoreText']),
265 esc_html($title)
266 );
267 }
268
269
270 /* Close the excerpt content */
271 $blockspare_content_markup .= sprintf(
272 '</div>'
273 );
274
275 }
276
277 /* Close the text content */
278 $blockspare_content_markup .= sprintf(
279 '</div>'
280 );
281
282 /* Close the post */
283 $blockspare_content_markup .= "</div>\n";
284 }
285
286 /* Restore original post data */
287 wp_reset_postdata();
288
289 /* Build the block classes */
290 $class = "wp-block-blockspare-block-blockspare-latest-posts blockspare-post-wrap align{$attributes['align']}";
291
292 if (isset($attributes['className'])) {
293 $class .= ' ' . $attributes['className'];
294 }
295
296
297 $list_layout_class = $attributes['design'];
298
299 /* Layout orientation class */
300 $grid_class = $blockuniqueclass . ' blockspare-latest-post-wrap blockspare-is-grid ' . $list_layout_class . ' ' . 'column-' . $attributes['columns'];
301
302
303 /* Post grid section title */
304 if (isset($attributes['displaySectionTitle']) && $attributes['displaySectionTitle'] && !empty($attributes['sectionTitle'])) {
305 if (isset($attributes['sectionTitleTag'])) {
306 $section_title_tag = $attributes['sectionTitleTag'];
307 } else {
308 $section_title_tag = 'h4';
309 }
310
311 $section_title = '<' . esc_attr($section_title_tag) . '>' . esc_html($attributes['sectionTitle']) . '</' . esc_attr($section_title_tag) . '>';
312 } else {
313 $section_title = null;
314 }
315
316 /* Post grid section tag */
317 if (isset($attributes['sectionTag'])) {
318 $section_tag = $attributes['sectionTag'];
319 } else {
320 $section_tag = 'section';
321 }
322
323 /* Output the post markup */
324 $block_content = sprintf(
325 '<%1$s class="%2$s" ><div class="%3$s">%4$s</div></%1$s>',
326 $section_tag,
327 esc_attr($class),
328 esc_attr($grid_class),
329 $blockspare_content_markup
330
331
332 );
333
334
335 $block_content .= '<style type="text/css">';
336 $block_content .= ' .' . $blockuniqueclass . '.blockspare-latest-post-wrap{
337 margin-top:' . $attributes['marginTop'] . 'px;
338 margin-bottom:' . $attributes['marginBottom'] . 'px;
339 }';
340
341 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-category{
342 color:' . $attributes['linkColor'] . ';
343 }';
344
345 $block_content .= ' .' . $blockuniqueclass . ' a.blockspare-text-link.blockspare-block-post-grid-more-link{
346 color:' . $attributes['linkColor'] . ';
347 }';
348
349 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-author a span{
350 color:' . $attributes['linkColor'] . ';
351 }';
352
353 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-text-link a span{
354 color:' . $attributes['linkColor'] . ';
355 }';
356
357 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a span{
358 color: ' . $attributes['postTitleColor'] . ';
359 font-size: ' . $attributes['postTitleFontSize'] . $attributes['titleFontSizeType'] . ';
360 font-family: ' . $attributes['titleFontFamily'] . ';
361 font-weight: ' . $attributes['titleFontWeight'] . ';
362 }';
363
364 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-date{
365 color:' . $attributes['generalColor'] . ';
366 }';
367
368 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-excerpt{
369 color:' . $attributes['generalColor'] . ';
370 }';
371
372
373
374
375 if ($attributes['design'] != 'blockspare-list-layout-4' && $attributes['design'] != 'blockspare-list-layout-5' && $attributes['design'] != 'blockspare-list-layout-6') {
376 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-single{
377 border: 1px solid ' . $attributes['borderColor'] . ';
378 background-color:' . $attributes['backGroundColor'] . ';
379 border-radius:' . $attributes['borderRadius'] . ';
380 }';
381
382 }
383
384 if ($attributes['enableBoxShadow']) {
385 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-single{
386 box-shadow = ' . $attributes['xOffset'] . 'px ' . $attributes['yOffset'] . 'px ' . $attributes['blur'] . 'px ' . $attributes['spread'] . 'px ' . $attributes['shadowColor'] . ';
387 }';
388 }
389
390
391 $block_content .= '@media (max-width: 1025px) { ';
392 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a{
393 font-size: ' . $attributes['titleFontSizeTablet'] . $attributes['titleFontSizeType'] . ';
394 }';
395 $block_content .= '}';
396
397
398 $block_content .= '@media (max-width: 768px) { ';
399 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a{
400 font-size: ' . $attributes['titleFontSizeMobile'] . $attributes['titleFontSizeType'] . ';
401 }';
402 $block_content .= '}';
403
404
405 $block_content .= '</style>';
406
407
408 return $block_content;
409 }
410
411
412 }
413
414
415 /**
416 * Registers the post grid block on server
417 */
418 function blockspare_blocks_register_block_core_latest_posts_grid()
419 {
420
421 /* Check if the register function exists */
422 if (!function_exists('register_block_type')) {
423 return;
424 }
425 ob_start();
426 include BLOCKSPARE_PLUGIN_DIR . '/src/blocks/latest-posts-grid/block.json';
427
428 $metadata = json_decode(ob_get_clean(), true);
429
430 /* Block attributes */
431 register_block_type(
432 'blockspare/blockspare-latest-posts-grid',
433 array(
434
435 'attributes' => $metadata['attributes'],
436 'render_callback' => 'blockspare_blocks_render_block_core_latest_posts_grid',
437 )
438 );
439 }
440
441 add_action('init', 'blockspare_blocks_register_block_core_latest_posts_grid');
442
443
444
445
446
447
448