From 6be588c72f768430c98b8bcc1ae0b9d30826fddf Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Mon, 4 Sep 2023 16:31:30 +0000 Subject: [PATCH] Widgets: Improve performance of has_content method in WP_Widget_Media_Gallery class. In the method `WP_Widget_Media_Gallery::has_content` call `_prime_post_caches` before the foreach loop. This ensures that the post objects are primed in memory before trying to access the post object in `get_post_type`. Props niravsherasiya7707, spacedmonkey, mukesh27. Fixes #58757. git-svn-id: https://develop.svn.wordpress.org/trunk@56512 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/widgets/class-wp-widget-media-gallery.php | 2 ++ 1 file changed, 2 insertions(+) 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 40cc5c2ba4664..ecc446c2218b0 100644 --- a/src/wp-includes/widgets/class-wp-widget-media-gallery.php +++ b/src/wp-includes/widgets/class-wp-widget-media-gallery.php @@ -248,6 +248,8 @@ public function render_control_template_scripts() { 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;