PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
← All changes | inc/sitemaps/class-sitemap-image-parser.php +57 -9 18.9 → trunk View file →
@@ -78,9 +78,10 @@
78 78 if ( ! is_object( $post ) ) {
79 79 return $images;
80 80 }
81 81
82 - $thumbnail_id = get_post_thumbnail_id( $post->ID );
82 + // Pass the post object rather than its ID, so the post does not get re-fetched from the database.
83 + $thumbnail_id = get_post_thumbnail_id( $post );
83 84
84 85 if ( $thumbnail_id ) {
85 86
86 87 $src = $this->get_absolute_url( $this->image_url( $thumbnail_id ) );
@@ -123,14 +124,45 @@
123 124 *
124 125 * @param array $images Array of image items.
125 126 * @param int $post_id ID of the post.
126 127 */
127 - $images = apply_filters( 'wpseo_sitemap_urlimages', $images, $post->ID );
128 + $image_list = apply_filters( 'wpseo_sitemap_urlimages', $images, $post->ID );
129 + if ( isset( $image_list ) && is_array( $image_list ) ) {
130 + $images = $image_list;
131 + }
128 132
129 133 return $images;
130 134 }
131 135
132 136 /**
137 + * Primes the meta caches of the featured images of the given posts.
138 + *
139 + * This parser reads each post's featured image file location from the attachment's
140 + * meta individually; warming that meta cache in bulk avoids one query per post on
141 + * setups without a persistent object cache.
142 + *
143 + * @param WP_Post[] $posts The posts to prime the featured-image caches for.
144 + *
145 + * @return void
146 + */
147 + public function prime_thumbnail_caches( $posts ) {
148 +
149 + $thumbnail_ids = [];
150 +
151 + foreach ( $posts as $post ) {
152 + $thumbnail_id = get_post_thumbnail_id( $post );
153 +
154 + if ( $thumbnail_id ) {
155 + $thumbnail_ids[] = $thumbnail_id;
156 + }
157 + }
158 +
159 + if ( ! empty( $thumbnail_ids ) ) {
160 + update_meta_cache( 'post', array_unique( $thumbnail_ids ) );
161 + }
162 + }
163 +
164 + /**
133 165 * Get the images in the term description.
134 166 *
135 167 * @param object $term Term to get images from description for.
136 168 *
@@ -146,8 +178,19 @@
146 178 'src' => $this->get_absolute_url( $this->image_url( $attachment->ID ) ),
147 179 ];
148 180 }
149 181
182 + /**
183 + * Filter images to be included for the term in XML sitemap.
184 + *
185 + * @param array $image_list Array of image items.
186 + * @param int $term_id ID of the post.
187 + */
188 + $image_list = apply_filters( 'wpseo_sitemap_urlimages_term', $images, $term->term_id );
189 + if ( isset( $image_list ) && is_array( $image_list ) ) {
190 + $images = $image_list;
191 + }
192 +
150 193 return $images;
151 194 }
152 195
153 196 /**
@@ -198,9 +241,14 @@
198 241 && ( strpos( $class, 'size-full' ) === false )
199 242 && preg_match( '|wp-image-(?P<id>\d+)|', $class, $matches )
200 243 && get_post_status( $matches['id'] )
201 244 ) {
202 - $src = $this->image_url( $matches['id'] );
245 + $query_params = wp_parse_url( $src, PHP_URL_QUERY );
246 + $src = $this->image_url( $matches['id'] );
247 +
248 + if ( $query_params ) {
249 + $src .= '?' . $query_params;
250 + }
203 251 }
204 252
205 253 $src = $this->get_absolute_url( $src );
206 254
@@ -207,9 +255,9 @@
207 255 if ( strpos( $src, $this->host ) === false ) {
208 256 continue;
209 257 }
210 258
211 - if ( $src !== esc_url( $src ) ) {
259 + if ( $src !== esc_url( $src, null, 'attribute' ) ) {
212 260 continue;
213 261 }
214 262
215 263 $images[] = [
@@ -237,9 +285,9 @@
237 285
238 286 $id = $post_id;
239 287
240 288 if ( ! empty( $gallery['id'] ) ) {
241 - $id = intval( $gallery['id'] );
289 + $id = (int) $gallery['id'];
242 290 }
243 291
244 292 // Forked from core gallery_shortcode() to have exact same logic. R.
245 293 if ( ! empty( $gallery['ids'] ) ) {
@@ -287,10 +335,10 @@
287 335
288 336 /**
289 337 * Get image item array with filters applied.
290 338 *
291 - * @param WP_Post $post Post object for the context.
292 - * @param string $src Image URL.
339 + * @param WP_Post $post Post object for the context.
340 + * @param string $src Image URL.
293 341 *
294 342 * @return array
295 343 */
296 344 protected function get_image_item( $post, $src ) {
@@ -447,13 +495,13 @@
447 495 $attachments = $this->get_attachments(
448 496 [
449 497 'posts_per_page' => count( $ids_to_include ),
450 498 'post__in' => $ids_to_include,
451 - ]
499 + ],
452 500 );
453 501
454 502 $gallery_attachments = [];
455 - foreach ( $attachments as $key => $val ) {
503 + foreach ( $attachments as $val ) {
456 504 $gallery_attachments[ $val->ID ] = $val;
457 505 }
458 506
459 507 return $gallery_attachments;