变更集52065
时间戳: 2021年9月11日上午12:34:17 ( 3年 前) 位置: 大旅行箱 文件夹: -
4已编辑
-
src/wp-includes/media.php (已修改) ( 7个差异 ) -
src/wp-includes/pluggable.php (已修改) ( 1个差异 ) -
src/wp-includes/post-thumbnail模板.php (已修改) ( 1个差异 ) -
tests/phpunit/tests/media.php (已修改) ( 3个差异 )
图例:
未修改 补充 远离的
-
trunk/src/wp-includes/media.php 51903兰特 52065兰特 1047 1047 //添加“加载”属性。 1048 1048 if(wp_lazyloading_enabled(“img”,“wp_get_attachment_image”){ 1049 $default_attr[正在加载]= “懒惰” ; 1049 $default_attr[正在加载]= wp_get_loading_attr_default(“wp_get附件图像”) ; 1050 1050 } 1051 1051 … … 1821 1821 } 1822 1822 1823 foreach($images作为$image=>$attachment_id){ 1824 $filtered_image=$image; 1825 1826 //如果适用,添加“宽度”和“高度”属性。 1827 如果($attachment_id>0&&false===strpos($filtered_image,'width=')&&false==strpos($filtered_image,'height=')){ 1828 $filtered_image=wp_img_tag_add_width_and_height_attr($filtered _image,$context,$attachment_id); 1829 } 1830 1831 //如果适用,添加“srcset”和“sizes”属性。 1832 如果($attachment_id>0&&false===strpos($filtered_image,'srcset=')){ 1833 $filtered_image=wp_img_tag_add_srcset_and_sizes_attr($filtered _image,$context,$attachment_id); 1834 } 1835 1836 //如果适用,添加“loading”属性。 1837 if($add_img_loading_attr&&false==strpos($filtered_image,'loading=')){ 1838 $filtered_image=wp_img_tag_add_loading_attr($filtered图像,$context); 1839 } 1840 1841 if($filtered_image!==$image){ 1842 $content=str_replace($image,$filtered_image,$内容); 1843 } 1844 } 1845 1846 foreach($iframes作为$iframe=>$attachment_id){ 1847 $filtered_iframe=$iframe; 1848 1849 //如果适用,添加“loading”属性。 1850 if($add_iframe_loading_attr&&false===strpos($filtered_iframe,'loading=')){ 1851 $filtered_iframe=wp_iframe_tag_add_loading_attr($filtered _ iframe,$context); 1852 } 1853 1854 if($filtered_iframe!==$iframe){ 1855 $content=str_replace($iframe,$filtered_iframe,$content); 1823 //按照出现的顺序迭代匹配,因为这与是否延迟加载有关。 1824 foreach($matches作为$match){ 1825 //筛选图像匹配项。 1826 if(isset($images[$match[0]]){ 1827 $filtered_image=$match[0]; 1828 $attachment_id=$images[$match[0]]; 1829 1830 //添加“width”和“height”属性(如果适用)。 1831 如果($attachment_id>0&&false===strpos($filtered_image,'width=')&&false==strpos($filtered_image,'height=')){ 1832 $filtered_image=wp_img_tag_add_width_and_height_attr($filtered _image,$context,$attachment_id); 1833 } 1834 1835 //如果适用,添加“srcset”和“sizes”属性。 1836 如果($attachment_id>0&&false===strpos($filtered_image,'srcset=')){ 1837 $filtered_image=wp_img_tag_add_srcset_and_sizes_attr($filtered _image,$context,$attachment_id); 1838 } 1839 1840 //如果适用,添加“loading”属性。 1841 if($add_img_loading_attr&&false===strpos($filtered_image,'loading=')){ 1842 $filtered_image=wp_img_tag_add_loading_attr($filtered图像,$context); 1843 } 1844 1845 if($filtered_image!==$match[0]){ 1846 $content=str_replace($match[0],$filtered_image,$content); 1847 } 1848 } 1849 1850 //筛选iframe匹配项。 1851 if(isset($iframes[$match[0]]){ 1852 $filtered_iframe=$match[0]; 1853 1854 //如果适用,添加“loading”属性。 1855 if($add_iframe_loading_attr&&false==strpos($filtered_iframe,'loading=')){ 1856 $filtered_iframe=wp_iframe_tag_add_loading_attr($filtered _ iframe,$context); 1857 } 1858 1859 if($filtered_iframe!==$match[0]){ 1860 $content=str_replace($match[0],$filtered_iframe,$content); 1861 } 1856 1862 } 1857 1863 } … … 1870 1876 */ 1871 1877 函数wp_img_tag_add_loading_attr($image,$context){ 1878 //获取要使用的加载属性值。 这必须发生在下面的条件检查之前,以便即使图像 1879 //不符合被延迟加载的条件。 1880 $value=wp_get_loading_attr_default($context); 1881 1872 1882 //图像应该具有要添加的“加载”属性的源属性和维度属性。 1873 1883 if(false===strpos($image,'src=“')||false==strpos{ … … 1884 1894 * 1885 1895 *@param string | bool$value“加载”属性值。 返回错误值将导致 1886 *为图像省略的属性。 默认为“lazy”。 1896 *图像的属性被省略。 1887 1897 *@param string$image要筛选的HTML`img`标记。 1888 1898 *@param string$context关于函数调用方式或img标记所在位置的其他上下文。 1889 1899 */ 1890 $value=应用过滤器('wp_img_tag_add_loading_attr', “懒惰” ,$image,$context); 1900 $value=应用过滤器('wp_img_tag_add_loading_attr', 价值$ ,$image,$context); 1891 1901 1892 1902 if($value){ … … 1996 2006 } 1997 2007 2008 //获取要使用的加载属性值。 这必须发生在下面的条件检查之前,以便即使iframe 2009 //不符合被延迟加载的条件。 2010 $value=wp_get_loading_attr_default($context); 2011 1998 2012 //Iframes应该具有要添加的“loading”属性的源和维度属性。 1999 2013 if(false===strpos($iframe,'src=“')||false==strpos{ … … 2010 2024 * 2011 2025 *@param string | bool$value“加载”属性值。 返回错误值将导致 2012 *为iframe省略的属性。 默认为“lazy”。 2026 *为iframe省略的属性。 2013 2027 *@param string$iframe要筛选的HTML“iframe”标记。 2014 2028 *@param string$context关于函数如何调用或iframe标记所在位置的附加上下文。 2015 2029 */ 2016 $value=应用过滤器('wp_iframe_tag_add_loading_attr', “懒惰” ,$iframe,$context); 2030 $value=应用过滤器('wp_iframe_tag_add_loading_attr', 价值$ ,$iframe,$context); 2017 2031 2018 2032 if($value){ … … 5178 5192 return compact(“宽度”、“高度”、“类型”); 5179 5193 } 5194 5195 /** 5196 *获取用于元素的“loading”属性的默认值。 5197 * 5198 *如果通常启用延迟加载,则只应为标记和上下文调用此函数。 5199 * 5200 *该函数通常返回“lazy”,但使用某些启发来猜测当前元素是否可能 5201 *出现在折叠上方,在这种情况下,它返回布尔值“false”,这将导致“loading”属性 5202 *元素上省略。 此优化的目的是避免初始值内的延迟加载元素 5203 *视口,可能会对性能产生负面影响。 5204 * 5205 *在幕后,该函数每次为元素调用时都使用{@see wp_increase_content_media_count()} 5206 *在主要内容中。 如果元素是第一个内容元素,则将省略“loading”属性。 5207 *可以使用 5208 *{@请参阅“wp_omit_loading_attr_threshold”}过滤器。 5209 * 5210 *@自5.9.0起 5211 * 5212 *@param string$context请求“loading”属性值的元素的上下文。 5213 *@return string | bool默认的“加载”属性值。 “lazy”、“eager”或布尔值“false”表示 5214 *应跳过“加载”属性。 5215 */ 5216 函数wp_get_loading_attr_default($context){ 5217 //只有具有“the_content”或“the_post_thumbnail”上下文的元素具有特殊处理。 5218 if('thecontent'!==$context&&'thepostthumbnail'!==$context){ 5219 return'lazy'; 5220 } 5221 5222 //只有主查询循环中的元素有特殊处理。 5223 if(is_admin()||! in_the_loop()||! is_main_query()){ 5224 return'lazy'; 5225 } 5226 5227 //增加计数器,因为这是主查询内容元素。 5228 $content_media_count=wp_increase_content_media-count(); 5229 5230 //如果到目前为止计数低于阈值,则返回“false”,以便省略“loading”属性。 5231 if($content_media_count<=wp_omit_loading_attr_threshold()){ 5232 返回false; 5233 } 5234 5235 //对于阈值之后的元素,像往常一样延迟加载。 5236 return'lazy'; 5237 } 5238 5239 /** 5240 *获取不延迟加载的第一个内容媒体元素的数量阈值。 5241 * 5242 *此函数运行{@see'wp_omit_loading_attr_threshold'}过滤器,该过滤器使用默认阈值1。 5243 *除非使用`$force`参数,否则每次加载页面时过滤器只运行一次。 5244 * 5245 *@自5.9.0起 5246 * 5247 *@param bool$force可选。 如果设置为true,则将(重新)应用过滤器,即使之前已经应用过。 5248 *默认为false。 5249 *@return int不延迟加载的内容媒体元素数。 5250 */ 5251 函数wp_omit_loading_attr_threshold($force=false){ 5252 静态$omit_threshold; 5253 5254 //此函数可以多次调用。 每次加载页面时只运行一次筛选器。 5255 if(!isset($omit_threshold)||$force){ 5256 /** 5257 *过滤不延迟加载的首批内容媒体元素的数量阈值。 5258 * 5259 *对于这些第一个内容媒体元素,将省略“加载”属性。 默认情况下是这样的 5260 *仅针对第一个内容媒体元素。 5261 * 5262 *@自5.9.0起 5263 * 5264 *@param int$omit_threshold不会添加“loading”属性的媒体元素数。 默认值1。 5265 */ 5266 $omit_threshold=应用过滤器('wp_omit_loading_attr_threshold',1); 5267 } 5268 5269 返回$omit_threshold; 5270 } 5271 5272 /** 5273 *增加内部内容媒体计数变量。 5274 * 5275 *@自5.9.0起 5276 *@access私有 5277 * 5278 *@param int$amount可选。 要增加的金额。默认为1。 5279 *@return int增加后的最新内容媒体计数。 5280 */ 5281 函数wp_increase_content_media_count($amount=1){ 5282 静态$content_media_count=0; 5283 5284 $content_media_count+=$amount; 5285 5286 返回$content_media_count; 5287 } -
主干/src/wp-includes/pluggable.php r51301号 52065兰特 2679 2679 2680 2680 if(wplazy_loading_enabled('img','get_avatar')){ 2681 $defaults[加载]= “懒惰” ; 2681 $defaults[正在加载]= wp_get_loading_attr_default(“get_avatar”) ; 2682 2682 } 2683 2683 -
trunk/src/wp-includes/post-thumbnail模板.php 52028兰特 52065兰特 187 187 } 188 188 189 //获取要用作默认值的“loading”属性值,优先于默认值 190 //`wp_get_attachment_image()`。 191 $loading=wp_get_loading_attr_default('the_post_thumbnail'); 192 193 //向给定属性添加默认值,除非它们已经包含“loading”指令。 194 if(空($attr)){ 195 $attr=数组(“加载”=>$loading); 196 }elseif(is_array($attr)&&! array_key_exists(“加载”,$attr){ 197 $attr['loading']=$loading; 198 }elseif(is_string($attr)&&! preg_match('/(^|&)loading=',$attr)){ 199 $attr.='& 加载=“$ 加载; 200 } 201 189 202 $html=wp_get_attachment_image($post_thumbnail_id,$size,false,$attr); 190 203 -
trunk/tests/phpunit/tests/media.php 52010兰特 52065兰特 3025 3025 *@票号50425 3026 3026 *@门票53463 3027 *@门票53675 3027 3028 *@数据提供程序data_wp_lazy_loading_enabled_context_defaults 3028 3029 * … … 3047 3048 “get_avatar=>true”=>数组(“get_avatar”,true), 3048 3049 “任意上下文=>true”=>数组(“something_completely_assionary”,true), 3050 'the_post_thumbnail=>true'=>数组('the_phost_thumbnail',true), 3049 3051 ); 3050 3052 } … … 3187 3189 ); 3188 3190 } 3191 3192 /** 3193 *@门票53675 3194 *@dataProvider数据_wp_get_loading_attr_default 3195 * 3196 *@param字符串$context 3197 */ 3198 函数test_wp_get_loading_attr_default($context){ 3199 全局$wp_query,$wp_the_query; 3200 3201 //默认返回“lazy”。 3202 $this->assertSame('lazy',wp_get_loading_attr_default('test')); 3203 $this->assertSame('lazy',wp_get_loading_attr_default('wp_get_attachment_image')); 3204 3205 //如果不在循环或主查询中,则返回“lazy”。 3206 $this->assertSame('lazy',wp_get_loading_attr_default($context)); 3207 3208 $wp_query=新wp_query(数组('post_in'=>数组(self::$post_ids['publish'])); 3209 $this->reset_content_media_count(); 3210 $this->reset_omit_loading_attr_filter(); 3211 3212 while(have_posts()){ 3213 post(); 3214 3215 //如果在循环中但不在主查询中,则返回“lazy”。 3216 $this->assertSame('lazy',wp_get_loading_attr_default($context)); 3217 3218 //设置为主查询。 3219 $wp_the_query=$wp_query; 3220 3221 //对于主内容以外的上下文,即使在循环中也会返回“lazy” 3222 //和在主查询中,并且不增加内容媒体计数。 3223 $this->assertSame('lazy',wp_get_loading_attr_default('wp_get_attachment_image')); 3224 3225 //如果在循环和主查询中,它是第一个元素,则返回“false”。 3226 $this->assertFalse(wp_get_loading_attr_default($context)); 3227 3228 //如果在循环和主查询中,则返回“lazy”以查找任何后续元素。 3229 $this->assertSame('lazy',wp_get_loading_attr_default($context)); 3230 3231 //是的,对于所有后续元素。 3232 $this->assertSame('lazy',wp_get_loading_attr_default($context)); 3233 } 3234 } 3235 3236 函数data_wp_get_loading_attr_default(){ 3237 返回数组( 3238 数组('the_content'), 3239 数组('the_post_thumbnail'), 3240 ); 3241 } 3242 3243 /** 3244 *@门票53675 3245 */ 3246 函数test_wp_omit_loading_attr_threshold_filter(){ 3247 全局$wp_query,$wp_the_query; 3248 3249 $wp_query=新wp_query(数组('post_in'=>数组(self::$post_ids['publish'])); 3250 $wp_the_query=$wp_query; 3251 $this->reset_content_media_count(); 3252 $this->reset_omit_loading_attr_filter(); 3253 3254 //使用过滤器将不延迟加载的阈值更改为前三个元素。 3255 添加筛选器( 3256 “wp_omit_loading_attr_threshold”, 3257 函数(){ 3258 返回3; 3259 } 3260 ); 3261 3262 while(have_posts()){ 3263 post(); 3264 3265 //由于过滤器的原因,现在不应延迟加载前三个元素,即返回“false”。 3266 对于($i=0;$i<3;$i++){ 3267 $this->assertFalse(wp_get_loading_attr_default('the_content')); 3268 } 3269 3270 //对于以下元素,请再次延迟加载它们。 3271 $this->assertSame('lazy',wp_get_loading_attr_default('the_content')); 3272 } 3273 } 3274 3275 /** 3276 *@票53675 3277 */ 3278 函数test_wp_filter_content_tags_with_wp_get_loading_attr_default(){ 3279 全局$wp_query,$wp_the_query; 3280 3281 $img1=get_image_tag(self::$large_id,“”,“”; 3282 $iframe1='<iframe src=“ https://www.example.com “width=”640“height=”360“></iframe>'; 3283 $img2=get_image_tag(自我::$large_id,“”,“”; 3284 $img3=get_image_tag(自我::$large_id,“”,“”、“”,‘缩略图’); 3285 $iframe2='<iframe src=“ https://wordpress.org “width=”640“height=”360“></iframe>'; 3286 $lazy_img2=wp_img_tag_add_loading_attr($img2,'the_content'); 3287 $lazy_img3=wp_img_tag_add_loading_attr($img3,'the_content'); 3288 $lazy_iframe2=wp_iframe_tag_add_loading_attr($iframe2,'the_content'); 3289 3290 //使用阈值2。 3291 添加筛选器( 3292 “wp_omit_loading_attr_threshold”, 3293 函数(){ 3294 返回2; 3295 } 3296 ); 3297 3298 //在阈值2之后,前两个内容媒体元素不应延迟加载。 3299 $content_unfiltered=$img1$ 如果框架1$ 图2$ 图3$ iframe2; 3300 $content_expected=$img1$ 如果框架1$ 懒惰_img2$ 懒惰_img3$ lazy_iframe2; 3301 3302 $wp_query=新wp_query(数组('post_in'=>数组(self::$post_ids['publish'])); 3303 $wp_the_query=$wp_query; 3304 $this->reset_content_media_count(); 3305 $this->reset_omit_loading_attr_filter(); 3306 3307 while(have_posts()){ 3308 post(); 3309 3310 添加筛选器('wp_img_tag_add_srcset_and_sizes_attr','__return_false'); 3311 $content_filtered=wp_filter_content_tags($content_unfiltered,'the_content'); 3312 删除过滤器('wp_img_tag_add_srcset_and_sizes_attr','__return_false'); 3313 } 3314 3315 //过滤后,第一个图像不应延迟加载,而其他图像应延迟加载。 3316 $this->assertSame($content_expected,$content_filtered); 3317 } 3318 3319 /** 3320 *@门票53675 3321 */ 3322 公共函数test_wp_omit_loading_attr_threshold(){ 3323 $this->reset_omit_loading_attr_filter(); 3324 3325 //应用过滤器,确保默认值为1。 3326 $omit_threshold=wp_omit_loading_attr_threshold(); 3327 $this->assertSame(1,$omit_threshold); 3328 3329 //添加一个将值更改为3的筛选器。 但是,在单个 3330 //默认情况下加载页面,因此该值仍为1。 3331 添加筛选器( 3332 “wp_omit_loading_attr_threshold”, 3333 函数(){ 3334 返回3; 3335 } 3336 ); 3337 $omit_threshold=wp_omit_loading_attr_threshold(); 3338 $this->assertSame(1,$omit_threshold); 3339 3340 //只有通过强制执行新检查,才能重新应用筛选器。 3341 $omit_threshold=wp_omit_loading_attr_thresho尔德(true); 3342 $this->assertSame(3,$omit_threshold); 3343 } 3344 3345 私有函数reset_content_media_count(){ 3346 //获取当前值而不增加。 3347 $content_media_count=wp_increase_content_media-count(0); 3348 3349 //将其减少当前值以将其“重置”回0。 3350 wp_increase_content_media_count(-$content_media-count); 3351 } 3352 3353 私有函数reset_omit_loading_attr_filter(){ 3354 //添加过滤器以“重置”省略阈值,使其返回空值(未设置)。 3355 add_filter('wp_omit_loading_attr_threshold','__return_null',100); 3356 3357 //强制重新运行筛选器应用程序。 3358 wp_omit_loading_attr_threshold(真); 3359 3360 //清洁上述过滤器。 3361 remove_filter('wp_omit_loading_attr_threshold','__return_null',100); 3362 } 3189 3363 } 3190 3364