PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.0.9
Gutenify – Visual Site Builder Blocks & Starter Templates v1.0.9
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.9, at src/blocks/gallery-carousel/index.php

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