frontblocks-advanced-option.jsx
11 months ago
frontblocks-carousel.css
11 months ago
frontblocks-carousel.js
11 months ago
frontblocks-carousel.php
11 months ago
frontblocks-carousel.php
239 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 | /** |
| 63 | * Hook to filter the block output on frontend. |
| 64 | * |
| 65 | * @param html $block_content Block content. |
| 66 | * @param array $block Block attributes. |
| 67 | * |
| 68 | * @return html |
| 69 | */ |
| 70 | function frbl_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 | $responsive_to_view = isset( $attrs['frblResponsiveToView'] ) ? (int) $attrs['frblResponsiveToView'] : 1; |
| 75 | $autoplay = isset( $attrs['frblAutoplay'] ) ? ( (int) $attrs['frblAutoplay'] * 1000 ) : ''; |
| 76 | $rewind = isset( $attrs['frblRewind'] ) ? (bool) $attrs['frblRewind'] : true; |
| 77 | $buttons = isset( $attrs['frblButtons'] ) ? sanitize_text_field( $attrs['frblButtons'] ) : 'arrows'; |
| 78 | $button_color = isset( $attrs['frblButtonColor'] ) ? sanitize_text_field( $attrs['frblButtonColor'] ) : ''; |
| 79 | $button_bg_color = isset( $attrs['frblButtonBgColor'] ) ? sanitize_text_field( $attrs['frblButtonBgColor'] ) : ''; |
| 80 | $buttons_position = isset( $attrs['frblButtonsPosition'] ) ? sanitize_text_field( $attrs['frblButtonsPosition'] ) : 'side'; |
| 81 | |
| 82 | // Example: Add data attributes to the wrapper div if carousel is enabled. |
| 83 | if ( 'carousel' === $custom_option || 'slider' === $custom_option ) { |
| 84 | $attributes = ''; |
| 85 | if ( 'slider' === $custom_option ) { |
| 86 | $attributes .= ' data-rewind="' . esc_attr( $rewind ) . '"'; |
| 87 | } |
| 88 | // Add data attributes and the 'frontblocks-carousel' class to the first div in the block content. |
| 89 | $block_content = preg_replace( |
| 90 | '/<div([^>]*)class="([^"]*gb-grid-wrapper[^"]*)"([^>]*)>/', |
| 91 | '<div$1class="$2 frontblocks-carousel"$3' . |
| 92 | ' data-type="' . esc_attr( $custom_option ) . '"' . |
| 93 | ' data-view="' . esc_attr( $items_to_view ) . '"' . |
| 94 | ' data-res-view="' . esc_attr( $responsive_to_view ) . '"' . |
| 95 | ' data-autoplay="' . esc_attr( $autoplay ) . '"' . |
| 96 | ' data-buttons="' . esc_attr( $buttons ) . '"' . |
| 97 | ' data-buttons-color="' . esc_attr( $button_color ) . '"' . |
| 98 | ' data-buttons-background-color="' . esc_attr( $button_bg_color ) . '"' . |
| 99 | ' data-buttons-position="' . esc_attr( $buttons_position ) . '"' . |
| 100 | $attributes . |
| 101 | '>', |
| 102 | $block_content, |
| 103 | 1 // Only replace the first occurrence. |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | return $block_content; |
| 108 | } |
| 109 | |
| 110 | // Registrar atributos antes que GenerateBlocks registre sus bloques. |
| 111 | add_action( |
| 112 | 'init', |
| 113 | function () { |
| 114 | // Usar prioridad 9 (antes del 10 predeterminado). |
| 115 | add_filter( |
| 116 | 'generateblocks_blocks_registered_block', |
| 117 | 'frbl_register_custom_attributes_for_grid_block', |
| 118 | 9, |
| 119 | 2 |
| 120 | ); |
| 121 | |
| 122 | // Registrar atributos desde el lado del frontend también. |
| 123 | add_action( |
| 124 | 'enqueue_block_editor_assets', |
| 125 | function () { |
| 126 | wp_add_inline_script( |
| 127 | 'wp-blocks', |
| 128 | " |
| 129 | wp.hooks.addFilter( |
| 130 | 'blocks.registerBlockType', |
| 131 | 'frontblocks/grid-attributes', |
| 132 | function( settings, name ) { |
| 133 | if ( name !== 'generateblocks/grid' ) { |
| 134 | return settings; |
| 135 | } |
| 136 | |
| 137 | settings.attributes = { |
| 138 | ...settings.attributes, |
| 139 | frblGridOption: { |
| 140 | type: 'string', |
| 141 | default: 'none' |
| 142 | }, |
| 143 | frblItemsToView: { |
| 144 | type: 'string', |
| 145 | default: '4' |
| 146 | }, |
| 147 | frblResponsiveToView: { |
| 148 | type: 'string', |
| 149 | default: '1' |
| 150 | }, |
| 151 | frblAutoplay: { |
| 152 | type: 'string', |
| 153 | default: '' |
| 154 | }, |
| 155 | frblButtons: { |
| 156 | type: 'string', |
| 157 | default: 'arrows' |
| 158 | }, |
| 159 | frblButtonsPosition: { |
| 160 | type: 'string', |
| 161 | default: 'side' |
| 162 | }, |
| 163 | frblButtonColor: { |
| 164 | type: 'string', |
| 165 | default: '' |
| 166 | }, |
| 167 | frblButtonBgColor: { |
| 168 | type: 'string', |
| 169 | default: '' |
| 170 | }, |
| 171 | frblRewind: { |
| 172 | type: 'boolean', |
| 173 | default: true |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | return settings; |
| 178 | } |
| 179 | ); |
| 180 | " |
| 181 | ); |
| 182 | } |
| 183 | ); |
| 184 | }, |
| 185 | 5 |
| 186 | ); |
| 187 | |
| 188 | /** |
| 189 | * Register custom attributes for GenerateBlocks Grid block. |
| 190 | * |
| 191 | * @param array $block_args The block arguments. |
| 192 | * @param string $block_type The name of the block. |
| 193 | * @return array Modified block arguments. |
| 194 | * @author Closetechnology |
| 195 | */ |
| 196 | function frbl_register_custom_attributes_for_grid_block( $block_args, $block_type ) { |
| 197 | if ( 'generateblocks/grid' !== $block_type ) { |
| 198 | return $block_args; |
| 199 | } |
| 200 | |
| 201 | $block_args['attributes']['frblGridOption'] = array( |
| 202 | 'type' => 'string', |
| 203 | 'default' => 'none', |
| 204 | ); |
| 205 | $block_args['attributes']['frblItemsToView'] = array( |
| 206 | 'type' => 'string', |
| 207 | 'default' => '4', |
| 208 | ); |
| 209 | $block_args['attributes']['frblResponsiveToView'] = array( |
| 210 | 'type' => 'string', |
| 211 | 'default' => '1', |
| 212 | ); |
| 213 | $block_args['attributes']['frblAutoplay'] = array( |
| 214 | 'type' => 'string', |
| 215 | 'default' => '', |
| 216 | ); |
| 217 | $block_args['attributes']['frblRewind'] = array( |
| 218 | 'type' => 'boolean', |
| 219 | 'default' => true, |
| 220 | ); |
| 221 | $block_args['attributes']['frblButtons'] = array( |
| 222 | 'type' => 'string', |
| 223 | 'default' => 'arrows', |
| 224 | ); |
| 225 | $block_args['attributes']['frblButtonColor'] = array( |
| 226 | 'type' => 'string', |
| 227 | 'default' => '', |
| 228 | ); |
| 229 | $block_args['attributes']['frblButtonBgColor'] = array( |
| 230 | 'type' => 'string', |
| 231 | 'default' => '', |
| 232 | ); |
| 233 | $block_args['attributes']['frblButtonsPosition'] = array( |
| 234 | 'type' => 'string', |
| 235 | 'default' => 'side', |
| 236 | ); |
| 237 | return $block_args; |
| 238 | } |
| 239 |