Repository navigation
Expand file tree
/
Copy pathanimations.php
More file actions
262 lines (221 loc) · 7.27 KB
/
Copy pathanimations.php
File metadata and controls
262 lines (221 loc) · 7.27 KB
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
<?php
/**
* Animation functionality for Extendable theme
*
* @package Extendable
* @since Extendable 2.0.33
*/
// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Register animation settings with WordPress Settings API
* Registers REST API endpoint for managing animation preferences
*
* @since Extendable 2.0.33
* @return void
*/
function extendable_register_animation_settings() {
register_setting( 'extendable_animations', 'extendify_animation_settings', array(
'type' => 'object',
'default' => array(
'type' => 'none',
'speed' => 'medium'
),
'show_in_rest' => array(
'schema' => array(
'type' => 'object',
'properties' => array(
'type' => array(
'type' => 'string',
'enum' => array( 'none', 'fade', 'fade-up', 'zoom-in' ),
),
'speed' => array(
'type' => 'string',
'enum' => array( 'slow', 'medium', 'fast' ),
),
),
),
),
'sanitize_callback' => 'extendable_sanitize_animation_settings',
));
}
add_action( 'rest_api_init', 'extendable_register_animation_settings' );
/**
* Sanitize animation settings
* Validates and sanitizes user input for animation configuration
*
* @since Extendable 2.0.33
* @param mixed $settings Animation settings to sanitize
* @return array Sanitized settings with type and speed keys
*/
function extendable_sanitize_animation_settings( $settings ) {
$valid_types = array( 'none', 'fade', 'fade-up', 'zoom-in' );
$valid_speeds = array( 'slow', 'medium', 'fast' );
if ( ! is_array( $settings ) ) {
$settings = array();
}
$sanitized = array(
'type' => 'none',
'speed' => 'medium'
);
if ( isset( $settings['type'] ) && in_array( $settings['type'], $valid_types, true ) ) {
$sanitized['type'] = $settings['type'];
}
if ( isset( $settings['speed'] ) && in_array( $settings['speed'], $valid_speeds, true ) ) {
$sanitized['speed'] = $settings['speed'];
}
return $sanitized;
}
/**
* Enqueue animations on frontend
* Loads animation CSS/JS and configures based on user settings
*
* @since Extendable 2.0.33
* @return void
*/
function extendable_enqueue_animations() {
$animation_settings = get_option( 'extendify_animation_settings', array(
'type' => 'none',
'speed' => 'medium'
));
$animation_type = $animation_settings['type'] ?? 'none';
$animation_speed = $animation_settings['speed'] ?? 'medium';
if ( ! apply_filters( 'extendable_enable_animations', true ) ) {
return;
}
// Skip loading for visitors when animations disabled
// Logged-in users need JS for real-time switching in agent UI
if ( 'none' === $animation_type && ! is_user_logged_in() ) {
return;
}
$config_file = get_template_directory() . '/assets/config/animations.json';
if ( ! file_exists( $config_file ) || ! is_readable( $config_file ) ) {
return;
}
$config = json_decode( file_get_contents( $config_file ), true );
if ( ! is_array( $config ) ) {
return;
}
$type = sanitize_key( $animation_type );
if ( ! isset( $config['types'][ $type ] ) ) {
$type = 'fade';
}
$mappings = $config['types'][ $type ]['mappings'] ?? array();
$defaults = $config['defaults'] ?? array();
$css_config = $config['css'] ?? array();
$all_types = array();
foreach ( $config['types'] as $type_key => $type_config ) {
$type_mappings = array();
if ( isset( $type_config['mappings'] ) && is_array( $type_config['mappings'] ) ) {
foreach ( $type_config['mappings'] as $selector => $animation ) {
$clean_selector = sanitize_text_field( trim( $selector ) );
$clean_animation = sanitize_key( trim( $animation ) );
if ( ! empty( $clean_selector ) && ! empty( $clean_animation ) ) {
$type_mappings[ $clean_selector ] = $clean_animation;
}
}
}
if ( ! empty( $type_mappings ) ) {
$all_types[ sanitize_key( $type_key ) ] = $type_mappings;
}
}
$sanitized = array();
foreach ( $mappings as $selector => $animation ) {
$clean_selector = sanitize_text_field( trim( $selector ) );
$clean_animation = sanitize_key( trim( $animation ) );
if ( ! empty( $clean_selector ) && ! empty( $clean_animation ) ) {
$sanitized[ $clean_selector ] = $clean_animation;
}
}
if ( empty( $sanitized ) ) {
return;
}
wp_enqueue_style(
'extendable-animations',
get_template_directory_uri() . '/assets/css/animations.css',
array( 'extendable-style' ),
EXTENDABLE_THEME_VERSION
);
wp_enqueue_script(
'extendable-animations',
get_template_directory_uri() . '/assets/js/animations-interactivity.js',
array(),
EXTENDABLE_THEME_VERSION,
true
);
wp_localize_script( 'extendable-animations', 'ExtendableAnimations', array(
'map' => $sanitized,
'allTypes' => $all_types,
'defaults' => array_map( 'sanitize_text_field', $defaults ),
'speed' => sanitize_key( $animation_speed ),
'type' => sanitize_key( $animation_type ),
));
// Generate FOUC prevention CSS (respects override classes and reduced motion preference)
if ( 'none' === $animation_type ) {
return;
}
$animation_css = '';
foreach ( $sanitized as $selector => $animation ) {
$css_rule = isset( $css_config[ $animation ] ) && ! empty( $css_config[ $animation ] )
? $css_config[ $animation ]
: 'opacity: 0;';
// Only hide elements if reduced motion is not preferred
$animation_css .= '@media (prefers-reduced-motion: no-preference) { ' . $selector . ':not(.ext-animate--off):not([data-ext-animated]) { ' . $css_rule . ' } } ';
}
if ( ! empty( $animation_css ) ) {
wp_add_inline_style( 'extendable-style', $animation_css );
}
}
add_action( 'wp_enqueue_scripts', 'extendable_enqueue_animations' );
/**
* Enqueue animation control for block editor
*
* @since Extendable 2.0.33
* @return void
*/
function extendable_enqueue_animation_editor_control() {
$animation_settings = get_option( 'extendify_animation_settings', array(
'type' => 'none',
'speed' => 'medium'
));
$animation_type = $animation_settings['type'] ?? 'none';
$is_enabled = apply_filters( 'extendable_enable_animations', true ) &&
! empty( $animation_type ) &&
'none' !== $animation_type;
wp_enqueue_script(
'extendable-animate-control',
get_template_directory_uri() . '/assets/editor/block-animate-control.js',
array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-block-editor', 'wp-compose', 'wp-hooks', 'wp-i18n' ),
EXTENDABLE_THEME_VERSION,
true
);
wp_localize_script( 'extendable-animate-control', 'ExtendableAnimateControl', array(
'enabled' => $is_enabled ? '1' : '0',
) );
}
add_action( 'enqueue_block_editor_assets', 'extendable_enqueue_animation_editor_control' );
/**
* Enqueue animation settings sidebar for site editor
*
* @since Extendable 2.0.33
* @return void
*/
function extendable_enqueue_animation_sidebar() {
wp_enqueue_script(
'extendable-animation-sidebar',
get_template_directory_uri() . '/assets/editor/animation-settings-modal.js',
array( 'wp-plugins', 'wp-element', 'wp-components', 'wp-data', 'wp-api-fetch', 'wp-i18n' ),
EXTENDABLE_THEME_VERSION,
true
);
$animation_settings = get_option( 'extendify_animation_settings', array(
'type' => 'none',
'speed' => 'medium'
));
wp_localize_script( 'extendable-animation-sidebar', 'ExtendableAnimationSettings', array(
'current' => $animation_settings,
) );
}
add_action( 'enqueue_block_editor_assets', 'extendable_enqueue_animation_sidebar' );