Animations.php
8 months ago
Carousel.php
8 months ago
Counter.php
8 months ago
Gallery.php
8 months ago
Headline.php
8 months ago
InsertPost.php
8 months ago
ProductCategories.php
8 months ago
StickyColumn.php
8 months ago
Testimonials.php
8 months ago
Gallery.php
237 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' ), |
| 90 | FRBL_VERSION, |
| 91 | true |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Add custom attributes to gallery block. |
| 97 | * |
| 98 | * @param string $block_content Block content. |
| 99 | * @param array $block Block attributes. |
| 100 | * @return string |
| 101 | */ |
| 102 | public function add_custom_attributes_to_gallery_block( $block_content, $block ) { |
| 103 | $attrs = $block['attrs'] ?? array(); |
| 104 | $gallery_layout = isset( $attrs['frblGalleryLayout'] ) ? sanitize_text_field( $attrs['frblGalleryLayout'] ) : 'grid'; |
| 105 | $gutter_size = isset( $attrs['frblGutterSize'] ) ? (int) $attrs['frblGutterSize'] : 20; |
| 106 | $enable_lightbox = isset( $attrs['frblEnableLightbox'] ) ? (bool) $attrs['frblEnableLightbox'] : false; |
| 107 | |
| 108 | // Use WordPress built-in columns setting. |
| 109 | $columns = isset( $attrs['columns'] ) ? (int) $attrs['columns'] : 3; |
| 110 | |
| 111 | // Enqueue scripts when we detect a gallery with custom layout. |
| 112 | if ( 'masonry' === $gallery_layout || 'grid' === $gallery_layout ) { |
| 113 | wp_enqueue_style( 'frontblocks-gallery' ); |
| 114 | wp_enqueue_script( 'frontblocks-masonry' ); |
| 115 | wp_enqueue_script( 'frontblocks-gallery-custom' ); |
| 116 | } |
| 117 | |
| 118 | // Add data attributes to the figure element if masonry is enabled. |
| 119 | if ( 'masonry' === $gallery_layout ) { |
| 120 | $block_content = preg_replace( |
| 121 | '/<figure([^>]*)class="([^"]*wp-block-gallery[^"]*)"([^>]*)>/', |
| 122 | '<figure$1class="$2 frontblocks-gallery-masonry"$3' . |
| 123 | ' data-layout="' . esc_attr( $gallery_layout ) . '"' . |
| 124 | ' data-columns="' . esc_attr( $columns ) . '"' . |
| 125 | ' data-gutter="' . esc_attr( $gutter_size ) . '"' . |
| 126 | ' data-lightbox="' . esc_attr( $enable_lightbox ? 'true' : 'false' ) . '"' . |
| 127 | '>', |
| 128 | $block_content, |
| 129 | 1 // Only replace the first occurrence. |
| 130 | ); |
| 131 | } elseif ( 'grid' === $gallery_layout ) { |
| 132 | $block_content = preg_replace( |
| 133 | '/<figure([^>]*)class="([^"]*wp-block-gallery[^"]*)"([^>]*)>/', |
| 134 | '<figure$1class="$2 frontblocks-gallery-grid"$3' . |
| 135 | ' data-layout="' . esc_attr( $gallery_layout ) . '"' . |
| 136 | ' data-columns="' . esc_attr( $columns ) . '"' . |
| 137 | ' data-gutter="' . esc_attr( $gutter_size ) . '"' . |
| 138 | ' data-lightbox="' . esc_attr( $enable_lightbox ? 'true' : 'false' ) . '"' . |
| 139 | '>', |
| 140 | $block_content, |
| 141 | 1 // Only replace the first occurrence. |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | return $block_content; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Register custom attributes for blocks. |
| 150 | * |
| 151 | * @return void |
| 152 | */ |
| 153 | public function register_custom_attributes() { |
| 154 | // Register attributes before WordPress registers its blocks. |
| 155 | add_filter( |
| 156 | 'register_block_type_args', |
| 157 | array( $this, 'register_custom_attributes_for_gallery_block' ), |
| 158 | 9, |
| 159 | 2 |
| 160 | ); |
| 161 | |
| 162 | // Register attributes from the frontend side as well. |
| 163 | add_action( |
| 164 | 'enqueue_block_editor_assets', |
| 165 | array( $this, 'add_inline_script_for_attributes' ) |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Register custom attributes for WordPress Gallery block. |
| 171 | * |
| 172 | * @param array $args The block arguments. |
| 173 | * @param string $name The name of the block. |
| 174 | * @return array Modified block arguments. |
| 175 | */ |
| 176 | public function register_custom_attributes_for_gallery_block( $args, $name ) { |
| 177 | if ( 'core/gallery' !== $name ) { |
| 178 | return $args; |
| 179 | } |
| 180 | |
| 181 | $args['attributes']['frblGalleryLayout'] = array( |
| 182 | 'type' => 'string', |
| 183 | 'default' => 'grid', |
| 184 | ); |
| 185 | $args['attributes']['frblGutterSize'] = array( |
| 186 | 'type' => 'number', |
| 187 | 'default' => 20, |
| 188 | ); |
| 189 | $args['attributes']['frblEnableLightbox'] = array( |
| 190 | 'type' => 'boolean', |
| 191 | 'default' => false, |
| 192 | ); |
| 193 | |
| 194 | return $args; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Add inline script for block attributes. |
| 199 | * |
| 200 | * @return void |
| 201 | */ |
| 202 | public function add_inline_script_for_attributes() { |
| 203 | wp_add_inline_script( |
| 204 | 'wp-blocks', |
| 205 | " |
| 206 | wp.hooks.addFilter( |
| 207 | 'blocks.registerBlockType', |
| 208 | 'frontblocks/gallery-attributes', |
| 209 | function( settings, name ) { |
| 210 | if ( name !== 'core/gallery' ) { |
| 211 | return settings; |
| 212 | } |
| 213 | |
| 214 | settings.attributes = { |
| 215 | ...settings.attributes, |
| 216 | frblGalleryLayout: { |
| 217 | type: 'string', |
| 218 | default: 'grid' |
| 219 | }, |
| 220 | frblGutterSize: { |
| 221 | type: 'number', |
| 222 | default: 20 |
| 223 | }, |
| 224 | frblEnableLightbox: { |
| 225 | type: 'boolean', |
| 226 | default: false |
| 227 | } |
| 228 | }; |
| 229 | |
| 230 | return settings; |
| 231 | } |
| 232 | ); |
| 233 | " |
| 234 | ); |
| 235 | } |
| 236 | } |
| 237 |