PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.2.0
FrontBlocks for Gutenberg/GeneratePress v1.2.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 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 ReadingTime.php 8 months ago StickyColumn.php 8 months ago Testimonials.php 8 months ago
Animations.php
268 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 };
161 } catch( error ) {
162 return settings;
163 }
164
165 return settings;
166 }
167 );
168 "
169 );
170 }
171
172 /**
173 * Add animation classes to blocks on frontend render.
174 *
175 * @param string $block_content Block content.
176 * @param array $block Block data.
177 * @return string Modified block content.
178 */
179 public function add_animation_classes_to_blocks( $block_content, $block ) {
180 if ( empty( $block['attrs'] ) ) {
181 return $block_content;
182 }
183
184 $attrs = $block['attrs'];
185
186 if ( ! isset( $attrs['frblAnimation'] ) || empty( $attrs['frblAnimation'] ) ) {
187 return $block_content;
188 }
189
190 $properties = array();
191 $animation = $attrs['frblAnimation'];
192 $properties['animation'] = $animation;
193 $delay = isset( $attrs['frblAnimationDelay'] ) ? $attrs['frblAnimationDelay'] : 0;
194 $properties['delay'] = $delay;
195 $duration = isset( $attrs['frblAnimationDuration'] ) ? $attrs['frblAnimationDuration'] : 1;
196 $properties['duration'] = $duration;
197 $repeat = isset( $attrs['frblAnimationRepeat'] ) ? $attrs['frblAnimationRepeat'] : false;
198 $properties['repeat'] = $repeat;
199 $infinite_repeat = isset( $attrs['frblAnimationInfinite'] ) ? $attrs['frblAnimationInfinite'] : false;
200 $properties['infinite_repeat'] = $infinite_repeat;
201 $disable_mobile = isset( $attrs['frblDisableAnimationMobile'] ) ? $attrs['frblDisableAnimationMobile'] : false;
202 $properties['disable_mobile'] = $disable_mobile;
203
204 // Build style attributes.
205 $style_attr = '';
206 if ( $delay > 0 ) {
207 $style_attr .= '--animate-delay:' . esc_attr( $delay ) . 's;';
208 }
209
210 if ( 1 !== $duration ) {
211 $style_attr .= '--animate-duration:' . esc_attr( $duration ) . 's;';
212 }
213
214 if ( $infinite_repeat ) {
215 $style_attr .= '--animate-repeat:infinite;';
216 } elseif ( $repeat ) {
217 $style_attr .= '--animate-repeat:2;';
218 }
219
220 // Add animation classes and styles to the first HTML tag.
221 $block_content = preg_replace_callback(
222 '/^<([a-z][a-z0-9]*)\s*((?:[^>]|\\n)*?)(?:style="([^"]*?)")?([^>]*?)>/i',
223 function ( $matches ) use ( $properties, $style_attr ) {
224 $tag = $matches[1] ?? 'div';
225 $beginning = $matches[2] ?? '';
226 $existing_style = $matches[3] ?? '';
227 $ending = $matches[4] ?? '';
228
229 $classes = 'animate__animated animate__' . esc_attr( $properties['animation'] );
230
231 if ( $properties['disable_mobile'] ) {
232 $classes .= ' frbl-no-mobile-animation';
233 }
234
235 // Add classes to existing class attribute or create new one.
236 if ( strpos( $beginning . $ending, 'class="' ) !== false ) {
237 $beginning = preg_replace(
238 '/class="([^"]*)"/',
239 'class="$1 ' . $classes . '"',
240 $beginning . $ending,
241 1
242 );
243 } else {
244 $beginning .= ' class="' . $classes . '"';
245 }
246
247 $beginning .= ' data-frontblocks-animation="' . esc_attr( $properties['animation'] ) . '"';
248 $beginning .= ' data-frontblocks-animation-delay="' . esc_attr( $properties['delay'] ) . '"';
249 $beginning .= ' data-frontblocks-animation-duration="' . esc_attr( $properties['duration'] ) . '"';
250 $beginning .= ' data-frontblocks-animation-repeat="' . esc_attr( $properties['repeat'] ) . '"';
251 $beginning .= ' data-frontblocks-animation-infinite="' . esc_attr( $properties['infinite_repeat'] ) . '"';
252
253 // Add styles if needed.
254 if ( ! empty( $style_attr ) ) {
255 $combined_style = $existing_style . ( ! empty( $existing_style ) ? ';' : '' ) . $style_attr;
256 return '<' . $tag . ' ' . $beginning . ' style="' . $combined_style . '">';
257 }
258
259 return '<' . $tag . ' ' . $beginning . '>';
260 },
261 $block_content,
262 1
263 );
264
265 return $block_content;
266 }
267 }
268