使WordPress成为核心

变更集52065


忽略:
时间戳:
2021年9月11日上午12:34:17(3年前)
作者:
弗利克索斯90
消息:

媒体:优化启发式以排除某些图像和iframe的延迟加载,从而提高性能。

此变更集实现了中概述的优化的延迟加载行为https://make.wordpress.org/core/2021/07/15/refining-wordpress-cores-lazy-loading-implementation网站/为了改进“最大内容绘制”度量,该度量可以从延迟加载的折叠上方的图像或iframe中看到回归。到目前为止,开发人员可以通过过滤器调整这一点,现在仍然是这样,不过,这种增强为大多数主题带来了更准确的行为。

具体来说,此变更集跳过页面上第一个“内容图像或iframe”的延迟加载。“Content-image or iframe”表示在当前主查询循环中任何帖子的内容中找到的任何图像或iframe,以及此类帖子的任何特色图像。这既适用于“单一”内容,也适用于“存档”内容:在“单一”页面上,帖子的第一个图像/iframe不会延迟加载,而在“存档”页面上查询中_first_post的第一个图片/iframe则不会延迟加载。

这种方法为大多数主题正确地细化了延迟加载行为,这些主题使用单列布局来发布内容。对于具有多列布局的主题wp_omit_loading_attr_threshold上限过滤器可用于更改从延迟加载中跳过多少第一个图像/iframe(默认为1). 例如,使用档案最新帖子的三列网格的主题可以使用过滤器覆盖阈值在归档页面上,这样前三个内容图像/iframe就不会被延迟加载。

道具亚当·西尔弗斯坦(adamsilverstein)、阿佐兹(azaozz)、弗利克索斯90(flixos90)、海洛夫罗姆托尼亚(hellofromtonya)、乔诺阿尔德森普(jonoaldersonwp)、mte90(mte90)、rviscomi(rviscomi)、。
修复#53675。请参阅#50425.

位置:
大旅行箱
文件夹:
4已编辑

图例:

未修改
补充
远离的
  • trunk/src/wp-includes/media.php

    51903兰特 52065兰特  
    10471047//添加“加载”属性。
    10481048if(wp_lazyloading_enabled(“img”,“wp_get_attachment_image”){
    1049 $default_attr[正在加载]=“懒惰”;
     1049$default_attr[正在加载]=wp_get_loading_attr_default(“wp_get附件图像”);
    10501050        }
    10511051
     
    18211821    }
    18221822
    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//按照出现的顺序迭代匹配,因为这与是否延迟加载有关。
     1824foreach($matches作为$match){
     1825//筛选图像匹配项。
     1826if(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”属性。
     1841if($add_img_loading_attr&&false===strpos($filtered_image,'loading=')){
     1842$filtered_image=wp_img_tag_add_loading_attr($filtered图像,$context);
     1843            }
     1844
     1845if($filtered_image!==$match[0]){
     1846$content=str_replace($match[0],$filtered_image,$content);
     1847            }
     1848        }
     1849
     1850//筛选iframe匹配项。
     1851if(isset($iframes[$match[0]]){
     1852$filtered_iframe=$match[0];
     1853
     1854//如果适用,添加“loading”属性。
     1855if($add_iframe_loading_attr&&false==strpos($filtered_iframe,'loading=')){
     1856$filtered_iframe=wp_iframe_tag_add_loading_attr($filtered _ iframe,$context);
     1857            }
     1858
     1859if($filtered_iframe!==$match[0]){
     1860$content=str_replace($match[0],$filtered_iframe,$content);
     1861            }
    18561862        }
    18571863    }
     
    18701876 */
    18711877函数wp_img_tag_add_loading_attr($image,$context){
     1878//获取要使用的加载属性值。这必须发生在下面的条件检查之前,以便即使图像
     1879//不符合被延迟加载的条件。
     1880$value=wp_get_loading_attr_default($context);
     1881
    18721882//图像应该具有要添加的“加载”属性的源属性和维度属性。
    18731883if(false===strpos($image,'src=“')||false==strpos{
     
    18841894     *
    18851895*@param string | bool$value“加载”属性值。返回错误值将导致
    1886 *为图像省略的属性。默认为“lazy”。
     1896*图像的属性被省略。
    18871897*@param string$image要筛选的HTML`img`标记。
    18881898*@param string$context关于函数调用方式或img标记所在位置的其他上下文。
    18891899     */
    1890 $value=应用过滤器('wp_img_tag_add_loading_attr',“懒惰”,$image,$context);
     1900$value=应用过滤器('wp_img_tag_add_loading_attr',价值$,$image,$context);
    18911901
    18921902if($value){
     
    19962006    }
    19972007
     2008//获取要使用的加载属性值。这必须发生在下面的条件检查之前,以便即使iframe
     2009//不符合被延迟加载的条件。
     2010$value=wp_get_loading_attr_default($context);
     2011
    19982012//Iframes应该具有要添加的“loading”属性的源和维度属性。
    19992013if(false===strpos($iframe,'src=“')||false==strpos{
     
    20102024     *
    20112025*@param string | bool$value“加载”属性值。返回错误值将导致
    2012 *为iframe省略的属性。默认为“lazy”。
     2026*为iframe省略的属性。
    20132027*@param string$iframe要筛选的HTML“iframe”标记。
    20142028*@param string$context关于函数如何调用或iframe标记所在位置的附加上下文。
    20152029     */
    2016 $value=应用过滤器('wp_iframe_tag_add_loading_attr',“懒惰”,$iframe,$context);
     2030$value=应用过滤器('wp_iframe_tag_add_loading_attr',价值$,$iframe,$context);
    20172031
    20182032if($value){
     
    51785192return compact(“宽度”、“高度”、“类型”);
    51795193}
     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”上下文的元素具有特殊处理。
     5218if('thecontent'!==$context&&'thepostthumbnail'!==$context){
     5219return'lazy';
     5220    }
     5221
     5222//只有主查询循环中的元素有特殊处理。
     5223if(is_admin()||!in_the_loop()||!is_main_query()){
     5224return'lazy';
     5225    }
     5226
     5227//增加计数器,因为这是主查询内容元素。
     5228$content_media_count=wp_increase_content_media-count();
     5229
     5230//如果到目前为止计数低于阈值,则返回“false”,以便省略“loading”属性。
     5231if($content_media_count<=wp_omit_loading_attr_threshold()){
     5232返回false;
     5233    }
     5234
     5235//对于阈值之后的元素,像往常一样延迟加载。
     5236return'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//此函数可以多次调用。每次加载页面时只运行一次筛选器。
     5255if(!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兰特  
    26792679
    26802680if(wplazy_loading_enabled('img','get_avatar')){
    2681 $defaults[加载]=“懒惰”;
     2681$defaults[正在加载]=wp_get_loading_attr_default(“get_avatar”);
    26822682        }
    26832683
  • trunk/src/wp-includes/post-thumbnail模板.php

    52028兰特 52065兰特  
    187187        }
    188188
     189//获取要用作默认值的“loading”属性值,优先于默认值
     190//`wp_get_attachment_image()`。
     191$loading=wp_get_loading_attr_default('the_post_thumbnail');
     192
     193//向给定属性添加默认值,除非它们已经包含“loading”指令。
     194if(空($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
    189202$html=wp_get_attachment_image($post_thumbnail_id,$size,false,$attr);
    190203
  • trunk/tests/phpunit/tests/media.php

    52010兰特 52065兰特  
    30253025*@票号50425
    30263026*@门票53463
     3027*@门票53675
    30273028*@数据提供程序data_wp_lazy_loading_enabled_context_defaults
    30283029     *
     
    30473048“get_avatar=>true”=>数组(“get_avatar”,true),
    30483049“任意上下文=>true”=>数组(“something_completely_assionary”,true),
     3050'the_post_thumbnail=>true'=>数组('the_phost_thumbnail',true),
    30493051        );
    30503052    }
     
    31873189        );
    31883190    }
     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
     3212while(have_posts()){
     3213post();
     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
     3262while(have_posts()){
     3263post();
     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
     3307while(have_posts()){
     3308post();
     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。
     3350wp_increase_content_media_count(-$content_media-count);
     3351    }
     3352
     3353私有函数reset_omit_loading_attr_filter(){
     3354//添加过滤器以“重置”省略阈值,使其返回空值(未设置)。
     3355add_filter('wp_omit_loading_attr_threshold','__return_null',100);
     3356
     3357//强制重新运行筛选器应用程序。
     3358wp_omit_loading_attr_threshold(真);
     3359
     3360//清洁上述过滤器。
     3361remove_filter('wp_omit_loading_attr_threshold','__return_null',100);
     3362    }
    31893363}
    31903364
注:请参见TracChangeset(跟踪变更集)获取有关使用变更集查看器的帮助。