frontblocks-gallery-option.jsx
11 months ago
frontblocks-gallery.css
11 months ago
frontblocks-gallery.js
11 months ago
frontblocks-gallery.php
11 months ago
frontblocks-gallery.php
192 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class Gallery |
| 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_gallery_scripts', 99 ); |
| 14 | /** |
| 15 | * Loads Scripts |
| 16 | * |
| 17 | * @return void |
| 18 | */ |
| 19 | function frbl_gallery_scripts() { |
| 20 | $dist_dir = WP_DEBUG ? 'gallery/' : 'dist/'; |
| 21 | wp_enqueue_style( |
| 22 | 'frontblocks-gallery', |
| 23 | FRBL_PLUGIN_URL . 'includes/' . $dist_dir . 'frontblocks-gallery.css', |
| 24 | array(), |
| 25 | FRBL_VERSION |
| 26 | ); |
| 27 | |
| 28 | // Enqueue Masonry library |
| 29 | wp_enqueue_script( |
| 30 | 'frontblocks-masonry', |
| 31 | FRBL_PLUGIN_URL . 'includes/dist/masonry.min.js', |
| 32 | array(), |
| 33 | '4.2.2', |
| 34 | true |
| 35 | ); |
| 36 | |
| 37 | |
| 38 | wp_enqueue_script( |
| 39 | 'frontblocks-gallery-custom', |
| 40 | FRBL_PLUGIN_URL . 'includes/' . $dist_dir . ( WP_DEBUG ? 'frontblocks-gallery.js' : 'frontblocks-gallery-min.js' ), |
| 41 | array( 'frontblocks-masonry' ), |
| 42 | FRBL_VERSION, |
| 43 | true |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | add_action( 'enqueue_block_editor_assets', 'frbl_enqueue_block_editor_assets_gallery' ); |
| 48 | /** |
| 49 | * Enqueue custom block editor script |
| 50 | * |
| 51 | * @return void |
| 52 | */ |
| 53 | function frbl_enqueue_block_editor_assets_gallery() { |
| 54 | wp_enqueue_script( |
| 55 | 'frontblocks-gallery-option', |
| 56 | FRBL_PLUGIN_URL . 'includes/dist/frontblocks-gallery-option.js', |
| 57 | array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-data', 'wp-edit-post' ), |
| 58 | FRBL_VERSION, |
| 59 | true |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | add_filter( 'render_block_core/gallery', 'frbl_add_custom_attributes_to_gallery_block', 10, 2 ); |
| 64 | /** |
| 65 | * Hook to filter the gallery block output on frontend. |
| 66 | * |
| 67 | * @param html $block_content Block content. |
| 68 | * @param array $block Block attributes. |
| 69 | * |
| 70 | * @return html |
| 71 | */ |
| 72 | function frbl_add_custom_attributes_to_gallery_block( $block_content, $block ) { |
| 73 | $attrs = $block['attrs'] ?? array(); |
| 74 | $gallery_layout = isset( $attrs['frblGalleryLayout'] ) ? sanitize_text_field( $attrs['frblGalleryLayout'] ) : 'grid'; |
| 75 | $gutter_size = isset( $attrs['frblGutterSize'] ) ? (int) $attrs['frblGutterSize'] : 20; |
| 76 | $enable_lightbox = isset( $attrs['frblEnableLightbox'] ) ? (bool) $attrs['frblEnableLightbox'] : false; |
| 77 | |
| 78 | // Use WordPress built-in columns setting |
| 79 | $columns = isset( $attrs['columns'] ) ? (int) $attrs['columns'] : 3; |
| 80 | |
| 81 | // Add data attributes to the figure element if masonry is enabled |
| 82 | if ( 'masonry' === $gallery_layout ) { |
| 83 | $block_content = preg_replace( |
| 84 | '/<figure([^>]*)class="([^"]*wp-block-gallery[^"]*)"([^>]*)>/', |
| 85 | '<figure$1class="$2 frontblocks-gallery-masonry"$3' . |
| 86 | ' data-layout="' . esc_attr( $gallery_layout ) . '"' . |
| 87 | ' data-columns="' . esc_attr( $columns ) . '"' . |
| 88 | ' data-gutter="' . esc_attr( $gutter_size ) . '"' . |
| 89 | ' data-lightbox="' . esc_attr( $enable_lightbox ? 'true' : 'false' ) . '"' . |
| 90 | '>', |
| 91 | $block_content, |
| 92 | 1 // Only replace the first occurrence |
| 93 | ); |
| 94 | } elseif ( 'grid' === $gallery_layout ) { |
| 95 | $block_content = preg_replace( |
| 96 | '/<figure([^>]*)class="([^"]*wp-block-gallery[^"]*)"([^>]*)>/', |
| 97 | '<figure$1class="$2 frontblocks-gallery-grid"$3' . |
| 98 | ' data-layout="' . esc_attr( $gallery_layout ) . '"' . |
| 99 | ' data-columns="' . esc_attr( $columns ) . '"' . |
| 100 | ' data-gutter="' . esc_attr( $gutter_size ) . '"' . |
| 101 | ' data-lightbox="' . esc_attr( $enable_lightbox ? 'true' : 'false' ) . '"' . |
| 102 | '>', |
| 103 | $block_content, |
| 104 | 1 // Only replace the first occurrence |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | return $block_content; |
| 109 | } |
| 110 | |
| 111 | // Register attributes before WordPress registers its blocks |
| 112 | add_action( |
| 113 | 'init', |
| 114 | function () { |
| 115 | // Use priority 9 (before the default 10) |
| 116 | add_filter( |
| 117 | 'register_block_type_args', |
| 118 | 'frbl_register_custom_attributes_for_gallery_block', |
| 119 | 9, |
| 120 | 2 |
| 121 | ); |
| 122 | |
| 123 | // Register attributes from the frontend side as well |
| 124 | add_action( |
| 125 | 'enqueue_block_editor_assets', |
| 126 | function () { |
| 127 | wp_add_inline_script( |
| 128 | 'wp-blocks', |
| 129 | " |
| 130 | wp.hooks.addFilter( |
| 131 | 'blocks.registerBlockType', |
| 132 | 'frontblocks/gallery-attributes', |
| 133 | function( settings, name ) { |
| 134 | if ( name !== 'core/gallery' ) { |
| 135 | return settings; |
| 136 | } |
| 137 | |
| 138 | settings.attributes = { |
| 139 | ...settings.attributes, |
| 140 | frblGalleryLayout: { |
| 141 | type: 'string', |
| 142 | default: 'grid' |
| 143 | }, |
| 144 | frblGutterSize: { |
| 145 | type: 'number', |
| 146 | default: 20 |
| 147 | }, |
| 148 | frblEnableLightbox: { |
| 149 | type: 'boolean', |
| 150 | default: false |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | return settings; |
| 155 | } |
| 156 | ); |
| 157 | " |
| 158 | ); |
| 159 | } |
| 160 | ); |
| 161 | }, |
| 162 | 5 |
| 163 | ); |
| 164 | |
| 165 | /** |
| 166 | * Register custom attributes for WordPress Gallery block. |
| 167 | * |
| 168 | * @param array $args The block arguments. |
| 169 | * @param string $name The name of the block. |
| 170 | * @return array Modified block arguments. |
| 171 | */ |
| 172 | function frbl_register_custom_attributes_for_gallery_block( $args, $name ) { |
| 173 | if ( 'core/gallery' !== $name ) { |
| 174 | return $args; |
| 175 | } |
| 176 | |
| 177 | $args['attributes']['frblGalleryLayout'] = array( |
| 178 | 'type' => 'string', |
| 179 | 'default' => 'grid', |
| 180 | ); |
| 181 | $args['attributes']['frblGutterSize'] = array( |
| 182 | 'type' => 'number', |
| 183 | 'default' => 20, |
| 184 | ); |
| 185 | $args['attributes']['frblEnableLightbox'] = array( |
| 186 | 'type' => 'boolean', |
| 187 | 'default' => false, |
| 188 | ); |
| 189 | |
| 190 | return $args; |
| 191 | } |
| 192 |