wp_filter_out_block_nodes()
云策文档标注
概述
wp_filter_out_block_nodes() 是一个 WordPress 函数,用于过滤从 WP_Theme_JSON::get_style_nodes() 获取的样式节点列表。该函数的核心作用是移除所有与块相关的节点,以保持 WP_Theme_JSON 类对 CSS 实现细节的独立性。
关键要点
- 函数用途:过滤样式节点数组,移除所有包含 'blocks' 路径的节点。
- 设计目的:允许根据是否加载独立资源来修改 WP_Theme_JSON 的输出,而不让类感知这些细节。
- 参数:$nodes(数组,必需),表示要过滤的节点数组。
- 返回值:返回过滤后的样式节点数组。
代码示例
function wp_filter_out_block_nodes( $nodes ) {
return array_filter(
$nodes,
static function ( $node ) {
return ! in_array( 'blocks', $node['path'], true );
},
ARRAY_FILTER_USE_BOTH
);
}注意事项
- 该函数在 WordPress 6.1.0 版本中引入。
- 使用 ARRAY_FILTER_USE_BOTH 标志,确保回调函数可以访问键和值。
原文内容
Applies a filter to the list of style nodes that comes from WP_Theme_JSON::get_style_nodes().
Description
This particular filter removes all of the blocks from the array.
We want WP_Theme_JSON to be ignorant of the implementation details of how the CSS is being used.
This filter allows us to modify the output of WP_Theme_JSON depending on whether or not we are loading separate assets, without making the class aware of that detail.
Parameters
$nodesarrayrequired-
The nodes to filter.
Source
function wp_filter_out_block_nodes( $nodes ) {
return array_filter(
$nodes,
static function ( $node ) {
return ! in_array( 'blocks', $node['path'], true );
},
ARRAY_FILTER_USE_BOTH
);
}
Changelog
| Version | Description |
|---|---|
| 6.1.0 | Introduced. |