Animations.php
4 months ago
BackButton.php
7 months ago
BlockPatterns.php
4 months ago
Carousel.php
7 months ago
ContainerEdgeAlignment.php
7 months ago
Counter.php
4 months ago
Events.php
7 months ago
FluidTypography.php
4 months ago
Gallery.php
8 months ago
GravityFormsInline.php
7 months ago
Headline.php
4 months ago
InsertPost.php
8 months ago
ProductCategories.php
8 months ago
ReadingProgress.php
7 months ago
ReadingTime.php
8 months ago
ShapeAnimations.php
7 months ago
StackedImages.php
4 months ago
StickyColumn.php
8 months ago
Testimonials.php
8 months ago
Carousel.php
406 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_filter( 'render_block_core/group', array( $this, 'add_custom_attributes_to_core_group_block' ), 10, 2 ); |
| 39 | add_action( 'init', array( $this, 'register_custom_attributes' ), 5 ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Enqueue block editor assets. |
| 44 | * |
| 45 | * @return void |
| 46 | */ |
| 47 | public function enqueue_block_editor_assets() { |
| 48 | wp_enqueue_script( |
| 49 | 'frontblocks-advanced-option', |
| 50 | FRBL_PLUGIN_URL . 'assets/carousel/frontblocks-advanced-option.js', |
| 51 | array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-data', 'wp-edit-post' ), |
| 52 | FRBL_VERSION, |
| 53 | true |
| 54 | ); |
| 55 | |
| 56 | // Set script translations for JavaScript. |
| 57 | wp_set_script_translations( |
| 58 | 'frontblocks-advanced-option', |
| 59 | 'frontblocks' |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Add custom attributes to grid block. |
| 65 | * |
| 66 | * @param string $block_content Block content. |
| 67 | * @param array $block Block attributes. |
| 68 | * @return string |
| 69 | */ |
| 70 | public function add_custom_attributes_to_grid_block( $block_content, $block ) { |
| 71 | $attrs = $block['attrs'] ?? array(); |
| 72 | $custom_option = isset( $attrs['frblGridOption'] ) ? sanitize_text_field( $attrs['frblGridOption'] ) : ''; |
| 73 | $items_to_view = isset( $attrs['frblItemsToView'] ) ? (int) $attrs['frblItemsToView'] : 4; |
| 74 | $laptop_to_view = isset( $attrs['frblLaptopToView'] ) ? (int) $attrs['frblLaptopToView'] : 3; |
| 75 | $tablet_to_view = isset( $attrs['frblTabletToView'] ) ? (int) $attrs['frblTabletToView'] : 2; |
| 76 | $responsive_to_view = isset( $attrs['frblResponsiveToView'] ) ? (int) $attrs['frblResponsiveToView'] : 1; |
| 77 | $autoplay = isset( $attrs['frblAutoplay'] ) ? ( (int) $attrs['frblAutoplay'] * 1000 ) : ''; |
| 78 | $rewind = isset( $attrs['frblRewind'] ) ? (bool) $attrs['frblRewind'] : true; |
| 79 | $buttons = isset( $attrs['frblButtons'] ) ? sanitize_text_field( $attrs['frblButtons'] ) : 'arrows'; |
| 80 | $button_color = isset( $attrs['frblButtonColor'] ) ? sanitize_text_field( $attrs['frblButtonColor'] ) : ''; |
| 81 | $button_bg_color = isset( $attrs['frblButtonBgColor'] ) ? sanitize_text_field( $attrs['frblButtonBgColor'] ) : ''; |
| 82 | $buttons_position = isset( $attrs['frblButtonsPosition'] ) ? sanitize_text_field( $attrs['frblButtonsPosition'] ) : 'side'; |
| 83 | $disable_on_desktop = isset( $attrs['frblDisableOnDesktop'] ) ? (bool) $attrs['frblDisableOnDesktop'] : false; |
| 84 | |
| 85 | // Add data attributes to the wrapper div if carousel is enabled. |
| 86 | if ( 'carousel' === $custom_option || 'slider' === $custom_option ) { |
| 87 | $attributes = ''; |
| 88 | if ( 'slider' === $custom_option ) { |
| 89 | $attributes .= ' data-rewind="' . esc_attr( $rewind ) . '"'; |
| 90 | } |
| 91 | |
| 92 | // Add data attributes and the 'frontblocks-carousel' class to the first div in the block content. |
| 93 | $block_content = preg_replace( |
| 94 | '/<div([^>]*)class="([^"]*gb-grid-wrapper[^"]*)"([^>]*)>/', |
| 95 | '<div$1class="$2 frontblocks-carousel"$3' . |
| 96 | ' data-type="' . esc_attr( $custom_option ) . '"' . |
| 97 | ' data-view="' . esc_attr( $items_to_view ) . '"' . |
| 98 | ' data-laptop-view="' . esc_attr( $laptop_to_view ) . '"' . |
| 99 | ' data-tablet-view="' . esc_attr( $tablet_to_view ) . '"' . |
| 100 | ' data-mobile-view="' . esc_attr( $responsive_to_view ) . '"' . |
| 101 | ' data-autoplay="' . esc_attr( $autoplay ) . '"' . |
| 102 | ' data-buttons="' . esc_attr( $buttons ) . '"' . |
| 103 | ' data-buttons-color="' . esc_attr( $button_color ) . '"' . |
| 104 | ' data-buttons-background-color="' . esc_attr( $button_bg_color ) . '"' . |
| 105 | ' data-buttons-position="' . esc_attr( $buttons_position ) . '"' . |
| 106 | ' data-disable-on-desktop="' . esc_attr( $disable_on_desktop ? 'true' : 'false' ) . '"' . |
| 107 | $attributes . |
| 108 | '>', |
| 109 | $block_content, |
| 110 | 1 // Only replace the first occurrence. |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | return $block_content; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Add custom attributes to element block. |
| 119 | * |
| 120 | * @param string $block_content Block content. |
| 121 | * @param array $block Block attributes. |
| 122 | * @return string |
| 123 | */ |
| 124 | public function add_custom_attributes_to_element_block( $block_content, $block ) { |
| 125 | $attrs = $block['attrs'] ?? array(); |
| 126 | |
| 127 | // Check if this element has grid display. |
| 128 | $styles = $attrs['styles'] ?? array(); |
| 129 | $display = $styles['display'] ?? ''; |
| 130 | |
| 131 | // Only process if it's a grid element. |
| 132 | if ( 'grid' !== $display ) { |
| 133 | return $block_content; |
| 134 | } |
| 135 | |
| 136 | $custom_option = isset( $attrs['frblGridOption'] ) ? sanitize_text_field( $attrs['frblGridOption'] ) : ''; |
| 137 | $items_to_view = isset( $attrs['frblItemsToView'] ) ? (int) $attrs['frblItemsToView'] : 4; |
| 138 | $laptop_to_view = isset( $attrs['frblLaptopToView'] ) ? (int) $attrs['frblLaptopToView'] : 3; |
| 139 | $tablet_to_view = isset( $attrs['frblTabletToView'] ) ? (int) $attrs['frblTabletToView'] : 2; |
| 140 | $responsive_to_view = isset( $attrs['frblResponsiveToView'] ) ? (int) $attrs['frblResponsiveToView'] : 1; |
| 141 | $autoplay = isset( $attrs['frblAutoplay'] ) ? ( (int) $attrs['frblAutoplay'] * 1000 ) : ''; |
| 142 | $rewind = isset( $attrs['frblRewind'] ) ? (bool) $attrs['frblRewind'] : true; |
| 143 | $buttons = isset( $attrs['frblButtons'] ) ? sanitize_text_field( $attrs['frblButtons'] ) : 'arrows'; |
| 144 | $button_color = isset( $attrs['frblButtonColor'] ) ? sanitize_text_field( $attrs['frblButtonColor'] ) : ''; |
| 145 | $button_bg_color = isset( $attrs['frblButtonBgColor'] ) ? sanitize_text_field( $attrs['frblButtonBgColor'] ) : ''; |
| 146 | $buttons_position = isset( $attrs['frblButtonsPosition'] ) ? sanitize_text_field( $attrs['frblButtonsPosition'] ) : 'side'; |
| 147 | $disable_on_desktop = isset( $attrs['frblDisableOnDesktop'] ) ? (bool) $attrs['frblDisableOnDesktop'] : false; |
| 148 | |
| 149 | // Add data attributes to the wrapper div if carousel is enabled. |
| 150 | if ( 'carousel' === $custom_option || 'slider' === $custom_option ) { |
| 151 | $attributes = ''; |
| 152 | if ( 'slider' === $custom_option ) { |
| 153 | $attributes .= ' data-rewind="' . esc_attr( $rewind ) . '"'; |
| 154 | } |
| 155 | |
| 156 | // Add data attributes and the 'frontblocks-carousel' class to the first div in the block content. |
| 157 | $block_content = preg_replace( |
| 158 | '/<div([^>]*)class="([^"]*gb-element-[^"]*)"([^>]*)>/', |
| 159 | '<div$1class="$2 frontblocks-carousel"$3' . |
| 160 | ' data-type="' . esc_attr( $custom_option ) . '"' . |
| 161 | ' data-view="' . esc_attr( $items_to_view ) . '"' . |
| 162 | ' data-laptop-view="' . esc_attr( $laptop_to_view ) . '"' . |
| 163 | ' data-tablet-view="' . esc_attr( $tablet_to_view ) . '"' . |
| 164 | ' data-mobile-view="' . esc_attr( $responsive_to_view ) . '"' . |
| 165 | ' data-autoplay="' . esc_attr( $autoplay ) . '"' . |
| 166 | ' data-buttons="' . esc_attr( $buttons ) . '"' . |
| 167 | ' data-buttons-color="' . esc_attr( $button_color ) . '"' . |
| 168 | ' data-buttons-background-color="' . esc_attr( $button_bg_color ) . '"' . |
| 169 | ' data-buttons-position="' . esc_attr( $buttons_position ) . '"' . |
| 170 | ' data-disable-on-desktop="' . esc_attr( $disable_on_desktop ? 'true' : 'false' ) . '"' . |
| 171 | $attributes . |
| 172 | '>', |
| 173 | $block_content, |
| 174 | 1 // Only replace the first occurrence. |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | return $block_content; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Add custom attributes to core/group block. |
| 183 | * |
| 184 | * @param string $block_content Block content. |
| 185 | * @param array $block Block attributes. |
| 186 | * @return string |
| 187 | */ |
| 188 | public function add_custom_attributes_to_core_group_block( $block_content, $block ) { |
| 189 | $attrs = $block['attrs'] ?? array(); |
| 190 | |
| 191 | // Check if this group has grid layout. |
| 192 | $layout = $attrs['layout'] ?? array(); |
| 193 | $layout_type = $layout['type'] ?? ''; |
| 194 | |
| 195 | // Only process if it's a grid layout. |
| 196 | if ( 'grid' !== $layout_type ) { |
| 197 | return $block_content; |
| 198 | } |
| 199 | |
| 200 | $custom_option = isset( $attrs['frblGridOption'] ) ? sanitize_text_field( $attrs['frblGridOption'] ) : ''; |
| 201 | $items_to_view = isset( $attrs['frblItemsToView'] ) ? (int) $attrs['frblItemsToView'] : 4; |
| 202 | $laptop_to_view = isset( $attrs['frblLaptopToView'] ) ? (int) $attrs['frblLaptopToView'] : 3; |
| 203 | $tablet_to_view = isset( $attrs['frblTabletToView'] ) ? (int) $attrs['frblTabletToView'] : 2; |
| 204 | $responsive_to_view = isset( $attrs['frblResponsiveToView'] ) ? (int) $attrs['frblResponsiveToView'] : 1; |
| 205 | $autoplay = isset( $attrs['frblAutoplay'] ) ? ( (int) $attrs['frblAutoplay'] * 1000 ) : ''; |
| 206 | $rewind = isset( $attrs['frblRewind'] ) ? (bool) $attrs['frblRewind'] : true; |
| 207 | $buttons = isset( $attrs['frblButtons'] ) ? sanitize_text_field( $attrs['frblButtons'] ) : 'arrows'; |
| 208 | $button_color = isset( $attrs['frblButtonColor'] ) ? sanitize_text_field( $attrs['frblButtonColor'] ) : ''; |
| 209 | $button_bg_color = isset( $attrs['frblButtonBgColor'] ) ? sanitize_text_field( $attrs['frblButtonBgColor'] ) : ''; |
| 210 | $buttons_position = isset( $attrs['frblButtonsPosition'] ) ? sanitize_text_field( $attrs['frblButtonsPosition'] ) : 'side'; |
| 211 | $disable_on_desktop = isset( $attrs['frblDisableOnDesktop'] ) ? (bool) $attrs['frblDisableOnDesktop'] : false; |
| 212 | |
| 213 | // Add data attributes to the wrapper div if carousel is enabled. |
| 214 | if ( 'carousel' === $custom_option || 'slider' === $custom_option ) { |
| 215 | $attributes = ''; |
| 216 | if ( 'slider' === $custom_option ) { |
| 217 | $attributes .= ' data-rewind="' . esc_attr( $rewind ) . '"'; |
| 218 | } |
| 219 | |
| 220 | // Add data attributes and the 'frontblocks-carousel' class to the first div in the block content. |
| 221 | $block_content = preg_replace( |
| 222 | '/<div([^>]*)class="([^"]*wp-block-group[^"]*)"([^>]*)>/', |
| 223 | '<div$1class="$2 frontblocks-carousel"$3' . |
| 224 | ' data-type="' . esc_attr( $custom_option ) . '"' . |
| 225 | ' data-view="' . esc_attr( $items_to_view ) . '"' . |
| 226 | ' data-laptop-view="' . esc_attr( $laptop_to_view ) . '"' . |
| 227 | ' data-tablet-view="' . esc_attr( $tablet_to_view ) . '"' . |
| 228 | ' data-mobile-view="' . esc_attr( $responsive_to_view ) . '"' . |
| 229 | ' data-autoplay="' . esc_attr( $autoplay ) . '"' . |
| 230 | ' data-buttons="' . esc_attr( $buttons ) . '"' . |
| 231 | ' data-buttons-color="' . esc_attr( $button_color ) . '"' . |
| 232 | ' data-buttons-background-color="' . esc_attr( $button_bg_color ) . '"' . |
| 233 | ' data-buttons-position="' . esc_attr( $buttons_position ) . '"' . |
| 234 | ' data-disable-on-desktop="' . esc_attr( $disable_on_desktop ? 'true' : 'false' ) . '"' . |
| 235 | $attributes . |
| 236 | '>', |
| 237 | $block_content, |
| 238 | 1 // Only replace the first occurrence. |
| 239 | ); |
| 240 | } |
| 241 | |
| 242 | return $block_content; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Register custom attributes for blocks. |
| 247 | * |
| 248 | * @return void |
| 249 | */ |
| 250 | public function register_custom_attributes() { |
| 251 | // Register attributes before GenerateBlocks registers its blocks. |
| 252 | add_filter( |
| 253 | 'generateblocks_blocks_registered_block', |
| 254 | array( $this, 'register_custom_attributes_for_grid_block' ), |
| 255 | 9, |
| 256 | 2 |
| 257 | ); |
| 258 | |
| 259 | // Register attributes from frontend side too. |
| 260 | add_action( |
| 261 | 'enqueue_block_editor_assets', |
| 262 | array( $this, 'add_inline_script_for_attributes' ) |
| 263 | ); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Register custom attributes for GenerateBlocks Grid and Element blocks. |
| 268 | * |
| 269 | * @param array $block_args The block arguments. |
| 270 | * @param string $block_type The name of the block. |
| 271 | * @return array Modified block arguments. |
| 272 | */ |
| 273 | public function register_custom_attributes_for_grid_block( $block_args, $block_type ) { |
| 274 | if ( 'generateblocks/grid' !== $block_type && 'generateblocks/element' !== $block_type ) { |
| 275 | return $block_args; |
| 276 | } |
| 277 | |
| 278 | $block_args['attributes']['frblGridOption'] = array( |
| 279 | 'type' => 'string', |
| 280 | 'default' => 'none', |
| 281 | ); |
| 282 | $block_args['attributes']['frblItemsToView'] = array( |
| 283 | 'type' => 'string', |
| 284 | 'default' => '4', |
| 285 | ); |
| 286 | $block_args['attributes']['frblLaptopToView'] = array( |
| 287 | 'type' => 'string', |
| 288 | 'default' => '3', |
| 289 | ); |
| 290 | $block_args['attributes']['frblTabletToView'] = array( |
| 291 | 'type' => 'string', |
| 292 | 'default' => '2', |
| 293 | ); |
| 294 | $block_args['attributes']['frblResponsiveToView'] = array( |
| 295 | 'type' => 'string', |
| 296 | 'default' => '1', |
| 297 | ); |
| 298 | $block_args['attributes']['frblAutoplay'] = array( |
| 299 | 'type' => 'string', |
| 300 | 'default' => '', |
| 301 | ); |
| 302 | $block_args['attributes']['frblRewind'] = array( |
| 303 | 'type' => 'boolean', |
| 304 | 'default' => true, |
| 305 | ); |
| 306 | $block_args['attributes']['frblButtons'] = array( |
| 307 | 'type' => 'string', |
| 308 | 'default' => 'arrows', |
| 309 | ); |
| 310 | $block_args['attributes']['frblButtonColor'] = array( |
| 311 | 'type' => 'string', |
| 312 | 'default' => '', |
| 313 | ); |
| 314 | $block_args['attributes']['frblButtonBgColor'] = array( |
| 315 | 'type' => 'string', |
| 316 | 'default' => '', |
| 317 | ); |
| 318 | $block_args['attributes']['frblButtonsPosition'] = array( |
| 319 | 'type' => 'string', |
| 320 | 'default' => 'side', |
| 321 | ); |
| 322 | $block_args['attributes']['frblDisableOnDesktop'] = array( |
| 323 | 'type' => 'boolean', |
| 324 | 'default' => false, |
| 325 | ); |
| 326 | |
| 327 | return $block_args; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Add inline script for block attributes. |
| 332 | * |
| 333 | * @return void |
| 334 | */ |
| 335 | public function add_inline_script_for_attributes() { |
| 336 | wp_add_inline_script( |
| 337 | 'wp-blocks', |
| 338 | " |
| 339 | wp.hooks.addFilter( |
| 340 | 'blocks.registerBlockType', |
| 341 | 'frontblocks/grid-attributes', |
| 342 | function( settings, name ) { |
| 343 | if ( name !== 'generateblocks/grid' && name !== 'generateblocks/element' && name !== 'core/group' ) { |
| 344 | return settings; |
| 345 | } |
| 346 | |
| 347 | settings.attributes = { |
| 348 | ...settings.attributes, |
| 349 | frblGridOption: { |
| 350 | type: 'string', |
| 351 | default: 'none' |
| 352 | }, |
| 353 | frblItemsToView: { |
| 354 | type: 'string', |
| 355 | default: '4' |
| 356 | }, |
| 357 | frblLaptopToView: { |
| 358 | type: 'string', |
| 359 | default: '3' |
| 360 | }, |
| 361 | frblTabletToView: { |
| 362 | type: 'string', |
| 363 | default: '2' |
| 364 | }, |
| 365 | frblResponsiveToView: { |
| 366 | type: 'string', |
| 367 | default: '1' |
| 368 | }, |
| 369 | frblAutoplay: { |
| 370 | type: 'string', |
| 371 | default: '' |
| 372 | }, |
| 373 | frblButtons: { |
| 374 | type: 'string', |
| 375 | default: 'arrows' |
| 376 | }, |
| 377 | frblButtonsPosition: { |
| 378 | type: 'string', |
| 379 | default: 'side' |
| 380 | }, |
| 381 | frblButtonColor: { |
| 382 | type: 'string', |
| 383 | default: '' |
| 384 | }, |
| 385 | frblButtonBgColor: { |
| 386 | type: 'string', |
| 387 | default: '' |
| 388 | }, |
| 389 | frblRewind: { |
| 390 | type: 'boolean', |
| 391 | default: true |
| 392 | }, |
| 393 | frblDisableOnDesktop: { |
| 394 | type: 'boolean', |
| 395 | default: false |
| 396 | } |
| 397 | }; |
| 398 | |
| 399 | return settings; |
| 400 | } |
| 401 | ); |
| 402 | " |
| 403 | ); |
| 404 | } |
| 405 | } |
| 406 |