From e19da4b9e1fe040538611b500e180b8439a8f7ab Mon Sep 17 00:00:00 2001 From: "Soare Robert Daniel (Mac 2023)" Date: Thu, 14 Sep 2023 10:38:51 +0300 Subject: [PATCH] fix: improve widget asset loading --- inc/class-registration.php | 54 +++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/inc/class-registration.php b/inc/class-registration.php index 5451e5924..3345fe8b4 100644 --- a/inc/class-registration.php +++ b/inc/class-registration.php @@ -35,6 +35,13 @@ class Registration { */ public static $block_dependencies = array(); + /** + * The ids of the used widgets in the page. + * + * @var array + */ + public static $widget_used = array(); // TODO: Monitor all the rendered widgets and enqueue the assets. + /** * Flag to mark that the scripts which have loaded. * @@ -81,6 +88,7 @@ public function init() { add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) ); add_filter( 'render_block', array( $this, 'load_sticky' ), 900, 2 ); add_filter( 'render_block', array( $this, 'subscribe_fa' ), 10, 2 ); + add_filter( 'dynamic_sidebar_params', array( $this, 'watch_used_widgets' ), 9999 ); add_action( 'wp_footer', @@ -348,7 +356,15 @@ function ( $content ) { } if ( $has_widgets ) { - $this->enqueue_dependencies( 'widgets' ); + add_filter( + 'wp_footer', + function ( $content ) { + $this->enqueue_dependencies( 'widgets' ); + + return $content; + } + ); + } if ( function_exists( 'get_block_templates' ) && function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() && current_theme_supports( 'block-templates' ) ) { @@ -967,25 +983,31 @@ public static function sticky_style() { * @return string */ public static function get_active_widgets_content() { + $content = ''; + + if ( 0 === count( self::$widget_used ) ) { + return $content; + } + global $wp_registered_widgets; - $content = ''; $valid_widgets = array(); $widget_data = get_option( 'widget_block', array() ); // Loop through all widgets, and add any that are active. foreach ( $wp_registered_widgets as $widget_name => $widget ) { - // Get the active sidebar the widget is located in. - $sidebar = is_active_widget( $widget['callback'], $widget['id'] ); + if ( ! in_array( $widget['id'], self::$widget_used, true ) ) { + continue; + } - if ( $sidebar && 'wp_inactive_widgets' !== $sidebar ) { - $key = $widget['params'][0]['number']; + $key = $widget['params'][0]['number']; - if ( isset( $widget_data[ $key ] ) ) { - $valid_widgets[] = (object) $widget_data[ $key ]; - } + if ( isset( $widget_data[ $key ] ) ) { + $valid_widgets[] = (object) $widget_data[ $key ]; } } + self::$widget_used = array(); + foreach ( $valid_widgets as $widget ) { if ( isset( $widget->content ) ) { $content .= $widget->content; @@ -995,6 +1017,20 @@ public static function get_active_widgets_content() { return $content; } + /** + * Watch and save the used widgets. + * + * @param array $params The widget params. + * @return mixed + */ + public function watch_used_widgets( $params ) { + if ( isset( $params[0]['widget_id'] ) && ! in_array( $params[0]['widget_id'], self::$widget_used ) ) { + self::$widget_used[] = $params[0]['widget_id']; + } + + return $params; + } + /** * The instance method for the static class. * Defines and returns the instance of the static class.