Skip to content

Commit

Permalink
fix: improve widget asset loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Soare-Robert-Daniel committed Sep 14, 2023
1 parent afd2ede commit e19da4b
Showing 1 changed file with 45 additions and 9 deletions.
54 changes: 45 additions & 9 deletions inc/class-registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class Registration {
*/
public static $block_dependencies = array();

/**
* The ids of the used widgets in the page.
*
* @var array<string>
*/
public static $widget_used = array(); // TODO: Monitor all the rendered widgets and enqueue the assets.

/**
* Flag to mark that the scripts which have loaded.
*
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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' ) ) {
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down

0 comments on commit e19da4b

Please sign in to comment.