-
Notifications
You must be signed in to change notification settings - Fork 4
/
animations-for-blocks.php
464 lines (380 loc) · 10.8 KB
/
animations-for-blocks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
/**
* Plugin Name: Animations for Blocks
* Plugin URI: https://wordpress.org/plugins/animations-for-blocks
* Description: Allows to add animations to Gutenberg blocks on scroll.
* Version: 1.1.6
* Author: websevendev
* Author URI: https://github.com/websevendev
*/
namespace wsd\anfb;
use WP_HTML_Tag_Processor;
defined('ABSPATH') || exit;
function_exists('get_plugin_data') || require_once ABSPATH . 'wp-admin/includes/plugin.php';
$anfb_plugin = get_plugin_data(__FILE__, false, false);
define('WSD_ANFB_VER', $anfb_plugin['Version']);
define('WSD_ANFB_FILE', __FILE__);
define('WSD_ANFB_DIR', dirname(__FILE__));
define('WSD_ANFB_AOS_HANDLE', 'animate-on-scroll');
/**
* Register global plugin options.
*/
function register_settings() {
$default_settings = [
'animateInEditor' => true,
'lazyloadAssets' => false,
'location' => 'default',
];
$sanitize_callback = function($settings) use ($default_settings) {
if(!is_array($settings)) {
return $default_settings;
}
return [
'animateInEditor' => (bool)($settings['animateInEditor'] ?? $default_settings['animateInEditor']),
'lazyloadAssets' => (bool)($settings['lazyloadAssets'] ?? $default_settings['lazyloadAssets']),
'location' => sanitize_text_field($settings['location']) ?? $default_settings['location'],
];
};
register_setting(
'animations-for-blocks',
'animations-for-blocks',
[
'description' => __('Animations for Blocks settings', 'animations-for-blocks'),
'show_in_rest' => [
'schema' => [
'type' => 'object',
'properties' => [
'animateInEditor' => [
'type' => 'boolean',
'default' => $default_settings['animateInEditor'],
],
'lazyloadAssets' => [
'type' => 'boolean',
'default' => $default_settings['lazyloadAssets'],
],
'location' => [
'type' => 'string',
'default' => $default_settings['location'],
],
],
'sanitize_callback' => $sanitize_callback,
],
],
'default' => $default_settings,
]
);
}
add_action('init', __NAMESPACE__ . '\\register_settings');
/**
* Blocks known to not work properly with Animations for Blocks.
*
* @return array
*/
function get_unsupported_blocks() {
return apply_filters('anfb_unsupported_blocks', [
'core/freeform',
'core/html',
'core/shortcode',
'core/legacy-widget',
]);
}
/**
* Determine if block is supported to have an animation.
*
* @param string $block_name
* @return boolean
*/
function is_supported($block_name) {
static $not_supported;
if(!is_array($not_supported)) {
$not_supported = get_unsupported_blocks();
}
return !in_array($block_name, $not_supported, true);
}
/**
* Register plugin assets.
*
* @see https://github.com/michalsnik/aos
*/
function register_assets() {
$asset = include WSD_ANFB_DIR . '/build/index.asset.php';
wp_register_style(
WSD_ANFB_AOS_HANDLE,
plugins_url('build/aos.css', WSD_ANFB_FILE),
[],
$asset['version'], // 3.0.0-beta.6
'all'
);
wp_register_script(
WSD_ANFB_AOS_HANDLE,
plugins_url('build/aos.js', WSD_ANFB_FILE),
[],
$asset['version'], // 3.0.0-beta.6
true
);
$asset = include WSD_ANFB_DIR . '/build/init.asset.php';
wp_register_script(
'animations-for-blocks',
plugins_url('build/init.js', WSD_ANFB_FILE),
[
/** Use the filter below if your current setup already loads AOS. */
apply_filters('anfb_aos_handle', WSD_ANFB_AOS_HANDLE),
],
$asset['version'],
true
);
}
add_action('init', __NAMESPACE__ . '\\register_assets');
/**
* Enqueue editor assets.
*/
function editor_assets() {
$asset = include WSD_ANFB_DIR . '/build/index.asset.php';
wp_enqueue_style(
'animations-for-blocks-admin',
plugins_url('build/style-index.css', WSD_ANFB_FILE),
[],
$asset['version'],
'all'
);
/** WP <6.6 compatibility. When this is removed the whole plugin should require at least WP 6.6. */
if(!wp_script_is('react-jsx-runtime', 'registered')) {
wp_register_script(
'react-jsx-runtime',
plugins_url('react-jsx-runtime.js', WSD_ANFB_FILE),
['react'],
'18.3.1',
true
);
}
wp_enqueue_script(
'animations-for-blocks-admin',
plugins_url('build/index.js', WSD_ANFB_FILE),
$asset['dependencies'],
$asset['version'],
false
);
wp_localize_script(
'animations-for-blocks-admin',
'anfbData',
[
'unsupportedBlocks' => get_unsupported_blocks(),
'settings' => get_option('animations-for-blocks'),
]
);
if(function_exists('wp_set_script_translations')) {
wp_set_script_translations('animations-for-blocks', 'animations-for-blocks');
}
}
add_action('enqueue_block_editor_assets', __NAMESPACE__ . '\\editor_assets', 5);
/**
* Enqueue block assets (styles for `.editor-styles-wrapper`).
*/
function block_assets() {
if(!is_admin()) {
return;
}
$asset = include WSD_ANFB_DIR . '/build/index.asset.php';
wp_enqueue_style(
'animations-for-blocks-editor',
plugins_url('build/editor.css', WSD_ANFB_FILE),
[],
$asset['version'],
'all'
);
}
add_action('enqueue_block_assets', __NAMESPACE__ . '\\block_assets');
/**
* Enqueues front end assets.
*/
function enqueue_front_end_assets() {
if(apply_filters('anfb_load_styles', true)) {
/** Load Animate on Scroll styles. */
wp_enqueue_style(WSD_ANFB_AOS_HANDLE);
}
if(apply_filters('anfb_load_scripts', true)) {
/** Initialize Animate on Scroll library. */
wp_enqueue_script('animations-for-blocks');
}
}
/**
* Enqueue front end assets in head.
*/
function front_end_assets() {
$options = get_option('animations-for-blocks');
if($options['lazyloadAssets']) {
return;
}
enqueue_front_end_assets();
}
add_action('wp_enqueue_scripts', __NAMESPACE__ . '\\front_end_assets', 500);
/**
* AMP behavior.
*
* @param bool $load Load assets.
* @return bool
*/
function disable_on_amp($load) {
if(function_exists('is_amp_endpoint') && is_amp_endpoint()) {
return false;
}
return $load;
}
add_filter('anfb_load_styles', __NAMESPACE__ . '\\disable_on_amp');
add_filter('anfb_load_scripts', __NAMESPACE__ . '\\disable_on_amp');
/**
* @param array $args ANFB settings.
* @return array Attributes to add to root element.
*/
function get_animation_attributes($args = []) {
$args = wp_parse_args($args, [
'animation' => 'none',
'variation' => '',
'delay' => 0,
'duration' => 400,
'once' => false,
'mirror' => false,
'easing' => 'ease',
'offset' => 120,
'anchorPlacement' => 'top-bottom',
]);
$attributes = [];
if(empty($args['animation']) || $args['animation'] === 'none') {
return $attributes;
}
/** Animation. */
$attributes['data-aos'] = $args['animation'] === $args['variation']
? $args['animation']
: $args['animation'] . '-' . $args['variation'];
/** Delay. */
if(is_numeric($args['delay']) && (int)$args['delay'] !== 0) {
$attributes['data-aos-delay'] = (int)$args['delay'];
}
/** Duration. */
if(is_numeric($args['duration']) && (int)$args['duration'] !== 400) {
$attributes['data-aos-duration'] = (int)$args['duration'];
}
/** Easing. */
if(!empty($args['easing']) && $args['easing'] !== 'ease') {
$attributes['data-aos-easing'] = $args['easing'];
}
/** Once. */
if($args['once'] === 'true' || $args['once'] === true) {
$attributes['data-aos-once'] = 'true';
}
/** Mirror. */
if($args['mirror'] === 'true' || $args['mirror'] === true) {
$attributes['data-aos-mirror'] = 'true';
}
/** Offset. */
if(is_numeric($args['offset']) && (int)$args['offset'] !== 120) {
$attributes['data-aos-offset'] = (int)$args['offset'];
}
/** Anchor placement. */
if(!empty($args['anchorPlacement']) && $args['anchorPlacement'] !== 'top-bottom') {
$attributes['data-aos-anchor-placement'] = $args['anchorPlacement'];
}
return apply_filters('anfb_aos_attributes', $attributes, $args);
}
/**
* Add animation attributes to root element.
*
* @param string $html Block HTML.
* @param array $args ANFB settings.
* @return string Block HTML with animation attributes.
*/
function add_animation_attributes($html, $args) {
$tags = new WP_HTML_Tag_Processor($html);
if($tags->next_tag()) {
/** Already has animation attributes. */
if($tags->get_attribute('data-aos')) {
return $html;
}
/** Add animation attributes. */
foreach(get_animation_attributes($args) as $key => $value) {
$tags->set_attribute($key, $value);
}
return $tags->get_updated_html();
}
return $html;
}
/**
* Determine if given block attributes have an animation.
*
* @param array $block_attributes
* @return boolean
*/
function has_animation($block_attributes) {
return (
is_array($block_attributes)
&& isset($block_attributes['animationsForBlocks'])
&& isset($block_attributes['animationsForBlocks']['animation'])
&& !empty($block_attributes['animationsForBlocks']['animation'])
&& $block_attributes['animationsForBlocks']['animation'] !== 'none'
);
}
/**
* @param array $args Array of arguments for registering a block type.
* @param string $block_name Block type name including namespace.
* @return array
*/
function block_args($args, $block_name) {
if(!is_supported($block_name)) {
return $args;
}
if(!isset($args['attributes']) || !is_array($args['attributes'])) {
$args['attributes'] = [];
}
/** Register ANFB attribute, this is necessary for `/wp-json/wp/v2/block-renderer` REST endpoint to not throw `rest_additional_properties_forbidden`. */
$args['attributes']['animationsForBlocks'] = [
'type' => 'object',
];
return $args;
}
add_filter('register_block_type_args', __NAMESPACE__ . '\\block_args', 10, 2);
/**
* Add animation attributes to blocks' root HTML element when applicable.
*
* @param string $block_content Rendered block.
* @param string $block Parsed array representation of block.
* @return string
*/
function animate_block($block_content, $block) {
if(is_supported($block['blockName']) && has_animation($block['attrs'])) {
if(
defined('REST_REQUEST')
&& REST_REQUEST
&& isset($_REQUEST['context'])
&& $_REQUEST['context'] === 'edit'
) {
/** Don't animate server-side rendered blocks. */
return $block_content;
}
/** Lazyload assets. */
static $lazyloaded = false;
if(!$lazyloaded) {
$options = get_option('animations-for-blocks');
if($options['lazyloadAssets']) {
enqueue_front_end_assets();
}
$lazyloaded = true;
}
return add_animation_attributes($block_content, $block['attrs']['animationsForBlocks']);
}
return $block_content;
}
add_filter('render_block', __NAMESPACE__ . '\\animate_block', 10, 2);
/**
* Add GitHub link on the plugins page.
*
* @param array $plugin_meta
* @param string $plugin_file
* @return array
*/
function github_link($plugin_meta, $plugin_file) {
if($plugin_file === plugin_basename(WSD_ANFB_FILE)) {
$plugin_meta[] = '<a href="https://github.com/websevendev/animations-for-blocks" target="_blank" rel="noopener noreferrer">GitHub</a>';
}
return $plugin_meta;
}
add_filter('plugin_row_meta', __NAMESPACE__ . '\\github_link', 10, 2);