| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `posts` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the block on server. |
| 10 |
* |
| 11 |
* @param array $attributes The block attributes. |
| 12 |
* |
| 13 |
* @return string Returns the block content. |
| 14 |
*/ |
| 15 |
function gutenify_render_post_carousel_block( $attributes, $content, $props ) { |
| 16 |
|
| 17 |
global $post; |
| 18 |
$args = array( |
| 19 |
'posts_per_page' => ! empty( $attributes['query']['numberOfItems'] ) ? $attributes['query']['numberOfItems'] : 10, |
| 20 |
'post_status' => 'publish', |
| 21 |
'order' => ! empty( $attributes['query']['order'] ) ? $attributes['query']['order'] : 'desc', |
| 22 |
'orderby' => ! empty( $attributes['query']['orderBy'] ) ? $attributes['query']['orderBy'] : 'date', |
| 23 |
'suppress_filters' => false, |
| 24 |
'post__not_in' => array( $post->ID ), |
| 25 |
'category__in' => array(), |
| 26 |
); |
| 27 |
|
| 28 |
if ( isset( $attributes['query']['selectedCategories'] ) ) { |
| 29 |
|
| 30 |
$args['category__in'] = array_column( $attributes['query']['selectedCategories'], 'id' ); |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
if ( ! empty( $attributes['query']['tax']['category'] ) ) { |
| 35 |
|
| 36 |
$args['category__in'] = array_merge( $attributes['query']['tax']['category'], $args['category__in'] ); |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
if ( ! empty( $attributes['query']['tax']['tag'] ) ) { |
| 41 |
$args['tag__in'] = $attributes['query']['tax']['tag']; |
| 42 |
} |
| 43 |
|
| 44 |
if ( ! empty( $attributes['query']['tax']['post_tag'] ) ) { |
| 45 |
$args['tag__in'] = $attributes['query']['tax']['post_tag']; |
| 46 |
} |
| 47 |
|
| 48 |
if ( ! empty( $attributes['query']['authorIds'] ) ) { |
| 49 |
$args['author__in'] = $attributes['query']['authorIds']; |
| 50 |
} |
| 51 |
|
| 52 |
if ( ! empty( $attributes['query']['postTypes'] ) && in_array( 'attachment', $attributes['query']['postTypes'] ) ) { |
| 53 |
$args['post_status'] = 'inherit'; |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
$recent_posts = get_posts( $args ); |
| 58 |
$formatted_posts = gutenify_get_post_info( $recent_posts ); |
| 59 |
|
| 60 |
$block_style = null; |
| 61 |
|
| 62 |
if ( isset( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-horizontal' ) !== false ) { |
| 63 |
|
| 64 |
$block_style = 'horizontal'; |
| 65 |
|
| 66 |
} elseif ( isset( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-stacked' ) !== false ) { |
| 67 |
|
| 68 |
$block_style = 'stacked'; |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
return gutenify_posts( $formatted_posts, $attributes, $props ); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* Renders the list and grid styles. |
| 78 |
* |
| 79 |
* @param array $posts Current posts. |
| 80 |
* @param array $attributes The block attributes. |
| 81 |
* |
| 82 |
* @return string Returns the block content for the list and grid styles. |
| 83 |
*/ |
| 84 |
function gutenify_posts( $posts, $attributes, $props ) { |
| 85 |
$client_id = ! empty( $attributes['blockClientId'] ) ? $attributes['blockClientId'] : ''; |
| 86 |
$class_name = array( 'wp-block-gutenify-post-carousel', 'gutenify-section-' . $attributes['blockClientId'], 'gutenify-post-carousel-' . $attributes['layout'] ); |
| 87 |
$class = array( 'swiper-container', 'gutenify-post-carousel' ); |
| 88 |
$styles = array(); |
| 89 |
$block_style = ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-stacked' ) !== false ? 'stacked' : 'horizontal'; |
| 90 |
|
| 91 |
if ( isset( $attributes['className'] ) ) { |
| 92 |
array_push( $class_name, $attributes['className'] ); |
| 93 |
} |
| 94 |
|
| 95 |
if ( isset( $attributes['align'] ) ) { |
| 96 |
array_push( $class_name, 'align' . $attributes['align'] ); |
| 97 |
} |
| 98 |
|
| 99 |
if ( isset( $attributes['columns'] ) ) { |
| 100 |
array_push( $class, 'has-columns has-' . $attributes['columns'] . '-columns has-responsive-columns' ); |
| 101 |
} |
| 102 |
|
| 103 |
if ( isset( $attributes['listPosition'] ) && 'right' === $attributes['listPosition'] && 'horizontal' === $block_style ) { |
| 104 |
array_push( $class, 'has-image-right' ); |
| 105 |
} |
| 106 |
|
| 107 |
if ( isset( $attributes['imageSize'] ) && 'horizontal' === $block_style ) { |
| 108 |
array_push( $class, 'has-' . $attributes['imageSize'] . '-image' ); |
| 109 |
} |
| 110 |
|
| 111 |
if ( isset( $attributes['imageStyle'] ) ) { |
| 112 |
array_push( $class, 'has-' . $attributes['imageStyle'] . '-image' ); |
| 113 |
} |
| 114 |
|
| 115 |
$class_name = apply_filters( 'gutenify--post-carousel--wrapper-class', $class_name, $props ); |
| 116 |
$class_name = apply_filters( 'gutenify_block_wrapper_class', esc_attr( implode( ' ', $class_name ) ), $attributes ); |
| 117 |
|
| 118 |
$block_content = sprintf( |
| 119 |
'<div class="%1$s"><div class="gutenify-post-carousel-section" id="gutenify-section-' . $client_id . '"><div class="%2$s" style="%3$s"><div class="swiper-wrapper">', |
| 120 |
$class_name, |
| 121 |
esc_attr( implode( ' ', apply_filters( 'gutenify_render_wrapper_class', $class, $attributes ) ) ), |
| 122 |
esc_attr( implode( ' ', apply_filters( 'gutenify_render_wrapper_styles', $styles, $attributes ) ) ) |
| 123 |
); |
| 124 |
|
| 125 |
$list_items_markup = ''; |
| 126 |
|
| 127 |
foreach ( $posts as $post ) { |
| 128 |
|
| 129 |
$list_class = ''; |
| 130 |
$align_self_class = ''; |
| 131 |
|
| 132 |
$list_items_markup .= '<div class="gutenify-post-carousel-item gutenify-post-carousel-item-wrapper swiper-slide">'; |
| 133 |
$list_items_markup .= '<div class="gutenify-post-carousel-item-inner-wrapper">'; |
| 134 |
|
| 135 |
if ( ! empty( $attributes['displayFeaturedImage'] ) && null !== $post['thumbnailURL'] && $post['thumbnailURL'] ) { |
| 136 |
|
| 137 |
$list_items_markup .= sprintf( |
| 138 |
'<div class="gutenify-post-carousel-thumb"> |
| 139 |
<a class="image-hover-zoom" href="%1$s"> |
| 140 |
<img src="%2$s" /> |
| 141 |
</a> |
| 142 |
</div>', |
| 143 |
esc_url( $post['postLink'] ), |
| 144 |
esc_url( $post['thumbnailURL'] ) |
| 145 |
); |
| 146 |
|
| 147 |
if ( 'horizontal' === $block_style && ( isset( $attributes['displayPostContent'] ) && ! $attributes['displayPostContent'] ) && ( isset( $attributes['columns'] ) && 2 >= $attributes['columns'] ) ) { |
| 148 |
|
| 149 |
$align_self_class = 'self-center'; |
| 150 |
} |
| 151 |
} else { |
| 152 |
$align_self_class = ' flex-start'; |
| 153 |
} |
| 154 |
|
| 155 |
$list_items_markup .= sprintf( |
| 156 |
'<div class="gutenify-post-carousel-text-content %s">', |
| 157 |
esc_attr( $align_self_class ) |
| 158 |
); |
| 159 |
|
| 160 |
if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] && 'stacked' === $block_style ) { |
| 161 |
|
| 162 |
$list_items_markup .= sprintf( |
| 163 |
'<time datetime="%1$s" class="wp-block-gutenify-posts__date">%2$s</time>', |
| 164 |
esc_url( $post['date'] ), |
| 165 |
esc_html( $post['dateReadable'] ) |
| 166 |
); |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
$title = $post['title']; |
| 171 |
|
| 172 |
if ( ! $title ) { |
| 173 |
|
| 174 |
$title = _x( '(no title)', 'placeholder when a post has no title', 'gutenify' ); |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
$list_items_markup .= sprintf( |
| 179 |
'<h3 class="gutenify-post-carousel-title"><a href="%1$s" rel="bookmark">%2$s</a></h3> ', |
| 180 |
$post['postLink'], |
| 181 |
$title |
| 182 |
); |
| 183 |
$meta_data = ''; |
| 184 |
if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] && 'horizontal' === $block_style ) { |
| 185 |
$meta_data .= sprintf( |
| 186 |
'<span class="posted-on"><time datetime="%1$s" class="gutenify-posts-date">%2$s</time></span>', |
| 187 |
esc_html( $post['date'] ), |
| 188 |
esc_html( $post['dateReadable'] ) |
| 189 |
); |
| 190 |
|
| 191 |
} |
| 192 |
if ( ! empty( $attributes['displayPostAuthor'] ) ) { |
| 193 |
if ( ! get_the_author_meta( 'description' ) && post_type_supports( get_post_type(), 'author' ) ) { |
| 194 |
$meta_data .= '<span class="byline">'; |
| 195 |
$meta_data .= sprintf( |
| 196 |
'%s', |
| 197 |
'<a href="' . esc_url( get_author_posts_url( $post['author_id'] ) ) . '" rel="author">' . esc_html( get_the_author_meta( 'display_name', $post['author_id'] ) ) . '</a>' |
| 198 |
); |
| 199 |
$meta_data .= '</span>'; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
if ( ! empty( $attributes['displayPostCategories'] ) ) { |
| 204 |
/* translators: used between list items, there is a space after the comma. */ |
| 205 |
$categories_list = get_the_category_list( __( ', ' ), '', $post['ID'] ); |
| 206 |
if ( $categories_list ) { |
| 207 |
$meta_data .= sprintf( |
| 208 |
'<span class="cat-links">%s </span>', |
| 209 |
$categories_list // phpcs:ignore WordPress.Security.EscapeOutput |
| 210 |
); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if ( ! empty( $meta_data ) ) { |
| 215 |
$list_items_markup .= sprintf( '<div class="gutenify-post-carousel-meta">%1$s</div>', $meta_data ); |
| 216 |
} |
| 217 |
|
| 218 |
if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent'] ) { |
| 219 |
|
| 220 |
$post_excerpt = $post['postExcerpt']; |
| 221 |
$trimmed_excerpt = esc_html( wp_trim_words( $post_excerpt, $attributes['excerptLength'], ' … ' ) ); |
| 222 |
$list_items_markup .= '<div class="entry-summary">'; |
| 223 |
$list_items_markup .= sprintf( |
| 224 |
'<p>%1$s</p>', |
| 225 |
esc_html( $trimmed_excerpt ) |
| 226 |
); |
| 227 |
|
| 228 |
if ( isset( $attributes['displayPostLink'] ) && $attributes['displayPostLink'] ) { |
| 229 |
|
| 230 |
$list_items_markup .= sprintf( |
| 231 |
'<a href="%1$s" class="more-link"><span class="more-button">%2$s<span class="screen-reader-text">%3$s</span></span></a>', |
| 232 |
esc_url( $post['postLink'] ), |
| 233 |
esc_html( $attributes['postLink'] ), |
| 234 |
esc_html( $post['title'] ) |
| 235 |
); |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
$list_items_markup .= '</div>'; |
| 240 |
} |
| 241 |
|
| 242 |
$list_items_markup .= '</div></div></div>'; |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
$block_content .= $list_items_markup; |
| 247 |
$block_content .= '</div>'; |
| 248 |
$has_navigation = ! empty( $attributes['hasNavigation'] ) && true === $attributes['hasNavigation']; |
| 249 |
$has_pagination = ! empty( $attributes['hasPagination'] ) && true === $attributes['hasPagination']; |
| 250 |
if ( $has_navigation ) { |
| 251 |
$block_content .= '<div class="swiper-button-next"></div> |
| 252 |
<div class="swiper-button-prev"></div>'; |
| 253 |
} |
| 254 |
if ( $has_pagination ) { |
| 255 |
$block_content .= '<div class="swiper-pagination"></div>'; |
| 256 |
} |
| 257 |
$block_content .= '</div>'; |
| 258 |
$block_content .= '</div>'; |
| 259 |
$block_content .= '</div>'; |
| 260 |
|
| 261 |
$columns = ! empty( $attributes['columns'] ) ? $attributes['columns'] : 1; |
| 262 |
$space_between = ! empty( $attributes['spaceBetween'] ) ? $attributes['spaceBetween'] : 10; |
| 263 |
$block_client_id = ! empty( $attributes['blockClientId'] ) ? $attributes['blockClientId'] : 1; |
| 264 |
ob_start(); |
| 265 |
wp_enqueue_script( 'gutenify-swiper' ); |
| 266 |
$config = '{ |
| 267 |
loop: true, |
| 268 |
slidesPerView: ' . $columns . ', |
| 269 |
breakpoints: { |
| 270 |
// when window width is >= 320px |
| 271 |
320: { |
| 272 |
slidesPerView: 1, |
| 273 |
spaceBetween: 20 |
| 274 |
}, |
| 275 |
// when window width is >= 640px |
| 276 |
640: { |
| 277 |
slidesPerView: 2, |
| 278 |
spaceBetween: 15 |
| 279 |
}, |
| 280 |
1024: { |
| 281 |
slidesPerView: ' . $columns . ', |
| 282 |
spaceBetween: ' . $space_between . ' |
| 283 |
} |
| 284 |
},'; |
| 285 |
|
| 286 |
$config .= ' spaceBetween:' . $space_between . ','; |
| 287 |
if ( $has_navigation ) { |
| 288 |
$config .= ' navigation:{ |
| 289 |
nextEl: ".swiper-button-next", |
| 290 |
prevEl: ".swiper-button-prev", |
| 291 |
},'; |
| 292 |
} |
| 293 |
$config .= ' |
| 294 |
pagination:{ |
| 295 |
el: ".swiper-pagination", |
| 296 |
clickable: true, |
| 297 |
}, |
| 298 |
'; |
| 299 |
$config .= ' |
| 300 |
scrollbar:{ draggable: true }, |
| 301 |
}'; |
| 302 |
$slider_var = 'slider_' . str_replace( '-', '_', $block_client_id ); |
| 303 |
$slider_function_name = 'reinitSlider' . $slider_var; |
| 304 |
wp_add_inline_script( |
| 305 |
'gutenify-swiper', |
| 306 |
'var ' . $slider_var . ' = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . '); |
| 307 |
console.log( ' . $config . ' ); |
| 308 |
jQuery(function($){ |
| 309 |
$(document).on("containerStretched", function(){ |
| 310 |
var swiperWrapper = $("' . $block_client_id . ' .swiper-wrapper"); |
| 311 |
var newSlides = swiperWrapper.children(".swiper-slide").clone(true); |
| 312 |
' . $slider_var . '.destroy(); |
| 313 |
swiperWrapper.empty().append(newSlides); |
| 314 |
swiperWrapper.attr("style", ""); |
| 315 |
mySwiper = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . '); |
| 316 |
}); |
| 317 |
});' |
| 318 |
); |
| 319 |
$block_content .= '<script> |
| 320 |
jQuery(function($){ |
| 321 |
function ' . $slider_function_name . '() { |
| 322 |
var swiperWrapper = $("' . $block_client_id . ' .swiper-wrapper"); |
| 323 |
var newSlides = swiperWrapper.children(".swiper-slide").clone(true); |
| 324 |
' . $slider_var . '.destroy(); |
| 325 |
swiperWrapper.empty().append(newSlides); |
| 326 |
swiperWrapper.attr("style", ""); |
| 327 |
mySwiper = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . '); |
| 328 |
} |
| 329 |
|
| 330 |
var ' . $slider_var . ' = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . '); |
| 331 |
$(window).on("resize", function(){ |
| 332 |
' . $slider_function_name . '(); |
| 333 |
}); |
| 334 |
$(document).on("containerStretched", function(){ |
| 335 |
' . $slider_function_name . '(); |
| 336 |
}); |
| 337 |
});</script>'; |
| 338 |
echo $block_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 339 |
$output = ob_get_contents(); |
| 340 |
ob_end_clean(); |
| 341 |
return $output; |
| 342 |
|
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Returns the posts for an internal posts. |
| 347 |
* |
| 348 |
* @param array $posts Current posts. |
| 349 |
* |
| 350 |
* @return array Returns posts. |
| 351 |
*/ |
| 352 |
function gutenify_get_post_info( $posts ) { |
| 353 |
|
| 354 |
$formatted_posts = array(); |
| 355 |
|
| 356 |
foreach ( $posts as $post ) { |
| 357 |
|
| 358 |
$formatted_post = null; |
| 359 |
|
| 360 |
$formatted_post['thumbnailURL'] = get_the_post_thumbnail_url( $post ); |
| 361 |
$formatted_post['date'] = esc_attr( get_the_date( 'c', $post ) ); |
| 362 |
$formatted_post['dateReadable'] = esc_html( get_the_date( '', $post ) ); |
| 363 |
$formatted_post['title'] = get_the_title( $post ); |
| 364 |
$formatted_post['postLink'] = esc_url( get_permalink( $post ) ); |
| 365 |
$formatted_post['ID'] = absint( $post->ID ); |
| 366 |
$formatted_post['author_id'] = absint( $post->post_author ); |
| 367 |
|
| 368 |
$post_excerpt = $post->post_excerpt; |
| 369 |
|
| 370 |
if ( ! ( $post_excerpt ) ) { |
| 371 |
|
| 372 |
$post_excerpt = $post->post_content; |
| 373 |
|
| 374 |
} |
| 375 |
|
| 376 |
$formatted_post['postExcerpt'] = $post_excerpt; |
| 377 |
|
| 378 |
$formatted_posts[] = $formatted_post; |
| 379 |
|
| 380 |
} |
| 381 |
|
| 382 |
return $formatted_posts; |
| 383 |
|
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Registers the `posts` block on server. |
| 388 |
*/ |
| 389 |
function gutenify_register_post_carousel_block() { |
| 390 |
// Return early if this function does not exist. |
| 391 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 392 |
return; |
| 393 |
} |
| 394 |
|
| 395 |
// Load attributes from block.json. |
| 396 |
ob_start(); |
| 397 |
include GUTENIFY_PLUGIN_DIR . 'src/blocks/post-carousel/block.json'; |
| 398 |
$metadata = json_decode( ob_get_clean(), true ); |
| 399 |
|
| 400 |
register_block_type( |
| 401 |
'gutenify/post-carousel', |
| 402 |
array( |
| 403 |
'attributes' => $metadata['attributes'], |
| 404 |
'render_callback' => 'gutenify_render_post_carousel_block', |
| 405 |
) |
| 406 |
); |
| 407 |
} |
| 408 |
add_action( 'init', 'gutenify_register_post_carousel_block' ); |
| 409 |
|