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

389 lines 13.5 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_grid($attributes)
4 {
5
6 $categories = isset($attributes['categories']) ? $attributes['categories'] : '';
7
8 /* Setup the query */
9 $grid_query = new WP_Query(
10 array(
11 'posts_per_page' => $attributes['postsToShow'],
12 'post_status' => 'publish',
13 'order' => $attributes['order'],
14 'orderby' => $attributes['orderBy'],
15 'cat' => $categories,
16 'offset' => $attributes['offset'],
17 'post_type' => $attributes['postType'],
18 'ignore_sticky_posts' => 1,
19 )
20 );
21
22 $blockspare_content_markup = '';
23
24 /* Start the loop */
25 if ($grid_query->have_posts()) {
26
27 while ($grid_query->have_posts()) {
28 $grid_query->the_post();
29
30 /* Setup the post ID */
31 $post_id = get_the_ID();
32
33 /* Setup the featured image ID */
34 $post_thumb_id = get_post_thumbnail_id($post_id);
35
36 /* Setup the post classes */
37 $post_classes = 'blockspare-post-single';
38
39 /* Add sticky class */
40 if (is_sticky($post_id)) {
41 $post_classes .= ' sticky';
42 } else {
43 $post_classes .= null;
44 }
45
46
47 if ($attributes['enableBackgroundColor']) {
48 $post_classes .= ' has-background';
49 }
50
51
52 $bxshadow = '';
53
54 if ($attributes['enableBoxShadow']) {
55 $bxshadow = $attributes['xOffset'] . "px " . $attributes['yOffset'] . "px " . $attributes['blur'] . "px " . $attributes['spread'] . "px " . $attributes['shadowColor'];
56 }
57
58 if ($attributes['equalImageHeight']) {
59 $post_classes .= ' has-img-equal-height';
60 } else {
61 $post_classes .= '';
62 }
63 /* Join classes together */
64 $post_classes = join(' ', get_post_class($post_classes, $post_id));
65
66
67 $item_background_color = ($attributes['enableBackgroundColor']) ? esc_attr($attributes['backGroundColor']) : '';
68 $item_style = '';
69 if (($attributes['design'] != 'blockspare-grid-layout-4') && ($attributes['design'] != 'blockspare-grid-layout-5') && ($attributes['design'] != 'blockspare-grid-layout-6')) {
70 $item_style .= 'background-color:' . esc_attr($item_background_color) . ';';
71 $item_style .= ' border-radius:' . esc_attr($attributes['borderRadius']) . "px;";
72 $item_style .= ' box-shadow:' . esc_attr($bxshadow) . ';';
73 }
74
75 /* Start the markup for the post */
76 $blockspare_content_markup .= sprintf(
77 '<article id="post-%1$s" class="%2$s" style="%3$s">',
78 esc_attr($post_id),
79 esc_attr($post_classes),
80 $item_style
81
82 );
83
84 /* Get the featured image */
85 if (isset($attributes['displayPostImage']) && $attributes['displayPostImage'] && $post_thumb_id) {
86
87
88 if (!empty($attributes['imageSize'])) {
89 $post_thumb_size = $attributes['imageSize'];
90 }
91
92 if (has_post_thumbnail($post_id)) {
93 /* Output the featured image */
94 $blockspare_content_markup .= sprintf(
95 '<figure class="blockspare-post-img">
96 <a href="%1$s" rel="bookmark" aria-hidden="true" tabindex="-1" > %2$s</a></figure>',
97 esc_url(get_permalink($post_id)),
98 wp_get_attachment_image($post_thumb_id, $post_thumb_size)
99
100
101 );
102 }
103
104 }
105
106
107 /* Wrap the text content */
108 $blockspare_content_markup .= sprintf(
109 '<div class="blockspare-post-content %1$s">',
110 $attributes['contentOrder']
111
112 );
113
114 $blockspare_content_markup .= sprintf(
115 '<div class="blockspare-bg-overlay" style="background-color: %1$s"></div>',
116 ($attributes['enableBackgroundColor']) ? esc_attr($attributes['backGroundColor']) : ''
117
118 );
119
120
121 $blockspare_content_markup .= sprintf(
122 '<header class="blockspare-block-post-grid-header">'
123 );
124 if ($attributes['displayPostCategory']) {
125 $blockspare_content_markup .= sprintf(
126 '<div class="blockspare-post-category" style="color:%2$s;">%1$s</div>',
127 get_the_category_list(esc_html__(' ', 'blockspare'), '', $post_id),
128 esc_attr($attributes['linkColor'])
129 );
130 }
131
132 /* Get the post title */
133 $title = get_the_title($post_id);
134
135 if (!$title) {
136 $title = __('Untitled', 'blockspare');
137 }
138
139 if (isset($attributes['displayPostTitle']) && $attributes['displayPostTitle']) {
140
141 if (isset($attributes['postTitleTag'])) {
142 $post_title_tag = $attributes['postTitleTag'];
143 } else {
144 $post_title_tag = 'h4';
145 }
146
147 $blockspare_content_markup .= sprintf(
148 '<%3$s class="blockspare-block-post-grid-title" style="font-size: %5$s;"><a style="color:%4$s;" href="%1$s" rel="bookmark">%2$s</a></%3$s>',
149 esc_url(get_permalink($post_id)),
150 esc_html($title),
151 esc_attr($post_title_tag),
152 esc_attr($attributes['postTitleColor']),
153 esc_attr($attributes['postTitleFontSize']) . 'px'
154 );
155 }
156
157 if (isset($attributes['postType']) && ($attributes['postType'] === 'post') && (isset($attributes['displayPostAuthor']) || isset($attributes['displayPostDate']))) {
158 /* Wrap the byline content */
159
160 $blockspare_content_markup .= sprintf(
161 '<div class="blockspare-block-post-grid-byline">'
162 );
163
164 /* Get the post author */
165 if (isset($attributes['displayPostAuthor']) && $attributes['displayPostAuthor']) {
166 $blockspare_content_markup .= sprintf(
167 '<div class="blockspare-block-post-grid-author"><a style="color:%3$s;" class="blockspare-text-link" href="%2$s" itemprop="url" rel="author"><span itemprop="name">%1$s</span></a></div>',
168 esc_html(get_the_author_meta('display_name', get_the_author_meta('ID'))),
169 esc_html(get_author_posts_url(get_the_author_meta('ID'))),
170 esc_attr($attributes['linkColor'])
171 );
172 }
173
174 /* Get the post date */
175 if (isset($attributes['displayPostDate']) && $attributes['displayPostDate']) {
176 $blockspare_content_markup .= sprintf(
177 '<time style="color: %3$s" datetime="%1$s" class="blockspare-block-post-grid-date" itemprop="datePublished">%2$s</time>',
178 esc_attr(get_the_date('c', $post_id)),
179 esc_html(get_the_date('', $post_id)),
180 esc_attr($attributes['generalColor'])
181 );
182 }
183
184 /* Close the byline content */
185 $blockspare_content_markup .= sprintf(
186 '</div>'
187 );
188 }
189
190 /* Close the header content */
191 $blockspare_content_markup .= sprintf(
192 '</header>'
193 );
194
195
196 /* Get the excerpt */
197
198 global $post;
199
200
201 $excerpt = blockspare_post_excerpt($post_id, $post);
202
203 if (!$excerpt) {
204 $excerpt = null;
205 }
206
207 if (!empty($excerpt)) {
208 $excerpt = explode(' ', $excerpt);
209 $trim_to_length = (int)$attributes['excerptLength'] ? (int)$attributes['excerptLength'] : 55;
210 if (count($excerpt) > $trim_to_length) {
211 $excerpt = implode(' ', array_slice($excerpt, 0, $trim_to_length)) . '...';
212 } else {
213 $excerpt = implode(' ', $excerpt);
214 }
215
216 }
217
218
219 if ((isset($attributes['displayPostExcerpt']) && $excerpt != null) || isset($attributes['displayPostLink'])) {
220
221
222 /* Wrap the excerpt content */
223 $blockspare_content_markup .= sprintf(
224 '<div class="blockspare-block-post-grid-excerpt" style="color: %1$s">',
225 esc_attr($attributes['generalColor'])
226
227 );
228
229 if (isset($attributes['displayPostExcerpt']) && $attributes['displayPostExcerpt']) {
230 $blockspare_content_markup .= sprintf(
231 '<div class="blockspare-block-post-grid-excerpt-content">%s</div>',
232 wp_kses_post($excerpt)
233 );
234 }
235
236
237 /* Get the read more link */
238 if (isset($attributes['displayPostLink']) && $attributes['displayPostLink']) {
239 $blockspare_content_markup .= sprintf(
240 '<p><a style="color:%4$s;" 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>',
241 esc_url(get_permalink($post_id)),
242 esc_html($attributes['readMoreText']),
243 esc_html($title),
244 esc_attr($attributes['linkColor'])
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 .= "</article>\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 = '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" style="margin-top: %6$s;margin-bottom: %7$s;" ><div class="%4$s">%5$s</div></%1$s>',
305 $section_tag,
306 esc_attr($class),
307 $section_title,
308 esc_attr($grid_class),
309 $blockspare_content_markup,
310 esc_html($attributes['marginTop']) . 'px',
311 esc_html($attributes['marginBottom']) . 'px'
312
313
314 );
315 return $block_content;
316 }
317
318
319 }
320
321 /**
322 * Registers the post grid block on server
323 */
324 function blockspare_blocks_register_block_core_latest_posts_grid()
325 {
326
327 /* Check if the register function exists */
328 if (!function_exists('register_block_type')) {
329 return;
330 }
331 ob_start();
332 include BLOCKSPARE_PLUGIN_DIR . '/src/blocks/latest-posts-grid/block.json';
333
334 $metadata = json_decode(ob_get_clean(), true);
335
336 /* Block attributes */
337 register_block_type(
338 'blockspare/blockspare-latest-posts-grid',
339 array(
340
341 'attributes' => $metadata['attributes'],
342 'render_callback' => 'blockspare_blocks_render_block_core_latest_posts_grid',
343 )
344 );
345 }
346
347 add_action('init', 'blockspare_blocks_register_block_core_latest_posts_grid');
348
349
350 if (!function_exists('blockspare_post_excerpt')) {
351 /**
352 * Get the post excerpt.
353 *
354 * @since 1.7
355 */
356 function blockspare_post_excerpt($object)
357 {
358 return blockspare_get_excerpt($object['id']);
359 }
360 }
361
362 if (!function_exists('blockspare_get_excerpt')) {
363 /**
364 * Get the excerpt.
365 *
366 * @since 1.7
367 */
368 function blockspare_get_excerpt($post_id, $post = null)
369 {
370 $excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id, 'display'));
371 if (!empty($excerpt)) {
372 return $excerpt;
373 }
374
375 $max_excerpt = 100; // WP default is 55.
376
377 if (!empty($post['post_content'])) {
378 return apply_filters('the_excerpt', wp_trim_words($post['post_content'], $max_excerpt));
379 }
380 $post_content = apply_filters('the_content', get_post_field('post_content', $post_id));
381 return apply_filters('the_excerpt', wp_trim_words($post_content, $max_excerpt));
382 }
383 }
384
385
386
387
388
389