PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.0.2
Gutenify – Visual Site Builder Blocks & Starter Templates v1.0.2
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 / slider / index.php

index.php in Gutenify – Visual Site Builder Blocks & Starter Templates 1.0.2, at src/blocks/slider/index.php

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