PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.5.0
Block Animations, Motion & Scroll Effects – Ghost Kit v3.5.0
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / classes / class-assets.php

class-assets.php in Block Animations, Motion & Scroll Effects – Ghost Kit 3.5.0, at classes/class-assets.php

855 lines 25.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Assets static and dynamic.
4 *
5 * @package ghostkit
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Class GhostKit_Assets
14 */
15 class GhostKit_Assets {
16 /**
17 * List with stored assets.
18 *
19 * @var array
20 */
21 private static $stored_assets = array(
22 'script' => array(),
23 'style' => array(),
24 'custom-css' => array(),
25 );
26
27 /**
28 * Already added custom assets in head.
29 *
30 * @var boolean
31 */
32 private static $already_added_custom_assets = false;
33
34 /**
35 * Already add styles.
36 *
37 * @var array
38 */
39 private static $already_added_styles = array();
40
41 /**
42 * List with assets to skip from Autoptimize when CSS generated.
43 *
44 * @var array
45 */
46 protected static $skip_from_autoptimize = array(
47 'ghostkit-blocks-content-custom-css',
48 'ghostkit-blocks-widget-custom-css',
49 );
50
51 /**
52 * GhostKit_Assets constructor.
53 */
54 public function __construct() {
55 add_action( 'init', array( $this, 'register_scripts' ) );
56 add_filter( 'render_block', array( $this, 'render_block_enqueue_assets' ), 9, 2 );
57 add_action( 'plugins_loaded', array( $this, 'enqueue_scripts_action' ), 11 );
58 add_action( 'ghostkit_parse_blocks', array( $this, 'maybe_enqueue_blocks_assets' ), 11, 2 );
59 add_action( 'wp_enqueue_scripts', array( $this, 'add_custom_assets' ), 100 );
60
61 add_action( 'autoptimize_filter_css_exclude', 'GhostKit_Assets::autoptimize_filter_css_exclude' );
62
63 // enqueue runtime.
64 add_action( 'enqueue_block_editor_assets', 'GhostKit_Assets::enqueue_runtime', 11 );
65 add_action( 'wp_enqueue_scripts', 'GhostKit_Assets::enqueue_runtime', 8 );
66 }
67
68 /**
69 * Check if Webpack HMR file available.
70 *
71 * @return boolean
72 */
73 public static function is_webpack_hmr_support() {
74 return file_exists( ghostkit()->plugin_path . 'build/runtime.js' );
75 }
76
77 /**
78 * Enqueue runtime script.
79 */
80 public static function enqueue_runtime() {
81 // HMR Webpack.
82 if ( self::is_webpack_hmr_support() ) {
83 self::enqueue_script( 'ghostkit-runtime', 'build/runtime', array(), null, false );
84 }
85 }
86
87 /**
88 * Get .asset.php file data.
89 *
90 * @param string $filepath asset file path.
91 * @param string $filetype asset file type [style|script].
92 *
93 * @return array
94 */
95 public static function get_asset_file( $filepath, $filetype = '' ) {
96 $asset_path = ghostkit()->plugin_path . str_replace( '.min', '', $filepath ) . '.asset.php';
97
98 if ( file_exists( $asset_path ) ) {
99 // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound
100 return include $asset_path;
101 } elseif ( ! empty( $filetype ) ) {
102 $file_ext = 'style' === $filetype ? 'css' : 'js';
103 $full_file_path = ghostkit()->plugin_path . $filepath . '.' . $file_ext;
104 $file_version = file_exists( $full_file_path ) ? filemtime( $full_file_path ) : null;
105 }
106
107 return array(
108 'dependencies' => array(),
109 'version' => $file_version ?? GHOSTKIT_VERSION,
110 );
111 }
112
113 /**
114 * Register script.
115 *
116 * @param string $name asset name.
117 * @param string $path file path.
118 * @param array $dependencies asset dependencies.
119 * @param string $version asset version.
120 * @param boolean $in_footer render in footer.
121 */
122 public static function register_script( $name, $path, $dependencies = array(), $version = null, $in_footer = true ) {
123 global $current_screen;
124
125 $script_data = self::get_asset_file( $path, 'script' );
126
127 if ( ! empty( $dependencies ) ) {
128 $script_data['dependencies'] = array_unique(
129 array_merge(
130 $script_data['dependencies'],
131 $dependencies
132 )
133 );
134 }
135
136 // Fix for Widgets screen.
137 if ( isset( $current_screen->id ) && 'widgets' === $current_screen->id ) {
138 foreach ( array( 'wp-edit-post', 'wp-editor' ) as $skip ) {
139 $key = array_search( $skip, $script_data['dependencies'], true );
140
141 if ( false !== $key ) {
142 unset( $script_data['dependencies'][ $key ] );
143 }
144 }
145 }
146
147 wp_register_script(
148 $name,
149 ghostkit()->plugin_url . $path . '.js',
150 $script_data['dependencies'],
151 $version ?? $script_data['version'],
152 $in_footer
153 );
154 }
155
156 /**
157 * Enqueue script.
158 *
159 * @param string $name asset name.
160 * @param string $path file path.
161 * @param array $dependencies asset dependencies.
162 * @param string $version asset version.
163 * @param boolean $in_footer render in footer.
164 */
165 public static function enqueue_script( $name, $path, $dependencies = array(), $version = null, $in_footer = true ) {
166 self::register_script( $name, $path, $dependencies, $version, $in_footer );
167
168 wp_enqueue_script( $name );
169 }
170
171 /**
172 * Register style
173 *
174 * @param string $name asset name.
175 * @param string $path file path.
176 * @param array $dependencies asset dependencies.
177 * @param string $version asset version.
178 */
179 public static function register_style( $name, $path, $dependencies = array(), $version = null ) {
180 $style_data = self::get_asset_file( $path, 'style' );
181
182 wp_register_style(
183 $name,
184 ghostkit()->plugin_url . $path . '.css',
185 $dependencies,
186 $version ?? $style_data['version']
187 );
188 }
189
190 /**
191 * Enqueue style
192 *
193 * @param string $name asset name.
194 * @param string $path file path.
195 * @param array $dependencies asset dependencies.
196 * @param string $version asset version.
197 */
198 public static function enqueue_style( $name, $path, $dependencies = array(), $version = null ) {
199 self::register_style( $name, $path, $dependencies, $version );
200
201 wp_enqueue_style( $name );
202 }
203
204 /**
205 * Store used assets, so we can enqueue it later.
206 *
207 * @param string $name - asset name.
208 * @param bool|string $value - just enqueue flag or url to asset.
209 * @param string $type - assets type [script|style|custom-css].
210 * @param int $priority - asset enqueue priority.
211 */
212 public static function store_used_assets( $name, $value = true, $type = 'script', $priority = 10 ) {
213 if ( ! isset( self::$stored_assets[ $type ] ) ) {
214 return;
215 }
216
217 // We need to check for duplicate assets, which may be added because of custom locations.
218 // Sometimes custom CSS is duplicated in Astra or Blocksy themes.
219 if ( 'custom-css' === $type ) {
220 if ( in_array( $value, self::$already_added_styles, true ) ) {
221 return;
222 }
223 self::$already_added_styles[] = $value;
224 }
225
226 if ( isset( self::$stored_assets[ $type ][ $name ] ) ) {
227 if ( 'custom-css' === $type ) {
228 if ( ! self::$stored_assets[ $type ][ $name ]['value'] ) {
229 self::$stored_assets[ $type ][ $name ]['value'] = '';
230 }
231
232 self::$stored_assets[ $type ][ $name ]['value'] .= $value;
233 }
234
235 do_action( 'gkt_assets_store', $name, $value, $type, $priority );
236
237 return;
238 }
239
240 self::$stored_assets[ $type ][ $name ] = array(
241 'value' => $value,
242 'priority' => $priority,
243 );
244
245 do_action( 'gkt_assets_store', $name, $value, $type, $priority );
246 }
247
248 /**
249 * Enqueue stored assets.
250 *
251 * @param string $type - assets type [script|style|custom-css].
252 */
253 public static function enqueue_stored_assets( $type = 'script' ) {
254 if ( ! isset( self::$stored_assets[ $type ] ) || empty( self::$stored_assets[ $type ] ) ) {
255 return;
256 }
257
258 uasort(
259 self::$stored_assets[ $type ],
260 function ( $a, $b ) {
261 if ( $a === $b ) {
262 return 0;
263 }
264
265 if ( isset( $a['priority'] ) && isset( $b['priority'] ) ) {
266 return $a['priority'] < $b['priority'] ? -1 : 1;
267 }
268
269 return 0;
270 }
271 );
272
273 foreach ( self::$stored_assets[ $type ] as $name => $data ) {
274 if ( isset( $data['value'] ) && $data['value'] ) {
275 if ( 'script' === $type ) {
276 wp_enqueue_script( $name );
277 } elseif ( 'style' === $type ) {
278 wp_enqueue_style( $name );
279 } elseif ( 'custom-css' === $type ) {
280 self::add_custom_css( $name, $data['value'] );
281 }
282
283 self::$stored_assets[ $type ]['value'] = false;
284 }
285 }
286 }
287
288 /**
289 * Enqueue assets for blocks.
290 *
291 * @param array $blocks - blocks array.
292 * @param string $location - blocks location [content,widget].
293 */
294 public static function enqueue( $blocks = array(), $location = 'content' ) {
295 $blocks_css = '';
296
297 // Prepare blocks assets.
298 foreach ( $blocks as $block ) {
299 // Filter blocks custom CSS.
300 if ( isset( $block['blockName'] ) ) {
301 $custom_styles = apply_filters( 'gkt_block_custom_styles', '', $block );
302
303 if ( ! empty( $custom_styles ) ) {
304 if ( ! empty( $blocks_css ) ) {
305 $blocks_css .= ' ';
306 }
307
308 $blocks_css .= $custom_styles;
309 }
310 }
311 }
312
313 // Store custom CSS.
314 if ( ! empty( $blocks_css ) && $blocks_css ) {
315 self::store_used_assets( 'ghostkit-blocks-' . $location . '-custom-css', ghostkit()->replace_vars( $blocks_css ), 'custom-css' );
316 }
317 }
318
319 /**
320 * Enqueue extension and style-variant assets on demand.
321 *
322 * @param string $block_content rendered block content.
323 * @param array $block block data.
324 *
325 * @return string
326 */
327 public function render_block_enqueue_assets( $block_content, $block ) {
328 if ( ! is_string( $block_content ) || '' === $block_content ) {
329 return $block_content;
330 }
331
332 $has_effects_attrs = ! empty( _wp_array_get( $block, array( 'attrs', 'ghostkit', 'effects' ), false ) );
333
334 if (
335 $has_effects_attrs ||
336 ( isset( $block['blockName'] ) && in_array( $block['blockName'], array( 'ghostkit/counter-box', 'ghostkit/progress' ), true ) ) ||
337 false !== strpos( $block_content, 'data-gkt-effects' ) ||
338 false !== strpos( $block_content, 'ghostkit-count-up' )
339 ) {
340 if ( wp_script_is( 'ghostkit-extension-effects', 'registered' ) ) {
341 wp_enqueue_script( 'ghostkit-extension-effects' );
342 }
343 }
344
345 if ( false !== strpos( $block_content, 'is-style-styled' ) ) {
346 if ( wp_script_is( 'ghostkit-style-variant-core-list', 'registered' ) ) {
347 wp_enqueue_script( 'ghostkit-style-variant-core-list' );
348 }
349 }
350
351 return $block_content;
352 }
353
354 /**
355 * Enqueue scripts and styles actions.
356 * We need this to be used inside 'plugins_loaded' action to prevent conflicts with plugins and themes.
357 *
358 * 1. Enqueue plugins assets
359 * 2. Enqueue Ghost Kit assets
360 * 3. Enqueue theme assets
361 * 4. Enqueue Ghost Kit custom css
362 */
363 public function enqueue_scripts_action() {
364 add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_head_assets' ) );
365 add_action( 'wp_footer', array( $this, 'wp_enqueue_foot_assets' ) );
366
367 // Custom css with higher priority to overwrite theme styles.
368 add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_head_custom_assets' ), 11 );
369 add_action( 'wp_footer', array( $this, 'wp_enqueue_foot_custom_assets' ), 11 );
370 }
371
372 /**
373 * Register scripts that will be used in the future when portfolio will be printed.
374 */
375 public function register_scripts() {
376 $css_deps = array();
377 $js_deps = array( 'ghostkit-helper', 'ghostkit-event-fallbacks' );
378
379 do_action( 'gkt_before_assets_register' );
380
381 // Motion.
382 if ( apply_filters( 'gkt_enqueue_plugin_motion', true ) ) {
383 self::register_script( 'motion', 'assets/vendor/motion/dist/motion.min', array(), '11.15.0' );
384
385 $js_deps[] = 'motion';
386 }
387
388 // Ivent.
389 if ( apply_filters( 'gkt_enqueue_plugin_ivent', true ) ) {
390 self::register_script( 'ivent', 'assets/vendor/ivent/dist/ivent.min', array(), '0.2.0' );
391 }
392
393 // Jarallax.
394 if ( apply_filters( 'gkt_enqueue_plugin_jarallax', true ) ) {
395 self::register_script( 'jarallax', 'assets/vendor/jarallax/dist/jarallax.min', array(), '2.0.1' );
396 self::register_script( 'jarallax-video', 'assets/vendor/jarallax/dist/jarallax-video.min', array( 'jarallax' ), '2.0.1' );
397 }
398
399 // Swiper.
400 if ( apply_filters( 'gkt_enqueue_plugin_swiper', true ) ) {
401 // Add legacy swiper version in order to support Elementor plugin.
402 // https://wordpress.org/support/topic/visual-portfolio-elementor-issue/.
403 if ( class_exists( '\Elementor\Plugin' ) ) {
404 self::register_style( 'swiper', 'assets/vendor/swiper-5-4-5/swiper.min', array(), '5.4.5' );
405 self::register_script( 'swiper', 'assets/vendor/swiper-5-4-5/swiper.min', array(), '5.4.5' );
406 } else {
407 self::register_style( 'swiper', 'assets/vendor/swiper/swiper-bundle.min', array(), '8.4.6' );
408 self::register_script( 'swiper', 'assets/vendor/swiper/swiper-bundle.min', array(), '8.4.6' );
409 }
410 }
411
412 // Luxon.
413 if ( apply_filters( 'gkt_enqueue_plugin_luxon', true ) ) {
414 self::register_script( 'luxon', 'assets/vendor/luxon/build/global/luxon.min', array(), '3.3.0' );
415 }
416
417 // Lottie Player.
418 if ( apply_filters( 'gkt_enqueue_plugin_lottie_player', true ) ) {
419 self::register_script( 'lottie-player', 'assets/vendor/lottie-player/dist/lottie-player', array(), '2.0.3' );
420 wp_script_add_data( 'lottie-player', 'async', true );
421 }
422
423 // GistEmbed.
424 if ( apply_filters( 'gkt_enqueue_plugin_gist_simple', true ) ) {
425 self::register_style( 'gist-simple', 'assets/vendor/gist-simple/dist/gist-simple', array(), '2.0.0' );
426 self::register_script( 'gist-simple', 'assets/vendor/gist-simple/dist/gist-simple.min', array(), '2.0.0' );
427 }
428
429 // Google reCaptcha.
430 $recaptcha_site_key = get_option( 'ghostkit_google_recaptcha_api_site_key' );
431 $recaptcha_secret_key = get_option( 'ghostkit_google_recaptcha_api_secret_key' );
432
433 if ( apply_filters( 'gkt_enqueue_google_recaptcha', true ) && $recaptcha_site_key && $recaptcha_secret_key ) {
434 wp_register_script( 'google-recaptcha', 'https://www.google.com/recaptcha/api.js?render=' . esc_attr( $recaptcha_site_key ), array(), '3.0.0', true );
435 }
436
437 // Get all sidebars.
438 $sidebars = array();
439 if ( ! empty( $GLOBALS['wp_registered_sidebars'] ) ) {
440 foreach ( $GLOBALS['wp_registered_sidebars'] as $k => $sidebar ) {
441 $sidebars[ $k ] = array(
442 'id' => $sidebar['id'],
443 'name' => $sidebar['name'],
444 );
445 }
446 }
447
448 // helper script.
449 self::register_script(
450 'ghostkit-helper',
451 'build/assets/js/helper',
452 array( 'ivent' )
453 );
454
455 // Google Maps prepare localization as in WordPress settings.
456 $gmaps_locale = get_locale();
457 $gmaps_suffix = '.com';
458 switch ( $gmaps_locale ) {
459 case 'he_IL':
460 // Hebrew correction.
461 $gmaps_locale = 'iw';
462 break;
463 case 'zh_CN':
464 // Chinese integration.
465 $gmaps_suffix = '.cn';
466 break;
467 }
468 $gmaps_locale = substr( $gmaps_locale, 0, 2 );
469
470 $theme_data = wp_get_theme( get_template() );
471
472 $breakpoints = GhostKit_Breakpoints::get_breakpoints();
473
474 $timezone = wp_timezone_string();
475
476 if ( substr( $timezone, 0, 1 ) === '+' || substr( $timezone, 0, 1 ) === '-' ) {
477 $timezone = 'UTC' . $timezone;
478 }
479
480 $global_data = array(
481 'version' => GHOSTKIT_VERSION,
482 'pro' => false,
483
484 'themeName' => $theme_data->get( 'Name' ),
485 'settings' => get_option( 'ghostkit_settings', array() ),
486 'disabledBlocks' => get_option( 'ghostkit_disabled_blocks', array() ),
487
488 // TODO: Due to different formats in scss and assets there is an offset.
489 'media_sizes' => array(
490 'sm' => $breakpoints['xs'],
491 'md' => $breakpoints['sm'],
492 'lg' => $breakpoints['md'],
493 'xl' => $breakpoints['lg'],
494 ),
495 'timezone' => $timezone,
496 'googleMapsAPIKey' => get_option( 'ghostkit_google_maps_api_key' ),
497 'googleMapsAPIUrl' => 'https://maps.googleapis' . $gmaps_suffix . '/maps/api/js?v=3.exp&language=' . esc_attr( $gmaps_locale ),
498 'googleReCaptchaAPISiteKey' => $recaptcha_site_key,
499 'googleReCaptchaAPISecretKey' => is_admin() ? $recaptcha_secret_key : '',
500 'sidebars' => $sidebars,
501 'icons' => is_admin() ? apply_filters( 'gkt_icons_list', array() ) : array(),
502 'shapes' => is_admin() ? apply_filters( 'gkt_shapes_list', array() ) : array(),
503 'fonts' => is_admin() ? apply_filters( 'gkt_fonts_list', array() ) : array(),
504 'customTypographyList' => is_admin() ? apply_filters( 'gkt_custom_typography', array() ) : array(),
505 'admin_url' => admin_url(),
506 'admin_templates_url' => admin_url( 'edit.php?post_type=ghostkit_template' ),
507 );
508
509 $global_data = apply_filters( 'gkt_global_data', $global_data );
510
511 wp_localize_script(
512 'ghostkit-helper',
513 'ghostkitVariables',
514 $global_data
515 );
516
517 // events fallback script.
518 self::register_script(
519 'ghostkit-event-fallbacks',
520 'build/assets/js/event-fallbacks',
521 array( 'ghostkit-helper' )
522 );
523
524 // Fallback for classic themes.
525 if ( ! wp_is_block_theme() ) {
526 self::register_style(
527 'ghostkit-classic-theme-fallback',
528 'assets/css/fallback-classic-theme'
529 );
530 $css_deps[] = 'ghostkit-classic-theme-fallback';
531 }
532
533 // Ghost Kit.
534 self::register_style(
535 'ghostkit',
536 'build/gutenberg/style',
537 $css_deps
538 );
539 wp_style_add_data( 'ghostkit', 'rtl', 'replace' );
540 wp_style_add_data( 'ghostkit', 'suffix', '.min' );
541
542 self::register_script(
543 'ghostkit',
544 'build/assets/js/main',
545 $js_deps
546 );
547
548 // Extensions.
549 foreach ( glob( ghostkit()->plugin_path . 'build/gutenberg/extend/*/frontend.js' ) as $file ) {
550 $ext_name = basename( dirname( $file ) );
551 $ext_script_url = 'build/gutenberg/extend/' . $ext_name . '/frontend';
552 $ext_js_deps = array( 'ghostkit' );
553
554 switch ( $ext_name ) {
555 case 'effects':
556 $ext_js_deps[] = 'motion';
557 break;
558 }
559
560 self::register_script(
561 'ghostkit-extension-' . $ext_name,
562 $ext_script_url,
563 array_unique( $ext_js_deps )
564 );
565 }
566
567 // Style Variants.
568 foreach ( glob( ghostkit()->plugin_path . 'build/gutenberg/style-variants/*/frontend.js' ) as $template ) {
569 $ext_name = basename( dirname( $template ) );
570 $ext_script_url = 'build/gutenberg/style-variants/' . $ext_name . '/frontend';
571 $ext_js_deps = array( 'ghostkit' );
572
573 self::register_script(
574 'ghostkit-style-variant-' . $ext_name,
575 $ext_script_url,
576 array_unique( $ext_js_deps )
577 );
578 }
579
580 // Blocks.
581 foreach ( glob( ghostkit()->plugin_path . 'build/gutenberg/blocks/*/frontend.js' ) as $file ) {
582 $block_name = basename( dirname( $file ) );
583 $block_script_url = 'build/gutenberg/blocks/' . $block_name . '/frontend';
584 $block_js_deps = array( 'ghostkit' );
585
586 switch ( $block_name ) {
587 case 'accordion':
588 case 'alert':
589 $block_js_deps[] = 'motion';
590 break;
591 case 'grid':
592 case 'grid-column':
593 if ( wp_script_is( 'jarallax-video' ) || wp_script_is( 'jarallax-video', 'registered' ) ) {
594 $block_js_deps[] = 'jarallax-video';
595 }
596 if ( wp_script_is( 'jarallax' ) || wp_script_is( 'jarallax', 'registered' ) ) {
597 $block_js_deps[] = 'jarallax';
598 }
599 break;
600 case 'video':
601 if ( wp_script_is( 'jarallax-video' ) || wp_script_is( 'jarallax-video', 'registered' ) ) {
602 $block_js_deps[] = 'jarallax-video';
603 }
604 break;
605 case 'gist':
606 if ( wp_script_is( 'gist-simple' ) || wp_script_is( 'gist-simple', 'registered' ) ) {
607 $block_js_deps[] = 'gist-simple';
608 }
609 break;
610 case 'carousel':
611 if ( wp_script_is( 'swiper' ) || wp_script_is( 'swiper', 'registered' ) ) {
612 $block_js_deps[] = 'swiper';
613 }
614 break;
615 case 'countdown':
616 $block_js_deps[] = 'luxon';
617 break;
618 case 'form':
619 if ( wp_script_is( 'google-recaptcha' ) || wp_script_is( 'google-recaptcha', 'registered' ) ) {
620 $block_js_deps[] = 'google-recaptcha';
621 }
622 $block_js_deps[] = 'wp-i18n';
623 break;
624 case 'lottie':
625 $block_js_deps[] = 'motion';
626 $block_js_deps[] = 'lottie-player';
627 break;
628 case 'tabs':
629 $block_name = 'tabs-v2';
630 break;
631 }
632
633 self::register_script(
634 'ghostkit-block-' . $block_name,
635 $block_script_url,
636 array_unique( $block_js_deps )
637 );
638 }
639 foreach ( glob( ghostkit()->plugin_path . 'build/gutenberg/blocks/*/styles/style.css' ) as $file ) {
640 $block_name = basename( dirname( dirname( $file ) ) );
641 $block_style_url = 'build/gutenberg/blocks/' . $block_name . '/styles/style';
642 $block_css_deps = array( 'ghostkit' );
643
644 switch ( $block_name ) {
645 case 'gist':
646 $block_css_deps[] = 'gist-simple';
647 break;
648 case 'carousel':
649 $block_css_deps[] = 'swiper';
650 break;
651 case 'tabs':
652 $block_name = 'tabs-v2';
653 break;
654 }
655
656 self::register_style(
657 'ghostkit-block-' . $block_name,
658 $block_style_url,
659 array_unique( $block_css_deps )
660 );
661 wp_style_add_data( 'ghostkit-block-' . $block_name, 'rtl', 'replace' );
662 wp_style_add_data( 'ghostkit-block-' . $block_name, 'suffix', '.min' );
663 }
664
665 do_action( 'gkt_after_assets_register' );
666 }
667
668 /**
669 * Enqueue styles in head.
670 */
671 public function wp_enqueue_head_assets() {
672 self::enqueue_stored_assets( 'style' );
673 self::enqueue_stored_assets( 'script' );
674 }
675
676 /**
677 * Enqueue custom styles in head.
678 */
679 public function wp_enqueue_head_custom_assets() {
680 self::enqueue_stored_assets( 'custom-css' );
681 }
682
683 /**
684 * Enqueue scripts and styles in foot.
685 */
686 public function wp_enqueue_foot_assets() {
687 self::enqueue_stored_assets( 'style' );
688 self::enqueue_stored_assets( 'script' );
689 }
690
691 /**
692 * Enqueue custom styles in foot.
693 */
694 public function wp_enqueue_foot_custom_assets() {
695 self::enqueue_stored_assets( 'custom-css' );
696 }
697
698 /**
699 * Enqueue blocks assets.
700 *
701 * @param array $blocks - blocks array.
702 * @param array $location - blocks location [content,widget].
703 */
704 public function maybe_enqueue_blocks_assets( $blocks, $location ) {
705 if ( self::$already_added_custom_assets ) {
706 $location = 'widget';
707 }
708
709 self::enqueue( $blocks, $location );
710 }
711
712 /**
713 * Add styles from blocks to the head section.
714 *
715 * @param int $post_id - current post id.
716 */
717 public function add_custom_assets( $post_id ) {
718 $global_code = get_option( 'ghostkit_custom_code', array() );
719
720 if ( is_singular() && ! $post_id ) {
721 $post_id = get_the_ID();
722 }
723
724 $is_single = is_singular() && $post_id;
725
726 // Global custom CSS.
727 if ( $global_code && isset( $global_code['ghostkit_custom_css'] ) && $global_code['ghostkit_custom_css'] ) {
728 self::add_custom_css( 'ghostkit-global-custom-css', $global_code['ghostkit_custom_css'] );
729 }
730
731 // Local custom CSS.
732 if ( $is_single ) {
733 $meta_css = get_post_meta( $post_id, 'ghostkit_custom_css', true );
734
735 if ( ! empty( $meta_css ) ) {
736 self::add_custom_css( 'ghostkit-custom-css', $meta_css );
737 }
738 }
739
740 // Global custom JS head.
741 if ( $global_code && isset( $global_code['ghostkit_custom_js_head'] ) && $global_code['ghostkit_custom_js_head'] ) {
742 self::add_custom_js( 'ghostkit-global-custom-js-head', $global_code['ghostkit_custom_js_head'] );
743 }
744
745 // Local custom JS head.
746 if ( $is_single ) {
747 $meta_js_head = get_post_meta( $post_id, 'ghostkit_custom_js_head', true );
748
749 if ( ! empty( $meta_js_head ) ) {
750 self::add_custom_js( 'ghostkit-custom-js-head', $meta_js_head );
751 }
752 }
753
754 // Global custom JS foot.
755 if ( $global_code && isset( $global_code['ghostkit_custom_js_foot'] ) && $global_code['ghostkit_custom_js_foot'] ) {
756 self::add_custom_js( 'ghostkit-global-custom-js-foot', $global_code['ghostkit_custom_js_foot'], true );
757 }
758
759 // Local custom JS foot.
760 if ( $is_single ) {
761 $meta_js_foot = get_post_meta( $post_id, 'ghostkit_custom_js_foot', true );
762
763 if ( ! empty( $meta_js_foot ) ) {
764 self::add_custom_js( 'ghostkit-custom-js-foot', $meta_js_foot, true );
765 }
766 }
767
768 self::$already_added_custom_assets = true;
769 }
770
771 /**
772 * Skip custom styles from Autoptimize.
773 * We need this since generated CSS with custom breakpoints are excluded
774 * from Autoptimize by default and this cause conflicts.
775 *
776 * @param string $result - allowed list.
777 * @return string
778 */
779 public static function autoptimize_filter_css_exclude( $result ) {
780 $upload_dir = defined( 'UPLOADS' ) ? UPLOADS : 'wp-content/uploads/';
781
782 // By default in Autoptimize excluded folder `wp-content/uploads/`.
783 // We need to check, if this folder is not excluded, then we
784 // don't need to use our hack.
785 if ( $result && strpos( $result, $upload_dir ) === false ) {
786 return $result;
787 }
788
789 foreach ( self::$skip_from_autoptimize as $name ) {
790 if ( $result ) {
791 $result .= ',';
792 } else {
793 $result = '';
794 }
795
796 $result .= $name;
797 }
798
799 return $result;
800 }
801
802 /**
803 * Add custom CSS.
804 *
805 * @param string $name - handle name.
806 * @param string $css - code.
807 */
808 public static function add_custom_css( $name, $css ) {
809 if ( ! wp_style_is( $name, 'enqueued' ) ) {
810 $css = wp_kses( $css, array( '\'', '\"' ) );
811 $css = str_replace( '&gt;', '>', $css );
812
813 $allow_js_fallback = apply_filters( 'gkt_custom_css_allow_js_fallback', self::$already_added_custom_assets, $name, $css );
814
815 // Enqueue custom CSS.
816 if ( ! $allow_js_fallback ) {
817 wp_register_style( $name, false, array(), GHOSTKIT_VERSION );
818 wp_enqueue_style( $name );
819 wp_add_inline_style( $name, $css );
820
821 // Enqueue JS instead of CSS when rendering in <body> to prevent W3C errors.
822 } elseif ( ! wp_script_is( $name, 'enqueued' ) ) {
823 wp_register_script( $name, false, array(), GHOSTKIT_VERSION, true );
824 wp_enqueue_script( $name );
825 wp_add_inline_script(
826 $name,
827 '(function(){
828 var styleTag = document.createElement("style");
829 styleTag.id = "' . esc_attr( $name ) . '-inline-css";
830 styleTag.innerHTML = ' . wp_json_encode( $css ) . ';
831 document.body.appendChild(styleTag);
832 }());'
833 );
834 }
835 }
836
837 self::$stored_assets[] = $name;
838 }
839
840 /**
841 * Add custom JS.
842 *
843 * @param string $name - handle name.
844 * @param string $js - code.
845 * @param boolean $footer - print in footer.
846 */
847 public static function add_custom_js( $name, $js, $footer = false ) {
848 wp_register_script( $name, '', array(), GHOSTKIT_VERSION, $footer );
849 wp_enqueue_script( $name );
850 wp_add_inline_script( $name, $js );
851 }
852 }
853
854 new GhostKit_Assets();
855