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 / gallery-carousel / index.php

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

227 lines 8.1 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_gallery_carousel_block( $attributes ) {
16 return gutenify_gallery_carousel( $attributes );
17 }
18
19
20 /**
21 * Renders the list and grid styles.
22 *
23 * @param array $attributes The block attributes.
24 *
25 * @return string Returns the block content for the list and grid styles.
26 */
27 function gutenify_gallery_carousel( $attributes ) {
28 $client_id = ! empty( $attributes['blockClientId'] ) ? $attributes['blockClientId'] : '';
29 $class_name = array();
30 $class = array( 'swiper-container', 'gutenify-gallery-carousel' );
31 $styles = array();
32 $block_style = ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-stacked' ) !== false ? 'stacked' : 'horizontal';
33
34 array_push( $class_name, 'wp-block-gutenify-gallery-carousel', 'gutenify-section-' . $attributes['blockClientId'], 'gutenify-post-carousel-' . $attributes['layout'] );
35
36 if ( isset( $attributes['className'] ) ) {
37 array_push( $class_name, $attributes['className'] );
38 }
39
40 if ( isset( $attributes['align'] ) ) {
41 array_push( $class_name, 'align' . $attributes['align'] );
42 }
43
44 if ( isset( $attributes['columns'] ) ) {
45 array_push( $class, 'has-columns has-' . $attributes['columns'] . '-columns has-responsive-columns' );
46 }
47
48 if ( isset( $attributes['listPosition'] ) && 'right' === $attributes['listPosition'] && 'horizontal' === $block_style ) {
49 array_push( $class, 'has-image-right' );
50 }
51
52 if ( isset( $attributes['imageSize'] ) && 'horizontal' === $block_style ) {
53 array_push( $class, 'has-' . $attributes['imageSize'] . '-image' );
54 }
55
56 if ( isset( $attributes['imageStyle'] ) ) {
57 array_push( $class, 'has-' . $attributes['imageStyle'] . '-image' );
58 }
59
60 $block_content = sprintf(
61 '<div class="%1$s"><div id="gutenify-section-' . $client_id . '" class="gutenify-gallery-carousel-section"><div class="%2$s" style="%3$s"><div class="swiper-wrapper">',
62 esc_attr( implode( ' ', $class_name ) ),
63 esc_attr( implode( ' ', apply_filters( 'gutenify_render_wrapper_class', $class, $attributes ) ) ),
64 esc_attr( implode( ' ', apply_filters( 'gutenify_render_wrapper_styles', $styles, $attributes ) ) )
65 );
66
67 $list_items_markup = '';
68
69 if ( ! empty( $attributes['images'] ) ) {
70 foreach ( $attributes['images'] as $image ) {
71 $title = ! empty( $attributes['imagesData'][ $image['id'] ]['title'] ) ? $attributes['imagesData'][ $image['id'] ]['title'] : '';
72 $sub_title = ! empty( $attributes['imagesData'][ $image['id'] ]['subTitle'] ) ? $attributes['imagesData'][ $image['id'] ]['subTitle'] : '';
73 $description = ! empty( $attributes['imagesData'][ $image['id'] ]['description'] ) ? $attributes['imagesData'][ $image['id'] ]['description'] : '';
74 $button_text = ! empty( $attributes['imagesData'][ $image['id'] ]['buttonText'] ) ? $attributes['imagesData'][ $image['id'] ]['buttonText'] : '';
75 $list_items_markup .= '<div class="gutenify-gallery-carousel-item swiper-slide">
76 <div class="gutenify-slider-image-wrapper">
77 <div class="gutenify-slider-content-image featured-image">
78 <img src="' . $image['url'] . '" />
79 </div>
80 </div>
81 <div class="gutenify-slider-content-wrapper">
82 <div class="gutenify-slider-content-inner">';
83 if ( ! empty( $title ) ) {
84 $list_items_markup .= '<h2 class="gutenify-slider-title">' . $title . '</h2>';
85 }
86 if ( ! empty( $sub_title ) ) {
87 $list_items_markup .= '<h2 class="gutenify-slider-sub-title">' . $sub_title . '</h2>';
88 }
89
90 $list_items_markup .= '<div class="gutenify-slider-content-inner-wrapper">';
91 if ( ! empty( $description ) ) {
92 $list_items_markup .= '<div class="gutenify-slider-content clear-fix">
93 <p>' . $description . '</p>
94 </div>';
95 }
96 // if ( ! empty( $button_text ) ) {
97 // $list_items_markup .= '<p class="gutenify-slider-buttons clear-fix gutenify-button-link wp-block-button__link">
98 // ' . $button_text . '
99 // </p>';
100 // }
101 $list_items_markup .= '</div>';
102 $list_items_markup .= '</div>
103 </div></div>';
104 }
105 }
106
107 $block_content .= $list_items_markup;
108 $block_content .= '</div>';
109 $has_navigation = ! empty( $attributes['hasNavigation'] ) && true === $attributes['hasNavigation'];
110 $has_pagination = ! empty( $attributes['hasPagination'] ) && true === $attributes['hasPagination'];
111 if ( $has_navigation ) {
112 $block_content .= '<div class="swiper-button-next"></div>
113 <div class="swiper-button-prev"></div>';
114 }
115 if ( $has_pagination ) {
116 $block_content .= '<div class="swiper-pagination"></div>';
117 }
118 $block_content .= '</div>';
119 $block_content .= '</div>';
120 $block_content .= '</div>';
121
122 $columns = ! empty( $attributes['columns'] ) ? $attributes['columns'] : 1;
123 $space_between = ! empty( $attributes['spaceBetween'] ) ? $attributes['spaceBetween'] : 0;
124 $block_client_id = ! empty( $attributes['blockClientId'] ) ? $attributes['blockClientId'] : 1;
125 ob_start();
126 wp_enqueue_script( 'gutenify-swiper' );
127 $config = '{
128 loop: true,
129 slidesPerView: ' . $columns . ',
130 breakpoints: {
131 // when window width is >= 320px
132 320: {
133 slidesPerView: 1,
134 spaceBetween: 20
135 },
136 // when window width is >= 640px
137 640: {
138 slidesPerView: 2,
139 spaceBetween: 15
140 },
141 1024: {
142 slidesPerView: ' . $columns . ',
143 spaceBetween: ' . $space_between . '
144 }
145 },';
146 $config .= ' spaceBetween:' . $space_between . ',';
147 if ( $has_navigation ) {
148 $config .= ' navigation:{
149 nextEl: ".swiper-button-next",
150 prevEl: ".swiper-button-prev",
151 },';
152 }
153 $config .= '
154 pagination:{
155 el: ".swiper-pagination",
156 clickable: true,
157 },
158 ';
159 $config .= '
160 scrollbar:{ draggable: true }
161 }';
162 $slider_var = 'slider_' . str_replace( '-', '_', $block_client_id );
163 $slider_function_name = 'reinitSlider' . $slider_var;
164 wp_add_inline_script(
165 'gutenify-swiper',
166 'var ' . $slider_var . ' = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . ');
167 jQuery(function($){
168 $(document).on("containerStretched", function(){
169 var swiperWrapper = $("' . $block_client_id . ' .swiper-wrapper");
170 var newSlides = swiperWrapper.children(".swiper-slide").clone(true);
171 ' . $slider_var . '.destroy();
172 swiperWrapper.empty().append(newSlides);
173 swiperWrapper.attr("style", "");
174 mySwiper = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . ');
175 });
176 });'
177 );
178 $block_content .= '<script>
179 jQuery(function($){
180 function ' . $slider_function_name . '() {
181 var swiperWrapper = $("' . $block_client_id . ' .swiper-wrapper");
182 var newSlides = swiperWrapper.children(".swiper-slide").clone(true);
183 ' . $slider_var . '.destroy();
184 swiperWrapper.empty().append(newSlides);
185 swiperWrapper.attr("style", "");
186 mySwiper = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . ');
187 }
188
189 var ' . $slider_var . ' = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . ');
190 $(window).on("resize", function(){
191 ' . $slider_function_name . '();
192 });
193 $(document).on("containerStretched", function(){
194 ' . $slider_function_name . '();
195 });
196 });</script>';
197 echo $block_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
198 $output = ob_get_contents();
199 ob_end_clean();
200 return $output;
201
202 }
203
204 /**
205 * Registers the `gallery` block on server.
206 */
207 function gutenify_register_gallery_carousel_block() {
208 // Return early if this function does not exist.
209 if ( ! function_exists( 'register_block_type' ) ) {
210 return;
211 }
212
213 // Load attributes from block.json.
214 ob_start();
215 include GUTENIFY_PLUGIN_DIR . 'src/blocks/gallery-carousel/block.json';
216 $metadata = json_decode( ob_get_clean(), true );
217
218 register_block_type(
219 'gutenify/gallery-carousel',
220 array(
221 'attributes' => $metadata['attributes'],
222 'render_callback' => 'gutenify_render_gallery_carousel_block',
223 )
224 );
225 }
226 add_action( 'init', 'gutenify_register_gallery_carousel_block' );
227