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

458 lines 15.1 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 global $post;
200
201
202 $excerpt = blockspare_post_excerpt($post_id, $post);
203
204 if (!$excerpt) {
205 $excerpt = null;
206 }
207
208 if (!empty($excerpt)) {
209 $excerpt = explode(' ', $excerpt);
210 $trim_to_length = (int)$attributes['excerptLength'] ? (int)$attributes['excerptLength'] : 55;
211 if (count($excerpt) > $trim_to_length) {
212 $excerpt = implode(' ', array_slice($excerpt, 0, $trim_to_length)) . '...';
213 } else {
214 $excerpt = implode(' ', $excerpt);
215 }
216
217 }
218
219
220 if ((isset($attributes['displayPostExcerpt']) && $excerpt != null) || isset($attributes['displayPostLink'])) {
221
222
223 /* Wrap the excerpt content */
224 $blockspare_content_markup .= sprintf(
225 '<div class="blockspare-block-post-grid-excerpt">'
226
227
228 );
229
230 if (isset($attributes['displayPostExcerpt']) && $attributes['displayPostExcerpt']) {
231 $blockspare_content_markup .= sprintf(
232 '<div class="blockspare-block-post-grid-excerpt-content">%s</div>',
233 wp_kses_post($excerpt)
234 );
235 }
236
237
238 /* Get the read more link */
239 if (isset($attributes['displayPostLink']) && $attributes['displayPostLink']) {
240 $blockspare_content_markup .= sprintf(
241 '<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>',
242 esc_url(get_permalink($post_id)),
243 esc_html($attributes['readMoreText']),
244 esc_html($title)
245 );
246 }
247
248
249 /* Close the excerpt content */
250 $blockspare_content_markup .= sprintf(
251 '</div>'
252 );
253
254 }
255
256 /* Close the text content */
257 $blockspare_content_markup .= sprintf(
258 '</div>'
259 );
260
261 /* Close the post */
262 $blockspare_content_markup .= "</div>\n";
263 }
264
265 /* Restore original post data */
266 wp_reset_postdata();
267
268 /* Build the block classes */
269 $class = "wp-block-blockspare-block-blockspare-latest-posts blockspare-post-wrap align{$attributes['align']}";
270
271 if (isset($attributes['className'])) {
272 $class .= ' ' . $attributes['className'];
273 }
274
275
276 $list_layout_class = $attributes['design'];
277
278 /* Layout orientation class */
279 $grid_class = $blockuniqueclass . ' blockspare-latest-post-wrap blockspare-is-grid ' . $list_layout_class . ' ' . 'column-' . $attributes['columns'];
280
281
282 /* Post grid section title */
283 if (isset($attributes['displaySectionTitle']) && $attributes['displaySectionTitle'] && !empty($attributes['sectionTitle'])) {
284 if (isset($attributes['sectionTitleTag'])) {
285 $section_title_tag = $attributes['sectionTitleTag'];
286 } else {
287 $section_title_tag = 'h4';
288 }
289
290 $section_title = '<' . esc_attr($section_title_tag) . '>' . esc_html($attributes['sectionTitle']) . '</' . esc_attr($section_title_tag) . '>';
291 } else {
292 $section_title = null;
293 }
294
295 /* Post grid section tag */
296 if (isset($attributes['sectionTag'])) {
297 $section_tag = $attributes['sectionTag'];
298 } else {
299 $section_tag = 'section';
300 }
301
302 /* Output the post markup */
303 $block_content = sprintf(
304 '<%1$s class="%2$s" ><div class="%3$s">%4$s</div></%1$s>',
305 $section_tag,
306 esc_attr($class),
307 esc_attr($grid_class),
308 $blockspare_content_markup
309
310
311 );
312
313
314 $block_content .= '<style type="text/css">';
315 $block_content .= ' .' . $blockuniqueclass . '.blockspare-latest-post-wrap{
316 margin-top:' . $attributes['marginTop'] . 'px;
317 margin-bottom:' . $attributes['marginBottom'] . 'px;
318 }';
319
320 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-category{
321 color:' . $attributes['linkColor'] . ';
322 }';
323
324 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-author a span{
325 color:' . $attributes['linkColor'] . ';
326 }';
327
328 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-text-link a span{
329 color:' . $attributes['linkColor'] . ';
330 }';
331
332 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a span{
333 color: ' . $attributes['postTitleColor'] . ';
334 font-size: ' . $attributes['postTitleFontSize'] . $attributes['titleFontSizeType'] . ';
335 font-family: ' . $attributes['titleFontFamily'] . ';
336 font-weight: ' . $attributes['titleFontWeight'] . ';
337 }';
338
339 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-date{
340 color:' . $attributes['generalColor'] . ';
341 }';
342
343 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-excerpt{
344 color:' . $attributes['generalColor'] . ';
345 }';
346
347
348
349
350 if ($attributes['design'] != 'blockspare-list-layout-4' && $attributes['design'] != 'blockspare-list-layout-5' && $attributes['design'] != 'blockspare-list-layout-6') {
351 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-single{
352 border: 1px solid ' . $attributes['borderColor'] . ';
353 background-color:' . $attributes['backGroundColor'] . ';
354 border-radius:' . $attributes['borderRadius'] . ';
355 }';
356
357 }
358
359 if ($attributes['enableBoxShadow']) {
360 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-single{
361 box-shadow = ' . $attributes['xOffset'] . 'px ' . $attributes['yOffset'] . 'px ' . $attributes['blur'] . 'px ' . $attributes['spread'] . 'px ' . $attributes['shadowColor'] . ';
362 }';
363 }
364
365
366 $block_content .= '@media (max-width: 1025px) { ';
367 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a{
368 font-size: ' . $attributes['titleFontSizeTablet'] . $attributes['titleFontSizeType'] . ';
369 }';
370 $block_content .= '}';
371
372
373 $block_content .= '@media (max-width: 768px) { ';
374 $block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a{
375 font-size: ' . $attributes['titleFontSizeMobile'] . $attributes['titleFontSizeType'] . ';
376 }';
377 $block_content .= '}';
378
379
380 $block_content .= '</style>';
381
382
383 return $block_content;
384 }
385
386
387 }
388
389
390 /**
391 * Registers the post grid block on server
392 */
393 function blockspare_blocks_register_block_core_latest_posts_grid()
394 {
395
396 /* Check if the register function exists */
397 if (!function_exists('register_block_type')) {
398 return;
399 }
400 ob_start();
401 include BLOCKSPARE_PLUGIN_DIR . '/src/blocks/latest-posts-grid/block.json';
402
403 $metadata = json_decode(ob_get_clean(), true);
404
405 /* Block attributes */
406 register_block_type(
407 'blockspare/blockspare-latest-posts-grid',
408 array(
409
410 'attributes' => $metadata['attributes'],
411 'render_callback' => 'blockspare_blocks_render_block_core_latest_posts_grid',
412 )
413 );
414 }
415
416 add_action('init', 'blockspare_blocks_register_block_core_latest_posts_grid');
417
418
419 if (!function_exists('blockspare_post_excerpt')) {
420 /**
421 * Get the post excerpt.
422 *
423 * @since 1.7
424 */
425 function blockspare_post_excerpt($object)
426 {
427 return blockspare_get_excerpt($object['id']);
428 }
429 }
430
431 if (!function_exists('blockspare_get_excerpt')) {
432 /**
433 * Get the excerpt.
434 *
435 * @since 1.7
436 */
437 function blockspare_get_excerpt($post_id, $post = null)
438 {
439 $excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id, 'display'));
440 if (!empty($excerpt)) {
441 return $excerpt;
442 }
443
444 $max_excerpt = 100; // WP default is 55.
445
446 if (!empty($post['post_content'])) {
447 return apply_filters('the_excerpt', wp_trim_words($post['post_content'], $max_excerpt));
448 }
449 $post_content = apply_filters('the_content', get_post_field('post_content', $post_id));
450 return apply_filters('the_excerpt', wp_trim_words($post_content, $max_excerpt));
451 }
452 }
453
454
455
456
457
458