From 10d3308439740fc9c54026589eb41a0a0d6d6ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Mon, 20 Jul 2026 13:04:11 +0200 Subject: [PATCH 1/3] Widgets: Simplify WP_Widget_Media_Gallery::has_content(). Flatten the nested conditional into an early return when no IDs are present, and replace the manual foreach loop that verifies every gallery ID resolves to an attachment post with the array_all() function, introduced in PHP 8.4 and available in Core via the polyfill in wp-includes/compat.php since 6.8.0. This makes the intent of the check more explicit without changing behavior. --- .../widgets/class-wp-widget-media-gallery.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/widgets/class-wp-widget-media-gallery.php b/src/wp-includes/widgets/class-wp-widget-media-gallery.php index 9f76bc87106f1..3b464cbcaff7a 100644 --- a/src/wp-includes/widgets/class-wp-widget-media-gallery.php +++ b/src/wp-includes/widgets/class-wp-widget-media-gallery.php @@ -245,17 +245,12 @@ public function render_control_template_scripts() { * @return bool Whether widget has content. */ protected function has_content( $instance ) { - if ( ! empty( $instance['ids'] ) ) { - $attachments = wp_parse_id_list( $instance['ids'] ); - // Prime attachment post caches. - _prime_post_caches( $attachments, false, false ); - foreach ( $attachments as $attachment ) { - if ( 'attachment' !== get_post_type( $attachment ) ) { - return false; - } - } - return true; + if ( empty( $instance['ids'] ) ) { + return false; } - return false; + $attachments = wp_parse_id_list( $instance['ids'] ); + // Prime attachment post caches. + _prime_post_caches( $attachments, false, false ); + return array_all( $attachments, fn( $attachment ) => 'attachment' === get_post_type( $attachment ) ); } } From 8dda85afc8bc6e0d8a0eec51bee045941cfaf59e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Mon, 20 Jul 2026 13:13:28 +0200 Subject: [PATCH 2/3] Format code --- src/wp-includes/widgets/class-wp-widget-media-gallery.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/widgets/class-wp-widget-media-gallery.php b/src/wp-includes/widgets/class-wp-widget-media-gallery.php index 3b464cbcaff7a..799db75b85cf1 100644 --- a/src/wp-includes/widgets/class-wp-widget-media-gallery.php +++ b/src/wp-includes/widgets/class-wp-widget-media-gallery.php @@ -248,9 +248,14 @@ protected function has_content( $instance ) { if ( empty( $instance['ids'] ) ) { return false; } + $attachments = wp_parse_id_list( $instance['ids'] ); // Prime attachment post caches. _prime_post_caches( $attachments, false, false ); - return array_all( $attachments, fn( $attachment ) => 'attachment' === get_post_type( $attachment ) ); + + return array_all( + $attachments, + fn( $attachment ) => 'attachment' === get_post_type( $attachment ) + ); } } From 56fec9662eec18b51dd954636b8c4aefa69ec499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Tue, 21 Jul 2026 08:53:45 +0200 Subject: [PATCH 3/3] Apply suggestion from @mukeshpanchal27 Co-authored-by: Mukesh Panchal --- src/wp-includes/widgets/class-wp-widget-media-gallery.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/widgets/class-wp-widget-media-gallery.php b/src/wp-includes/widgets/class-wp-widget-media-gallery.php index 799db75b85cf1..f7167792740f5 100644 --- a/src/wp-includes/widgets/class-wp-widget-media-gallery.php +++ b/src/wp-includes/widgets/class-wp-widget-media-gallery.php @@ -255,7 +255,7 @@ protected function has_content( $instance ) { return array_all( $attachments, - fn( $attachment ) => 'attachment' === get_post_type( $attachment ) + fn( $attachment ) => 'attachment' === get_post_type( $attachment ) ); } }