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