Ticket #44427: 44427.5.diff
File 44427.5.diff, 8.8 KB (added by , 16 months ago) |
---|
-
src/wp-includes/default-filters.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8
176 176 add_filter( 'the_content', 'shortcode_unautop' ); 177 177 add_filter( 'the_content', 'prepend_attachment' ); 178 178 add_filter( 'the_content', 'wp_make_content_images_responsive' ); 179 add_filter( 'the_content', 'wp_lazy_load_content_media' ); 179 180 180 181 add_filter( 'the_excerpt', 'wptexturize' ); 181 182 add_filter( 'the_excerpt', 'convert_smilies' ); 183 add_filter( 'the_excerpt', 'wp_lazy_load_content_media' ); 182 184 add_filter( 'the_excerpt', 'convert_chars' ); 183 185 add_filter( 'the_excerpt', 'wpautop' ); 184 186 add_filter( 'the_excerpt', 'shortcode_unautop' ); … … 193 195 add_filter( 'comment_text', 'make_clickable', 9 ); 194 196 add_filter( 'comment_text', 'force_balance_tags', 25 ); 195 197 add_filter( 'comment_text', 'convert_smilies', 20 ); 198 add_filter( 'comment_text', 'wp_lazy_load_content_media' ); 196 199 add_filter( 'comment_text', 'wpautop', 30 ); 197 200 198 201 add_filter( 'comment_excerpt', 'convert_chars' ); -
src/wp-includes/formatting.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8
3296 3296 return $img; 3297 3297 } 3298 3298 3299 $loading = 'auto'; 3300 /** This filter is documented in wp-includes/media.php */ 3301 if ( apply_filters( 'wp_lazy_load_content_media', true ) ) { 3302 $loading = 'lazy'; 3303 } 3304 3299 3305 /** 3300 3306 * Filters the Smiley image URL before it's used in the image element. 3301 3307 * … … 3307 3313 */ 3308 3314 $src_url = apply_filters( 'smilies_src', includes_url( "images/smilies/$img" ), $img, site_url() ); 3309 3315 3310 return sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', esc_url( $src_url ), esc_attr( $smiley) );3316 return sprintf( '<img src="%s" alt="%s" loading="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', esc_url( $src_url ), esc_attr( $smiley ), esc_attr( $loading ) ); 3311 3317 } 3312 3318 3313 3319 /** -
src/wp-includes/media.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8
1013 1013 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), 1014 1014 ); 1015 1015 1016 /** This filter is documented in wp-includes/media.php */ 1017 if ( apply_filters( 'wp_lazy_load_content_media', true ) ) { 1018 $default_attr['loading'] = 'lazy'; 1019 } 1020 1016 1021 $attr = wp_parse_args( $attr, $default_attr ); 1017 1022 1018 1023 // Generate 'srcset' and 'sizes' if not already present. … … 1593 1598 return $image; 1594 1599 } 1595 1600 1601 /** 1602 * Filters 'img' and 'iframe' elements in post content to add 'loading' attributes. 1603 * 1604 * @since 5.4.0 1605 * 1606 * @param string $content The raw post content to be filtered. 1607 * @return string Converted content with 'loading' attributes added to images. 1608 */ 1609 function wp_lazy_load_content_media( $content ) { 1610 $tags = array( 'img', 'iframe' ); 1611 1612 /** 1613 * Filters whether media in post content should be lazy-loaded with 'loading' attributes. 1614 * 1615 * @since 5.4.0 1616 * 1617 * @param bool $enabled Whether to lazy-load content media. Default true. 1618 */ 1619 if ( ! apply_filters( 'wp_lazy_load_content_media', true ) ) { 1620 return $content; 1621 } 1622 1623 return preg_replace_callback( 1624 '/<(' . implode( '|', $tags ) . ') [^>]+>/', 1625 function( array $matches ) { 1626 if ( false === strpos( $matches[0], ' loading=' ) ) { 1627 $tag = $matches[1]; 1628 return str_replace( '<' . $tag . ' ', '<' . $tag . ' loading="lazy" ', $matches[0] ); 1629 } 1630 return $matches[0]; 1631 }, 1632 $content 1633 ); 1634 } 1635 1596 1636 /** 1597 1637 * Adds a 'wp-post-image' class to post thumbnails. Internal use only. 1598 1638 * -
src/wp-includes/pluggable.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8
2596 2596 'extra_attr' => '', 2597 2597 ); 2598 2598 2599 /** This filter is documented in wp-includes/media.php */ 2600 if ( apply_filters( 'wp_lazy_load_content_media', true ) ) { 2601 $defaults['loading'] = 'lazy'; 2602 } 2603 2599 2604 if ( empty( $args ) ) { 2600 2605 $args = array(); 2601 2606 } -
tests/phpunit/tests/media.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8
1301 1301 * Tests the default output of `wp_get_attachment_image()`. 1302 1302 * 1303 1303 * @ticket 34635 1304 * @ticket 44427 1304 1305 */ 1305 1306 function test_wp_get_attachment_image_defaults() { 1306 1307 $image = image_downsize( self::$large_id, 'thumbnail' ); 1307 1308 $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" />', $image[1], $image[2], $image[0] ); 1308 1309 1310 add_filter( 'wp_lazy_load_content_media', '__return_false' ); 1311 $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) ); 1312 1313 $expected = str_replace( ' alt=""', ' alt="" loading="lazy"', $expected ); 1314 1315 remove_filter( 'wp_lazy_load_content_media', '__return_false' ); 1309 1316 $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) ); 1310 1317 } 1311 1318 … … 1319 1326 update_post_meta( self::$large_id, '_wp_attachment_image_alt', 'Some very clever alt text', true ); 1320 1327 1321 1328 $image = image_downsize( self::$large_id, 'thumbnail' ); 1322 $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" />', $image[1], $image[2], $image[0] );1329 $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" loading="lazy" />', $image[1], $image[2], $image[0] ); 1323 1330 1324 1331 $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) ); 1325 1332 … … 2232 2239 $month = gmdate( 'm' ); 2233 2240 2234 2241 $expected = '<img width="999" height="999" src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png"' . 2235 ' class="attachment-testsize size-testsize" alt="" ' .2242 ' class="attachment-testsize size-testsize" alt="" loading="lazy"' . 2236 2243 ' srcset="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png 999w,' . 2237 2244 ' http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-large-150x150.png 150w"' . 2238 2245 ' sizes="(max-width: 999px) 100vw, 999px" />'; … … 2495 2502 2496 2503 $this->assertSame( $expected, $url ); 2497 2504 } 2505 2506 /** 2507 * @ticket 44427 2508 */ 2509 function test_wp_lazy_load_content_media() { 2510 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2511 $img_xhtml = str_replace( ' />', '/>', $img ); 2512 $img_html5 = str_replace( ' />', '>', $img ); 2513 $iframe = '<iframe src="https://www.example.com"></iframe>'; 2514 2515 $lazy_img = str_replace( '<img ', '<img loading="lazy" ', $img ); 2516 $lazy_img_xhtml = str_replace( '<img ', '<img loading="lazy" ', $img_xhtml ); 2517 $lazy_img_html5 = str_replace( '<img ', '<img loading="lazy" ', $img_html5 ); 2518 $lazy_iframe = str_replace( '<iframe ', '<iframe loading="lazy" ', $iframe ); 2519 2520 // The following should not be modified because there already is a 'loading' attribute. 2521 $img_eager = str_replace( ' />', ' loading="eager" />', $img ); 2522 2523 $content = ' 2524 <p>Image, standard.</p> 2525 %1$s 2526 2527 <p>Image, XHTML 1.0 style (no space before the closing slash).</p> 2528 %2$s 2529 2530 <p>Image, HTML 5.0 style.</p> 2531 %3$s 2532 2533 <p>Image, with pre-existing "loading" attribute.</p> 2534 %5$s 2535 2536 <p>Iframe, standard.</p> 2537 %4$s'; 2538 2539 $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $iframe, $img_eager ); 2540 $content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $lazy_iframe, $img_eager ); 2541 2542 $this->assertSame( $content_filtered, wp_lazy_load_content_media( $content_unfiltered ) ); 2543 } 2544 2545 /** 2546 * @ticket 44427 2547 */ 2548 function test_wp_lazy_load_content_media_opted_out() { 2549 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2550 2551 $content = ' 2552 <p>Image, standard.</p> 2553 %1$s'; 2554 $content = sprintf( $content, $img ); 2555 2556 add_filter( 'wp_lazy_load_content_media', '__return_false' ); 2557 $this->assertSame( $content, wp_lazy_load_content_media( $content ) ); 2558 } 2498 2559 } 2499 2560 2500 2561 /**