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
This commit is contained in:
Jonny Harris
2023-09-04 16:31:30 +00:00
parent fa9f8397bc
commit 6be588c72f

View File

@@ -248,6 +248,8 @@ class WP_Widget_Media_Gallery extends WP_Widget_Media {
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;