| 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 |
if(!$excerpt) { |
| 227 |
$excerpt = null; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
if ((isset($attributes['displayPostExcerpt']) && $excerpt != null) || isset($attributes['displayPostLink'])) { |
| 232 |
|
| 233 |
|
| 234 |
/* Wrap the excerpt content */ |
| 235 |
$blockspare_content_markup .= sprintf( |
| 236 |
'<div class="blockspare-block-post-grid-excerpt">' |
| 237 |
|
| 238 |
|
| 239 |
); |
| 240 |
|
| 241 |
if (isset($attributes['displayPostExcerpt']) && $attributes['displayPostExcerpt']) { |
| 242 |
$blockspare_content_markup .= sprintf( |
| 243 |
'<div class="blockspare-block-post-grid-excerpt-content">%1$s</div>', |
| 244 |
wp_kses_post($excerpt) |
| 245 |
); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
/* Get the read more link */ |
| 250 |
if (isset($attributes['displayPostLink']) && $attributes['displayPostLink']) { |
| 251 |
$blockspare_content_markup .= sprintf( |
| 252 |
'<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>', |
| 253 |
esc_url(get_permalink($post_id)), |
| 254 |
esc_html($attributes['readMoreText']), |
| 255 |
esc_html($title) |
| 256 |
); |
| 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 |
/* Close the post */ |
| 273 |
$blockspare_content_markup .= "</div>\n"; |
| 274 |
} |
| 275 |
|
| 276 |
/* Restore original post data */ |
| 277 |
wp_reset_postdata(); |
| 278 |
|
| 279 |
/* Build the block classes */ |
| 280 |
$class = "wp-block-blockspare-block-blockspare-latest-posts blockspare-post-wrap align{$attributes['align']}"; |
| 281 |
|
| 282 |
if (isset($attributes['className'])) { |
| 283 |
$class .= ' ' . $attributes['className']; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
$list_layout_class = $attributes['design']; |
| 288 |
|
| 289 |
/* Layout orientation class */ |
| 290 |
$grid_class = $blockuniqueclass . ' blockspare-latest-post-wrap blockspare-is-grid ' . $list_layout_class . ' ' . 'column-' . $attributes['columns']; |
| 291 |
|
| 292 |
|
| 293 |
/* Post grid section title */ |
| 294 |
if (isset($attributes['displaySectionTitle']) && $attributes['displaySectionTitle'] && !empty($attributes['sectionTitle'])) { |
| 295 |
if (isset($attributes['sectionTitleTag'])) { |
| 296 |
$section_title_tag = $attributes['sectionTitleTag']; |
| 297 |
} else { |
| 298 |
$section_title_tag = 'h4'; |
| 299 |
} |
| 300 |
|
| 301 |
$section_title = '<' . esc_attr($section_title_tag) . '>' . esc_html($attributes['sectionTitle']) . '</' . esc_attr($section_title_tag) . '>'; |
| 302 |
} else { |
| 303 |
$section_title = null; |
| 304 |
} |
| 305 |
|
| 306 |
/* Post grid section tag */ |
| 307 |
if (isset($attributes['sectionTag'])) { |
| 308 |
$section_tag = $attributes['sectionTag']; |
| 309 |
} else { |
| 310 |
$section_tag = 'section'; |
| 311 |
} |
| 312 |
|
| 313 |
/* Output the post markup */ |
| 314 |
$block_content = sprintf( |
| 315 |
'<%1$s class="%2$s" ><div class="%3$s">%4$s</div></%1$s>', |
| 316 |
$section_tag, |
| 317 |
esc_attr($class), |
| 318 |
esc_attr($grid_class), |
| 319 |
$blockspare_content_markup |
| 320 |
|
| 321 |
|
| 322 |
); |
| 323 |
|
| 324 |
|
| 325 |
$block_content .= '<style type="text/css">'; |
| 326 |
$block_content .= ' .' . $blockuniqueclass . '.blockspare-latest-post-wrap{ |
| 327 |
margin-top:' . $attributes['marginTop'] . 'px; |
| 328 |
margin-bottom:' . $attributes['marginBottom'] . 'px; |
| 329 |
}'; |
| 330 |
|
| 331 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-category{ |
| 332 |
color:' . $attributes['linkColor'] . '; |
| 333 |
}'; |
| 334 |
|
| 335 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-author a span{ |
| 336 |
color:' . $attributes['linkColor'] . '; |
| 337 |
}'; |
| 338 |
|
| 339 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-text-link a span{ |
| 340 |
color:' . $attributes['linkColor'] . '; |
| 341 |
}'; |
| 342 |
|
| 343 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a span{ |
| 344 |
color: ' . $attributes['postTitleColor'] . '; |
| 345 |
font-size: ' . $attributes['postTitleFontSize'] . $attributes['titleFontSizeType'] . '; |
| 346 |
font-family: ' . $attributes['titleFontFamily'] . '; |
| 347 |
font-weight: ' . $attributes['titleFontWeight'] . '; |
| 348 |
}'; |
| 349 |
|
| 350 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-date{ |
| 351 |
color:' . $attributes['generalColor'] . '; |
| 352 |
}'; |
| 353 |
|
| 354 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-excerpt{ |
| 355 |
color:' . $attributes['generalColor'] . '; |
| 356 |
}'; |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
if ($attributes['design'] != 'blockspare-list-layout-4' && $attributes['design'] != 'blockspare-list-layout-5' && $attributes['design'] != 'blockspare-list-layout-6') { |
| 362 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-single{ |
| 363 |
border: 1px solid ' . $attributes['borderColor'] . '; |
| 364 |
background-color:' . $attributes['backGroundColor'] . '; |
| 365 |
border-radius:' . $attributes['borderRadius'] . '; |
| 366 |
}'; |
| 367 |
|
| 368 |
} |
| 369 |
|
| 370 |
if ($attributes['enableBoxShadow']) { |
| 371 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-post-single{ |
| 372 |
box-shadow = ' . $attributes['xOffset'] . 'px ' . $attributes['yOffset'] . 'px ' . $attributes['blur'] . 'px ' . $attributes['spread'] . 'px ' . $attributes['shadowColor'] . '; |
| 373 |
}'; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
$block_content .= '@media (max-width: 1025px) { '; |
| 378 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a{ |
| 379 |
font-size: ' . $attributes['titleFontSizeTablet'] . $attributes['titleFontSizeType'] . '; |
| 380 |
}'; |
| 381 |
$block_content .= '}'; |
| 382 |
|
| 383 |
|
| 384 |
$block_content .= '@media (max-width: 768px) { '; |
| 385 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-block-post-grid-title a{ |
| 386 |
font-size: ' . $attributes['titleFontSizeMobile'] . $attributes['titleFontSizeType'] . '; |
| 387 |
}'; |
| 388 |
$block_content .= '}'; |
| 389 |
|
| 390 |
|
| 391 |
$block_content .= '</style>'; |
| 392 |
|
| 393 |
|
| 394 |
return $block_content; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
/** |
| 402 |
* Registers the post grid block on server |
| 403 |
*/ |
| 404 |
function blockspare_blocks_register_block_core_latest_posts_grid() |
| 405 |
{ |
| 406 |
|
| 407 |
/* Check if the register function exists */ |
| 408 |
if (!function_exists('register_block_type')) { |
| 409 |
return; |
| 410 |
} |
| 411 |
ob_start(); |
| 412 |
include BLOCKSPARE_PLUGIN_DIR . '/src/blocks/latest-posts-grid/block.json'; |
| 413 |
|
| 414 |
$metadata = json_decode(ob_get_clean(), true); |
| 415 |
|
| 416 |
/* Block attributes */ |
| 417 |
register_block_type( |
| 418 |
'blockspare/blockspare-latest-posts-grid', |
| 419 |
array( |
| 420 |
|
| 421 |
'attributes' => $metadata['attributes'], |
| 422 |
'render_callback' => 'blockspare_blocks_render_block_core_latest_posts_grid', |
| 423 |
) |
| 424 |
); |
| 425 |
} |
| 426 |
|
| 427 |
add_action('init', 'blockspare_blocks_register_block_core_latest_posts_grid'); |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|