PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.2.0
FrontBlocks for Gutenberg/GeneratePress v1.2.0
trunk 0.2.0 0.2.1 0.2.2 0.2.3 0.2.4 0.2.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 ci-artifacts
frontblocks / includes / Frontend / Carousel.php
frontblocks / includes / Frontend Last commit date
Animations.php 8 months ago Carousel.php 8 months ago Counter.php 8 months ago Gallery.php 8 months ago Headline.php 8 months ago InsertPost.php 8 months ago ProductCategories.php 8 months ago ReadingTime.php 8 months ago StickyColumn.php 8 months ago Testimonials.php 8 months ago
Carousel.php
341 lines
1 <?php
2 /**
3 * Carousel module for FrontBlocks.
4 *
5 * @package FrontBlocks
6 * @author David Perez <david@close.technology>
7 * @copyright 2023 Closemarketing
8 * @version 1.0
9 */
10
11 namespace FrontBlocks\Frontend;
12
13 defined( 'ABSPATH' ) || exit;
14
15 /**
16 * Carousel class.
17 *
18 * @since 1.0.0
19 */
20 class Carousel {
21
22 /**
23 * Constructor.
24 */
25 public function __construct() {
26 $this->init_hooks();
27 }
28
29 /**
30 * Initialize hooks.
31 *
32 * @return void
33 */
34 private function init_hooks() {
35 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
36 add_filter( 'render_block_generateblocks/grid', array( $this, 'add_custom_attributes_to_grid_block' ), 10, 2 );
37 add_filter( 'render_block_generateblocks/element', array( $this, 'add_custom_attributes_to_element_block' ), 10, 2 );
38 add_action( 'init', array( $this, 'register_custom_attributes' ), 5 );
39 }
40
41 /**
42 * Enqueue block editor assets.
43 *
44 * @return void
45 */
46 public function enqueue_block_editor_assets() {
47 wp_enqueue_script(
48 'frontblocks-advanced-option',
49 FRBL_PLUGIN_URL . 'assets/carousel/frontblocks-advanced-option.js',
50 array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-data', 'wp-edit-post' ),
51 FRBL_VERSION,
52 true
53 );
54
55 // Set script translations for JavaScript.
56 wp_set_script_translations(
57 'frontblocks-advanced-option',
58 'frontblocks'
59 );
60 }
61
62 /**
63 * Add custom attributes to grid block.
64 *
65 * @param string $block_content Block content.
66 * @param array $block Block attributes.
67 * @return string
68 */
69 public function add_custom_attributes_to_grid_block( $block_content, $block ) {
70 $attrs = $block['attrs'] ?? array();
71 $custom_option = isset( $attrs['frblGridOption'] ) ? sanitize_text_field( $attrs['frblGridOption'] ) : '';
72 $items_to_view = isset( $attrs['frblItemsToView'] ) ? (int) $attrs['frblItemsToView'] : 4;
73 $laptop_to_view = isset( $attrs['frblLaptopToView'] ) ? (int) $attrs['frblLaptopToView'] : 3;
74 $tablet_to_view = isset( $attrs['frblTabletToView'] ) ? (int) $attrs['frblTabletToView'] : 2;
75 $responsive_to_view = isset( $attrs['frblResponsiveToView'] ) ? (int) $attrs['frblResponsiveToView'] : 1;
76 $autoplay = isset( $attrs['frblAutoplay'] ) ? ( (int) $attrs['frblAutoplay'] * 1000 ) : '';
77 $rewind = isset( $attrs['frblRewind'] ) ? (bool) $attrs['frblRewind'] : true;
78 $buttons = isset( $attrs['frblButtons'] ) ? sanitize_text_field( $attrs['frblButtons'] ) : 'arrows';
79 $button_color = isset( $attrs['frblButtonColor'] ) ? sanitize_text_field( $attrs['frblButtonColor'] ) : '';
80 $button_bg_color = isset( $attrs['frblButtonBgColor'] ) ? sanitize_text_field( $attrs['frblButtonBgColor'] ) : '';
81 $buttons_position = isset( $attrs['frblButtonsPosition'] ) ? sanitize_text_field( $attrs['frblButtonsPosition'] ) : 'side';
82 $disable_on_desktop = isset( $attrs['frblDisableOnDesktop'] ) ? (bool) $attrs['frblDisableOnDesktop'] : false;
83
84 // Add data attributes to the wrapper div if carousel is enabled.
85 if ( 'carousel' === $custom_option || 'slider' === $custom_option ) {
86 $attributes = '';
87 if ( 'slider' === $custom_option ) {
88 $attributes .= ' data-rewind="' . esc_attr( $rewind ) . '"';
89 }
90
91 // Add data attributes and the 'frontblocks-carousel' class to the first div in the block content.
92 $block_content = preg_replace(
93 '/<div([^>]*)class="([^"]*gb-grid-wrapper[^"]*)"([^>]*)>/',
94 '<div$1class="$2 frontblocks-carousel"$3' .
95 ' data-type="' . esc_attr( $custom_option ) . '"' .
96 ' data-view="' . esc_attr( $items_to_view ) . '"' .
97 ' data-laptop-view="' . esc_attr( $laptop_to_view ) . '"' .
98 ' data-tablet-view="' . esc_attr( $tablet_to_view ) . '"' .
99 ' data-mobile-view="' . esc_attr( $responsive_to_view ) . '"' .
100 ' data-autoplay="' . esc_attr( $autoplay ) . '"' .
101 ' data-buttons="' . esc_attr( $buttons ) . '"' .
102 ' data-buttons-color="' . esc_attr( $button_color ) . '"' .
103 ' data-buttons-background-color="' . esc_attr( $button_bg_color ) . '"' .
104 ' data-buttons-position="' . esc_attr( $buttons_position ) . '"' .
105 ' data-disable-on-desktop="' . esc_attr( $disable_on_desktop ? 'true' : 'false' ) . '"' .
106 $attributes .
107 '>',
108 $block_content,
109 1 // Only replace the first occurrence.
110 );
111 }
112
113 return $block_content;
114 }
115
116 /**
117 * Add custom attributes to element block.
118 *
119 * @param string $block_content Block content.
120 * @param array $block Block attributes.
121 * @return string
122 */
123 public function add_custom_attributes_to_element_block( $block_content, $block ) {
124 $attrs = $block['attrs'] ?? array();
125
126 // Check if this element has grid display.
127 $styles = $attrs['styles'] ?? array();
128 $display = $styles['display'] ?? '';
129
130 // Only process if it's a grid element.
131 if ( 'grid' !== $display ) {
132 return $block_content;
133 }
134
135 $custom_option = isset( $attrs['frblGridOption'] ) ? sanitize_text_field( $attrs['frblGridOption'] ) : '';
136 $items_to_view = isset( $attrs['frblItemsToView'] ) ? (int) $attrs['frblItemsToView'] : 4;
137 $laptop_to_view = isset( $attrs['frblLaptopToView'] ) ? (int) $attrs['frblLaptopToView'] : 3;
138 $tablet_to_view = isset( $attrs['frblTabletToView'] ) ? (int) $attrs['frblTabletToView'] : 2;
139 $responsive_to_view = isset( $attrs['frblResponsiveToView'] ) ? (int) $attrs['frblResponsiveToView'] : 1;
140 $autoplay = isset( $attrs['frblAutoplay'] ) ? ( (int) $attrs['frblAutoplay'] * 1000 ) : '';
141 $rewind = isset( $attrs['frblRewind'] ) ? (bool) $attrs['frblRewind'] : true;
142 $buttons = isset( $attrs['frblButtons'] ) ? sanitize_text_field( $attrs['frblButtons'] ) : 'arrows';
143 $button_color = isset( $attrs['frblButtonColor'] ) ? sanitize_text_field( $attrs['frblButtonColor'] ) : '';
144 $button_bg_color = isset( $attrs['frblButtonBgColor'] ) ? sanitize_text_field( $attrs['frblButtonBgColor'] ) : '';
145 $buttons_position = isset( $attrs['frblButtonsPosition'] ) ? sanitize_text_field( $attrs['frblButtonsPosition'] ) : 'side';
146 $disable_on_desktop = isset( $attrs['frblDisableOnDesktop'] ) ? (bool) $attrs['frblDisableOnDesktop'] : false;
147
148 // Add data attributes to the wrapper div if carousel is enabled.
149 if ( 'carousel' === $custom_option || 'slider' === $custom_option ) {
150 $attributes = '';
151 if ( 'slider' === $custom_option ) {
152 $attributes .= ' data-rewind="' . esc_attr( $rewind ) . '"';
153 }
154
155 // Add data attributes and the 'frontblocks-carousel' class to the first div in the block content.
156 $block_content = preg_replace(
157 '/<div([^>]*)class="([^"]*gb-element-[^"]*)"([^>]*)>/',
158 '<div$1class="$2 frontblocks-carousel"$3' .
159 ' data-type="' . esc_attr( $custom_option ) . '"' .
160 ' data-view="' . esc_attr( $items_to_view ) . '"' .
161 ' data-laptop-view="' . esc_attr( $laptop_to_view ) . '"' .
162 ' data-tablet-view="' . esc_attr( $tablet_to_view ) . '"' .
163 ' data-mobile-view="' . esc_attr( $responsive_to_view ) . '"' .
164 ' data-autoplay="' . esc_attr( $autoplay ) . '"' .
165 ' data-buttons="' . esc_attr( $buttons ) . '"' .
166 ' data-buttons-color="' . esc_attr( $button_color ) . '"' .
167 ' data-buttons-background-color="' . esc_attr( $button_bg_color ) . '"' .
168 ' data-buttons-position="' . esc_attr( $buttons_position ) . '"' .
169 ' data-disable-on-desktop="' . esc_attr( $disable_on_desktop ? 'true' : 'false' ) . '"' .
170 $attributes .
171 '>',
172 $block_content,
173 1 // Only replace the first occurrence.
174 );
175 }
176
177 return $block_content;
178 }
179
180 /**
181 * Register custom attributes for blocks.
182 *
183 * @return void
184 */
185 public function register_custom_attributes() {
186 // Register attributes before GenerateBlocks registers its blocks.
187 add_filter(
188 'generateblocks_blocks_registered_block',
189 array( $this, 'register_custom_attributes_for_grid_block' ),
190 9,
191 2
192 );
193
194 // Register attributes from frontend side too.
195 add_action(
196 'enqueue_block_editor_assets',
197 array( $this, 'add_inline_script_for_attributes' )
198 );
199 }
200
201 /**
202 * Register custom attributes for GenerateBlocks Grid and Element blocks.
203 *
204 * @param array $block_args The block arguments.
205 * @param string $block_type The name of the block.
206 * @return array Modified block arguments.
207 */
208 public function register_custom_attributes_for_grid_block( $block_args, $block_type ) {
209 if ( 'generateblocks/grid' !== $block_type && 'generateblocks/element' !== $block_type ) {
210 return $block_args;
211 }
212
213 $block_args['attributes']['frblGridOption'] = array(
214 'type' => 'string',
215 'default' => 'none',
216 );
217 $block_args['attributes']['frblItemsToView'] = array(
218 'type' => 'string',
219 'default' => '4',
220 );
221 $block_args['attributes']['frblLaptopToView'] = array(
222 'type' => 'string',
223 'default' => '3',
224 );
225 $block_args['attributes']['frblTabletToView'] = array(
226 'type' => 'string',
227 'default' => '2',
228 );
229 $block_args['attributes']['frblResponsiveToView'] = array(
230 'type' => 'string',
231 'default' => '1',
232 );
233 $block_args['attributes']['frblAutoplay'] = array(
234 'type' => 'string',
235 'default' => '',
236 );
237 $block_args['attributes']['frblRewind'] = array(
238 'type' => 'boolean',
239 'default' => true,
240 );
241 $block_args['attributes']['frblButtons'] = array(
242 'type' => 'string',
243 'default' => 'arrows',
244 );
245 $block_args['attributes']['frblButtonColor'] = array(
246 'type' => 'string',
247 'default' => '',
248 );
249 $block_args['attributes']['frblButtonBgColor'] = array(
250 'type' => 'string',
251 'default' => '',
252 );
253 $block_args['attributes']['frblButtonsPosition'] = array(
254 'type' => 'string',
255 'default' => 'side',
256 );
257 $block_args['attributes']['frblDisableOnDesktop'] = array(
258 'type' => 'boolean',
259 'default' => false,
260 );
261
262 return $block_args;
263 }
264
265 /**
266 * Add inline script for block attributes.
267 *
268 * @return void
269 */
270 public function add_inline_script_for_attributes() {
271 wp_add_inline_script(
272 'wp-blocks',
273 "
274 wp.hooks.addFilter(
275 'blocks.registerBlockType',
276 'frontblocks/grid-attributes',
277 function( settings, name ) {
278 if ( name !== 'generateblocks/grid' && name !== 'generateblocks/element' ) {
279 return settings;
280 }
281
282 settings.attributes = {
283 ...settings.attributes,
284 frblGridOption: {
285 type: 'string',
286 default: 'none'
287 },
288 frblItemsToView: {
289 type: 'string',
290 default: '4'
291 },
292 frblLaptopToView: {
293 type: 'string',
294 default: '3'
295 },
296 frblTabletToView: {
297 type: 'string',
298 default: '2'
299 },
300 frblResponsiveToView: {
301 type: 'string',
302 default: '1'
303 },
304 frblAutoplay: {
305 type: 'string',
306 default: ''
307 },
308 frblButtons: {
309 type: 'string',
310 default: 'arrows'
311 },
312 frblButtonsPosition: {
313 type: 'string',
314 default: 'side'
315 },
316 frblButtonColor: {
317 type: 'string',
318 default: ''
319 },
320 frblButtonBgColor: {
321 type: 'string',
322 default: ''
323 },
324 frblRewind: {
325 type: 'boolean',
326 default: true
327 },
328 frblDisableOnDesktop: {
329 type: 'boolean',
330 default: false
331 }
332 };
333
334 return settings;
335 }
336 );
337 "
338 );
339 }
340 }
341