PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.3.0
FrontBlocks for Gutenberg/GeneratePress v1.3.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 / Animations.php
frontblocks / includes / Frontend Last commit date
Animations.php 7 months ago BackButton.php 7 months ago Carousel.php 8 months ago ContainerEdgeAlignment.php 7 months ago Counter.php 8 months ago Gallery.php 8 months ago GravityFormsInline.php 7 months ago Headline.php 8 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 StickyColumn.php 8 months ago Testimonials.php 8 months ago
Animations.php
321 lines
1 <?php
2 /**
3 * Animations 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 * Animations class.
17 *
18 * @since 1.0.0
19 */
20 class Animations {
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' ), 100 );
36 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ), 5 );
37 add_action( 'enqueue_block_editor_assets', array( $this, 'register_animation_attributes' ), 15 );
38 add_filter( 'render_block', array( $this, 'add_animation_classes_to_blocks' ), 10, 2 );
39 }
40
41 /**
42 * Enqueue scripts and styles.
43 *
44 * @return void
45 */
46 public function enqueue_scripts() {
47 // Enqueue custom animations CSS.
48 wp_enqueue_style(
49 'frontblocks-animations',
50 FRBL_PLUGIN_URL . 'assets/animations/frontblocks-animations.css',
51 array(),
52 FRBL_VERSION
53 );
54
55 wp_enqueue_script(
56 'frontblocks-animations-custom',
57 FRBL_PLUGIN_URL . 'assets/animations/frontblocks-animations.js',
58 array(),
59 FRBL_VERSION,
60 true
61 );
62 }
63
64 /**
65 * Enqueue block editor assets.
66 *
67 * @return void
68 */
69 public function enqueue_block_editor_assets() {
70 // Enqueue custom animations CSS for editor.
71 wp_enqueue_style(
72 'frontblocks-animations-editor',
73 FRBL_PLUGIN_URL . 'assets/animations/frontblocks-animations.css',
74 array(),
75 FRBL_VERSION
76 );
77
78 // Enqueue custom block editor script.
79 wp_enqueue_script(
80 'frontblocks-animation-editor',
81 FRBL_PLUGIN_URL . 'assets/animations/frontblocks-animation-option.js',
82 array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-i18n' ),
83 FRBL_VERSION,
84 true
85 );
86
87 // Set script translations for JavaScript.
88 wp_set_script_translations(
89 'frontblocks-animation-editor',
90 'frontblocks'
91 );
92
93 // Localize script with custom CSS URL.
94 wp_localize_script(
95 'frontblocks-animation-editor',
96 'frontblocksAnimationData',
97 array(
98 'customCss' => FRBL_PLUGIN_URL . 'assets/animations/frontblocks-animations.css',
99 )
100 );
101 }
102
103 /**
104 * Register animation attributes for all blocks.
105 *
106 * @return void
107 */
108 public function register_animation_attributes() {
109 // This script runs on the client side to add animation attributes to all blocks.
110 wp_add_inline_script(
111 'wp-blocks',
112 "
113 wp.hooks.addFilter(
114 'blocks.registerBlockType',
115 'frontblocks/animation-attributes',
116 function( settings, name ) {
117 // Exclude Gravity Forms blocks from animation attributes.
118 if ( name && name.indexOf('gravityforms/') === 0 ) {
119 return settings;
120 }
121
122 // Defensive check for settings object.
123 if ( ! settings || typeof settings !== 'object' ) {
124 return settings;
125 }
126
127 // Ensure attributes property exists and is an object.
128 if ( ! settings.attributes || typeof settings.attributes !== 'object' || Array.isArray( settings.attributes ) ) {
129 settings.attributes = {};
130 }
131
132 // Use spread operator for safer attribute merging.
133 try {
134 settings.attributes = {
135 ...settings.attributes,
136 frblAnimation: {
137 type: 'string',
138 default: ''
139 },
140 frblAnimationDelay: {
141 type: 'number',
142 default: 0
143 },
144 frblAnimationDuration: {
145 type: 'number',
146 default: 1
147 },
148 frblAnimationRepeat: {
149 type: 'boolean',
150 default: false
151 },
152 frblAnimationInfinite: {
153 type: 'boolean',
154 default: false
155 },
156 frblDisableAnimationMobile: {
157 type: 'boolean',
158 default: false
159 },
160 frblGlassEffect: {
161 type: 'boolean',
162 default: false
163 },
164 frblGlassBlur: {
165 type: 'number',
166 default: 10
167 }
168 };
169 } catch( error ) {
170 return settings;
171 }
172
173 return settings;
174 }
175 );
176 "
177 );
178 }
179
180 /**
181 * Add animation classes to blocks on frontend render.
182 *
183 * @param string $block_content Block content.
184 * @param array $block Block data.
185 * @return string Modified block content.
186 */
187 public function add_animation_classes_to_blocks( $block_content, $block ) {
188 if ( empty( $block['attrs'] ) ) {
189 return $block_content;
190 }
191
192 $attrs = $block['attrs'];
193
194 // Check if either animation or glass effect is set.
195 $has_animation = isset( $attrs['frblAnimation'] ) && ! empty( $attrs['frblAnimation'] );
196 $has_glass_effect = isset( $attrs['frblGlassEffect'] ) && $attrs['frblGlassEffect'];
197
198 if ( ! $has_animation && ! $has_glass_effect ) {
199 return $block_content;
200 }
201
202 $properties = array();
203
204 // Animation properties.
205 if ( $has_animation ) {
206 $animation = $attrs['frblAnimation'];
207 $properties['animation'] = $animation;
208 $delay = isset( $attrs['frblAnimationDelay'] ) ? $attrs['frblAnimationDelay'] : 0;
209 $properties['delay'] = $delay;
210 $duration = isset( $attrs['frblAnimationDuration'] ) ? $attrs['frblAnimationDuration'] : 1;
211 $properties['duration'] = $duration;
212 $repeat = isset( $attrs['frblAnimationRepeat'] ) ? $attrs['frblAnimationRepeat'] : false;
213 $properties['repeat'] = $repeat;
214 $infinite_repeat = isset( $attrs['frblAnimationInfinite'] ) ? $attrs['frblAnimationInfinite'] : false;
215 $properties['infinite_repeat'] = $infinite_repeat;
216 $disable_mobile = isset( $attrs['frblDisableAnimationMobile'] ) ? $attrs['frblDisableAnimationMobile'] : false;
217 $properties['disable_mobile'] = $disable_mobile;
218 }
219
220 // Glass effect properties.
221 if ( $has_glass_effect ) {
222 $properties['glass_effect'] = true;
223 $properties['glass_blur'] = isset( $attrs['frblGlassBlur'] ) ? $attrs['frblGlassBlur'] : 10;
224 }
225
226 // Build style attributes.
227 $style_attr = '';
228
229 // Animation styles.
230 if ( $has_animation ) {
231 if ( $properties['delay'] > 0 ) {
232 $style_attr .= '--animate-delay:' . esc_attr( $properties['delay'] ) . 's;';
233 }
234
235 if ( 1 !== $properties['duration'] ) {
236 $style_attr .= '--animate-duration:' . esc_attr( $properties['duration'] ) . 's;';
237 }
238
239 if ( $properties['infinite_repeat'] ) {
240 $style_attr .= '--animate-repeat:infinite;';
241 } elseif ( $properties['repeat'] ) {
242 $style_attr .= '--animate-repeat:2;';
243 }
244 }
245
246 // Glass effect styles.
247 if ( $has_glass_effect ) {
248 $blur_value = $properties['glass_blur'];
249 $style_attr .= 'backdrop-filter:blur(' . esc_attr( $blur_value ) . 'px);';
250 $style_attr .= '-webkit-backdrop-filter:blur(' . esc_attr( $blur_value ) . 'px);';
251 }
252
253 // Add animation classes and styles to the first HTML tag.
254 $block_content = preg_replace_callback(
255 '/^<([a-z][a-z0-9]*)\s*((?:[^>]|\\n)*?)(?:style="([^"]*?)")?([^>]*?)>/i',
256 function ( $matches ) use ( $properties, $style_attr, $has_animation, $has_glass_effect ) {
257 $tag = $matches[1] ?? 'div';
258 $beginning = $matches[2] ?? '';
259 $existing_style = $matches[3] ?? '';
260 $ending = $matches[4] ?? '';
261
262 $classes = '';
263
264 // Add animation classes.
265 if ( $has_animation ) {
266 $classes = 'animate__animated animate__' . esc_attr( $properties['animation'] );
267
268 if ( isset( $properties['disable_mobile'] ) && $properties['disable_mobile'] ) {
269 $classes .= ' frbl-no-mobile-animation';
270 }
271 }
272
273 // Add glass effect class.
274 if ( $has_glass_effect ) {
275 $classes .= ( ! empty( $classes ) ? ' ' : '' ) . 'frbl-glass-effect';
276 }
277
278 // Add classes to existing class attribute or create new one.
279 if ( ! empty( $classes ) ) {
280 if ( strpos( $beginning . $ending, 'class="' ) !== false ) {
281 $beginning = preg_replace(
282 '/class="([^"]*)"/',
283 'class="$1 ' . $classes . '"',
284 $beginning . $ending,
285 1
286 );
287 } else {
288 $beginning .= ' class="' . $classes . '"';
289 }
290 }
291
292 // Add animation data attributes.
293 if ( $has_animation ) {
294 $beginning .= ' data-frontblocks-animation="' . esc_attr( $properties['animation'] ) . '"';
295 $beginning .= ' data-frontblocks-animation-delay="' . esc_attr( $properties['delay'] ) . '"';
296 $beginning .= ' data-frontblocks-animation-duration="' . esc_attr( $properties['duration'] ) . '"';
297 $beginning .= ' data-frontblocks-animation-repeat="' . esc_attr( $properties['repeat'] ) . '"';
298 $beginning .= ' data-frontblocks-animation-infinite="' . esc_attr( $properties['infinite_repeat'] ) . '"';
299 }
300
301 // Add glass effect data attributes.
302 if ( $has_glass_effect ) {
303 $beginning .= ' data-frontblocks-glass-blur="' . esc_attr( $properties['glass_blur'] ) . '"';
304 }
305
306 // Add styles if needed.
307 if ( ! empty( $style_attr ) ) {
308 $combined_style = $existing_style . ( ! empty( $existing_style ) ? ';' : '' ) . $style_attr;
309 return '<' . $tag . ' ' . $beginning . ' style="' . $combined_style . '">';
310 }
311
312 return '<' . $tag . ' ' . $beginning . '>';
313 },
314 $block_content,
315 1
316 );
317
318 return $block_content;
319 }
320 }
321