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

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