PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.0.5
Gutenify – Visual Site Builder Blocks & Starter Templates v1.0.5
1.9.0 1.8.0 1.7.0 1.6.6 1.6.5 1.6.4 trunk 0.0.1 0.0.2 0.0.3 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 All 74 releases
gutenify / src / blocks / post-carousel / index.php

index.php in Gutenify – Visual Site Builder Blocks & Starter Templates 1.0.5, at src/blocks/post-carousel/index.php

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