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