使WordPress成为核心

变更集58041


忽略:
时间戳:
2024年4月24日下午12:00:29(4周(之前)
作者:
伯恩哈德·雷特
消息:

阻止挂钩:将正确的上下文传递给筛选器。

这个$上下文传递给筛选器的参数,如挂钩块类型,挂钩块、和挂钩块{$hooked_block_type}允许他们有条件地插入钩块。如果锚块包含在模板或模板零件中,$上下文将设置为工作包块模板反映该模板或部分的对象。

当钩子块插入在从数据库读取模板(或部分)时运行时(以及在通过REST API向客户端发送带有钩子块的模板/部分内容之前),也在写入数据库时应用上述过滤器,因为此时忽略挂钩块元数据属性已设置。

在此变更集之前$上下文在后一种情况下,传递给Block Hooks相关过滤器反映了数据库中已存储的模板/部件(如果有),这是一个错误;相反,它需要反映将由传入的邮政将触发数据库更新的网络请求。

这些传入的更改封装在$个更改参数传递给重置预插入模板重置预插入模板部件过滤器,因此注入_忽略_预订_块_元数据_属性与它们挂钩的函数。$个更改属于类型标准类别并且只包含需要更新的字段。这意味着为了创建工作包块模板对象,需要两步过程:

  • 模拟更新的内容wp_模板wp_模板_部分通过合并,数据库中的post对象看起来像$个更改在现有基础上$帖子从数据库或主题的块模板(部件)文件(如果有)获取的对象。
  • 创建一个工作包块模板来自结果对象。

为了实现后者,一个新的helper方法(_构建块模板对象自发布对象)从现有的_构建块模板结果自发布功能。(后者不能直接使用,因为它包含一些数据库调用,如果数据库中还没有模板的post对象,这些调用将失败。)

虽然实现起来有些复杂,但总体更改允许更好地分离关注点和隔离实体。这是显而易见的,例如注入_ignored_hooked_blocks_metadata_attributes不再需要$请求参数,这由单元测试反映,不再需要创建$请求对象传递给它,从而将函数与模板端点控制器分离。

的单元测试注入_ignored_hooked_blocks_metadata_attributes已移动到新的单独文件中。增加了测试覆盖范围,因此现在涵盖了所有三个相关场景:

  • 数据库中不存在该模板,也没有用于该模板的块主题模板文件。
  • 数据库中不存在该模板,但有一个用于该模板的块主题模板文件。
  • 数据库中已存在该模板。

这些场景也对应于内部的逻辑分支WP_REST_Templates_Controller::prepare_item_for_database,就是在哪里注入_ignored_hooked_blocks_metadata_attributes从获取其数据。

由gziolo审核。
合并[57919]到6.5分支。

道具tomjcafferkey,bernhard reiter,gziolo,swissspidy。
修复#60754.

位置:
分支/6.5
文件夹:
添加了1个
5已编辑

图例:

未修改
补充
已删除
  • 分支/6.5/src/wp-includes/block-template-utils.php

    57947兰特 58041兰特  
    724724
    725725/**
    726 *基于post object构建统一的模板对象。
    727  *
    728 *@自5.9.0起
    729 *@自6.3.0起在模板对象中添加了“modified”属性。
    730 *@自6.4.0起增加了对传递给此函数的修订帖子的支持。
     726*从post对象构建块模板对象。
     727 *
     728*这是一个帮助函数,用于从给定的post对象创建块模板对象。
     729*它是自给自足的,因为它只使用作为参数传递的信息;它没有
     730*查询数据库以获取更多信息。
     731 *
     732*@自6.5.1起
    731733*@access私有
    732734 *
    733 *@param WP_Post$Post模板帖子。
     735*@param WP_Post$Post模板帖子。
     736*@param array$terms通知模板对象的其他术语。
     737*@param array$meta通知模板对象的其他元字段。
    734738*@return WP_Block_Template | WP_Error模板或错误对象。
    735739 */
    736 函数_build_block_template_result_from_post($post){
     740函数_build_block_template_object_from_post_object($post,$terms=array(),$meta=array()){
     741if(空($terms['wp_theme']){
     742return new WP_Error('template_missing_theme',__('没有为此模板定义主题。'));
     743    }
     744$theme=$terms['wp_theme'];
     745
    737746$default_template_types=获取默认块模板类型();
    738747
    739 $post_id=wp_is_post_revision($post);
    740 if(!$post_id){
    741 $post_id=$post;
    742     }
    743 $parent_post=获取post($post_id);
    744 
    745 $terms=get_the_terms($parent_post,'wp_theme');
    746 
    747 if(iswp_error($terms)){
    748 返回$terms;
    749     }
    750 
    751 if(!$terms){
    752 return new WP_Error('template_missing_theme',__('没有为此模板定义主题。'));
    753     }
    754 
    755 $theme=$terms[0]->名称;
    756748$template_file=_get_block_template_file($post->post_type,$post->post_name);
    757749$has_theme_file=get_stylesheet()===$theme&&null!==$template_file;
    758750
    759 $origin=get_post_meta($parent_post->ID,'origin',true);
    760 $is_wp_suggestion=get_post_meta($parent_post->ID,'is_wp_suggession',true);
    761 
    762751$template=新的WP_Block_template();
    763752$template->wp_id=$post->id;
    764 $template->id=$theme.'//'$父母ost->post_name;
     753$template->id=$theme.'//'$ost->post_name;
    765754$template->theme=$theme;
    766755$template->content=$post->post_content;
    767756$template->slug=$post->post_name;
    768757$template->source=“自定义”;
    769 $template->origin=!空($原产地)$起源:null;
     758$template->origin=!空($meta[“源”])$meta[“源”]:null;
    770759$template->type=$post->post_type;
    771760$template->description=$post->post_excerpt;
     
    773762$template->status=$post->post_status;
    774763$template->has_theme_file=$has_ttheme_file;
    775 $template->is_custom=空($is_wp_建议);
     764$template->is_custom=空($元['is_wp_suggestion']);
    776765$template->author=$post->post_author;
    777766$template->modified=$post->post_modified;
    778767
    779 if('wp_template'===$p父母ost->post_type&&$has_theme_file&&isset($template_file['postTypes']){
     768if('wp_template'===$post->post_type&&$has_theme_file&&isset($template_file['postTypes']){
    780769$template->post_types=$template_file['postTypes'];
    781770    }
    782771
    783 if('wp_template'===$p父母ost->post_type&&isset($default_template_types[$template->slug]){
     772if('wp_template'===$post->post_type&&isset($default_template_types[$template->slug]){
    784773$template->is_custom=false;
    785774    }
     775
     776if('wp_template_part'====$post->post_type&&isset($terms['wp_template_part_area'])){
     777$template->区域=$terms['wp_template_part_area'];
     778    }
     779
     780返回$template;
     781}
     782
     783/**
     784*基于post object构建统一的模板对象。
     785 *
     786*@自5.9.0起
     787*@自6.3.0起在模板对象中添加了“modified”属性。
     788*@自6.4.0起增加了对传递给此函数的修订帖子的支持。
     789*@access私有
     790 *
     791*@param WP_Post$Post模板帖子。
     792*@return WP_Block_Template | WP_Error模板或错误对象。
     793 */
     794函数_build_block_template_result_from_post($post){
     795$post_id=wp_is_post_revision($post);
     796if(!$post_id){
     797$post_id=$post;
     798    }
     799$parent_post=获取post($post_id);
     800$post->post_name=$parent_post->post-name;
     801$post->post_type=$parent_post->post-type;
     802
     803$terms=get_the_terms($parent_post,'wp_theme');
     804
     805if(iswp_error($terms)){
     806返回$terms;
     807    }
     808
     809如果(!$terms){
     810return new WP_Error('template_missing_theme',__('没有为此模板定义主题。'));
     811    }
     812
     813$terms=数组(
     814“wp_theme”=>$terms[0]->名称,
     815    );
    786816
    787817if(“wp_template_part”===$parent_post->post_type){
    788818$type_terms=get_the_terms($parent_post,'wp_template_part_area');
    789819if(!iswp_error($type_terms)&&false!==$type_terms){
    790 $template->area=$type_terms[0]->名称;
    791         }
     820$terms['wp_template_part_area']=$type_terms[0]->名称;
     821        }
     822    }
     823
     824$meta=数组(
     825'origin'=>get_post_meta($parent_post->ID,'origin'',true),
     826“is_wp_suggestion”=>get_post_meta($parent_post->ID,“is_wp_suggession”,true),
     827    );
     828
     829$template=_build_block_template_object_from_post_object($post、$terms、$meta);
     830
     831if(iswp_error($template)){
     832返回$template;
    792833    }
    793834
     
    14431484*@access私有
    14441485 *
    1445 *@param stdClass$post表示模板或模板部分的对象
    1446 *准备插入或更新数据库。
    1447 *@param WP_REST_Request$请求请求对象。
    1448 *@return stdClass表示模板或模板部分的更新对象。
    1449  */
    1450 函数inject_ignored_hooked_blocks_metadata_attributes($post,$request){
    1451 $filter_name=当前过滤器();
    1452 if(!str_starts_with($filter_name,'rest_pre_insert_')){
    1453 返回$post;
    1454     }
    1455 $post_type=str_replace('rest_pre_insert_','',$filter_name);
     1486*@param stdClass$changes表示模板或模板部分的对象
     1487*准备插入或更新数据库。
     1488*@param WP_REST_Request$已弃用。未使用。
     1489*@return stdClass | WP_Error表示模板或模板部分的更新对象。
     1490 */
     1491函数inject_ignored_hooked_blocks_metadata_attributes($changes,$deprecated=null){
     1492if(null!==$已弃用){
     1493_预制_参数(__FUNCTION_,'6.5.1');
     1494    }
    14561495
    14571496$hooked_blocks=获取挂钩块();
    14581497if(空($hooked_blocks)&&!has_filter('挂钩块类型'){
    1459 返回$post;
    1460     }
    1461 
    1462 //此时,帖子已经创建。
    1463 //我们需要构建相应的“WP_Block_Template”对象作为访问者的上下文参数。
    1464 //为此,我们需要禁止钩子块插入到模板中。
    1465 add_filter('hooked_block_types','__return_empty_array',99999,0);
    1466 $template=$request['id']?get_block_template($request['id'],$post_type):null;
    1467 删除过滤器('挂钩块类型','返回空数组',99999);
     1498返回$changes;
     1499    }
     1500
     1501$meta=isset($changes->meta_input)$更改->meta_input:array();
     1502$terms=isset($changes->tax_input)$更改->tax_input:数组();
     1503
     1504if(空($changes->ID)){
     1505//该模板的数据库中还没有该模板的post对象。
     1506$post=$个更改;
     1507}其他{
     1508//查找现有的post对象。
     1509$post=get_post($changes->ID);
     1510
     1511//如果文章是修订版,请使用父文章的post_name和post_type。
     1512$post_id=wp_is_post_revision($post);
     1513if($post_id){
     1514$parent_post=获取post($post_id);
     1515$post->post_name=$parent_post->post-name;
     1516$post->post_type=$parent_post->post-type;
     1517        }
     1518
     1519//将更改应用于现有post对象。
     1520$post=(对象)array_merge((数组)$post,(数组)$更改);
     1521
     1522$type_terms=get_the_terms($changes->ID,'wp_theme');
     1523$terms['wp_theme']=!is_wp_error($type_terms)&&!空($type_terms)$type_terms[0]->名称:空;
     1524    }
     1525
     1526//WP_Block_Template必需。使用当前时间更新post对象。
     1527$post->post_modified=当前时间('mysql');
     1528
     1529//如果post_author为空,则将其设置为当前用户。
     1530if(空($post->post_author)){
     1531$post->post_author=get_current_user_id();
     1532    }
     1533
     1534if('wp_template_part'===$post->post_type&&!isset($terms['wp_template_part_area']){
     1535$area_terms=get_the_terms($changes->ID,'wp_template_part_area');
     1536$terms['wp_template_part_area']=!is_wp_error($area_terms)&&!空($area_terms)$area_terms[0]->名称:空;
     1537    }
     1538
     1539$template=_build_block_template_object_from_post_object(新WP_post($post),$terms,$meta);
     1540
     1541if(iswp_error($template)){
     1542返回$template;
     1543    }
    14681544
    14691545$before_block_visitor=make_before_block_visitor($hooked_blocks,$template,'set_ignored_blocks_metadata');
    14701546$after_block_visitor=make_after_block_访客($hooked_blocks,$template,'set_ignored_hooked_ blocks_metadata');
    14711547
    1472 $blocks=解析块($post->post_content);
    1473 $content=traverse_and_serialize_blocks($blocks,$before_block_visitor,$after_block_visitors);
    1474 
    1475 $post->post_content=$content;
    1476 返回$post;
    1477 }
     1548$blocks=parse_blocks($changes->post_content);
     1549$changes->post_content=traverse_and_serialize_blocks($blocks,$before_block_visitor,$after_block_visitors);
     1550
     1551返回$changes;
     1552}
  • 分支/6.5/src/wp-includes/default-filters.php

    r57802码 58041兰特  
    754754
    755755//将ignoredHookedBlocks元数据属性添加到模板和模板零件发布类型。
    756 add_filter(“rest_pre_insert_wp_template”,“inject_ignored_hooked_blocks_metadata_attributes”, 10, 2);
    757 添加过滤器(“rest_pre_insert_wp_template_part”,“inject_ignored_hooked_blocks_metadata_attributes”, 10, 2);
     756add_filter(“rest_pre_insert_wp_template”,“inject_ignored_hooked_blocks_metadata_attributes”);
     757添加过滤器(“rest_pre_insert_wp_template_part”,“inject_ignored_hooked_blocks_metadata_attributes”);
    758758
    759759未设置($filter,$action);
  • 分支/6.5/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r57802码 58041兰特  
    533533     *
    534534*@param WP_REST_Request$请求请求对象。
    535 *@return stdClass要传递给wp_update_post的更改。
     535*@return stdClass|WP_错误要传递给wp_update_post的更改。
    536536     */
    537537受保护函数prepare_item_for_database($request){
  • 分支/6.5/tests/phpunit/tests/block-template-utils.php

    57803兰特 58041兰特  
    404404$this->assertTrue($has_html_files,'包含至少一个html文件');
    405405    }
    406 
    407     /**
    408 *@票60671
    409      *
    410 *@covers inject_ignored_hooked_blocks_metadata_attributes
    411      */
    412 公共函数test_inject_ignored_hooked_blocks_metadata_attributes_into_template(){
    413 全局$wp_current_filter;
    414 //模拟当前设置的过滤器。$wp_current_filter全局在拆卸过程中由重置
    415 //测试/phpunit/includes/abstract-testcase.php中的WP_UnitTestCase_Base::_restore_hooks()。
    416 $wp_current_filter[]=“rest_pre_insert_wp_template”;
    417 
    418 寄存器块类型(
    419 '测试/挂接块',
    420 阵列(
    421 “block_hooks”=>数组(
    422 “测试/锚块”=>“之后”,
    423                 ),
    424             )
    425         );
    426 
    427 $id=self::TEST_THEME.'//'。'my_template';
    428 $request=新的WP_REST_request('POST','/WP/v2/templates/'.$id);
    429 
    430 $changes=新stdClass();
    431 $changes->post_content='<!--wp:tests/anchor-block-->你好<!--/wp:测试/锚块-->';
    432 
    433 $post=inject_ignored_hooked_blocks_metadata_attributes($changes,$request);
    434 $this->资产相同(
    435             '<!-- wp:tests/anchor-block{“metadata”:{“ignoredHookedBlocks”:[“tests/hoked-block”]}-->Hello<!--/wp:测试/锚块-->',
    436 $post->post_content,
    437 “钩子块未插入锚块的ignoredHookedBlocks元数据。”
    438         );
    439     }
    440 
    441     /**
    442 *@票60671
    443      *
    444 *@covers inject_ignored_hooked_blocks_metadata_attributes
    445      */
    446 公共函数test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part(){
    447 全局$wp_current_filter;
    448 //模拟当前设置的过滤器。$wp_current_filter全局变量在拆卸期间被重置
    449 //测试/phpunit/includes/abstract-testcase.php中的WP_UnitTestCase_Base::_restore_hooks()。
    450 $wp_current_filter[]=“rest_pre_insert_wp_template_part”;
    451 
    452 寄存器块类型(
    453 '测试/挂接块',
    454 阵列(
    455 “block_hooks”=>数组(
    456 “测试/锚块”=>“之后”,
    457                 ),
    458             )
    459         );
    460 
    461 $id=self::TEST_THEME.'//'。'我的模板部件';
    462 $request=新的WP_REST_request('POST','/WP/v2/template-parts/'.$id);
    463 
    464 $changes=新的stdClass();
    465 $changes->post_content='<!--wp:tests/anchor-block-->你好<!--/wp:测试/锚块-->';
    466 
    467 $post=inject_ignored_hooked_blocks_metadata_attributes($changes,$request);
    468 $this->资产相同(
    469             '<!-- wp:tests/anchor-block{“metadata”:{“ignoredHookedBlocks”:[“tests/hoked-block”]}-->Hello<!--/wp:测试/锚块-->',
    470 $post->post_content,
    471 “钩子块未插入锚块的ignoredHookedBlocks元数据。”
    472         );
    473     }
    474406}
  • 分支/6.5/tests/phpunit/tests/rest-api/wpRestTemplatesController.php

    r57802码 58041兰特  
    1515     */
    1616受保护的静态$admin_id;
    17 私有静态$post;
     17私有静态$template_post;
     18私有静态$template_part_post;
    1819
    1920    /**
     
    3031
    3132//设置模板帖子。
    32 $个参数=数组(
     33$个参数         =数组(
    3334“post_type”=>“wp_template”,
    3435“post_name”=>“my_template”,
     
    4243            ),
    4344        );
    44 self::$post=self:;factory()->post->create_and_get($args);
    45 wp_set_post_terms(self::$post->ID,get_stylesheet(),'wp_theme');
     45自我::$template_post=self::factory()->post->create_and_get($args);
     46wp_set_post_terms(self::$template_post->ID,get_stylesheet(),'wp_theme');
     47
     48//设置模板部件发布。
     49$args=阵列(
     50'post_type'=>'wp_template_part',
     51“post_name”=>“my_template_part”,
     52“post_title”=>“我的模板部件”,
     53'post_content'=>'内容',
     54“post_excerpt”=>“我的模板部件说明”,
     55“tax_input”=>数组(
     56“wp_theme”=>数组(
     57获取样式表(),
     58                ),
     59“wp_template_part_area”=>数组(
     60WP_模板_零件_区域_标题,
     61                ),
     62            ),
     63        );
     64self::$template_part_post=self::factory()->post->create_and_get($args);
     65wp_set_post_terms(self::$template_part_post->ID,get_stylesheet(),'wp_theme');
     66wp_set_post_terms(自::$template_part_post->ID,wp_template_part_AREA_HEADER,'wp_template_part_AREA');
    4667    }
    4768
    4869公共静态函数wpTearDownAfterClass(){
    49 wp_delete_post(自身::$post->ID);
     70wp_delete_post(自身::$模板_post->ID);
    5071    }
    5172
     
    5778公共函数tear_down(){
    5879if(has_filter('rest_pre_insert_wp_template_part','inject_ignored_hooked_blocks_metadata_attributes')){
    59 删除过滤器('rest_pre_insert_wp_template_part','注入_ignored_hooked_blocks_metadata_attributes', 10);
     80删除过滤器('rest_pre_insert_wp_template_part','注入_ignored_hooked_blocks_metadata_attributes');
    6081        }
    6182if(WP_Block_Type_Registry::get_instance()->已注册(“测试/块”)){
     
    131152                ),
    132153“status”=>“publish”,
    133 “wp_id”=>自我::$帖子->ID,
     154“wp_id”=>自我::$模板_帖子->ID,
    134155“has_theme_file”=>假,
    135156“is_custom”=>为true,
    136157'作者'=>0,
    137 “修改”=>mysql_to_rfc3339(自我::$post->post_modified),
     158“修改”=>mysql_to_rfc3339(自我::$模板_post->post_modified),
    138159“author_text”=>“测试博客”,
    139160“original_source”=>“site”,
     
    178199                ),
    179200“status”=>“发布”,
    180 “wp_id”=>自我::$帖子->ID,
     201“wp_id”=>自我::$模板_帖子->ID,
    181202“has_theme_file”=>假,
    182203“is_custom”=>为true,
    183204“作者”=>0,
    184 “修改”=>mysql_to_rfc3339(自我::$post->post_modified),
     205“修改”=>mysql_to_rfc3339(自我::$模板_post->post_modified),
    185206“author_text”=>“测试博客”,
    186207“original_source”=>“site”,
     
    217238                ),
    218239“status”=>“publish”,
    219 “wp_id”=>自我::$帖子->ID,
     240“wp_id”=>自我::$模板_帖子->ID,
    220241“has_theme_file”=>假,
    221242“is_custom”=>为true,
    222243'作者'=>0,
    223 “修改”=>mysql_to_rfc3339(自我::$post->post_modified),
     244“修改”=>mysql_to_rfc3339(自我::$模板_post->post_modified),
    224245“author_text”=>“测试博客”,
    225246“original_source”=>“site”,
     
    945966        );
    946967
    947 添加过滤器(“rest_pre_insert_wp_template_part”,“inject_ignored_hooked_blocks_metadata_attributes”,10,2);
     968添加过滤器(“rest_pre_insert_wp_template_part”,“inject_ignored_hooked_blocks_metadata_attributes”);
    948969
    949970$endpoint=新WP_REST_Templates_Controller('WP_template_part');
     
    952973$prepare_item_for_database->setAccessible(true);
    953974
     975$id=get_stylesheet().'//'。'我的模板部件';
    954976$body_params=数组(
    955             'title“=>”无标题模板部件“,
    956 '段塞'=>'无标题模板-部件',
     977            'id’=>$id,
     978'段塞'=>'我的模板(_T)_部件',
    957979“内容”=>“<!--wp:tests/anchor-block-->你好<!--/wp:测试/锚块-->',
    958980        );
注:请参见TracChangeset(跟踪变更集)获取有关使用变更集查看器的帮助。