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

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

204 lines 6.8 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 on: {
128 init: function () {
129 jQuery(document).trigger("gutenifySlideinit" );
130 },
131 },
132 }';
133 $slider_var = 'slider_' . str_replace( '-', '_', $block_client_id );
134 $slider_function_name = 'reinitSlider' . $slider_var;
135 wp_add_inline_script(
136 'gutenify-swiper',
137 'var ' . $slider_var . ' = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . ');
138 jQuery(function($){
139 $(document).on("containerStretched", function(){
140 var swiperWrapper = $("' . $block_client_id . ' .swiper-wrapper");
141 var newSlides = swiperWrapper.children(".swiper-slide").clone(true);
142 ' . $slider_var . '.destroy();
143 swiperWrapper.empty().append(newSlides);
144 swiperWrapper.attr("style", "");
145 mySwiper = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . ');
146 });
147 });'
148 );
149 $block_content .= '<script>
150 jQuery(function($){
151 function ' . $slider_function_name . '() {
152 var swiperWrapper = $("#gutenify-section-' . $block_client_id . ' .swiper-wrapper");
153 var newSlides = swiperWrapper.children(".swiper-slide").clone(true);
154 ' . $slider_var . '.destroy();
155 swiperWrapper.empty().append(newSlides);
156 swiperWrapper.attr("style", "");
157 mySwiper = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . ');
158 }
159
160 var ' . $slider_var . ' = new Swiper("#gutenify-section-' . $block_client_id . ' .swiper-container", ' . $config . ');
161 $(window).on("resize", function(){
162 ' . $slider_function_name . '();
163 });
164 ' . $slider_var . '.on("slideChangeTransitionEnd", function (a,b) {
165 jQuery(document).trigger("gutenifySlideChangeTransitionEnd" );
166 });
167 ' . $slider_var . '.on("slideChangeTransitionStart", function (a,b) {
168 jQuery(document).trigger("gutenifySlideChangeTransitionStart" );
169 });
170 $(document).on("containerStretched", function(){
171 ' . $slider_function_name . '();
172 });
173 });</script>';
174 echo $block_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
175 $output = ob_get_contents();
176 ob_end_clean();
177 return $output;
178
179 }
180
181 /**
182 * Registers the `gallery` block on server.
183 */
184 function gutenify_register_slider_block() {
185 // Return early if this function does not exist.
186 if ( ! function_exists( 'register_block_type' ) ) {
187 return;
188 }
189
190 // Load attributes from block.json.
191 ob_start();
192 include GUTENIFY_PLUGIN_DIR . 'src/blocks/slider/block.json';
193 $metadata = json_decode( ob_get_clean(), true );
194
195 register_block_type(
196 'gutenify/slider',
197 array(
198 'attributes' => $metadata['attributes'],
199 'render_callback' => 'gutenify_render_slider_block',
200 )
201 );
202 }
203 add_action( 'init', 'gutenify_register_slider_block' );
204