PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.5.0
FrontBlocks for Gutenberg/GeneratePress v1.5.0
1.5.2 1.5.1 1.4.0 1.5.0 trunk 0.2.0 0.2.1 0.2.2 0.2.3 0.2.4 0.2.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 ci-artifacts
frontblocks / includes / Frontend / Gallery.php
frontblocks / includes / Frontend Last commit date
Animations.php 3 months ago BackButton.php 9 months ago BeforeAfter.php 3 months ago BlockPatterns.php 6 months ago Carousel.php 2 months ago ColumnsSameHeight.php 2 months ago ContainerEdgeAlignment.php 1 week ago CookieNotice.php 1 week ago Counter.php 3 months ago DownloadButton.php 2 months ago Events.php 2 months ago FaqSchema.php 2 months ago FluidTypography.php 2 months ago Gallery.php 1 week ago GravityFormsInline.php 3 months ago Headline.php 2 months ago InsertPost.php 3 months ago Maintenance.php 1 week ago ProductCategories.php 2 months ago ReadingProgress.php 9 months ago ReadingTime.php 1 week ago ScrollTop.php 1 week ago ShapeAnimations.php 1 week ago StackedImages.php 6 months ago StickyColumn.php 2 months ago SvgUpload.php 2 months ago Testimonials.php 10 months ago TextAnimation.php 3 months ago UserText.php 2 months ago
Gallery.php
270 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_editor_script' ) );
37 add_action( 'enqueue_block_assets', array( $this, 'enqueue_editor_style' ) );
38 add_filter( 'render_block_core/gallery', array( $this, 'add_custom_attributes_to_gallery_block' ), 10, 2 );
39 add_action( 'init', array( $this, 'register_custom_attributes' ), 5 );
40 }
41
42 /**
43 * Enqueue scripts and styles.
44 *
45 * @return void
46 */
47 public function enqueue_scripts() {
48 // Register scripts and styles.
49 wp_register_style(
50 'frontblocks-gallery',
51 FRBL_PLUGIN_URL . 'assets/gallery/frontblocks-gallery.css',
52 array(),
53 FRBL_VERSION
54 );
55
56 // Register Masonry library.
57 wp_register_script(
58 'frontblocks-masonry',
59 FRBL_PLUGIN_URL . 'assets/gallery/masonry.min.js',
60 array(),
61 '4.2.2',
62 true
63 );
64
65 wp_register_script(
66 'frontblocks-gallery-custom',
67 FRBL_PLUGIN_URL . 'assets/gallery/frontblocks-gallery.js',
68 array( 'frontblocks-masonry' ),
69 FRBL_VERSION,
70 true
71 );
72
73 // Check if we have gallery blocks in the content.
74 if ( is_admin() || has_block( 'core/gallery' ) ) {
75 wp_enqueue_style( 'frontblocks-gallery' );
76 wp_enqueue_script( 'frontblocks-masonry' );
77 wp_enqueue_script( 'frontblocks-gallery-custom' );
78 }
79 }
80
81 /**
82 * Enqueue the block editor's own script.
83 *
84 * @return void
85 */
86 public function enqueue_editor_script() {
87 wp_enqueue_script(
88 'frontblocks-gallery-option',
89 FRBL_PLUGIN_URL . 'assets/gallery/frontblocks-gallery-option.js',
90 array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-data', 'wp-edit-post', 'wp-i18n', 'wp-block-editor', 'wp-compose' ),
91 FRBL_VERSION,
92 true
93 );
94
95 // Set script translations for JavaScript.
96 wp_set_script_translations(
97 'frontblocks-gallery-option',
98 'frontblocks'
99 );
100 }
101
102 /**
103 * Enqueue the gallery style on both the frontend and the block editor
104 * (including inside its iframed canvas).
105 *
106 * Hooked on enqueue_block_assets rather than enqueue_block_editor_assets:
107 * the editor canvas is rendered in an iframe, and a style enqueued via the
108 * editor-only hook is appended to the wp-admin document instead of that
109 * iframe, which WordPress now flags as an incorrect registration.
110 *
111 * @return void
112 */
113 public function enqueue_editor_style() {
114 // Frontend enqueueing is handled conditionally in enqueue_scripts() and
115 // add_custom_attributes_to_gallery_block().
116 if ( ! is_admin() ) {
117 return;
118 }
119
120 wp_enqueue_style(
121 'frontblocks-gallery',
122 FRBL_PLUGIN_URL . 'assets/gallery/frontblocks-gallery.css',
123 array(),
124 FRBL_VERSION
125 );
126 }
127
128 /**
129 * Add custom attributes to gallery block.
130 *
131 * @param string $block_content Block content.
132 * @param array $block Block attributes.
133 * @return string
134 */
135 public function add_custom_attributes_to_gallery_block( $block_content, $block ) {
136 $attrs = $block['attrs'] ?? array();
137 $gallery_layout = isset( $attrs['frblGalleryLayout'] ) ? sanitize_text_field( $attrs['frblGalleryLayout'] ) : 'grid';
138 $gutter_size = isset( $attrs['frblGutterSize'] ) ? (int) $attrs['frblGutterSize'] : 20;
139 $enable_lightbox = isset( $attrs['frblEnableLightbox'] ) ? (bool) $attrs['frblEnableLightbox'] : false;
140
141 // Use WordPress built-in columns setting.
142 $columns = isset( $attrs['columns'] ) ? (int) $attrs['columns'] : 3;
143
144 // Enqueue scripts when we detect a gallery with custom layout.
145 if ( 'masonry' === $gallery_layout || 'grid' === $gallery_layout ) {
146 wp_enqueue_style( 'frontblocks-gallery' );
147 wp_enqueue_script( 'frontblocks-masonry' );
148 wp_enqueue_script( 'frontblocks-gallery-custom' );
149 }
150
151 // Add data attributes to the figure element if masonry is enabled.
152 if ( 'masonry' === $gallery_layout ) {
153 $block_content = preg_replace(
154 '/<figure([^>]*)class="([^"]*wp-block-gallery[^"]*)"([^>]*)>/',
155 '<figure$1class="$2 frontblocks-gallery-masonry"$3' .
156 ' data-layout="' . esc_attr( $gallery_layout ) . '"' .
157 ' data-columns="' . esc_attr( $columns ) . '"' .
158 ' data-gutter="' . esc_attr( $gutter_size ) . '"' .
159 ' data-lightbox="' . esc_attr( $enable_lightbox ? 'true' : 'false' ) . '"' .
160 '>',
161 $block_content,
162 1 // Only replace the first occurrence.
163 );
164 } elseif ( 'grid' === $gallery_layout ) {
165 $block_content = preg_replace(
166 '/<figure([^>]*)class="([^"]*wp-block-gallery[^"]*)"([^>]*)>/',
167 '<figure$1class="$2 frontblocks-gallery-grid"$3' .
168 ' data-layout="' . esc_attr( $gallery_layout ) . '"' .
169 ' data-columns="' . esc_attr( $columns ) . '"' .
170 ' data-gutter="' . esc_attr( $gutter_size ) . '"' .
171 ' data-lightbox="' . esc_attr( $enable_lightbox ? 'true' : 'false' ) . '"' .
172 '>',
173 $block_content,
174 1 // Only replace the first occurrence.
175 );
176 }
177
178 return $block_content;
179 }
180
181 /**
182 * Register custom attributes for blocks.
183 *
184 * @return void
185 */
186 public function register_custom_attributes() {
187 // Register attributes before WordPress registers its blocks.
188 add_filter(
189 'register_block_type_args',
190 array( $this, 'register_custom_attributes_for_gallery_block' ),
191 9,
192 2
193 );
194
195 // Register attributes from the frontend side as well.
196 add_action(
197 'enqueue_block_editor_assets',
198 array( $this, 'add_inline_script_for_attributes' )
199 );
200 }
201
202 /**
203 * Register custom attributes for WordPress Gallery block.
204 *
205 * @param array $args The block arguments.
206 * @param string $name The name of the block.
207 * @return array Modified block arguments.
208 */
209 public function register_custom_attributes_for_gallery_block( $args, $name ) {
210 if ( 'core/gallery' !== $name ) {
211 return $args;
212 }
213
214 $args['attributes']['frblGalleryLayout'] = array(
215 'type' => 'string',
216 'default' => 'grid',
217 );
218 $args['attributes']['frblGutterSize'] = array(
219 'type' => 'number',
220 'default' => 20,
221 );
222 $args['attributes']['frblEnableLightbox'] = array(
223 'type' => 'boolean',
224 'default' => false,
225 );
226
227 return $args;
228 }
229
230 /**
231 * Add inline script for block attributes.
232 *
233 * @return void
234 */
235 public function add_inline_script_for_attributes() {
236 wp_add_inline_script(
237 'wp-blocks',
238 "
239 wp.hooks.addFilter(
240 'blocks.registerBlockType',
241 'frontblocks/gallery-attributes',
242 function( settings, name ) {
243 if ( name !== 'core/gallery' ) {
244 return settings;
245 }
246
247 settings.attributes = {
248 ...settings.attributes,
249 frblGalleryLayout: {
250 type: 'string',
251 default: 'grid'
252 },
253 frblGutterSize: {
254 type: 'number',
255 default: 20
256 },
257 frblEnableLightbox: {
258 type: 'boolean',
259 default: false
260 }
261 };
262
263 return settings;
264 }
265 );
266 "
267 );
268 }
269 }
270