- Timestamp:
- 12/16/2020 09:17:24 PM (4 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/media.php
r49769 r49808 1704 1704 * 1705 1705 * @since 5.5.0 1706 * @since 5.7.0 Now returns `true` by default for `iframe` tags. 1706 1707 * 1707 1708 * @param string $tag_name The tag name. … … 1711 1712 */ 1712 1713 function wp_lazy_loading_enabled( $tag_name, $context ) { 1713 // By default add to all 'img' tags.1714 // By default add to all 'img' and 'iframe' tags. 1714 1715 // See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading 1715 $default = ( 'img' === $tag_name ); 1716 // See https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-iframe-loading 1717 $default = ( 'img' === $tag_name || 'iframe' === $tag_name ); 1716 1718 1717 1719 /** … … 1733 1735 * Modifies HTML tags in post content to include new browser and HTML technologies 1734 1736 * that may not have existed at the time of post creation. These modifications currently 1735 * include adding `srcset`, `sizes`, and `loading` attributes to `img` HTML tags. 1737 * include adding `srcset`, `sizes`, and `loading` attributes to `img` HTML tags, as well 1738 * as adding `loading` attributes to `iframe` HTML tags. 1736 1739 * Future similar optimizations should be added/expected here. 1737 1740 * 1738 1741 * @since 5.5.0 1742 * @since 5.7.0 Now supports adding `loading` attributes to `iframe` tags. 1739 1743 * 1740 1744 * @see wp_img_tag_add_width_and_height_attr() 1741 1745 * @see wp_img_tag_add_srcset_and_sizes_attr() 1742 1746 * @see wp_img_tag_add_loading_attr() 1747 * @see wp_iframe_tag_add_loading_attr() 1743 1748 * 1744 1749 * @param string $content The HTML content to be filtered. … … 1752 1757 } 1753 1758 1754 $add_loading_attr = wp_lazy_loading_enabled( 'img', $context ); 1755 1756 if ( false === strpos( $content, '<img' ) ) { 1757 return $content; 1758 } 1759 1760 if ( ! preg_match_all( '/<img\s[^>]+>/', $content, $matches ) ) { 1759 $add_img_loading_attr = wp_lazy_loading_enabled( 'img', $context ); 1760 $add_iframe_loading_attr = wp_lazy_loading_enabled( 'iframe', $context ); 1761 1762 if ( ! preg_match_all( '/<(img|iframe)\s[^>]+>/', $content, $matches, PREG_SET_ORDER ) ) { 1761 1763 return $content; 1762 1764 } … … 1765 1767 $images = array(); 1766 1768 1767 foreach ( $matches[0] as $image ) { 1768 if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) { 1769 $attachment_id = absint( $class_id[1] ); 1770 1771 if ( $attachment_id ) { 1772 // If exactly the same image tag is used more than once, overwrite it. 1773 // All identical tags will be replaced later with 'str_replace()'. 1774 $images[ $image ] = $attachment_id; 1775 continue; 1776 } 1777 } 1778 1779 $images[ $image ] = 0; 1769 // List of the unique `iframe` tags found in $content. 1770 $iframes = array(); 1771 1772 foreach ( $matches as $match ) { 1773 list( $tag, $tag_name ) = $match; 1774 1775 switch ( $tag_name ) { 1776 case 'img': 1777 if ( preg_match( '/wp-image-([0-9]+)/i', $tag, $class_id ) ) { 1778 $attachment_id = absint( $class_id[1] ); 1779 1780 if ( $attachment_id ) { 1781 // If exactly the same image tag is used more than once, overwrite it. 1782 // All identical tags will be replaced later with 'str_replace()'. 1783 $images[ $tag ] = $attachment_id; 1784 break; 1785 } 1786 } 1787 $images[ $tag ] = 0; 1788 break; 1789 case 'iframe': 1790 $iframes[ $tag ] = 0; 1791 break; 1792 } 1780 1793 } 1781 1794 … … 1805 1818 1806 1819 // Add 'loading' attribute if applicable. 1807 if ( $add_ loading_attr && false === strpos( $filtered_image, ' loading=' ) ) {1820 if ( $add_img_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) { 1808 1821 $filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context ); 1809 1822 } … … 1811 1824 if ( $filtered_image !== $image ) { 1812 1825 $content = str_replace( $image, $filtered_image, $content ); 1826 } 1827 } 1828 1829 foreach ( $iframes as $iframe => $attachment_id ) { 1830 $filtered_iframe = $iframe; 1831 1832 // Add 'loading' attribute if applicable. 1833 if ( $add_iframe_loading_attr && false === strpos( $filtered_iframe, ' loading=' ) ) { 1834 $filtered_iframe = wp_iframe_tag_add_loading_attr( $filtered_iframe, $context ); 1835 } 1836 1837 if ( $filtered_iframe !== $iframe ) { 1838 $content = str_replace( $iframe, $filtered_iframe, $content ); 1813 1839 } 1814 1840 } … … 1828 1854 function wp_img_tag_add_loading_attr( $image, $context ) { 1829 1855 /** 1830 * Filters the `loading` attribute value . Default `lazy`.1856 * Filters the `loading` attribute value to add to an image. Default `lazy`. 1831 1857 * 1832 1858 * Returning `false` or an empty string will not add the attribute. … … 1935 1961 1936 1962 return $image; 1963 } 1964 1965 /** 1966 * Adds `loading` attribute to an `iframe` HTML tag. 1967 * 1968 * @since 5.7.0 1969 * 1970 * @param string $iframe The HTML `iframe` tag where the attribute should be added. 1971 * @param string $context Additional context to pass to the filters. 1972 * @return string Converted `iframe` tag with `loading` attribute added. 1973 */ 1974 function wp_iframe_tag_add_loading_attr( $iframe, $context ) { 1975 /** 1976 * Filters the `loading` attribute value to add to an iframe. Default `lazy`. 1977 * 1978 * Returning `false` or an empty string will not add the attribute. 1979 * Returning `true` will add the default value. 1980 * 1981 * @since 5.7.0 1982 * 1983 * @param string|bool $value The `loading` attribute value. Returning a falsey value will result in 1984 * the attribute being omitted for the iframe. Default 'lazy'. 1985 * @param string $iframe The HTML `iframe` tag to be filtered. 1986 * @param string $context Additional context about how the function was called or where the iframe tag is. 1987 */ 1988 $value = apply_filters( 'wp_iframe_tag_add_loading_attr', 'lazy', $iframe, $context ); 1989 1990 if ( $value ) { 1991 if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) { 1992 $value = 'lazy'; 1993 } 1994 1995 // Iframes should have source and dimension attributes for the `loading` attribute to be added. 1996 if ( false === strpos( $iframe, ' src="' ) || false === strpos( $iframe, ' width="' ) || false === strpos( $iframe, ' height="' ) ) { 1997 return $iframe; 1998 } 1999 2000 return str_replace( '<iframe', '<iframe loading="' . esc_attr( $value ) . '"', $iframe ); 2001 } 2002 2003 return $iframe; 1937 2004 } 1938 2005 -
trunk/tests/phpunit/tests/media.php
r49622 r49808 2737 2737 * @ticket 44427 2738 2738 * @ticket 50367 2739 * @ticket 50756 2739 2740 * @requires function imagejpeg 2740 2741 */ … … 2743 2744 $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' ); 2744 2745 2745 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2746 $img_xhtml = str_replace( ' />', '/>', $img ); 2747 $img_html5 = str_replace( ' />', '>', $img ); 2748 $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img ); 2749 $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height ); 2750 $iframe = '<iframe src="https://www.example.com"></iframe>'; 2746 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2747 $img_xhtml = str_replace( ' />', '/>', $img ); 2748 $img_html5 = str_replace( ' />', '>', $img ); 2749 $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img ); 2750 $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height ); 2751 $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>'; 2752 $iframe_no_width_height = '<iframe src="https://www.example.com"></iframe>'; 2751 2753 2752 2754 $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' ); 2753 2755 $lazy_img_xhtml = wp_img_tag_add_loading_attr( $img_xhtml, 'test' ); 2754 2756 $lazy_img_html5 = wp_img_tag_add_loading_attr( $img_html5, 'test' ); 2757 $lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' ); 2755 2758 2756 2759 // The following should not be modified because there already is a 'loading' attribute. 2757 $img_eager = str_replace( ' />', ' loading="eager" />', $img ); 2760 $img_eager = str_replace( ' />', ' loading="eager" />', $img ); 2761 $iframe_eager = str_replace( '">', '" loading="eager">', $iframe ); 2758 2762 2759 2763 $content = ' … … 2768 2772 <p>Image, without dimension attributes. Should not be modified.</p> 2769 2773 %5$s 2770 <p>Iframe, standard. Should not be modified.</p> 2771 %6$s'; 2772 2773 $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe ); 2774 $content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $img_eager, $img_no_width_height, $iframe ); 2774 <p>Iframe, standard.</p> 2775 %6$s 2776 <p>Iframe, with pre-existing "loading" attribute. Should not be modified.</p> 2777 %7$s 2778 <p>Iframe, without dimension attributes. Should not be modified.</p> 2779 %8$s'; 2780 2781 $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe, $iframe_eager, $iframe_no_width_height ); 2782 $content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $img_eager, $img_no_width_height, $lazy_iframe, $iframe_eager, $iframe_no_width_height ); 2775 2783 2776 2784 // Do not add width, height, srcset, and sizes. … … 2786 2794 /** 2787 2795 * @ticket 44427 2796 * @ticket 50756 2788 2797 */ 2789 2798 function test_wp_filter_content_tags_loading_lazy_opted_in() { 2790 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2791 $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' ); 2799 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2800 $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' ); 2801 $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>'; 2802 $lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' ); 2792 2803 2793 2804 $content = ' 2794 2805 <p>Image, standard.</p> 2795 %1$s'; 2796 2797 $content_unfiltered = sprintf( $content, $img ); 2798 $content_filtered = sprintf( $content, $lazy_img ); 2806 %1$s 2807 <p>Iframe, standard.</p> 2808 %2$s'; 2809 2810 $content_unfiltered = sprintf( $content, $img, $iframe ); 2811 $content_filtered = sprintf( $content, $lazy_img, $lazy_iframe ); 2799 2812 2800 2813 // Do not add srcset and sizes while testing. … … 2811 2824 /** 2812 2825 * @ticket 44427 2826 * @ticket 50756 2813 2827 */ 2814 2828 function test_wp_filter_content_tags_loading_lazy_opted_out() { 2815 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2829 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2830 $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>'; 2816 2831 2817 2832 $content = ' 2818 2833 <p>Image, standard.</p> 2819 %1$s'; 2820 $content = sprintf( $content, $img ); 2834 %1$s 2835 <p>Iframe, standard.</p> 2836 %2$s'; 2837 $content = sprintf( $content, $img, $iframe ); 2821 2838 2822 2839 // Do not add srcset and sizes while testing. … … 2877 2894 2878 2895 $this->assertNotContains( ' loading=', $img ); 2896 } 2897 2898 /** 2899 * @ticket 50756 2900 */ 2901 function test_wp_iframe_tag_add_loading_attr() { 2902 $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>'; 2903 $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' ); 2904 2905 $this->assertContains( ' loading="lazy"', $iframe ); 2906 } 2907 2908 /** 2909 * @ticket 50756 2910 */ 2911 function test_wp_iframe_tag_add_loading_attr_without_src() { 2912 $iframe = '<iframe width="640" height="360"></iframe>'; 2913 $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' ); 2914 2915 $this->assertNotContains( ' loading=', $iframe ); 2916 } 2917 2918 /** 2919 * @ticket 50756 2920 */ 2921 function test_wp_iframe_tag_add_loading_attr_with_single_quotes() { 2922 $iframe = "<iframe src='https://www.example.com' width='640' height='360'></iframe>"; 2923 $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' ); 2924 2925 $this->assertNotContains( ' loading=', $iframe ); 2926 2927 // Test specifically that the attribute is not there with double-quotes, 2928 // to avoid regressions. 2929 $this->assertNotContains( ' loading="lazy"', $iframe ); 2930 } 2931 2932 /** 2933 * @ticket 50756 2934 */ 2935 function test_wp_iframe_tag_add_loading_attr_opt_out() { 2936 $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>'; 2937 add_filter( 'wp_iframe_tag_add_loading_attr', '__return_false' ); 2938 2939 $this->assertNotContains( ' loading=', $iframe ); 2879 2940 } 2880 2941 … … 2919 2980 * @ticket 44427 2920 2981 * @ticket 50425 2982 * @ticket 50756 2921 2983 * @dataProvider data_wp_lazy_loading_enabled_tag_name_defaults 2922 2984 * … … 2935 2997 return array( 2936 2998 'img => true' => array( 'img', true ), 2937 'iframe => false' => array( 'iframe', false ),2999 'iframe => true' => array( 'iframe', true ), 2938 3000 'arbitrary tag => false' => array( 'blink', false ), 2939 3001 );
Note: See TracChangeset
for help on using the changeset viewer.