WP_块{}

表示块的已分析实例的类。

方法

姓名描述
WP_Block::__construct建造师。
WP_块::__get从无法访问的属性返回值。
WP_Block::进程块绑定处理块绑定并使用源中的值更新块属性。
WP_Block::呈现生成块的渲染输出。
WP_Block::replace_html根据块属性名称,根据提供的值替换HTML中的值。

来源

WP_Block类{/***块的原始解析数组表示。**@自5.5.0起*@var数组*/公共$parsed_block;/***块的名称。**@示例“核心/段落”**@自5.5.0起*@var字符串*/公共$name;/***与实例关联的块类型。**@自5.5.0起*@var WP_Block_Type(@变量WP_Block类型)*/公共$block_type;/***块上下文值。**@自5.5.0起*@var数组*/public$context=array();/***当前层次结构的所有可用上下文。**@自5.5.0起*@var数组*@访问保护*/受保护的$available_context;/***块类型注册表。**@自5.9.0起*@var WP_Block_Type_Registry(变量WP_Block_Type_Registry)*@访问保护*/受保护的$registry;/***内部块列表(同类)**@自5.5.0起*@var WP_Block列表*/public$inner_blocks=array();/***删除内部块注释分隔符后从内部块注释定界符生成的HTML*块。**@example“…只是<!--wp:test/-->testing…”->“只是测试…”**@自5.5.0起*@var字符串*/public$inner_html=“”;/***找到内部块的字符串片段和空标记列表**@示例数组(*“inner_html”=>“BeforeInnerAfter”,*“inner_blocks”=>数组(块,块),*“inner_content”=>数组(“Before”,null,“inner”,null,“After”),* )**@自5.5.0起*@var数组*/public$inner_content=array();/***建造师。**从提供的块实例参数填充对象特性。**给定的上下文值数组不一定在上可用*实例本身,但被视为*这个街区的祖先。这被分配给私有`available_context`*属性。仅限块通过配置为使用的值*它的注册类型将被分配给块的“context”属性。**@自5.5.0起**@param数组$block{*单个解析块对象的代表数组。请参阅WP_Block_Parser_Block。**@type string$blockName块的名称。*@type array$attrs块注释分隔符中的属性。*@type array$innerBlocks内部块的列表。数组的数组*结构和这个一样。*来自内部块注释分隔符的@type string$innerHTML HTML。*@type数组$innerContent找到内部块的字符串片段和空标记的列表。* }*@param array$available_context祖先上下文值的可选数组。*@param WP_Block_Type_Registry$Registry可选块类型注册表。*/公共函数__construct($block,$available_context=array(),$registry=null){$this->parsed_block=$block;$this->名称=$block['blockName'];if(is_null($registry)){$registry=WP_Block_Type_registry::get_instance();}$this->registry=$registry;$this->block_type=$registry->get_registered($this->名称);$this->available_context=$available_context;if(!空($this->block_type->uses_context)){foreach($this->block_type->uses_context作为$context_name){if(array_key_exists($context_name,$this->available_context)){$this->上下文[$context_name]=$this->available_context[$context _name];}}}if(!空($block['innerBlocks']){$child_context=$this->available_context;if(!空($this->block_type->provides_context)){foreach($this->block_type->provides_context为$context_name=>$attribute_name){if(array_key_exists($attribute_name,$this->attributes)){$child_context[$context_name]=$this->属性[$attribute_name];}}}$this->inner_blocks=new WP_Block_List($Block['inerBlocks'],$child_context,$registry);}if(!空($block['innerHTML']){$this->inner_html=$block['innerHTML'];}if(!空($block['innerContent']){$this->inner_content=$block['innerContent'];}}/***从无法访问的属性返回值。**这用于延迟初始化块的“attributes”属性,*以便在*属性即被访问。对于所有其他无法访问的属性,`null`*返回值。**@自5.5.0起**@param string$name属性名。*@return array|null准备好的属性,或null。*/公共函数__get($name){if(“属性”===$name){$this->attributes=isset($this->parsed_block['attrs'])?$this->parsed_block['attrs']:数组();if(!is_null($this->block_type)){$this->属性=$this->block_type->prepare_attributes_for_render($this-->属性);}返回$this->属性;}返回null;}/***处理块绑定并使用源中的值更新块属性。**块的属性中可能包含绑定。绑定是映射*块的属性和源之间。“源”是一个函数*使用“register_block_bindings_source()”注册,定义如何*从块外部检索值,例如从post-meta。**此函数将处理这些绑定并更新块的属性*值来自绑定。**###示例**图像块的“bindings”属性可能如下所示:**```杰森* {*“元数据”:{*“绑定”:{*“标题”:{*“source”:“核心/后元”,*“args”:{“key”:“text_custom_field”}*       },*“url”:{*“source”:“核心/后元”,*“args”:{“key”:“url_custom_field”}*       }*     }*   }* }* ```**上述示例将替换图像的“title”和“url”属性*具有`text_custom_field`和`url_custom_field`后元值的块。**自6.5.0起*@since 6.6.0为模式重写处理“__default”属性。**@return array提供的块绑定的计算块属性。*/私有函数process_block_bindings(){$parsed_block=$this->parsed_plock;$computed_attributes=数组();$supported_block_attributes=数组(“核心/段落”=>数组(“内容”),“core/heading”=>数组(“content”),“core/image”=>数组(“id”,“url”,“title”,“alt”),“core/button”=>数组(“url”,“text”,“linkTarget”,“rel”),);//如果块没有bindings属性,则不是受支持的//块类型或bindings属性不是数组,则返回块内容。如果(! isset($supported_block_attributes[$this->name])||空($parsed_block['attrs']['metadata']['绑定'])||! is_array($parsed_block['attrs']['metadata']['绑定'])) {返回$computed_attributes;}$bindings=$parsed_block['attrs']['metadata']['绑定'];/**如果为模式覆盖设置了默认绑定,请替换它*对所有支持的属性进行模式覆盖绑定。*/如果(isset($bindings['__default']['source'])&&“core/pattern-overrides”===$bindings['__default']['source']) {$updated_bindings=数组();/**构建所有受支持属性的绑定数组。*注意,这也省略了*结果数组。*/foreach($supported_block_attributes[$parsed_block['blockName']]作为$attribute_name){//保留可能存在的任何非模式覆盖绑定。$updated_bindings[$attribute_name]=isset($bindings[$属性_name])? $绑定[$attribute_name]:array('source'=>'core/pattern-overrides');}$bindings=$updated_bindings;}foreach($bindings作为$attribute_name=>$block_binding){//如果该属性不在支持列表中,请处理下一个属性。if(!in_array($attribute_name,$supported_block_attributes[$this->name],true)){继续;}//如果未提供源,或者该源未注册,则处理下一个属性。if(!isset($block_binding['source'])||!is_string($block_binding['source']){继续;}$block_binding_source=get_block_bindings_source($block_binding['source']);if(null===$block_binding_source){继续;}$source_args=!空($block_binding['args'])&&is_array($block绑定['args'])$块绑定['args']:数组();$source_value=$block_binding_source->get_value($source_args,$this,$attribute_name);//如果值不为null,则基于块和属性处理HTML。if(!is_null($source_value)){$computed_attributes[$attribute_name]=$source_value;}}返回$computed_attributes;}/***根据块属性名称,根据提供的值替换HTML中的值。**自6.5.0起**@param string$block_content块内容。*@param string$attribute_name要替换的属性名。*@param混合了$source_value用于替换HTML中的值。*@return string修改的块内容。*/私有函数replace_html(字符串$block_content,字符串$attribute_name,$source_value){$block_type=$this->块类型;if(!isset($block_type->attributes[$attribute_name]['source']){返回$block_content;}//根据属性源,处理将有所不同。开关($block_type->attributes[$attribute_name]['source']){案例“html”:案例“rich-text”:$block_reader=新的WP_HTML_Tag_Processor($block_content);//TODO:只要CSS选择器在HTML API中就绪,就支持它们。//同时,通过将逗号分隔的选择器分解为数组来支持它们。$selectors=爆炸(',',$block_type->attributes[$attribute_name]['selector']);//向第一个标记添加书签,以便能够迭代选择器。$block_reader->next_tag();$block_reader->set_bookmark('迭代选择器');//TODO:当`set_iner_html`函数准备就绪时,不需要使用此选项。//存储父标记及其属性,以便以后能够在按钮中恢复它们。//按钮块有包装,而段落和标题块没有包装。if('核心/按钮'===$this->name){$button_wrapper=$block_reader->get_tag();$button_wrapper_attribute_names=$block_reader->get_attribute_names_with_prefix('');$button_wrapper_attrs=数组();foreach($button_wrapper_attribute_names作为$name){$button_wrapper_attrs[$name]=$block_reader->get_attribute($name);}}foreach($selectors作为$selector){//如果父标记或其任何子标记与选择器匹配,请替换HTML。if(strcasecmp($block_reader->get_tag($selector),$selecter)===0||$block_ reader->next_tag(阵列(“标记名”=>$选择器,)) ) {$block_reader->release_bookmark(“iterate-selectors”);//TODO:只要在html API中准备好,就使用`set_iner_html`方法。//在此之前,它是段落、标题和按钮块的硬编码。//存储标记及其属性,以便以后恢复它们。$selector_attribute_names=$block_reader->获取属性名称_with_prefix('');$selector_attrs=数组();foreach($selector_attribute_names作为$name){$selector_attrs[$name]=$block_reader->get_attribute($name);}$selector_markup=“<$selector>”。wp_kses_post($source_value)。“</$selector>”;$amended_content=新的WP_HTML_Tag_Processor($selector_markup);$amended_content->next_tag();foreach($selector_attrs作为$attribute_key=>$attripte_value){$amended_content->set_attribute($attribute_key,$attrivate_value);}if(“核心/段落”===$this->name||“核心/标题”==$his->name){返回$ameded_content->get_updated_html();}if('核心/按钮'===$this->name){$button_markup=“<$button包装器>{$amended_content->get_updated_html()}</$button-wrapper>”;$amended_button=新WP_HTML_Tag_Processor($button_markup);$amered_button->next_tag();foreach($button_wrapper_attrs作为$attribute_key=>$attrivate_value){$amered_button->set_attribute($attribute_key,$attrivate_value);}return$ameded_button->获取更新的html();}}其他{$block_reader->seek(“迭代选择器”);}}$block_reader->release_bookmark(“迭代选择器”);返回$block_content;case“属性”:$amended_content=新的WP_HTML_Tag_Processor($block_content);如果(!$amended_content->next_tag(阵列(//TODO:从CSS选择器生成查询。'tag_name'=>$block_type->属性[$attribute_name]['selector'],)) ) {返回$block_content;}$amended_content->set_attribute($block_type->attributes[$attribute_name]['attribute'],$source_value);返回$ameded_content->get_updated_html();违约:返回$block_content;}}/***生成块的渲染输出。**@自5.5.0起*@自6.5.0起增加了块绑定处理。**@global WP_Post$Post全局发布对象。**@param数组$选项{*可选选项对象。**@type bool$dynamic默认为“true”。(可选)设置为false以避免使用块的render_callback。* }*@return string渲染块输出。*/公共函数render($options=array()){全球$邮政;/**一次只能有一个根交互式块,因为该块的呈现HTML包含*所有内部块(包括任何交互式块)的呈现HTML。*/静态$root_interactive_block=空;/***筛选交互API是否应处理指令。**@自6.6.0起**@param bool$enabled是否启用指令处理。*/$interactity_process_directives_enabled=应用过滤器(“interactity_process_directives”,true);如果($interactivity_process_directives_enabled&&null===$root_interactive_block&&((isset($this->block_type->支持['interactivity'])&&true===$this->块类型->支持['interactivity'])||! 空($this->block_type->支持['interactive']['interative']))) {$root_interactive_block=$this;}$options=wp_parse_args($选项,阵列(“动态”=>true,));//处理块绑定并使用源中的值更新属性。$computed_attributes=$this->process_block_bindings();if(!空($computed_attributes)){//将计算的属性与原始属性合并。$this->attributes=array_merge($this->属性,$computed_attributes);}$is_dynamic=$options['dynamic']&&$this->名称&&null!==$this->block_type&&$this->block_type->is_dynamic();$block_content=“”;if(!$options['dynamic']||空($this->block_type->skip_inner_blocks)){$索引=0;foreach($this->inner_content作为$chunk){if(is_string($chunk)){$block_content.=块内容$块;}其他{$inner_block=$this->inner_blocks[$index];$parent_block=$this;/**此过滤器记录在wp-includes/blocks.php中*/$pre_render=应用过滤器('pre_render_block',null,$inner_block->parsed_block,$parent_block);if(!is_null($pre_render)){$block_content.=块内容$pre_render;}其他{$source_block=$inner_block->parsed_block;/**此过滤器记录在wp-includes/blocks.php中*/$inner_block->parsed_block=apply_filters('render_block_data',$inner_ block->parsed_klock,$source_block,$parent_block);/**此过滤器记录在wp-includes/blocks.php中*/$inner_block->context=apply_filters('render_block_context',$inner-block->context,$iner_block->parsed_block,$parent_block);$block_content.=块内容$inner_block->render();}++$index;}}}if(!空($computed_attributes)&&!空($block_content){foreach($computed_attributes作为$attribute_name=>$source_value){$block_content=$this->replace_html($block-content,$attribute_name,$source_value);}}if($is_dynamic){$global_post=$post;$parent=WP_Block_Supports::$Block_to_render;WP_Block_Supports::$Block_to_render=$this->parsed_Block;$block_content=(string)call_user_func($this->block_type->render_callback,$this->attributes,$block_content,$this);WP_Block_Supports::$Block_to_render=$parent;$post=$global_post;}if((!empty($this->block_type->script_handles)){foreach($this->block_type->script_handles作为$script_handle){wp_enqueue_script($script_handle);}}if(!空($this->block_type->view_script_handles)){foreach($this->block_type->view_script_handles作为$view_script_chandle){wp_enqueue_script($view_script_handle);}}if(!空($this->block_type->view_script_module_ids){foreach($this->block_type->view_script_module_ids作为$view_script_module_id){wp_enqueue_script_module($view_script_modele_id);}}if((!empty($this->block_type->stylehandles)){foreach($this->block_type->style_handles作为$style_hannel){wp_enqueue_style($style_handle);}}if((!empty($this->block_type->view_style_handles)){foreach($this->block_type->view_style_handles作为$view_styled_handle){wp_enqueue_style($view_style_handle);}}/***过滤单个块的内容。**@自5.0.0起*@since 5.9.0添加了`$instance`参数。**@param string$block_content块内容。*@param array$block完整块,包括名称和属性。*@param WP_Block$instance块实例。*/$block_content=应用过滤器('render_block',$block-content,$this->parsed_block,$this);/***过滤单个块的内容。**钩子名称的动态部分`$name`表示*块名称,例如“核心/段落”。**@自5.7.0起*@since 5.9.0添加了`$instance`参数。**@param string$block_content块内容。*@param array$block完整块,包括名称和属性。*@param WP_Block$instance块实例。*/$block_content=应用过滤器(“render_block_{$this->name}”,$block-content,$this->解析块,$this);if($root_interactive_block===$this){//根交互式块已完成呈现。处理指令的时间到了。$block_content=wp_interactivity_process_directives($block_content);$root_interactive_block=空;}返回$block_content;}}

变更日志

版本描述
5.5.0介绍。

用户贡献的笔记

你必须登录在能够发表注释或反馈之前。