Animations.php
9 months ago
Carousel.php
9 months ago
Gallery.php
9 months ago
InsertPost.php
9 months ago
StickyColumn.php
9 months ago
Testimonials.php
9 months ago
Carousel.php
299 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/dist/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 | |
| 56 | /** |
| 57 | * Add custom attributes to grid block. |
| 58 | * |
| 59 | * @param string $block_content Block content. |
| 60 | * @param array $block Block attributes. |
| 61 | * @return string |
| 62 | */ |
| 63 | public function add_custom_attributes_to_grid_block( $block_content, $block ) { |
| 64 | $attrs = $block['attrs'] ?? array(); |
| 65 | $custom_option = isset( $attrs['frblGridOption'] ) ? sanitize_text_field( $attrs['frblGridOption'] ) : ''; |
| 66 | $items_to_view = isset( $attrs['frblItemsToView'] ) ? (int) $attrs['frblItemsToView'] : 4; |
| 67 | $responsive_to_view = isset( $attrs['frblResponsiveToView'] ) ? (int) $attrs['frblResponsiveToView'] : 1; |
| 68 | $autoplay = isset( $attrs['frblAutoplay'] ) ? ( (int) $attrs['frblAutoplay'] * 1000 ) : ''; |
| 69 | $rewind = isset( $attrs['frblRewind'] ) ? (bool) $attrs['frblRewind'] : true; |
| 70 | $buttons = isset( $attrs['frblButtons'] ) ? sanitize_text_field( $attrs['frblButtons'] ) : 'arrows'; |
| 71 | $button_color = isset( $attrs['frblButtonColor'] ) ? sanitize_text_field( $attrs['frblButtonColor'] ) : ''; |
| 72 | $button_bg_color = isset( $attrs['frblButtonBgColor'] ) ? sanitize_text_field( $attrs['frblButtonBgColor'] ) : ''; |
| 73 | $buttons_position = isset( $attrs['frblButtonsPosition'] ) ? sanitize_text_field( $attrs['frblButtonsPosition'] ) : 'side'; |
| 74 | |
| 75 | // Add data attributes to the wrapper div if carousel is enabled. |
| 76 | if ( 'carousel' === $custom_option || 'slider' === $custom_option ) { |
| 77 | $attributes = ''; |
| 78 | if ( 'slider' === $custom_option ) { |
| 79 | $attributes .= ' data-rewind="' . esc_attr( $rewind ) . '"'; |
| 80 | } |
| 81 | |
| 82 | // Add data attributes and the 'frontblocks-carousel' class to the first div in the block content. |
| 83 | $block_content = preg_replace( |
| 84 | '/<div([^>]*)class="([^"]*gb-grid-wrapper[^"]*)"([^>]*)>/', |
| 85 | '<div$1class="$2 frontblocks-carousel"$3' . |
| 86 | ' data-type="' . esc_attr( $custom_option ) . '"' . |
| 87 | ' data-view="' . esc_attr( $items_to_view ) . '"' . |
| 88 | ' data-res-view="' . esc_attr( $responsive_to_view ) . '"' . |
| 89 | ' data-autoplay="' . esc_attr( $autoplay ) . '"' . |
| 90 | ' data-buttons="' . esc_attr( $buttons ) . '"' . |
| 91 | ' data-buttons-color="' . esc_attr( $button_color ) . '"' . |
| 92 | ' data-buttons-background-color="' . esc_attr( $button_bg_color ) . '"' . |
| 93 | ' data-buttons-position="' . esc_attr( $buttons_position ) . '"' . |
| 94 | $attributes . |
| 95 | '>', |
| 96 | $block_content, |
| 97 | 1 // Only replace the first occurrence. |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | return $block_content; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Add custom attributes to element block. |
| 106 | * |
| 107 | * @param string $block_content Block content. |
| 108 | * @param array $block Block attributes. |
| 109 | * @return string |
| 110 | */ |
| 111 | public function add_custom_attributes_to_element_block( $block_content, $block ) { |
| 112 | $attrs = $block['attrs'] ?? array(); |
| 113 | |
| 114 | // Check if this element has grid display. |
| 115 | $styles = $attrs['styles'] ?? array(); |
| 116 | $display = $styles['display'] ?? ''; |
| 117 | |
| 118 | // Only process if it's a grid element. |
| 119 | if ( 'grid' !== $display ) { |
| 120 | return $block_content; |
| 121 | } |
| 122 | |
| 123 | $custom_option = isset( $attrs['frblGridOption'] ) ? sanitize_text_field( $attrs['frblGridOption'] ) : ''; |
| 124 | $items_to_view = isset( $attrs['frblItemsToView'] ) ? (int) $attrs['frblItemsToView'] : 4; |
| 125 | $responsive_to_view = isset( $attrs['frblResponsiveToView'] ) ? (int) $attrs['frblResponsiveToView'] : 1; |
| 126 | $autoplay = isset( $attrs['frblAutoplay'] ) ? ( (int) $attrs['frblAutoplay'] * 1000 ) : ''; |
| 127 | $rewind = isset( $attrs['frblRewind'] ) ? (bool) $attrs['frblRewind'] : true; |
| 128 | $buttons = isset( $attrs['frblButtons'] ) ? sanitize_text_field( $attrs['frblButtons'] ) : 'arrows'; |
| 129 | $button_color = isset( $attrs['frblButtonColor'] ) ? sanitize_text_field( $attrs['frblButtonColor'] ) : ''; |
| 130 | $button_bg_color = isset( $attrs['frblButtonBgColor'] ) ? sanitize_text_field( $attrs['frblButtonBgColor'] ) : ''; |
| 131 | $buttons_position = isset( $attrs['frblButtonsPosition'] ) ? sanitize_text_field( $attrs['frblButtonsPosition'] ) : 'side'; |
| 132 | |
| 133 | // Add data attributes to the wrapper div if carousel is enabled. |
| 134 | if ( 'carousel' === $custom_option || 'slider' === $custom_option ) { |
| 135 | $attributes = ''; |
| 136 | if ( 'slider' === $custom_option ) { |
| 137 | $attributes .= ' data-rewind="' . esc_attr( $rewind ) . '"'; |
| 138 | } |
| 139 | |
| 140 | // Add data attributes and the 'frontblocks-carousel' class to the first div in the block content. |
| 141 | $block_content = preg_replace( |
| 142 | '/<div([^>]*)class="([^"]*gb-element-[^"]*)"([^>]*)>/', |
| 143 | '<div$1class="$2 frontblocks-carousel"$3' . |
| 144 | ' data-type="' . esc_attr( $custom_option ) . '"' . |
| 145 | ' data-view="' . esc_attr( $items_to_view ) . '"' . |
| 146 | ' data-res-view="' . esc_attr( $responsive_to_view ) . '"' . |
| 147 | ' data-autoplay="' . esc_attr( $autoplay ) . '"' . |
| 148 | ' data-buttons="' . esc_attr( $buttons ) . '"' . |
| 149 | ' data-buttons-color="' . esc_attr( $button_color ) . '"' . |
| 150 | ' data-buttons-background-color="' . esc_attr( $button_bg_color ) . '"' . |
| 151 | ' data-buttons-position="' . esc_attr( $buttons_position ) . '"' . |
| 152 | $attributes . |
| 153 | '>', |
| 154 | $block_content, |
| 155 | 1 // Only replace the first occurrence. |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | return $block_content; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Register custom attributes for blocks. |
| 164 | * |
| 165 | * @return void |
| 166 | */ |
| 167 | public function register_custom_attributes() { |
| 168 | // Register attributes before GenerateBlocks registers its blocks. |
| 169 | add_filter( |
| 170 | 'generateblocks_blocks_registered_block', |
| 171 | array( $this, 'register_custom_attributes_for_grid_block' ), |
| 172 | 9, |
| 173 | 2 |
| 174 | ); |
| 175 | |
| 176 | // Register attributes from frontend side too. |
| 177 | add_action( |
| 178 | 'enqueue_block_editor_assets', |
| 179 | array( $this, 'add_inline_script_for_attributes' ) |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Register custom attributes for GenerateBlocks Grid and Element blocks. |
| 185 | * |
| 186 | * @param array $block_args The block arguments. |
| 187 | * @param string $block_type The name of the block. |
| 188 | * @return array Modified block arguments. |
| 189 | */ |
| 190 | public function register_custom_attributes_for_grid_block( $block_args, $block_type ) { |
| 191 | if ( 'generateblocks/grid' !== $block_type && 'generateblocks/element' !== $block_type ) { |
| 192 | return $block_args; |
| 193 | } |
| 194 | |
| 195 | $block_args['attributes']['frblGridOption'] = array( |
| 196 | 'type' => 'string', |
| 197 | 'default' => 'none', |
| 198 | ); |
| 199 | $block_args['attributes']['frblItemsToView'] = array( |
| 200 | 'type' => 'string', |
| 201 | 'default' => '4', |
| 202 | ); |
| 203 | $block_args['attributes']['frblResponsiveToView'] = array( |
| 204 | 'type' => 'string', |
| 205 | 'default' => '1', |
| 206 | ); |
| 207 | $block_args['attributes']['frblAutoplay'] = array( |
| 208 | 'type' => 'string', |
| 209 | 'default' => '', |
| 210 | ); |
| 211 | $block_args['attributes']['frblRewind'] = array( |
| 212 | 'type' => 'boolean', |
| 213 | 'default' => true, |
| 214 | ); |
| 215 | $block_args['attributes']['frblButtons'] = array( |
| 216 | 'type' => 'string', |
| 217 | 'default' => 'arrows', |
| 218 | ); |
| 219 | $block_args['attributes']['frblButtonColor'] = array( |
| 220 | 'type' => 'string', |
| 221 | 'default' => '', |
| 222 | ); |
| 223 | $block_args['attributes']['frblButtonBgColor'] = array( |
| 224 | 'type' => 'string', |
| 225 | 'default' => '', |
| 226 | ); |
| 227 | $block_args['attributes']['frblButtonsPosition'] = array( |
| 228 | 'type' => 'string', |
| 229 | 'default' => 'side', |
| 230 | ); |
| 231 | |
| 232 | return $block_args; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Add inline script for block attributes. |
| 237 | * |
| 238 | * @return void |
| 239 | */ |
| 240 | public function add_inline_script_for_attributes() { |
| 241 | wp_add_inline_script( |
| 242 | 'wp-blocks', |
| 243 | " |
| 244 | wp.hooks.addFilter( |
| 245 | 'blocks.registerBlockType', |
| 246 | 'frontblocks/grid-attributes', |
| 247 | function( settings, name ) { |
| 248 | if ( name !== 'generateblocks/grid' && name !== 'generateblocks/element' ) { |
| 249 | return settings; |
| 250 | } |
| 251 | |
| 252 | settings.attributes = { |
| 253 | ...settings.attributes, |
| 254 | frblGridOption: { |
| 255 | type: 'string', |
| 256 | default: 'none' |
| 257 | }, |
| 258 | frblItemsToView: { |
| 259 | type: 'string', |
| 260 | default: '4' |
| 261 | }, |
| 262 | frblResponsiveToView: { |
| 263 | type: 'string', |
| 264 | default: '1' |
| 265 | }, |
| 266 | frblAutoplay: { |
| 267 | type: 'string', |
| 268 | default: '' |
| 269 | }, |
| 270 | frblButtons: { |
| 271 | type: 'string', |
| 272 | default: 'arrows' |
| 273 | }, |
| 274 | frblButtonsPosition: { |
| 275 | type: 'string', |
| 276 | default: 'side' |
| 277 | }, |
| 278 | frblButtonColor: { |
| 279 | type: 'string', |
| 280 | default: '' |
| 281 | }, |
| 282 | frblButtonBgColor: { |
| 283 | type: 'string', |
| 284 | default: '' |
| 285 | }, |
| 286 | frblRewind: { |
| 287 | type: 'boolean', |
| 288 | default: true |
| 289 | } |
| 290 | }; |
| 291 | |
| 292 | return settings; |
| 293 | } |
| 294 | ); |
| 295 | " |
| 296 | ); |
| 297 | } |
| 298 | } |
| 299 |