| @@ -52,8 +52,9 @@ | ||
| 52 | 52 | * GhostKit_Assets constructor. |
| 53 | 53 | */ |
| 54 | 54 | public function __construct() { |
| 55 | 55 | add_action( 'init', array( $this, 'register_scripts' ) ); |
| 56 | + add_filter( 'render_block', array( $this, 'render_block_enqueue_assets' ), 9, 2 ); | |
| 56 | 57 | add_action( 'plugins_loaded', array( $this, 'enqueue_scripts_action' ), 11 ); |
| 57 | 58 | add_action( 'ghostkit_parse_blocks', array( $this, 'maybe_enqueue_blocks_assets' ), 11, 2 ); |
| 58 | 59 | add_action( 'wp_enqueue_scripts', array( $this, 'add_custom_assets' ), 100 ); |
| 59 | 60 | |
| @@ -278,9 +279,9 @@ | ||
| 278 | 279 | } elseif ( 'custom-css' === $type ) { |
| 279 | 280 | self::add_custom_css( $name, $data['value'] ); |
| 280 | 281 | } |
| 281 | 282 | |
| 282 | - self::$stored_assets[ $type ]['value'] = false; | |
| 283 | + self::$stored_assets[ $type ][ $name ]['value'] = false; | |
| 283 | 284 | } |
| 284 | 285 | } |
| 285 | 286 | } |
| 286 | 287 | |
| @@ -290,23 +291,17 @@ | ||
| 290 | 291 | * @param array $blocks - blocks array. |
| 291 | 292 | * @param string $location - blocks location [content,widget]. |
| 292 | 293 | */ |
| 293 | 294 | public static function enqueue( $blocks = array(), $location = 'content' ) { |
| 294 | - self::store_used_assets( 'ghostkit', true, 'style', 9 ); | |
| 295 | - self::store_used_assets( 'ghostkit', true, 'script', 11 ); | |
| 295 | + if ( ! empty( $blocks ) && is_array( $blocks ) ) { | |
| 296 | + $detected_assets = GhostKit_Assets_Detector::detect_from_blocks( $blocks, 'parse' ); | |
| 297 | + GhostKit_Assets_Detector::store_detected_assets( $detected_assets ); | |
| 298 | + } | |
| 296 | 299 | |
| 297 | 300 | $blocks_css = ''; |
| 298 | 301 | |
| 299 | 302 | // Prepare blocks assets. |
| 300 | 303 | foreach ( $blocks as $block ) { |
| 301 | - // GhostKit blocks. | |
| 302 | - if ( isset( $block['blockName'] ) && strpos( $block['blockName'], 'ghostkit/' ) === 0 ) { | |
| 303 | - $block_name = preg_replace( '/^ghostkit\//', '', $block['blockName'] ); | |
| 304 | - | |
| 305 | - self::store_used_assets( 'ghostkit-block-' . $block_name, true, 'style' ); | |
| 306 | - self::store_used_assets( 'ghostkit-block-' . $block_name, true, 'script' ); | |
| 307 | - } | |
| 308 | - | |
| 309 | 304 | // Filter blocks custom CSS. |
| 310 | 305 | if ( isset( $block['blockName'] ) ) { |
| 311 | 306 | $custom_styles = apply_filters( 'gkt_block_custom_styles', '', $block ); |
| 312 | 307 | |
| @@ -326,8 +321,32 @@ | ||
| 326 | 321 | } |
| 327 | 322 | } |
| 328 | 323 | |
| 329 | 324 | /** |
| 325 | + * Enqueue extension, style-variant, and attr-based assets during block render. | |
| 326 | + * | |
| 327 | + * Block.json handles are detected at parse-time only; this callback covers | |
| 328 | + * attrs and rendered HTML markers (e.g. effects, list style variants). | |
| 329 | + * | |
| 330 | + * @param string $block_content rendered block content. | |
| 331 | + * @param array $block block data. | |
| 332 | + * | |
| 333 | + * @return string | |
| 334 | + */ | |
| 335 | + public function render_block_enqueue_assets( $block_content, $block ) { | |
| 336 | + if ( ! is_array( $block ) ) { | |
| 337 | + return $block_content; | |
| 338 | + } | |
| 339 | + | |
| 340 | + $content = is_string( $block_content ) ? $block_content : ''; | |
| 341 | + | |
| 342 | + $detected_assets = GhostKit_Assets_Detector::detect_from_block( $block, 'render', $content ); | |
| 343 | + GhostKit_Assets_Detector::enqueue_detected_assets( $detected_assets ); | |
| 344 | + | |
| 345 | + return $block_content; | |
| 346 | + } | |
| 347 | + | |
| 348 | + /** | |
| 330 | 349 | * Enqueue scripts and styles actions. |
| 331 | 350 | * We need this to be used inside 'plugins_loaded' action to prevent conflicts with plugins and themes. |
| 332 | 351 | * |
| 333 | 352 | * 1. Enqueue plugins assets |
| @@ -354,9 +373,9 @@ | ||
| 354 | 373 | do_action( 'gkt_before_assets_register' ); |
| 355 | 374 | |
| 356 | 375 | // Motion. |
| 357 | 376 | if ( apply_filters( 'gkt_enqueue_plugin_motion', true ) ) { |
| 358 | - self::register_script( 'motion', 'assets/vendor/motion/dist/motion.min', array(), '10.16.2' ); | |
| 377 | + self::register_script( 'motion', 'assets/vendor/motion/dist/motion.min', array(), '11.15.0' ); | |
| 359 | 378 | |
| 360 | 379 | $js_deps[] = 'motion'; |
| 361 | 380 | } |
| 362 | 381 | |
| @@ -366,10 +385,10 @@ | ||
| 366 | 385 | } |
| 367 | 386 | |
| 368 | 387 | // Jarallax. |
| 369 | 388 | if ( apply_filters( 'gkt_enqueue_plugin_jarallax', true ) ) { |
| 370 | - self::register_script( 'jarallax', 'assets/vendor/jarallax/dist/jarallax.min', array(), '2.0.1' ); | |
| 371 | - self::register_script( 'jarallax-video', 'assets/vendor/jarallax/dist/jarallax-video.min', array( 'jarallax' ), '2.0.1' ); | |
| 389 | + self::register_script( 'jarallax', 'assets/vendor/jarallax/dist/jarallax.min', array(), '3.1.0' ); | |
| 390 | + self::register_script( 'jarallax-video', 'assets/vendor/jarallax/dist/jarallax-video.min', array( 'jarallax' ), '3.1.0' ); | |
| 372 | 391 | } |
| 373 | 392 | |
| 374 | 393 | // Swiper. |
| 375 | 394 | if ( apply_filters( 'gkt_enqueue_plugin_swiper', true ) ) { |
| @@ -390,16 +409,16 @@ | ||
| 390 | 409 | } |
| 391 | 410 | |
| 392 | 411 | // Lottie Player. |
| 393 | 412 | if ( apply_filters( 'gkt_enqueue_plugin_lottie_player', true ) ) { |
| 394 | - self::register_script( 'lottie-player', 'assets/vendor/lottie-player/dist/lottie-player', array(), '2.0.3' ); | |
| 413 | + self::register_script( 'lottie-player', 'assets/vendor/lottie-player/dist/lottie-player', array(), '2.0.12' ); | |
| 395 | 414 | wp_script_add_data( 'lottie-player', 'async', true ); |
| 396 | 415 | } |
| 397 | 416 | |
| 398 | 417 | // GistEmbed. |
| 399 | 418 | if ( apply_filters( 'gkt_enqueue_plugin_gist_simple', true ) ) { |
| 400 | - self::register_style( 'gist-simple', 'assets/vendor/gist-simple/dist/gist-simple', array(), '2.0.0' ); | |
| 401 | - self::register_script( 'gist-simple', 'assets/vendor/gist-simple/dist/gist-simple.min', array(), '2.0.0' ); | |
| 419 | + self::register_style( 'gist-simple', 'assets/vendor/gist-simple/dist/gist-simple', array(), '2.1.0' ); | |
| 420 | + self::register_script( 'gist-simple', 'assets/vendor/gist-simple/dist/gist-simple.min', array(), '2.1.0' ); | |
| 402 | 421 | } |
| 403 | 422 | |
| 404 | 423 | // Google reCaptcha. |
| 405 | 424 | $recaptcha_site_key = get_option( 'ghostkit_google_recaptcha_api_site_key' ); |
| @@ -511,9 +530,8 @@ | ||
| 511 | 530 | 'build/gutenberg/style', |
| 512 | 531 | $css_deps |
| 513 | 532 | ); |
| 514 | 533 | wp_style_add_data( 'ghostkit', 'rtl', 'replace' ); |
| 515 | - wp_style_add_data( 'ghostkit', 'suffix', '.min' ); | |
| 516 | 534 | |
| 517 | 535 | self::register_script( |
| 518 | 536 | 'ghostkit', |
| 519 | 537 | 'build/assets/js/main', |
| @@ -536,9 +554,8 @@ | ||
| 536 | 554 | 'ghostkit-extension-' . $ext_name, |
| 537 | 555 | $ext_script_url, |
| 538 | 556 | array_unique( $ext_js_deps ) |
| 539 | 557 | ); |
| 540 | - self::store_used_assets( 'ghostkit-extension-' . $ext_name, true, 'script' ); | |
| 541 | 558 | } |
| 542 | 559 | |
| 543 | 560 | // Style Variants. |
| 544 | 561 | foreach ( glob( ghostkit()->plugin_path . 'build/gutenberg/style-variants/*/frontend.js' ) as $template ) { |
| @@ -550,9 +567,8 @@ | ||
| 550 | 567 | 'ghostkit-style-variant-' . $ext_name, |
| 551 | 568 | $ext_script_url, |
| 552 | 569 | array_unique( $ext_js_deps ) |
| 553 | 570 | ); |
| 554 | - self::store_used_assets( 'ghostkit-style-variant-' . $ext_name, true, 'script' ); | |
| 555 | 571 | } |
| 556 | 572 | |
| 557 | 573 | // Blocks. |
| 558 | 574 | foreach ( glob( ghostkit()->plugin_path . 'build/gutenberg/blocks/*/frontend.js' ) as $file ) { |
| @@ -613,9 +629,9 @@ | ||
| 613 | 629 | array_unique( $block_js_deps ) |
| 614 | 630 | ); |
| 615 | 631 | } |
| 616 | 632 | foreach ( glob( ghostkit()->plugin_path . 'build/gutenberg/blocks/*/styles/style.css' ) as $file ) { |
| 617 | - $block_name = basename( dirname( dirname( $file ) ) ); | |
| 633 | + $block_name = basename( dirname( $file, 2 ) ); | |
| 618 | 634 | $block_style_url = 'build/gutenberg/blocks/' . $block_name . '/styles/style'; |
| 619 | 635 | $block_css_deps = array( 'ghostkit' ); |
| 620 | 636 | |
| 621 | 637 | switch ( $block_name ) { |
| @@ -635,9 +651,8 @@ | ||
| 635 | 651 | $block_style_url, |
| 636 | 652 | array_unique( $block_css_deps ) |
| 637 | 653 | ); |
| 638 | 654 | wp_style_add_data( 'ghostkit-block-' . $block_name, 'rtl', 'replace' ); |
| 639 | - wp_style_add_data( 'ghostkit-block-' . $block_name, 'suffix', '.min' ); | |
| 640 | 655 | } |
| 641 | 656 | |
| 642 | 657 | do_action( 'gkt_after_assets_register' ); |
| 643 | 658 | } |
| @@ -753,12 +768,14 @@ | ||
| 753 | 768 | * @param string $result - allowed list. |
| 754 | 769 | * @return string |
| 755 | 770 | */ |
| 756 | 771 | public static function autoptimize_filter_css_exclude( $result ) { |
| 772 | + $upload_dir = defined( 'UPLOADS' ) ? UPLOADS : 'wp-content/uploads/'; | |
| 773 | + | |
| 757 | 774 | // By default in Autoptimize excluded folder `wp-content/uploads/`. |
| 758 | 775 | // We need to check, if this folder is not excluded, then we |
| 759 | 776 | // don't need to use our hack. |
| 760 | - if ( $result && strpos( $result, 'wp-content/uploads/' ) === false ) { | |
| 777 | + if ( $result && strpos( $result, $upload_dir ) === false ) { | |
| 761 | 778 | return $result; |
| 762 | 779 | } |
| 763 | 780 | |
| 764 | 781 | foreach ( self::$skip_from_autoptimize as $name ) { |