Ticket #44427: 44427.10.diff
File 44427.10.diff, 25.3 KB (added by , 4 years ago) |
---|
-
src/wp-includes/default-filters.php
175 175 add_filter( 'the_content', 'wpautop' ); 176 176 add_filter( 'the_content', 'shortcode_unautop' ); 177 177 add_filter( 'the_content', 'prepend_attachment' ); 178 add_filter( 'the_content', 'wp_ make_content_images_responsive' );178 add_filter( 'the_content', 'wp_filter_content_tags' ); 179 179 180 180 add_filter( 'the_excerpt', 'wptexturize' ); 181 181 add_filter( 'the_excerpt', 'convert_smilies' ); … … 182 182 add_filter( 'the_excerpt', 'convert_chars' ); 183 183 add_filter( 'the_excerpt', 'wpautop' ); 184 184 add_filter( 'the_excerpt', 'shortcode_unautop' ); 185 add_filter( 'the_excerpt', 'wp_filter_content_tags' ); 185 186 add_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 ); 186 187 187 188 add_filter( 'the_post_thumbnail_caption', 'wptexturize' ); … … 207 208 add_filter( 'widget_text_content', 'convert_smilies', 20 ); 208 209 add_filter( 'widget_text_content', 'wpautop' ); 209 210 add_filter( 'widget_text_content', 'shortcode_unautop' ); 211 add_filter( 'widget_text_content', 'wp_filter_content_tags' ); 210 212 add_filter( 'widget_text_content', 'do_shortcode', 11 ); // Runs after wpautop(); note that $post global will be null when shortcodes run. 211 213 212 214 // RSS filters. -
src/wp-includes/deprecated.php
2509 2509 * Determines whether the current admin page is generated by a plugin. 2510 2510 * 2511 2511 * Use global $plugin_page and/or get_plugin_page_hookname() hooks. 2512 * 2512 * 2513 2513 * For more information on this and similar theme functions, check out 2514 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 2514 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 2515 2515 * Conditional Tags} article in the Theme Developer Handbook. 2516 * 2516 * 2517 2517 * @since 1.5.0 2518 2518 * @deprecated 3.1.0 2519 2519 * … … 3716 3716 3717 3717 /** 3718 3718 * Determines whether the current URL is within the comments popup window. 3719 * 3719 * 3720 3720 * For more information on this and similar theme functions, check out 3721 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 3721 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 3722 3722 * Conditional Tags} article in the Theme Developer Handbook. 3723 * 3723 * 3724 3724 * @since 1.5.0 3725 3725 * @deprecated 4.5.0 3726 3726 * … … 3950 3950 wp_send_json_error( array( 'errorMessage' => __( 'The Press This plugin is required.' ) ) ); 3951 3951 } 3952 3952 } 3953 3954 /** 3955 * Filters 'img' elements in post content to add 'srcset' and 'sizes' attributes. 3956 * 3957 * @since 4.4.0 3958 * @deprecated 5.5.0 3959 * 3960 * @see wp_image_add_srcset_and_sizes() 3961 * 3962 * @param string $content The raw post content to be filtered. 3963 * @return string Converted content with 'srcset' and 'sizes' attributes added to images. 3964 */ 3965 function wp_make_content_images_responsive( $content ) { 3966 _deprecated_function( __FUNCTION__, '5.5.0', 'wp_filter_content_tags()' ); 3967 3968 // This will also add the `loading` attribute to `img` tags, if enabled. 3969 return wp_filter_content_tags( $content ); 3970 } -
src/wp-includes/media.php
1021 1021 function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) { 1022 1022 $html = ''; 1023 1023 $image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); 1024 1024 1025 if ( $image ) { 1025 1026 list( $src, $width, $height ) = $image; 1026 $hwstring = image_hwstring( $width, $height ); 1027 $size_class = $size; 1027 1028 $attachment = get_post( $attachment_id ); 1029 $hwstring = image_hwstring( $width, $height ); 1030 $size_class = $size; 1031 1028 1032 if ( is_array( $size_class ) ) { 1029 1033 $size_class = join( 'x', $size_class ); 1030 1034 } 1031 $attachment = get_post( $attachment_id ); 1035 1032 1036 $default_attr = array( 1033 1037 'src' => $src, 1034 1038 'class' => "attachment-$size_class size-$size_class", … … 1035 1039 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), 1036 1040 ); 1037 1041 1042 // Add `loading` attribute. 1043 if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) { 1044 $default_attr['loading'] = 'lazy'; 1045 } 1046 1038 1047 $attr = wp_parse_args( $attr, $default_attr ); 1039 1048 1040 1049 // Generate 'srcset' and 'sizes' if not already present. … … 1061 1070 * 1062 1071 * @since 2.8.0 1063 1072 * 1064 * @param string[]$attr Array of attribute values for the image markup, keyed by attribute name.1073 * @param array $attr Array of attribute values for the image markup, keyed by attribute name. 1065 1074 * See wp_get_attachment_image(). 1066 1075 * @param WP_Post $attachment Image attachment post. 1067 1076 * @param string|array $size Requested size. Image size or array of width and height values … … 1068 1077 * (in that order). Default 'thumbnail'. 1069 1078 */ 1070 1079 $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size ); 1080 1071 1081 $attr = array_map( 'esc_attr', $attr ); 1072 1082 $html = rtrim( "<img $hwstring" ); 1083 1073 1084 foreach ( $attr as $name => $value ) { 1074 1085 $html .= " $name=" . '"' . $value . '"'; 1075 1086 } 1087 1076 1088 $html .= ' />'; 1077 1089 } 1078 1090 … … 1478 1490 } 1479 1491 1480 1492 /** 1481 * Filters 'img' elements in post content to add 'srcset' and 'sizes' attributes.1482 *1483 * @since 4.4.01484 *1485 * @see wp_image_add_srcset_and_sizes()1486 *1487 * @param string $content The raw post content to be filtered.1488 * @return string Converted content with 'srcset' and 'sizes' attributes added to images.1489 */1490 function wp_make_content_images_responsive( $content ) {1491 if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) {1492 return $content;1493 }1494 1495 $selected_images = array();1496 $attachment_ids = array();1497 1498 foreach ( $matches[0] as $image ) {1499 if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) {1500 $attachment_id = absint( $class_id[1] );1501 1502 if ( $attachment_id ) {1503 /*1504 * If exactly the same image tag is used more than once, overwrite it.1505 * All identical tags will be replaced later with 'str_replace()'.1506 */1507 $selected_images[ $image ] = $attachment_id;1508 // Overwrite the ID when the same image is included more than once.1509 $attachment_ids[ $attachment_id ] = true;1510 }1511 }1512 }1513 1514 if ( count( $attachment_ids ) > 1 ) {1515 /*1516 * Warm the object cache with post and meta information for all found1517 * images to avoid making individual database calls.1518 */1519 _prime_post_caches( array_keys( $attachment_ids ), false, true );1520 }1521 1522 foreach ( $selected_images as $image => $attachment_id ) {1523 $image_meta = wp_get_attachment_metadata( $attachment_id );1524 $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content );1525 }1526 1527 return $content;1528 }1529 1530 /**1531 1493 * Adds 'srcset' and 'sizes' attributes to an existing 'img' element. 1532 1494 * 1533 1495 * @since 4.4.0 … … 1617 1579 } 1618 1580 1619 1581 /** 1582 * Determine whether to add the `loading` attribute to the specified tag in the specified context. 1583 * 1584 * @since 5.5.0 1585 * 1586 * @param string $tag_name The tag name. 1587 * @param string $context Additional context, like the current filter name or the function name from where this was called. 1588 * @return bool Whether to add the attribute. 1589 */ 1590 function wp_lazy_loading_enabled( $tag_name, $context ) { 1591 // By default add to all 'img' tags. 1592 // See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading 1593 $default = ( 'img' === $tag_name ); 1594 1595 /** 1596 * Filters whether to add the `loading` attribute to the specified tag in the specified context. 1597 * 1598 * @since 5.5.0 1599 * 1600 * @param bool $default Default value. 1601 * @param string $tag_name The tag name. 1602 * @param string $context Additional context, like the current filter name or the function name from where this was called. 1603 */ 1604 return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context ); 1605 } 1606 1607 /** 1608 * Filters specific tags in post content and modifies their markup. 1609 * 1610 * This function adds `srcset`, `sizes`, and `loading` attributes to `img` HTML tags. 1611 * 1612 * @since 5.5.0 1613 * 1614 * @see wp_img_tag_add_loading_attr() 1615 * @see wp_img_tag_add_srcset_and_sizes_attr() 1616 * 1617 * @param string $content The HTML content to be filtered. 1618 * @param string $context Optional. Additional context to pass to the filters. Defaults to `current_filter()` when not set. 1619 * @return string Converted content with images modified. 1620 */ 1621 function wp_filter_content_tags( $content, $context = null ) { 1622 if ( null === $context ) { 1623 $context = current_filter(); 1624 } 1625 1626 $add_loading_attr = wp_lazy_loading_enabled( 'img', $context ); 1627 1628 if ( false === strpos( $content, '<img' ) ) { 1629 return $content; 1630 } 1631 1632 if ( ! preg_match_all( '/<img\s[^>]+>/', $content, $matches ) ) { 1633 return $content; 1634 } 1635 1636 // List of the unique `img` tags found in $content. 1637 $images = array(); 1638 1639 foreach ( $matches[0] as $image ) { 1640 if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) { 1641 $attachment_id = absint( $class_id[1] ); 1642 1643 if ( $attachment_id ) { 1644 // If exactly the same image tag is used more than once, overwrite it. 1645 // All identical tags will be replaced later with 'str_replace()'. 1646 $images[ $image ] = $attachment_id; 1647 continue; 1648 } 1649 } 1650 1651 $images[ $image ] = 0; 1652 } 1653 1654 // Reduce the array to unique attachment IDs. 1655 $attachment_ids = array_unique( array_filter( array_values( $images ) ) ); 1656 1657 if ( count( $attachment_ids ) > 1 ) { 1658 /* 1659 * Warm the object cache with post and meta information for all found 1660 * images to avoid making individual database calls. 1661 */ 1662 _prime_post_caches( $attachment_ids, false, true ); 1663 } 1664 1665 foreach ( $images as $image => $attachment_id ) { 1666 $filtered_image = $image; 1667 1668 // Add 'srcset' and 'sizes' attributes if applicable. 1669 if ( $attachment_id > 0 && false === strpos( $filtered_image, ' srcset=' ) ) { 1670 $filtered_image = wp_img_tag_add_srcset_and_sizes_attr( $filtered_image, $context, $attachment_id ); 1671 } 1672 1673 // Add 'loading' attribute if applicable. 1674 if ( $add_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) { 1675 $filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context ); 1676 } 1677 1678 if ( $filtered_image !== $image ) { 1679 $content = str_replace( $image, $filtered_image, $content ); 1680 } 1681 } 1682 1683 return $content; 1684 } 1685 1686 /** 1687 * Adds `loading` attribute to an existing `img` HTML tag. 1688 * 1689 * @since 5.5.0 1690 * 1691 * @param string $image The HTML `img` tag where the attribute should be added. 1692 * @param string $context Additional context to pass to the filters. 1693 * @return string Converted `img` tag with `loading` attribute added. 1694 */ 1695 function wp_img_tag_add_loading_attr( $image, $context ) { 1696 /** 1697 * Filters the `loading` attribute value. Default `lazy`. 1698 * 1699 * Returning `false` or an empty string will not add the attribute. 1700 * Returning `true` will add the default value. 1701 * 1702 * @since 5.5.0 1703 * 1704 * @param string $value The 'loading' attribute value, defaults to `lazy`. 1705 * @param string $image The HTML 'img' element to be filtered. 1706 * @param string $context Additional context about how the function was called or where the img tag is. 1707 */ 1708 $value = apply_filters( 'wp_img_tag_add_loading_attr', 'lazy', $image, $context ); 1709 1710 if ( $value ) { 1711 if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) { 1712 $value = 'lazy'; 1713 } 1714 1715 $quote = null; 1716 1717 // Get the quote character used in the tag. 1718 // Normally it will be a double quote but in some rare cases may be a single quote. 1719 // This also checks if the `img` tag is not malformed, i.e. it has a `src` attribute. 1720 if ( false !== strpos( $image, ' src="' ) ) { 1721 $quote = '"'; 1722 } elseif ( preg_match( '/\ssrc\s*=(["\'])/', $image, $matches ) ) { 1723 $quote = $matches[1]; 1724 } 1725 1726 if ( $quote ) { 1727 $loading = "loading={$quote}{$value}{$quote}"; 1728 1729 return str_replace( '<img', "<img {$loading}", $image ); 1730 } 1731 } 1732 1733 return $image; 1734 } 1735 1736 /** 1737 * Adds `srcset` and `sizes` attributes to an existing `img` HTML tag. 1738 * 1739 * @since 5.5.0 1740 * 1741 * @param string $image The HTML `img` tag where the attribute should be added. 1742 * @param string $context Additional context to pass to the filters. 1743 * @param int $attachment_id Image attachment ID. 1744 * @return string Converted 'img' element with 'loading' attribute added. 1745 */ 1746 function wp_img_tag_add_srcset_and_sizes_attr( $image, $context, $attachment_id ) { 1747 /** 1748 * Filters whether to add the `srcset` and `sizes` HTML attributes to the img tag. Default `true`. 1749 * 1750 * Returning anything else than `true` will not add the attributes. 1751 * 1752 * @since 5.5.0 1753 * 1754 * @param bool $value The filtered value, defaults to `true`. 1755 * @param string $image The HTML `img` tag where the attribute should be added. 1756 * @param string $context Additional context about how the function was called or where the img tag is. 1757 * @param int $attachment_id The image attachment ID. 1758 */ 1759 $add = apply_filters( 'wp_img_tag_add_srcset_and_sizes_attr', true, $image, $context, $attachment_id ); 1760 1761 if ( true === $add ) { 1762 $image_meta = wp_get_attachment_metadata( $attachment_id ); 1763 return wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ); 1764 } 1765 1766 return $image; 1767 } 1768 1769 /** 1620 1770 * Adds a 'wp-post-image' class to post thumbnails. Internal use only. 1621 1771 * 1622 1772 * Uses the {@see 'begin_fetch_post_thumbnail_html'} and {@see 'end_fetch_post_thumbnail_html'} -
src/wp-includes/pluggable.php
2601 2601 * Default null. 2602 2602 * @type bool $force_display Whether to always show the avatar - ignores the show_avatars option. 2603 2603 * Default false. 2604 * @type string $loading Value for the `loading` attribute. 2605 * Default null. 2604 2606 * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty. 2605 2607 * } 2606 2608 * @return string|false `<img>` tag for the user's avatar. False on failure. … … 2618 2620 'alt' => '', 2619 2621 'class' => null, 2620 2622 'force_display' => false, 2623 'loading' => null, 2621 2624 'extra_attr' => '', 2622 2625 ); 2623 2626 2627 if ( wp_lazy_loading_enabled( 'img', 'get_avatar' ) ) { 2628 $defaults['loading'] = 'lazy'; 2629 } 2630 2624 2631 if ( empty( $args ) ) { 2625 2632 $args = array(); 2626 2633 } … … 2690 2697 } 2691 2698 } 2692 2699 2700 // Add `loading` attribute. 2701 $extra_attr = $args['extra_attr']; 2702 $loading = $args['loading']; 2703 2704 if ( in_array( $loading, array( 'lazy', 'eager' ), true ) && ! preg_match( '/\bloading\s*=/', $extra_attr ) ) { 2705 if ( ! empty( $extra_attr ) ) { 2706 $extra_attr .= ' '; 2707 } 2708 2709 $extra_attr .= "loading='{$loading}'"; 2710 } 2711 2693 2712 $avatar = sprintf( 2694 2713 "<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>", 2695 2714 esc_attr( $args['alt'] ), … … 2698 2717 esc_attr( join( ' ', $class ) ), 2699 2718 (int) $args['height'], 2700 2719 (int) $args['width'], 2701 $ args['extra_attr']2720 $extra_attr 2702 2721 ); 2703 2722 2704 2723 /** -
tests/phpunit/tests/avatar.php
169 169 170 170 public function test_get_avatar() { 171 171 $img = get_avatar( 1 ); 172 $this->assertEquals( preg_match( "|^<img alt='[^']*' src='[^']*' srcset='[^']*' class='[^']*' height='[^']*' width='[^']*' />$|", $img ), 1 );172 $this->assertEquals( preg_match( "|^<img alt='[^']*' src='[^']*' srcset='[^']*' class='[^']*' height='[^']*' width='[^']*' loading='lazy'/>$|", $img ), 1 ); 173 173 } 174 174 175 175 public function test_get_avatar_size() { -
tests/phpunit/tests/media.php
1332 1332 */ 1333 1333 function test_wp_get_attachment_image_defaults() { 1334 1334 $image = image_downsize( self::$large_id, 'thumbnail' ); 1335 $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" />', $image[1], $image[2], $image[0] );1335 $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" />', $image[1], $image[2], $image[0] ); 1336 1336 1337 1337 $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) ); 1338 1338 } … … 1347 1347 update_post_meta( self::$large_id, '_wp_attachment_image_alt', 'Some very clever alt text', true ); 1348 1348 1349 1349 $image = image_downsize( self::$large_id, 'thumbnail' ); 1350 $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] );1350 $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] ); 1351 1351 1352 1352 $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) ); 1353 1353 … … 1964 1964 /** 1965 1965 * @ticket 33641 1966 1966 */ 1967 function test_wp_ make_content_images_responsive() {1967 function test_wp_filter_content_tags() { 1968 1968 $image_meta = wp_get_attachment_metadata( self::$large_id ); 1969 1969 $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' ); 1970 1970 … … 1973 1973 1974 1974 // Function used to build HTML for the editor. 1975 1975 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 1976 $img = wp_img_tag_add_loading_attr( $img, 'test' ); 1976 1977 $img_no_size_in_class = str_replace( 'size-', '', $img ); 1977 1978 $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img ); 1978 1979 $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height ); … … 2014 2015 $content_unfiltered = sprintf( $content, $img, $img_no_size_in_class, $img_no_width_height, $img_no_size_id, $img_with_sizes_attr, $img_xhtml, $img_html5 ); 2015 2016 $content_filtered = sprintf( $content, $respimg, $respimg_no_size_in_class, $respimg_no_width_height, $img_no_size_id, $respimg_with_sizes_attr, $respimg_xhtml, $respimg_html5 ); 2016 2017 2017 $this->assertSame( $content_filtered, wp_ make_content_images_responsive( $content_unfiltered ) );2018 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); 2018 2019 } 2019 2020 2020 2021 /** … … 2028 2029 * @ticket 34898 2029 2030 * @ticket 33641 2030 2031 */ 2031 function test_wp_make_content_images_responsive_wrong() { 2032 $image = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2032 function test_wp_filter_content_tags_wrong() { 2033 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2034 $img = wp_img_tag_add_loading_attr( $img, 'test' ); 2033 2035 2034 2036 // Replace the src URL. 2035 $image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/foo.jpg"', $im age);2037 $image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/foo.jpg"', $img ); 2036 2038 2037 $this->assertSame( $image_wrong_src, wp_ make_content_images_responsive( $image_wrong_src ) );2039 $this->assertSame( $image_wrong_src, wp_filter_content_tags( $image_wrong_src ) ); 2038 2040 } 2039 2041 2040 2042 /** 2041 2043 * @ticket 33641 2042 2044 */ 2043 function test_wp_ make_content_images_responsive_with_preexisting_srcset() {2045 function test_wp_filter_content_tags_with_preexisting_srcset() { 2044 2046 // Generate HTML and add a dummy srcset attribute. 2045 $image_html = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2046 $image_html = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $image_html ); 2047 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2048 $img = wp_img_tag_add_loading_attr( $img, 'test' ); 2049 $img = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $img ); 2047 2050 2048 2051 // The content filter should return the image unchanged. 2049 $this->assertSame( $im age_html, wp_make_content_images_responsive( $image_html) );2052 $this->assertSame( $img, wp_filter_content_tags( $img ) ); 2050 2053 } 2051 2054 2052 2055 /** … … 2097 2100 * @ticket 35045 2098 2101 * @ticket 33641 2099 2102 */ 2100 function test_wp_ make_content_images_responsive_schemes() {2103 function test_wp_filter_content_tags_schemes() { 2101 2104 $image_meta = wp_get_attachment_metadata( self::$large_id ); 2102 2105 $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' ); 2103 2106 … … 2106 2109 2107 2110 // Build HTML for the editor. 2108 2111 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2112 $img = wp_img_tag_add_loading_attr( $img, 'test' ); 2109 2113 $img_https = str_replace( 'http://', 'https://', $img ); 2110 2114 $img_relative = str_replace( 'http://', '//', $img ); 2111 2115 … … 2126 2130 2127 2131 $unfiltered = sprintf( $content, $img, $img_https, $img_relative ); 2128 2132 $expected = sprintf( $content, $respimg, $respimg_https, $respimg_relative ); 2129 $actual = wp_ make_content_images_responsive( $unfiltered );2133 $actual = wp_filter_content_tags( $unfiltered ); 2130 2134 2131 2135 $this->assertSame( $expected, $actual ); 2132 2136 } … … 2260 2264 $month = gmdate( 'm' ); 2261 2265 2262 2266 $expected = '<img width="999" height="999" src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png"' . 2263 ' class="attachment-testsize size-testsize" alt="" ' .2267 ' class="attachment-testsize size-testsize" alt="" loading="lazy"' . 2264 2268 ' srcset="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png 999w,' . 2265 2269 ' http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-large-150x150.png 150w"' . 2266 2270 ' sizes="(max-width: 999px) 100vw, 999px" />'; … … 2523 2527 2524 2528 $this->assertSame( $expected, $url ); 2525 2529 } 2530 2531 /** 2532 * @ticket 44427 2533 */ 2534 function test_wp_lazy_load_content_media() { 2535 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2536 $img_xhtml = str_replace( ' />', '/>', $img ); 2537 $img_html5 = str_replace( ' />', '>', $img ); 2538 $iframe = '<iframe src="https://www.example.com"></iframe>'; 2539 2540 $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' ); 2541 $lazy_img_xhtml = wp_img_tag_add_loading_attr( $img_xhtml, 'test' ); 2542 $lazy_img_html5 = wp_img_tag_add_loading_attr( $img_html5, 'test' ); 2543 2544 // The following should not be modified because there already is a 'loading' attribute. 2545 $img_eager = str_replace( ' />', ' loading="eager" />', $img ); 2546 2547 $content = ' 2548 <p>Image, standard.</p> 2549 %1$s 2550 <p>Image, XHTML 1.0 style (no space before the closing slash).</p> 2551 %2$s 2552 <p>Image, HTML 5.0 style.</p> 2553 %3$s 2554 <p>Image, with pre-existing "loading" attribute.</p> 2555 %5$s 2556 <p>Iframe, standard. Should not be modified.</p> 2557 %4$s'; 2558 2559 $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $iframe, $img_eager ); 2560 $content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $iframe, $img_eager ); 2561 2562 // Do not add srcset and sizes. 2563 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2564 2565 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); 2566 2567 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2568 } 2569 2570 /** 2571 * @ticket 44427 2572 */ 2573 function test_wp_lazy_load_content_media_opted_in() { 2574 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2575 $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' ); 2576 2577 $content = ' 2578 <p>Image, standard.</p> 2579 %1$s'; 2580 2581 $content_unfiltered = sprintf( $content, $img ); 2582 $content_filtered = sprintf( $content, $lazy_img ); 2583 2584 // Do not add srcset and sizes while testing. 2585 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2586 2587 // Enable globally for all tags. 2588 add_filter( 'wp_lazy_loading_enabled', '__return_true' ); 2589 2590 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); 2591 remove_filter( 'wp_lazy_loading_enabled', '__return_true' ); 2592 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2593 } 2594 2595 /** 2596 * @ticket 44427 2597 */ 2598 function test_wp_lazy_load_content_media_opted_out() { 2599 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2600 2601 $content = ' 2602 <p>Image, standard.</p> 2603 %1$s'; 2604 $content = sprintf( $content, $img ); 2605 2606 // Do not add srcset and sizes while testing. 2607 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2608 2609 // Disable globally for all tags. 2610 add_filter( 'wp_lazy_loading_enabled', '__return_false' ); 2611 2612 $this->assertSame( $content, wp_filter_content_tags( $content ) ); 2613 remove_filter( 'wp_lazy_loading_enabled', '__return_false' ); 2614 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2615 } 2616 2617 /** 2618 * @ticket 44427 2619 */ 2620 function test_wp_img_tag_add_loading_attr_single_quote() { 2621 $img = "<img src='example.png' alt='' width='300' height='225' />"; 2622 $img = wp_img_tag_add_loading_attr( $img, 'test' ); 2623 2624 $this->assertContains( " loading='lazy'", $img ); 2625 } 2526 2626 } 2527 2627 2528 2628 /**