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 +69 -41 18.1 → trunk View file →
@@ -78,16 +78,15 @@
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 ) );
87 - $alt = WPSEO_Image_Utils::get_alt_tag( $thumbnail_id );
88 - $title = get_post_field( 'post_title', $thumbnail_id );
89 - $images[] = $this->get_image_item( $post, $src, $title, $alt );
88 + $images[] = $this->get_image_item( $post, $src );
90 89 }
91 90
92 91 /**
93 92 * Filter: 'wpseo_sitemap_content_before_parse_html_images' - Filters the post content
@@ -99,25 +98,19 @@
99 98
100 99 $unfiltered_images = $this->parse_html_images( $content );
101 100
102 101 foreach ( $unfiltered_images as $image ) {
103 - $images[] = $this->get_image_item( $post, $image['src'], $image['title'], $image['alt'] );
102 + $images[] = $this->get_image_item( $post, $image['src'] );
104 103 }
105 104
106 105 foreach ( $this->parse_galleries( $content, $post->ID ) as $attachment ) {
107 -
108 - $src = $this->get_absolute_url( $this->image_url( $attachment->ID ) );
109 - $alt = WPSEO_Image_Utils::get_alt_tag( $attachment->ID );
110 -
111 - $images[] = $this->get_image_item( $post, $src, $attachment->post_title, $alt );
106 + $src = $this->get_absolute_url( $this->image_url( $attachment->ID ) );
107 + $images[] = $this->get_image_item( $post, $src );
112 108 }
113 109
114 110 if ( $post->post_type === 'attachment' && wp_attachment_is_image( $post ) ) {
115 -
116 - $src = $this->get_absolute_url( $this->image_url( $post->ID ) );
117 - $alt = WPSEO_Image_Utils::get_alt_tag( $post->ID );
118 -
119 - $images[] = $this->get_image_item( $post, $src, $post->post_title, $alt );
111 + $src = $this->get_absolute_url( $this->image_url( $post->ID ) );
112 + $images[] = $this->get_image_item( $post, $src );
120 113 }
121 114
122 115 foreach ( $images as $key => $image ) {
123 116
@@ -131,14 +124,45 @@
131 124 *
132 125 * @param array $images Array of image items.
133 126 * @param int $post_id ID of the post.
134 127 */
135 - $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 + }
136 132
137 133 return $images;
138 134 }
139 135
140 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 + /**
141 165 * Get the images in the term description.
142 166 *
143 167 * @param object $term Term to get images from description for.
144 168 *
@@ -151,13 +175,22 @@
151 175 foreach ( $this->parse_galleries( $term->description ) as $attachment ) {
152 176
153 177 $images[] = [
154 178 'src' => $this->get_absolute_url( $this->image_url( $attachment->ID ) ),
155 - 'title' => $attachment->post_title,
156 - 'alt' => WPSEO_Image_Utils::get_alt_tag( $attachment->ID ),
157 179 ];
158 180 }
159 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 +
160 193 return $images;
161 194 }
162 195
163 196 /**
@@ -187,9 +220,13 @@
187 220
188 221 // Clear the errors, so they don't get kept in memory.
189 222 libxml_clear_errors();
190 223
191 - /** @var DOMElement $img */
224 + /**
225 + * Image attribute.
226 + *
227 + * @var DOMElement $img
228 + */
192 229 foreach ( $post_dom->getElementsByTagName( 'img' ) as $img ) {
193 230
194 231 $src = $img->getAttribute( 'src' );
195 232
@@ -204,9 +241,14 @@
204 241 && ( strpos( $class, 'size-full' ) === false )
205 242 && preg_match( '|wp-image-(?P<id>\d+)|', $class, $matches )
206 243 && get_post_status( $matches['id'] )
207 244 ) {
208 - $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 + }
209 251 }
210 252
211 253 $src = $this->get_absolute_url( $src );
212 254
@@ -213,16 +255,14 @@
213 255 if ( strpos( $src, $this->host ) === false ) {
214 256 continue;
215 257 }
216 258
217 - if ( $src !== esc_url( $src ) ) {
259 + if ( $src !== esc_url( $src, null, 'attribute' ) ) {
218 260 continue;
219 261 }
220 262
221 263 $images[] = [
222 264 'src' => $src,
223 - 'title' => $img->getAttribute( 'title' ),
224 - 'alt' => $img->getAttribute( 'alt' ),
225 265 ];
226 266 }
227 267
228 268 return $images;
@@ -245,9 +285,9 @@
245 285
246 286 $id = $post_id;
247 287
248 288 if ( ! empty( $gallery['id'] ) ) {
249 - $id = intval( $gallery['id'] );
289 + $id = (int) $gallery['id'];
250 290 }
251 291
252 292 // Forked from core gallery_shortcode() to have exact same logic. R.
253 293 if ( ! empty( $gallery['ids'] ) ) {
@@ -295,16 +335,14 @@
295 335
296 336 /**
297 337 * Get image item array with filters applied.
298 338 *
299 - * @param WP_Post $post Post object for the context.
300 - * @param string $src Image URL.
301 - * @param string $title Optional image title.
302 - * @param string $alt Optional image alt text.
339 + * @param WP_Post $post Post object for the context.
340 + * @param string $src Image URL.
303 341 *
304 342 * @return array
305 343 */
306 - protected function get_image_item( $post, $src, $title = '', $alt = '' ) {
344 + protected function get_image_item( $post, $src ) {
307 345
308 346 $image = [];
309 347
310 348 /**
@@ -314,16 +352,8 @@
314 352 * @param object $post Post object.
315 353 */
316 354 $image['src'] = apply_filters( 'wpseo_xml_sitemap_img_src', $src, $post );
317 355
318 - if ( ! empty( $title ) ) {
319 - $image['title'] = $title;
320 - }
321 -
322 - if ( ! empty( $alt ) ) {
323 - $image['alt'] = $alt;
324 - }
325 -
326 356 /**
327 357 * Filter image data to be included in XML sitemap for the post.
328 358 *
329 359 * @param array $image {
@@ -329,10 +359,8 @@
329 359 * @param array $image {
330 360 * Array of image data.
331 361 *
332 362 * @type string $src Image URL.
333 - * @type string $title Image title attribute (optional).
334 - * @type string $alt Image alt attribute (optional).
335 363 * }
336 364 *
337 365 * @param object $post Post object.
338 366 */
@@ -467,13 +495,13 @@
467 495 $attachments = $this->get_attachments(
468 496 [
469 497 'posts_per_page' => count( $ids_to_include ),
470 498 'post__in' => $ids_to_include,
471 - ]
499 + ],
472 500 );
473 501
474 502 $gallery_attachments = [];
475 - foreach ( $attachments as $key => $val ) {
503 + foreach ( $attachments as $val ) {
476 504 $gallery_attachments[ $val->ID ] = $val;
477 505 }
478 506
479 507 return $gallery_attachments;