Animations.php
4 months ago
BackButton.php
7 months ago
BlockPatterns.php
4 months ago
Carousel.php
6 months ago
ContainerEdgeAlignment.php
7 months ago
Counter.php
4 months ago
Events.php
6 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
Gallery.php
251 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Gallery 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 | * Gallery class. |
| 17 | * |
| 18 | * @since 1.0.0 |
| 19 | */ |
| 20 | class Gallery { |
| 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( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 10 ); |
| 36 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
| 37 | add_filter( 'render_block_core/gallery', array( $this, 'add_custom_attributes_to_gallery_block' ), 10, 2 ); |
| 38 | add_action( 'init', array( $this, 'register_custom_attributes' ), 5 ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Enqueue scripts and styles. |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public function enqueue_scripts() { |
| 47 | // Register scripts and styles. |
| 48 | wp_register_style( |
| 49 | 'frontblocks-gallery', |
| 50 | FRBL_PLUGIN_URL . 'assets/gallery/frontblocks-gallery.css', |
| 51 | array(), |
| 52 | FRBL_VERSION |
| 53 | ); |
| 54 | |
| 55 | // Register Masonry library. |
| 56 | wp_register_script( |
| 57 | 'frontblocks-masonry', |
| 58 | FRBL_PLUGIN_URL . 'assets/gallery/masonry.min.js', |
| 59 | array(), |
| 60 | '4.2.2', |
| 61 | true |
| 62 | ); |
| 63 | |
| 64 | wp_register_script( |
| 65 | 'frontblocks-gallery-custom', |
| 66 | FRBL_PLUGIN_URL . 'assets/gallery/frontblocks-gallery.js', |
| 67 | array( 'frontblocks-masonry' ), |
| 68 | FRBL_VERSION, |
| 69 | true |
| 70 | ); |
| 71 | |
| 72 | // Check if we have gallery blocks in the content. |
| 73 | if ( is_admin() || has_block( 'core/gallery' ) ) { |
| 74 | wp_enqueue_style( 'frontblocks-gallery' ); |
| 75 | wp_enqueue_script( 'frontblocks-masonry' ); |
| 76 | wp_enqueue_script( 'frontblocks-gallery-custom' ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Enqueue block editor assets. |
| 82 | * |
| 83 | * @return void |
| 84 | */ |
| 85 | public function enqueue_block_editor_assets() { |
| 86 | wp_enqueue_script( |
| 87 | 'frontblocks-gallery-option', |
| 88 | FRBL_PLUGIN_URL . 'assets/gallery/frontblocks-gallery-option.js', |
| 89 | array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-data', 'wp-edit-post', 'wp-i18n', 'wp-block-editor', 'wp-compose' ), |
| 90 | FRBL_VERSION, |
| 91 | true |
| 92 | ); |
| 93 | |
| 94 | // Set script translations for JavaScript. |
| 95 | wp_set_script_translations( |
| 96 | 'frontblocks-gallery-option', |
| 97 | 'frontblocks' |
| 98 | ); |
| 99 | |
| 100 | // Enqueue gallery styles in editor for live preview. |
| 101 | wp_enqueue_style( |
| 102 | 'frontblocks-gallery', |
| 103 | FRBL_PLUGIN_URL . 'assets/gallery/frontblocks-gallery.css', |
| 104 | array(), |
| 105 | FRBL_VERSION |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Add custom attributes to gallery block. |
| 111 | * |
| 112 | * @param string $block_content Block content. |
| 113 | * @param array $block Block attributes. |
| 114 | * @return string |
| 115 | */ |
| 116 | public function add_custom_attributes_to_gallery_block( $block_content, $block ) { |
| 117 | $attrs = $block['attrs'] ?? array(); |
| 118 | $gallery_layout = isset( $attrs['frblGalleryLayout'] ) ? sanitize_text_field( $attrs['frblGalleryLayout'] ) : 'grid'; |
| 119 | $gutter_size = isset( $attrs['frblGutterSize'] ) ? (int) $attrs['frblGutterSize'] : 20; |
| 120 | $enable_lightbox = isset( $attrs['frblEnableLightbox'] ) ? (bool) $attrs['frblEnableLightbox'] : false; |
| 121 | |
| 122 | // Use WordPress built-in columns setting. |
| 123 | $columns = isset( $attrs['columns'] ) ? (int) $attrs['columns'] : 3; |
| 124 | |
| 125 | // Enqueue scripts when we detect a gallery with custom layout. |
| 126 | if ( 'masonry' === $gallery_layout || 'grid' === $gallery_layout ) { |
| 127 | wp_enqueue_style( 'frontblocks-gallery' ); |
| 128 | wp_enqueue_script( 'frontblocks-masonry' ); |
| 129 | wp_enqueue_script( 'frontblocks-gallery-custom' ); |
| 130 | } |
| 131 | |
| 132 | // Add data attributes to the figure element if masonry is enabled. |
| 133 | if ( 'masonry' === $gallery_layout ) { |
| 134 | $block_content = preg_replace( |
| 135 | '/<figure([^>]*)class="([^"]*wp-block-gallery[^"]*)"([^>]*)>/', |
| 136 | '<figure$1class="$2 frontblocks-gallery-masonry"$3' . |
| 137 | ' data-layout="' . esc_attr( $gallery_layout ) . '"' . |
| 138 | ' data-columns="' . esc_attr( $columns ) . '"' . |
| 139 | ' data-gutter="' . esc_attr( $gutter_size ) . '"' . |
| 140 | ' data-lightbox="' . esc_attr( $enable_lightbox ? 'true' : 'false' ) . '"' . |
| 141 | '>', |
| 142 | $block_content, |
| 143 | 1 // Only replace the first occurrence. |
| 144 | ); |
| 145 | } elseif ( 'grid' === $gallery_layout ) { |
| 146 | $block_content = preg_replace( |
| 147 | '/<figure([^>]*)class="([^"]*wp-block-gallery[^"]*)"([^>]*)>/', |
| 148 | '<figure$1class="$2 frontblocks-gallery-grid"$3' . |
| 149 | ' data-layout="' . esc_attr( $gallery_layout ) . '"' . |
| 150 | ' data-columns="' . esc_attr( $columns ) . '"' . |
| 151 | ' data-gutter="' . esc_attr( $gutter_size ) . '"' . |
| 152 | ' data-lightbox="' . esc_attr( $enable_lightbox ? 'true' : 'false' ) . '"' . |
| 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 WordPress registers its blocks. |
| 169 | add_filter( |
| 170 | 'register_block_type_args', |
| 171 | array( $this, 'register_custom_attributes_for_gallery_block' ), |
| 172 | 9, |
| 173 | 2 |
| 174 | ); |
| 175 | |
| 176 | // Register attributes from the frontend side as well. |
| 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 WordPress Gallery block. |
| 185 | * |
| 186 | * @param array $args The block arguments. |
| 187 | * @param string $name The name of the block. |
| 188 | * @return array Modified block arguments. |
| 189 | */ |
| 190 | public function register_custom_attributes_for_gallery_block( $args, $name ) { |
| 191 | if ( 'core/gallery' !== $name ) { |
| 192 | return $args; |
| 193 | } |
| 194 | |
| 195 | $args['attributes']['frblGalleryLayout'] = array( |
| 196 | 'type' => 'string', |
| 197 | 'default' => 'grid', |
| 198 | ); |
| 199 | $args['attributes']['frblGutterSize'] = array( |
| 200 | 'type' => 'number', |
| 201 | 'default' => 20, |
| 202 | ); |
| 203 | $args['attributes']['frblEnableLightbox'] = array( |
| 204 | 'type' => 'boolean', |
| 205 | 'default' => false, |
| 206 | ); |
| 207 | |
| 208 | return $args; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Add inline script for block attributes. |
| 213 | * |
| 214 | * @return void |
| 215 | */ |
| 216 | public function add_inline_script_for_attributes() { |
| 217 | wp_add_inline_script( |
| 218 | 'wp-blocks', |
| 219 | " |
| 220 | wp.hooks.addFilter( |
| 221 | 'blocks.registerBlockType', |
| 222 | 'frontblocks/gallery-attributes', |
| 223 | function( settings, name ) { |
| 224 | if ( name !== 'core/gallery' ) { |
| 225 | return settings; |
| 226 | } |
| 227 | |
| 228 | settings.attributes = { |
| 229 | ...settings.attributes, |
| 230 | frblGalleryLayout: { |
| 231 | type: 'string', |
| 232 | default: 'grid' |
| 233 | }, |
| 234 | frblGutterSize: { |
| 235 | type: 'number', |
| 236 | default: 20 |
| 237 | }, |
| 238 | frblEnableLightbox: { |
| 239 | type: 'boolean', |
| 240 | default: false |
| 241 | } |
| 242 | }; |
| 243 | |
| 244 | return settings; |
| 245 | } |
| 246 | ); |
| 247 | " |
| 248 | ); |
| 249 | } |
| 250 | } |
| 251 |