| 1 |
<?php |
| 2 |
if(!function_exists('blockspare_render_block_core_latest_posts_list')){ |
| 3 |
function blockspare_render_block_core_latest_posts_list($attributes) |
| 4 |
{ |
| 5 |
|
| 6 |
if($attributes['enablePagination']){ |
| 7 |
wp_enqueue_script('blockspare-pagination-js'); |
| 8 |
} |
| 9 |
ob_start(); |
| 10 |
$unq_class = mt_rand(100000,999999); |
| 11 |
$blockuniqueclass = ''; |
| 12 |
|
| 13 |
if($attributes['enableTwoColumn']){ |
| 14 |
$design = $attributes['design'].' list-col-2'; |
| 15 |
}else{ |
| 16 |
$design = $attributes['design'].' '.$attributes['columns']; |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
if(!empty($attributes['uniqueClass'])){ |
| 21 |
$blockuniqueclass = $attributes['uniqueClass']; |
| 22 |
}else{ |
| 23 |
$blockuniqueclass = 'blockspare-posts-block-list-'.$unq_class; |
| 24 |
} |
| 25 |
$hasimgclass = 'featured-img-not-enabaled'; |
| 26 |
if($attributes['enableFeatureImage'] == true){ |
| 27 |
$hasimgclass = $attributes['imageSize'].'-'.$attributes['ImageUnit']; |
| 28 |
|
| 29 |
} |
| 30 |
$blockName= 'post-list'; |
| 31 |
$layoutClass= false; |
| 32 |
if($attributes['design'] != 'blockspare-posts-block-list-layout-4' && $attributes['design'] != 'blockspare-posts-block-list-layout-5' && $attributes['design'] != 'blockspare-posts-block-list-layout-6') { |
| 33 |
$layoutClass= true; |
| 34 |
} |
| 35 |
|
| 36 |
$block_class = 'blockspare-posts-block-is-list '.$hasimgclass .' has-gutter-'.$attributes['gutterSpace']; |
| 37 |
if($attributes['displayPostExcerpt'] == true) { |
| 38 |
$block_class .= ' blockspare-post-expert-enabled'; |
| 39 |
} |
| 40 |
if($attributes['imageSize'] == 'thumbnail') { |
| 41 |
$block_class .= ' blockspare-has-thumbnail-image'; |
| 42 |
} |
| 43 |
blockspare_query_loop_and_wrapper($attributes,$block_class,$design,$blockName,$layoutClass); |
| 44 |
|
| 45 |
$data_content = blockspare_post_style_css($blockuniqueclass ,$attributes); |
| 46 |
$data_content .= ob_get_clean(); |
| 47 |
return $data_content; |
| 48 |
|
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Registers the post grid block on server |
| 54 |
*/ |
| 55 |
if(!function_exists('blockspare_register_block_core_latest_posts_list')){ |
| 56 |
function blockspare_register_block_core_latest_posts_list() |
| 57 |
{ |
| 58 |
|
| 59 |
if (!function_exists('register_block_type')) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
ob_start(); |
| 65 |
include BLOCKSPARE_PLUGIN_DIR . 'inc/latest-post-block/posts-list/block.json'; |
| 66 |
$metadata = json_decode(ob_get_clean(), true); |
| 67 |
|
| 68 |
|
| 69 |
/* Block attributes */ |
| 70 |
register_block_type( |
| 71 |
'blockspare/blockspare-latest-posts-list', |
| 72 |
array( |
| 73 |
'attributes' =>$metadata['attributes'], |
| 74 |
'render_callback' => 'blockspare_render_block_core_latest_posts_list', |
| 75 |
) |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
add_action('init', 'blockspare_register_block_core_latest_posts_list'); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Create API fields for additional info |
| 85 |
*/ |
| 86 |
if(!function_exists('blockspare_post_register_rest_fields')){ |
| 87 |
function blockspare_post_register_rest_fields() |
| 88 |
{ |
| 89 |
|
| 90 |
register_rest_field('post', 'featured_image_urls', |
| 91 |
array( |
| 92 |
'get_callback' => 'blockspare_featured_image_urls', |
| 93 |
'update_callback' => null, |
| 94 |
'schema' => array( |
| 95 |
'description' => __('Different sized featured images', 'blockspare'), |
| 96 |
'type' => 'array', |
| 97 |
), |
| 98 |
) |
| 99 |
); |
| 100 |
|
| 101 |
// Excer |
| 102 |
|
| 103 |
/* Add author info */ |
| 104 |
register_rest_field( |
| 105 |
'post', |
| 106 |
'author_info', |
| 107 |
array( |
| 108 |
'get_callback' => 'blockspare_get_author_infos', |
| 109 |
'update_callback' => null, |
| 110 |
'schema' => null, |
| 111 |
) |
| 112 |
); |
| 113 |
|
| 114 |
/* Add author info */ |
| 115 |
register_rest_field( |
| 116 |
'post', |
| 117 |
'category_info', |
| 118 |
array( |
| 119 |
'get_callback' => 'blockspare_get_category_infos', |
| 120 |
'update_callback' => null, |
| 121 |
'schema' => null, |
| 122 |
) |
| 123 |
); |
| 124 |
|
| 125 |
/* Add author info */ |
| 126 |
register_rest_field( |
| 127 |
'post', |
| 128 |
'tag_info', |
| 129 |
array( |
| 130 |
'get_callback' => 'blockspare_get_tag_infos', |
| 131 |
'update_callback' => null, |
| 132 |
'schema' => null, |
| 133 |
) |
| 134 |
); |
| 135 |
|
| 136 |
register_rest_field( |
| 137 |
'post', |
| 138 |
'comment_count', |
| 139 |
array( |
| 140 |
'get_callback' => 'blockspare_get_comment_count', |
| 141 |
'update_callback' => null, |
| 142 |
'schema' => null, |
| 143 |
) |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
add_action('rest_api_init', 'blockspare_post_register_rest_fields'); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
/** |
| 152 |
* Get author info for the rest field |
| 153 |
* |
| 154 |
* @param String $object The object type. |
| 155 |
* @param String $field_name Name of the field to retrieve. |
| 156 |
* @param String $request The current request object. |
| 157 |
*/ |
| 158 |
if(!function_exists('blockspare_get_author_infos')){ |
| 159 |
function blockspare_get_author_infos($object, $field_name, $request){ |
| 160 |
if(!isset($object['author'])) |
| 161 |
return; |
| 162 |
global $post; |
| 163 |
$author_id = get_post_field( 'post_author', $object['author'] ); |
| 164 |
$blocspare = new BlocksapreMultiAuthorForBackend(); |
| 165 |
$author_data= $blocspare->blockspare_by_author($post); |
| 166 |
return $author_data; |
| 167 |
} |
| 168 |
} |
| 169 |
if(!function_exists('blockspare_get_category_infos')){ |
| 170 |
function blockspare_get_category_infos($object, $field_name, $request){ |
| 171 |
return get_the_category_list(' ', '', $object['id']); |
| 172 |
|
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if(!function_exists('blockspare_get_tag_infos')){ |
| 177 |
function blockspare_get_tag_infos($object, $field_name, $request) { |
| 178 |
|
| 179 |
ob_start(); |
| 180 |
$cate_name = ''; |
| 181 |
if (!empty($object)) { |
| 182 |
foreach ($object['categories'] as $cat_id) { |
| 183 |
$cate_name = get_cat_name($cat_id); |
| 184 |
} |
| 185 |
} |
| 186 |
ob_clean(); |
| 187 |
return $cate_name; |
| 188 |
|
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
if(!function_exists('blockspare_get_comment_count')){ |
| 193 |
function blockspare_get_comment_count($object, $field_name, $reques){ |
| 194 |
|
| 195 |
return get_comments_number( $object['id'] ); |
| 196 |
|
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if (!function_exists('blockspare_featured_image_urls')) { |
| 201 |
/** |
| 202 |
* Get the different featured image sizes that the blog will use. |
| 203 |
* Used in the custom REST API endpoint. |
| 204 |
* |
| 205 |
* @since 1.7 |
| 206 |
*/ |
| 207 |
function blockspare_featured_image_urls($object, $field_name, $request) |
| 208 |
{ |
| 209 |
return blockspare_featured_image_urls_from_url(!empty($object['featured_media']) ? $object['featured_media'] : ''); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
if (!function_exists('blockspare_featured_image_urls_from_url')) { |
| 215 |
/** |
| 216 |
* Get the different featured image sizes that the blog will use. |
| 217 |
* |
| 218 |
* @since 2.0 |
| 219 |
*/ |
| 220 |
function blockspare_featured_image_urls_from_url($attachment_id) |
| 221 |
{ |
| 222 |
|
| 223 |
$image = wp_get_attachment_image_src($attachment_id, 'full', false); |
| 224 |
$sizes = get_intermediate_image_sizes(); |
| 225 |
|
| 226 |
$imageSizes = array( |
| 227 |
'full' => is_array($image) ? $image : '', |
| 228 |
); |
| 229 |
|
| 230 |
foreach ($sizes as $size) { |
| 231 |
$imageSizes[$size] = is_array($image) ? wp_get_attachment_image_src($attachment_id, $size, false) : ''; |
| 232 |
} |
| 233 |
|
| 234 |
return $imageSizes; |
| 235 |
} |
| 236 |
} |
| 237 |
if(!function_exists('blockspare_post_style_css')){ |
| 238 |
function blockspare_post_style_css($blockuniqueclass ,$attributes){ |
| 239 |
|
| 240 |
|
| 241 |
$block_content =''; |
| 242 |
$block_content .= '<style type="text/css">'; |
| 243 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-wrap{ |
| 244 |
margin-top:' . $attributes['marginTop'] . 'px; |
| 245 |
margin-bottom:' . $attributes['marginBottom'] . 'px; |
| 246 |
margin-left:' . $attributes['marginLeft'] . 'px; |
| 247 |
margin-right:' . $attributes['marginRight'] . 'px; |
| 248 |
}'; |
| 249 |
|
| 250 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-content{ |
| 251 |
padding-top:' . $attributes['contentPaddingTop'] . 'px; |
| 252 |
padding-right:' . $attributes['contentPaddingRight'] . 'px; |
| 253 |
padding-bottom:' . $attributes['contentPaddingBottom'] . 'px; |
| 254 |
padding-left:' . $attributes['contentPaddingLeft'] . 'px; |
| 255 |
}'; |
| 256 |
|
| 257 |
|
| 258 |
if ($attributes['categoryLayoutOption'] == 'solid') { |
| 259 |
|
| 260 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-category a{ |
| 261 |
color:' . $attributes['categoryTextColor'] . "!important" . '; |
| 262 |
background-color:' . $attributes['categoryBackgroundColor'] . "!important" . '; |
| 263 |
border-radius:' . $attributes['categoryBorderRadius'] . "px" . '; |
| 264 |
}'; |
| 265 |
|
| 266 |
|
| 267 |
} else if ($attributes['categoryLayoutOption'] == 'border') { |
| 268 |
|
| 269 |
|
| 270 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-category a{ |
| 271 |
color:' . $attributes['categoryTextColor'] . "!important" . '; |
| 272 |
background-color:' . "transparent" . '; |
| 273 |
border:' . "1px solid" . $attributes['categoryBorderColor'] . '; |
| 274 |
border-radius:' . $attributes['categoryBorderRadius'] . "px" . '; |
| 275 |
border-width:' . $attributes['categoryBorderWidth'] . "px" . '; |
| 276 |
}'; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
} else { |
| 281 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-category a{ |
| 282 |
color:' . $attributes['categoryTextColor'] . "!important" . '; |
| 283 |
}'; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-category{ |
| 288 |
margin-top:' . $attributes['categoryMarginTop'] . 'px' . '; |
| 289 |
margin-bottom:' . $attributes['categoryMarginBottom'] . 'px' . '; |
| 290 |
margin-left:' . $attributes['categoryMarginLeft'] . 'px' . '; |
| 291 |
margin-right:' . $attributes['categoryMarginRight'] . 'px' . '; |
| 292 |
}'; |
| 293 |
|
| 294 |
|
| 295 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-byline{ |
| 296 |
|
| 297 |
margin-top:' . $attributes['metaMarginTop'] . 'px' . '; |
| 298 |
margin-bottom:' . $attributes['metaMarginBottom'] . 'px' . '; |
| 299 |
margin-left:' . $attributes['metaMarginLeft'] . 'px' . '; |
| 300 |
margin-right:' . $attributes['metaMarginRight'] . 'px' . '; |
| 301 |
}'; |
| 302 |
|
| 303 |
|
| 304 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-more-link{ |
| 305 |
margin-top:' . $attributes['moreLinkMarginTop'] . 'px' . '; |
| 306 |
margin-bottom:' . $attributes['moreLinkMarginBottom'] . 'px' . '; |
| 307 |
margin-left:' . $attributes['moreLinkMarginLeft'] . 'px' . '; |
| 308 |
margin-right:' . $attributes['moreLinkMarginRight'] . 'px' . '; |
| 309 |
}'; |
| 310 |
|
| 311 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-title a span{ |
| 312 |
color: ' . $attributes['postTitleColor'] . '; |
| 313 |
|
| 314 |
}'; |
| 315 |
|
| 316 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-author a span{ |
| 317 |
color:' . $attributes['linkColor'] . '; |
| 318 |
}'; |
| 319 |
|
| 320 |
|
| 321 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-more-link span{ |
| 322 |
color:' . $attributes['linkColor'] . '; |
| 323 |
}'; |
| 324 |
|
| 325 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-date{ |
| 326 |
color:' . $attributes['generalColor'] . '; |
| 327 |
}'; |
| 328 |
|
| 329 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-excerpt-content,'.' .' . $blockuniqueclass . ' .comment_count{ |
| 330 |
color:' . $attributes['generalColor'] . '; |
| 331 |
|
| 332 |
}'; |
| 333 |
|
| 334 |
|
| 335 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-title{ |
| 336 |
margin-top:' . $attributes['titleMarginTop'] . 'px' . '; |
| 337 |
margin-bottom:' . $attributes['titleMarginBottom'] . 'px' . '; |
| 338 |
margin-left:' . $attributes['titleMarginLeft'] . 'px' . '; |
| 339 |
margin-right:' . $attributes['titleMarginRight'] . 'px' . '; |
| 340 |
}'; |
| 341 |
|
| 342 |
|
| 343 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-excerpt-content{ |
| 344 |
margin-top:' . $attributes['exceprtMarginTop'] . 'px' . '; |
| 345 |
margin-bottom:' . $attributes['exceprtMarginBottom'] . 'px' . '; |
| 346 |
margin-left:' . $attributes['exceprtMarginLeft'] . 'px' . '; |
| 347 |
margin-right:' . $attributes['exceprtMarginRight'] . 'px' . '; |
| 348 |
}'; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
$layoutstyle = explode('-',$attributes['design']); |
| 353 |
if($layoutstyle[5] >3){ |
| 354 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-single .blockspare-posts-block-post-img img{ |
| 355 |
border-radius:' . $attributes['borderRadius'] . 'px'.'; |
| 356 |
|
| 357 |
}'; |
| 358 |
if($attributes['enableBoxShadow']){ |
| 359 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-single .blockspare-posts-block-post-img img{ |
| 360 |
box-shadow: ' . $attributes['xOffset'] . 'px ' . $attributes['yOffset'] . 'px ' . $attributes['blur'] . 'px ' . $attributes['spread'] . 'px ' . $attributes['shadowColor'] . '; |
| 361 |
}'; |
| 362 |
} |
| 363 |
}else{ |
| 364 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-single{ |
| 365 |
border-radius:' . $attributes['borderRadius'] . 'px'.'; |
| 366 |
background-color:'.$attributes['backGroundColor'].' |
| 367 |
}'; |
| 368 |
|
| 369 |
if($attributes['enableBoxShadow']){ |
| 370 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-single{ |
| 371 |
box-shadow: ' . $attributes['xOffset'] . 'px ' . $attributes['yOffset'] . 'px ' . $attributes['blur'] . 'px ' . $attributes['spread'] . 'px ' . $attributes['shadowColor'] . '; |
| 372 |
}'; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
//Title Hover |
| 378 |
|
| 379 |
if($attributes['titleOnHover']=='lpc-title-hover') { |
| 380 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-content .blockspare-posts-block-title-link:hover span{ |
| 381 |
color: ' . $attributes['titleOnHoverColor'] . '; |
| 382 |
|
| 383 |
}'; |
| 384 |
} |
| 385 |
|
| 386 |
if($attributes['titleOnHover']=='lpc-title-border') { |
| 387 |
$block_content .= ' .' . $blockuniqueclass . ' .lpc-title-border .blockspare-posts-block-post-grid-title .blockspare-posts-block-title-link span:hover{ |
| 388 |
box-shadow: inset 0 -2px 0 0 ' . $attributes['titleOnHoverColor'] . '; |
| 389 |
|
| 390 |
}'; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
//Font Settings |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-title a span{ |
| 400 |
|
| 401 |
font-size: ' . $attributes['postTitleFontSize'] . $attributes['titleFontSizeType'] . '; |
| 402 |
|
| 403 |
'.bscheckFontfamily($attributes['titleFontFamily']).'; |
| 404 |
'.bscheckFontfamilyWeight($attributes['titleFontWeight']).'; |
| 405 |
}'; |
| 406 |
|
| 407 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-content .blockspare-posts-block-post-grid-excerpt .blockspare-posts-block-post-grid-excerpt-content { |
| 408 |
font-size:' . $attributes['descriptionFontSize'] . $attributes['descriptionFontSizeType'] . '; |
| 409 |
|
| 410 |
'.bscheckFontfamily($attributes['descriptionFontFamily']).'; |
| 411 |
'.bscheckFontfamilyWeight($attributes['descriptionFontWeight']).'; |
| 412 |
|
| 413 |
}'; |
| 414 |
|
| 415 |
$block_content .= '@media (max-width: 1025px) { '; |
| 416 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-title a span{ |
| 417 |
font-size: ' . $attributes['titleFontSizeTablet'] . $attributes['titleFontSizeType'] . '; |
| 418 |
}'; |
| 419 |
$block_content .= ' .' . $blockuniqueclass .' .blockspare-posts-block-post-content .blockspare-posts-block-post-grid-excerpt .blockspare-posts-block-post-grid-excerpt-content { |
| 420 |
font-size:' . $attributes['descriptionFontSizeTablet'].$attributes['descriptionFontSizeType'].' |
| 421 |
}'; |
| 422 |
$block_content .= '}'; |
| 423 |
|
| 424 |
|
| 425 |
$block_content .= '@media (max-width: 767px) { '; |
| 426 |
$block_content .= ' .' . $blockuniqueclass . ' .blockspare-posts-block-post-grid-title a span{ |
| 427 |
font-size: ' . $attributes['titleFontSizeMobile'] . $attributes['titleFontSizeType'] . '; |
| 428 |
}'; |
| 429 |
$block_content .= ' .' . $blockuniqueclass .' .blockspare-posts-block-post-content .blockspare-posts-block-post-grid-excerpt .blockspare-posts-block-post-grid-excerpt-content { |
| 430 |
font-size:' . $attributes['descriptionFontSizeMobile'].$attributes['descriptionFontSizeType'].' |
| 431 |
}'; |
| 432 |
$block_content .= '}'; |
| 433 |
|
| 434 |
//Pagination |
| 435 |
if($attributes['enablePagination']){ |
| 436 |
if($attributes['loadMoreStyle'] == 'bs-loadmore-solid') { |
| 437 |
$block_content .=' .' . $blockuniqueclass . ' .bs_blockspare_loadmore a.blockspare-readmore .load-btn{ |
| 438 |
color:'.$attributes['loadMoreSolidTextColor'].'; |
| 439 |
background-color:'.$attributes['loadMoreTextBgColor'].'; |
| 440 |
}'; |
| 441 |
$block_content .=' .' . $blockuniqueclass . ' .bs_blockspare_loadmore a.blockspare-readmore .load-btn:hover{ |
| 442 |
color:'.$attributes['loadMoreSolidTextHoverColor'].'; |
| 443 |
background-color:'.$attributes['loadMoreTextBgHoverColor'].'; |
| 444 |
}'; |
| 445 |
} else { |
| 446 |
$block_content .=' .' . $blockuniqueclass . ' .bs_blockspare_loadmore a.blockspare-readmore .load-btn{ |
| 447 |
color:'.$attributes['loadMoreTextColor'].'; |
| 448 |
}'; |
| 449 |
$block_content .=' .' . $blockuniqueclass . ' .bs_blockspare_loadmore a.blockspare-readmore .load-btn:hover{ |
| 450 |
color:'.$attributes['loadMoreTextHoverColor'].'; |
| 451 |
}'; |
| 452 |
} |
| 453 |
|
| 454 |
$block_content .=' .' . $blockuniqueclass . ' .bs_blockspare_loadmore{ |
| 455 |
text-align:'.$attributes['loadMoreAlignment'].'; |
| 456 |
}'; |
| 457 |
$block_content .=' .' . $blockuniqueclass . ' .bs_blockspare_loadmore a.blockspare-readmore .ajax-loader-enabled{ |
| 458 |
border-top-color:'.$attributes['loadMoreColor'].'; |
| 459 |
}'; |
| 460 |
$block_content .= '}'; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
$block_content .= '</style>'; |
| 465 |
return $block_content; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
|