PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.0.8
Gutenify – Visual Site Builder Blocks & Starter Templates v1.0.8
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.8, at src/blocks/post-carousel/index.php

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