PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.0.1
FrontBlocks for Gutenberg/GeneratePress v1.0.1
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 / animations / frontblocks-animations.php
frontblocks / includes / animations Last commit date
frontblocks-animation-option.jsx 11 months ago frontblocks-animations.css 11 months ago frontblocks-animations.js 11 months ago frontblocks-animations.php 11 months ago
frontblocks-animations.php
201 lines
1 <?php
2 /**
3 * Class Animations
4 *
5 * @package WordPress
6 * @author David Perez <david@close.technology>
7 * @copyright 2023 Closemarketing
8 * @version 1.0
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 add_action( 'wp_enqueue_scripts', 'frbl_theme_scripts_animations', 100 );
14 /**
15 * Loads Scripts
16 *
17 * @return void
18 */
19 function frbl_theme_scripts_animations() {
20 $dist_dir = WP_DEBUG ? 'animations/' : 'dist/';
21
22 // Then enqueue our custom animations CSS
23 wp_enqueue_style(
24 'frontblocks-animations',
25 FRBL_PLUGIN_URL . 'includes/' . $dist_dir . 'frontblocks-animations.css',
26 array(),
27 FRBL_VERSION
28 );
29
30 wp_enqueue_script(
31 'frontblocks-animations-custom',
32 FRBL_PLUGIN_URL . 'includes/' . $dist_dir . ( WP_DEBUG ? 'frontblocks-animations.js' : 'frontblocks-animations-min.js' ),
33 array(),
34 FRBL_VERSION,
35 true
36 );
37 }
38
39 add_action( 'enqueue_block_editor_assets', 'frbl_editor_scripts_animations', 5 );
40 /**
41 * Enqueue Animation Scripts and Styles for Editor
42 */
43 function frbl_editor_scripts_animations() {
44 $dist_dir = WP_DEBUG ? 'animations/' : 'dist/';
45
46 // Then enqueue our custom animations CSS
47 wp_enqueue_style(
48 'frontblocks-animations-editor',
49 FRBL_PLUGIN_URL . 'includes/' . $dist_dir . 'frontblocks-animations.css',
50 array(),
51 FRBL_VERSION
52 );
53
54 // Enqueue our custom block editor script.
55 wp_enqueue_script(
56 'frontblocks-animation-editor',
57 FRBL_PLUGIN_URL . 'includes/dist/frontblocks-animation-option.js',
58 array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-i18n' ),
59 FRBL_VERSION,
60 true
61 );
62 // Localize script with custom CSS URL
63 wp_localize_script(
64 'frontblocks-animation-editor',
65 'frontblocksAnimationData',
66 array(
67 'customCss' => FRBL_PLUGIN_URL . 'includes/' . $dist_dir . 'frontblocks-animations.css',
68 )
69 );
70 }
71
72 add_action( 'enqueue_block_editor_assets', 'frbl_register_animation_attributes', 9 );
73 /**
74 * Register animation attributes for all blocks
75 */
76 function frbl_register_animation_attributes() {
77 // This script runs on the client side to add animation attributes to all blocks.
78 wp_add_inline_script(
79 'wp-blocks',
80 "
81 wp.hooks.addFilter(
82 'blocks.registerBlockType',
83 'frontblocks/animation-attributes',
84 function( settings, name ) {
85 // Add animation attributes to all blocks
86 settings.attributes = Object.assign( settings.attributes || {}, {
87 frblAnimation: {
88 type: 'string',
89 default: ''
90 },
91 frblAnimationDelay: {
92 type: 'number',
93 default: 0
94 },
95 frblAnimationDuration: {
96 type: 'number',
97 default: 1
98 },
99 frblAnimationRepeat: {
100 type: 'boolean',
101 default: false
102 },
103 frblAnimationInfinite: {
104 type: 'boolean',
105 default: false
106 }
107 });
108
109 return settings;
110 }
111 );
112 "
113 );
114 }
115
116
117 add_filter( 'render_block', 'frbl_add_animation_classes_to_blocks', 10, 2 );
118 /**
119 * Add animation classes to blocks on frontend render
120 *
121 * @param string $block_content Block content.
122 * @param array $block Block data.
123 * @return string Modified block content.
124 */
125 function frbl_add_animation_classes_to_blocks( $block_content, $block ) {
126 if ( empty( $block['attrs'] ) ) {
127 return $block_content;
128 }
129
130 $attrs = $block['attrs'];
131
132 if ( ! isset( $attrs['frblAnimation'] ) || empty( $attrs['frblAnimation'] ) ) {
133 return $block_content;
134 }
135
136 $properties = array();
137 $animation = $properties['animation'] = $attrs['frblAnimation'];
138 $delay = $properties['delay'] = isset( $attrs['frblAnimationDelay'] ) ? $attrs['frblAnimationDelay'] : 0;
139 $duration = $properties['duration'] = isset( $attrs['frblAnimationDuration'] ) ? $attrs['frblAnimationDuration'] : 1;
140 $repeat = $properties['repeat'] = isset( $attrs['frblAnimationRepeat'] ) ? $attrs['frblAnimationRepeat'] : false;
141 $infinite_repeat = $properties['infinite_repeat'] = isset( $attrs['frblAnimationInfinite'] ) ? $attrs['frblAnimationInfinite'] : false;
142
143 // Build style attributes.
144 $style_attr = '';
145 if ( $delay > 0 ) {
146 $style_attr .= '--animate-delay:' . esc_attr( $delay ) . 's;';
147 }
148
149 if ( $duration !== 1 ) {
150 $style_attr .= '--animate-duration:' . esc_attr( $duration ) . 's;';
151 }
152
153 if ( $infinite_repeat ) {
154 $style_attr .= '--animate-repeat:infinite;';
155 } elseif ( $repeat ) {
156 $style_attr .= '--animate-repeat:2;';
157 }
158
159 // Add animation classes and styles to the first HTML tag.
160 $block_content = preg_replace_callback(
161 '/^<([a-z][a-z0-9]*)\s*((?:[^>]|\\n)*?)(?:style="([^"]*?)")?([^>]*?)>/i',
162 function ( $matches ) use ( $properties, $style_attr ) {
163 $tag = $matches[1] ?? 'div';
164 $beginning = $matches[2] ?? '';
165 $existing_style = $matches[3] ?? '';
166 $ending = $matches[4] ?? '';
167
168 $classes = 'animate__animated animate__' . esc_attr( $properties['animation'] );
169
170 // Add classes to existing class attribute or create new one.
171 if ( strpos( $beginning . $ending, 'class="' ) !== false ) {
172 $beginning = preg_replace(
173 '/class="([^"]*)"/',
174 'class="$1 ' . $classes . '"',
175 $beginning . $ending,
176 1
177 );
178 } else {
179 $beginning .= ' class="' . $classes . '"';
180 }
181 $beginning .= ' data-frontblocks-animation="' . esc_attr( $properties['animation'] ) . '"';
182 $beginning .= ' data-frontblocks-animation-delay="' . esc_attr( $properties['delay'] ) . '"';
183 $beginning .= ' data-frontblocks-animation-duration="' . esc_attr( $properties['duration'] ) . '"';
184 $beginning .= ' data-frontblocks-animation-repeat="' . esc_attr( $properties['repeat'] ) . '"';
185 $beginning .= ' data-frontblocks-animation-infinite="' . esc_attr( $properties['infinite_repeat'] ) . '"';
186 // Add styles if needed.
187 if ( ! empty( $style_attr ) ) {
188 $combined_style = $existing_style . ( ! empty( $existing_style ) ? ';' : '' ) . $style_attr;
189 return '<' . $tag . ' ' . $beginning . ' style="' . $combined_style . '">';
190 }
191
192 return '<' . $tag . ' ' . $beginning . '>';
193 },
194 $block_content,
195 1
196 );
197
198 return $block_content;
199 }
200
201