Skip to content

Commit

Permalink
Widgets: enqueue stylesheet directly in the widget display method
Browse files Browse the repository at this point in the history
Fixes #39813

Instead of relying on a conditional check to see if the widget is active, let's call the widgets' enqueuing methods directly from within the widget display method. That should simplify things, and ensure compatiblity with systems that are not compatible with `is_active_widget`.
  • Loading branch information
jeherve committed Oct 18, 2024
1 parent c82adbd commit fcc3a72
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Classic Widget: update assets' enqueuing strategy to ensure compatibility with the Elementor plugin.
7 changes: 5 additions & 2 deletions projects/packages/search/src/widgets/class-search-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function widget_admin_setup() {
* @since 5.8.0
*/
public function enqueue_frontend_scripts() {
if ( ! is_active_widget( false, false, $this->id_base, true ) || Options::is_instant_enabled() ) {
if ( Options::is_instant_enabled() ) {
return;
}
Assets::register_script(
Expand All @@ -178,9 +178,9 @@ public function enqueue_frontend_scripts() {
// @see https://github.com/Automattic/jetpack/blob/b3de78dce3d88b0d9b283282a5b04515245c8057/projects/plugins/jetpack/tools/builder/frontend-css.js#L52.
// @see https://github.com/Automattic/jetpack/blob/bb1b6a9a9cfa98600441f8fa31c9f9c4ef9a04a5/projects/plugins/jetpack/class.jetpack.php#L106.
'css_path' => 'css/search-widget-frontend.css',
'enqueue' => true,
)
);
Assets::enqueue_script( 'jetpack-search-widget' );
}

/**
Expand Down Expand Up @@ -301,6 +301,9 @@ public function widget( $args, $instance ) {
return;
}

// Enqueue front end assets.
$this->enqueue_frontend_scripts();

if ( Options::is_instant_enabled() ) {
if ( array_key_exists( 'id', $args ) && Instant_Search::INSTANT_SEARCH_SIDEBAR === $args['id'] ) {
$this->widget_empty_instant( $args, $instance );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: compat

Extra Sidebar Widgets: update assets' enqueuing strategy to ensure compatibility with the Elementor plugin.
5 changes: 4 additions & 1 deletion projects/plugins/jetpack/modules/widgets/authors.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct() {
)
);

if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
}

Expand Down Expand Up @@ -68,6 +68,9 @@ public static function flush_cache() {
* @param array $instance Widget settings for the instance.
*/
public function widget( $args, $instance ) {
// Enqueue front end assets.
$this->enqueue_style();

$cache_bucket = is_ssl() ? 'widget_authors_ssl' : 'widget_authors';

if ( '%BEG_OF_TITLE%' !== $args['before_title'] ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function __construct() {
array()
);

if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
}
}
Expand Down Expand Up @@ -165,6 +165,9 @@ public function widget( $args, $instance ) {
return;
}

// Enqueue front end assets.
$this->enqueue_frontend_scripts();

$instance = wp_parse_args( $instance, $this->defaults() );

if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct() {
)
);

if ( is_active_widget( false, false, self::ID_BASE ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_css' ) );
}

Expand Down Expand Up @@ -275,6 +275,9 @@ public function widget( $args, $instance ) {
return;
}

// Enqueue front end assets.
$this->enqueue_css();

echo $args['before_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

// Always show a title on an unconfigured widget.
Expand Down
5 changes: 4 additions & 1 deletion projects/plugins/jetpack/modules/widgets/flickr.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct() {
array()
);

if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
}
}
Expand Down Expand Up @@ -74,6 +74,9 @@ public function defaults() {
public function widget( $args, $instance ) {
$instance = wp_parse_args( $instance, $this->defaults() );

// Enqueue front end assets.
$this->enqueue_style();

if ( ! empty( $instance['flickr_rss_url'] ) ) {
/*
* Parse the URL, and rebuild a URL that's sure to display images.
Expand Down
7 changes: 5 additions & 2 deletions projects/plugins/jetpack/modules/widgets/goodreads.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public function __construct() {
'to-read' => _x( 'To Read', 'my list of books to read', 'jetpack' ),
);

if ( is_active_widget( '', '', 'wpcom-goodreads' ) || is_customize_preview() ) {
add_action( 'wp_print_styles', array( $this, 'enqueue_style' ) );
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
}
add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) );
}
Expand Down Expand Up @@ -114,6 +114,9 @@ public function widget( $args, $instance ) {
return;
}

// Enqueue front end assets.
$this->enqueue_style();

$instance['user_id'] = absint( $instance['user_id'] );

// Set widget ID based on shelf.
Expand Down
5 changes: 4 additions & 1 deletion projects/plugins/jetpack/modules/widgets/image-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct() {
)
);

if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
}
}
Expand Down Expand Up @@ -79,6 +79,9 @@ public function widget( $args, $instance ) {

if ( $instance['img_url'] ) {

// Enqueue front end assets.
$this->enqueue_style();

$output = '<img src="' . esc_url( $instance['img_url'] ) . '" ';

if ( '' !== (string) $instance['alt_text'] ) {
Expand Down
5 changes: 4 additions & 1 deletion projects/plugins/jetpack/modules/widgets/my-community.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct() {
)
);

if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
}

Expand Down Expand Up @@ -191,6 +191,9 @@ public function widget( $args, $instance ) {
$title = $this->default_title;
}

// Enqueue front end assets.
$this->enqueue_style();

echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
Expand Down
5 changes: 4 additions & 1 deletion projects/plugins/jetpack/modules/widgets/simple-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function __construct() {
add_action( 'wp_ajax_customize-jetpack-simple-payments-button-delete', array( $this, 'ajax_delete_payment_button' ) );
}

if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
}

Expand Down Expand Up @@ -419,6 +419,9 @@ public function get_first_product_id() {
public function widget( $args, $instance ) {
$instance = wp_parse_args( $instance, $this->defaults() );

// Enqueue front end assets.
$this->enqueue_style();

echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

/** This filter is documented in core/src/wp-includes/default-widgets.php */
Expand Down
8 changes: 6 additions & 2 deletions projects/plugins/jetpack/modules/widgets/social-icons.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public function __construct() {
add_action( 'admin_print_footer_scripts', array( $this, 'render_admin_js' ) );
}

// Enqueue scripts and styles for the display of the widget, on the frontend or in the customizer.
if ( is_active_widget( false, $this->id, $this->id_base, true ) || is_customize_preview() ) {
// Enqueue scripts and styles for the display of the widget in the customizer.
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_icon_scripts' ) );
add_action( 'wp_footer', array( $this, 'include_svg_icons' ), 9999 );
}
Expand Down Expand Up @@ -160,6 +160,10 @@ public function include_svg_icons() {
public function widget( $args, $instance ) {
$instance = wp_parse_args( $instance, $this->defaults );

// Enqueue front end assets.
$this->enqueue_icon_scripts();
add_action( 'wp_footer', array( $this, 'include_svg_icons' ), 9999 );

/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );

Expand Down
10 changes: 9 additions & 1 deletion projects/plugins/jetpack/modules/widgets/social-media-icons.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function __construct() {
'twitch' => array( 'Twitch', 'https://www.twitch.tv/%s/' ),
'tumblr' => array( 'Tumblr', 'https://%s.tumblr.com' ),
);
if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
}
}
Expand Down Expand Up @@ -128,9 +128,17 @@ public function widget( $args, $instance ) {
$instance = wp_parse_args( (array) $instance, $this->defaults );
/** This filter is documented in core/src/wp-includes/default-widgets.php */
$instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );

/*
* Enqueue frontend assets.
*/

if ( ! $this->check_genericons() ) {
wp_enqueue_style( 'genericons' );
}

$this->enqueue_style();

$index = 10;
$html = array();
/* Translators: 1. Username. 2. Service name. */
Expand Down
5 changes: 4 additions & 1 deletion projects/plugins/jetpack/modules/widgets/top-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct() {

$this->default_title = __( 'Top Posts &amp; Pages', 'jetpack' );

if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
}

Expand Down Expand Up @@ -306,6 +306,9 @@ public function widget( $args, $instance ) {
/** This filter is documented in core/src/wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title );

// Enqueue front end assets.
$this->enqueue_style();

$count = isset( $instance['count'] ) ? (int) $instance['count'] : false;
if ( $count < 1 || 10 < $count ) {
$count = 10;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Classic Widget: update assets' enqueuing strategy to ensure compatibility with the Elementor plugin.

0 comments on commit fcc3a72

Please sign in to comment.