frontblocks-advanced-option.jsx
11 months ago
frontblocks-carousel.css
11 months ago
frontblocks-carousel.js
11 months ago
frontblocks-carousel.php
10 months ago
frontblocks-carousel.php
298 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class Carousel |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @author David Perez <david@close.technology> |
| 7 | * @copyright 2023 Closemarketing |
| 8 | * @version 1.0 |
| 9 | */ |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | add_action( 'wp_enqueue_scripts', 'frbl_theme_scripts', 99 ); |
| 14 | /** |
| 15 | * Loads Scripts |
| 16 | * |
| 17 | * @return void |
| 18 | */ |
| 19 | function frbl_theme_scripts() { |
| 20 | $dist_dir = WP_DEBUG ? 'carousel/' : 'dist/'; |
| 21 | wp_enqueue_style( |
| 22 | 'frontblocks-carousel', |
| 23 | FRBL_PLUGIN_URL . 'includes/' . $dist_dir . 'frontblocks-carousel.css', |
| 24 | array(), |
| 25 | FRBL_VERSION |
| 26 | ); |
| 27 | |
| 28 | wp_enqueue_script( |
| 29 | 'frontblocks-carousel', |
| 30 | FRBL_PLUGIN_URL . 'includes/dist/glide.min.js', |
| 31 | array(), |
| 32 | FRBL_VERSION, |
| 33 | true |
| 34 | ); |
| 35 | |
| 36 | wp_enqueue_script( |
| 37 | 'frontblocks-carousel-custom', |
| 38 | FRBL_PLUGIN_URL . 'includes/' . $dist_dir . ( WP_DEBUG ? 'frontblocks-carousel.js' : 'frontblocks-carousel-min.js' ), |
| 39 | array( 'frontblocks-carousel' ), |
| 40 | FRBL_VERSION, |
| 41 | true |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | add_action( 'enqueue_block_editor_assets', 'frbl_enqueue_block_editor_assets' ); |
| 46 | /** |
| 47 | * Enqueue custom block editor script |
| 48 | * |
| 49 | * @return void |
| 50 | */ |
| 51 | function frbl_enqueue_block_editor_assets() { |
| 52 | wp_enqueue_script( |
| 53 | 'frontblocks-advanced-option', |
| 54 | FRBL_PLUGIN_URL . 'includes/dist/frontblocks-advanced-option.js', |
| 55 | array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-data', 'wp-edit-post' ), |
| 56 | FRBL_VERSION, |
| 57 | true |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | add_filter( 'render_block_generateblocks/grid', 'frbl_add_custom_attributes_to_grid_block', 10, 2 ); |
| 62 | add_filter( 'render_block_generateblocks/element', 'frbl_add_custom_attributes_to_element_block', 10, 2 ); |
| 63 | /** |
| 64 | * Hook to filter the block output on frontend for grid blocks. |
| 65 | * |
| 66 | * @param html $block_content Block content. |
| 67 | * @param array $block Block attributes. |
| 68 | * |
| 69 | * @return html |
| 70 | */ |
| 71 | function frbl_add_custom_attributes_to_grid_block( $block_content, $block ) { |
| 72 | $attrs = $block['attrs'] ?? array(); |
| 73 | $custom_option = isset( $attrs['frblGridOption'] ) ? sanitize_text_field( $attrs['frblGridOption'] ) : ''; |
| 74 | $items_to_view = isset( $attrs['frblItemsToView'] ) ? (int) $attrs['frblItemsToView'] : 4; |
| 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 | |
| 83 | // Example: Add data attributes to the wrapper div if carousel is enabled. |
| 84 | if ( 'carousel' === $custom_option || 'slider' === $custom_option ) { |
| 85 | $attributes = ''; |
| 86 | if ( 'slider' === $custom_option ) { |
| 87 | $attributes .= ' data-rewind="' . esc_attr( $rewind ) . '"'; |
| 88 | } |
| 89 | // Add data attributes and the 'frontblocks-carousel' class to the first div in the block content. |
| 90 | $block_content = preg_replace( |
| 91 | '/<div([^>]*)class="([^"]*gb-grid-wrapper[^"]*)"([^>]*)>/', |
| 92 | '<div$1class="$2 frontblocks-carousel"$3' . |
| 93 | ' data-type="' . esc_attr( $custom_option ) . '"' . |
| 94 | ' data-view="' . esc_attr( $items_to_view ) . '"' . |
| 95 | ' data-res-view="' . esc_attr( $responsive_to_view ) . '"' . |
| 96 | ' data-autoplay="' . esc_attr( $autoplay ) . '"' . |
| 97 | ' data-buttons="' . esc_attr( $buttons ) . '"' . |
| 98 | ' data-buttons-color="' . esc_attr( $button_color ) . '"' . |
| 99 | ' data-buttons-background-color="' . esc_attr( $button_bg_color ) . '"' . |
| 100 | ' data-buttons-position="' . esc_attr( $buttons_position ) . '"' . |
| 101 | $attributes . |
| 102 | '>', |
| 103 | $block_content, |
| 104 | 1 // Only replace the first occurrence. |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | return $block_content; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Hook to filter the block output on frontend for element blocks with grid display. |
| 113 | * |
| 114 | * @param html $block_content Block content. |
| 115 | * @param array $block Block attributes. |
| 116 | * |
| 117 | * @return html |
| 118 | */ |
| 119 | function frbl_add_custom_attributes_to_element_block( $block_content, $block ) { |
| 120 | $attrs = $block['attrs'] ?? array(); |
| 121 | |
| 122 | // Check if this element has grid display |
| 123 | $styles = $attrs['styles'] ?? array(); |
| 124 | $display = $styles['display'] ?? ''; |
| 125 | |
| 126 | // Only process if it's a grid element |
| 127 | if ( 'grid' !== $display ) { |
| 128 | return $block_content; |
| 129 | } |
| 130 | |
| 131 | $custom_option = isset( $attrs['frblGridOption'] ) ? sanitize_text_field( $attrs['frblGridOption'] ) : ''; |
| 132 | $items_to_view = isset( $attrs['frblItemsToView'] ) ? (int) $attrs['frblItemsToView'] : 4; |
| 133 | $responsive_to_view = isset( $attrs['frblResponsiveToView'] ) ? (int) $attrs['frblResponsiveToView'] : 1; |
| 134 | $autoplay = isset( $attrs['frblAutoplay'] ) ? ( (int) $attrs['frblAutoplay'] * 1000 ) : ''; |
| 135 | $rewind = isset( $attrs['frblRewind'] ) ? (bool) $attrs['frblRewind'] : true; |
| 136 | $buttons = isset( $attrs['frblButtons'] ) ? sanitize_text_field( $attrs['frblButtons'] ) : 'arrows'; |
| 137 | $button_color = isset( $attrs['frblButtonColor'] ) ? sanitize_text_field( $attrs['frblButtonColor'] ) : ''; |
| 138 | $button_bg_color = isset( $attrs['frblButtonBgColor'] ) ? sanitize_text_field( $attrs['frblButtonBgColor'] ) : ''; |
| 139 | $buttons_position = isset( $attrs['frblButtonsPosition'] ) ? sanitize_text_field( $attrs['frblButtonsPosition'] ) : 'side'; |
| 140 | |
| 141 | // Example: Add data attributes to the wrapper div if carousel is enabled. |
| 142 | if ( 'carousel' === $custom_option || 'slider' === $custom_option ) { |
| 143 | $attributes = ''; |
| 144 | if ( 'slider' === $custom_option ) { |
| 145 | $attributes .= ' data-rewind="' . esc_attr( $rewind ) . '"'; |
| 146 | } |
| 147 | // Add data attributes and the 'frontblocks-carousel' class to the first div in the block content. |
| 148 | $block_content = preg_replace( |
| 149 | '/<div([^>]*)class="([^"]*gb-element-[^"]*)"([^>]*)>/', |
| 150 | '<div$1class="$2 frontblocks-carousel"$3' . |
| 151 | ' data-type="' . esc_attr( $custom_option ) . '"' . |
| 152 | ' data-view="' . esc_attr( $items_to_view ) . '"' . |
| 153 | ' data-res-view="' . esc_attr( $responsive_to_view ) . '"' . |
| 154 | ' data-autoplay="' . esc_attr( $autoplay ) . '"' . |
| 155 | ' data-buttons="' . esc_attr( $buttons ) . '"' . |
| 156 | ' data-buttons-color="' . esc_attr( $button_color ) . '"' . |
| 157 | ' data-buttons-background-color="' . esc_attr( $button_bg_color ) . '"' . |
| 158 | ' data-buttons-position="' . esc_attr( $buttons_position ) . '"' . |
| 159 | $attributes . |
| 160 | '>', |
| 161 | $block_content, |
| 162 | 1 // Only replace the first occurrence. |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | return $block_content; |
| 167 | } |
| 168 | |
| 169 | // Registrar atributos antes que GenerateBlocks registre sus bloques. |
| 170 | add_action( |
| 171 | 'init', |
| 172 | function () { |
| 173 | // Usar prioridad 9 (antes del 10 predeterminado). |
| 174 | add_filter( |
| 175 | 'generateblocks_blocks_registered_block', |
| 176 | 'frbl_register_custom_attributes_for_grid_block', |
| 177 | 9, |
| 178 | 2 |
| 179 | ); |
| 180 | |
| 181 | // Registrar atributos desde el lado del frontend también. |
| 182 | add_action( |
| 183 | 'enqueue_block_editor_assets', |
| 184 | function () { |
| 185 | wp_add_inline_script( |
| 186 | 'wp-blocks', |
| 187 | " |
| 188 | wp.hooks.addFilter( |
| 189 | 'blocks.registerBlockType', |
| 190 | 'frontblocks/grid-attributes', |
| 191 | function( settings, name ) { |
| 192 | if ( name !== 'generateblocks/grid' && name !== 'generateblocks/element' ) { |
| 193 | return settings; |
| 194 | } |
| 195 | |
| 196 | settings.attributes = { |
| 197 | ...settings.attributes, |
| 198 | frblGridOption: { |
| 199 | type: 'string', |
| 200 | default: 'none' |
| 201 | }, |
| 202 | frblItemsToView: { |
| 203 | type: 'string', |
| 204 | default: '4' |
| 205 | }, |
| 206 | frblResponsiveToView: { |
| 207 | type: 'string', |
| 208 | default: '1' |
| 209 | }, |
| 210 | frblAutoplay: { |
| 211 | type: 'string', |
| 212 | default: '' |
| 213 | }, |
| 214 | frblButtons: { |
| 215 | type: 'string', |
| 216 | default: 'arrows' |
| 217 | }, |
| 218 | frblButtonsPosition: { |
| 219 | type: 'string', |
| 220 | default: 'side' |
| 221 | }, |
| 222 | frblButtonColor: { |
| 223 | type: 'string', |
| 224 | default: '' |
| 225 | }, |
| 226 | frblButtonBgColor: { |
| 227 | type: 'string', |
| 228 | default: '' |
| 229 | }, |
| 230 | frblRewind: { |
| 231 | type: 'boolean', |
| 232 | default: true |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | return settings; |
| 237 | } |
| 238 | ); |
| 239 | " |
| 240 | ); |
| 241 | } |
| 242 | ); |
| 243 | }, |
| 244 | 5 |
| 245 | ); |
| 246 | |
| 247 | /** |
| 248 | * Register custom attributes for GenerateBlocks Grid and Element blocks. |
| 249 | * |
| 250 | * @param array $block_args The block arguments. |
| 251 | * @param string $block_type The name of the block. |
| 252 | * @return array Modified block arguments. |
| 253 | * @author Closetechnology |
| 254 | */ |
| 255 | function frbl_register_custom_attributes_for_grid_block( $block_args, $block_type ) { |
| 256 | if ( 'generateblocks/grid' !== $block_type && 'generateblocks/element' !== $block_type ) { |
| 257 | return $block_args; |
| 258 | } |
| 259 | |
| 260 | $block_args['attributes']['frblGridOption'] = array( |
| 261 | 'type' => 'string', |
| 262 | 'default' => 'none', |
| 263 | ); |
| 264 | $block_args['attributes']['frblItemsToView'] = array( |
| 265 | 'type' => 'string', |
| 266 | 'default' => '4', |
| 267 | ); |
| 268 | $block_args['attributes']['frblResponsiveToView'] = array( |
| 269 | 'type' => 'string', |
| 270 | 'default' => '1', |
| 271 | ); |
| 272 | $block_args['attributes']['frblAutoplay'] = array( |
| 273 | 'type' => 'string', |
| 274 | 'default' => '', |
| 275 | ); |
| 276 | $block_args['attributes']['frblRewind'] = array( |
| 277 | 'type' => 'boolean', |
| 278 | 'default' => true, |
| 279 | ); |
| 280 | $block_args['attributes']['frblButtons'] = array( |
| 281 | 'type' => 'string', |
| 282 | 'default' => 'arrows', |
| 283 | ); |
| 284 | $block_args['attributes']['frblButtonColor'] = array( |
| 285 | 'type' => 'string', |
| 286 | 'default' => '', |
| 287 | ); |
| 288 | $block_args['attributes']['frblButtonBgColor'] = array( |
| 289 | 'type' => 'string', |
| 290 | 'default' => '', |
| 291 | ); |
| 292 | $block_args['attributes']['frblButtonsPosition'] = array( |
| 293 | 'type' => 'string', |
| 294 | 'default' => 'side', |
| 295 | ); |
| 296 | return $block_args; |
| 297 | } |
| 298 |