wp_get_global_styles_svg_filters()
云策文档标注
概述
wp_get_global_styles_svg_filters() 函数用于返回包含SVG过滤器(双色调)的字符串,但自WordPress 6.3.0起已被弃用。该函数通过合并主题JSON数据生成SVG过滤器,并支持缓存机制以提高性能。
关键要点
- 函数返回一个字符串,包含用作过滤器的SVG(双色调)。
- 自WordPress 6.3.0起被弃用,SVG生成现在通过块支持在每块基础上处理。
- 函数内部使用缓存机制,但在开发模式为'theme'时忽略缓存以避免干扰主题开发工作流。
- 依赖WP_Theme_JSON_Resolver::get_merged_data()合并数据,并根据主题是否支持theme.json文件调整数据来源。
代码示例
function wp_get_global_styles_svg_filters() {
_deprecated_function( __FUNCTION__, '6.3.0' );
$can_use_cached = ! wp_is_development_mode( 'theme' );
$cache_group = 'theme_json';
$cache_key = 'wp_get_global_styles_svg_filters';
if ( $can_use_cached ) {
$cached = wp_cache_get( $cache_key, $cache_group );
if ( $cached ) {
return $cached;
}
}
$supports_theme_json = wp_theme_has_theme_json();
$origins = array( 'default', 'theme', 'custom' );
if ( ! $supports_theme_json ) {
$origins = array( 'default' );
}
$tree = WP_Theme_JSON_Resolver::get_merged_data();
$svgs = $tree->get_svg_filters( $origins );
if ( $can_use_cached ) {
wp_cache_set( $cache_key, $svgs, $cache_group );
}
return $svgs;
}注意事项
- 此函数已弃用,建议开发者避免在新代码中使用,转而使用块支持的SVG生成方法。
- 在开发模式为'theme'时,缓存被禁用,以确保主题开发者能实时看到更改效果。
- 函数依赖于多个WordPress核心函数和类,如wp_is_development_mode()、wp_theme_has_theme_json()和WP_Theme_JSON_Resolver。
原文内容
Returns a string containing the SVGs to be referenced as filters (duotone).
Source
function wp_get_global_styles_svg_filters() {
_deprecated_function( __FUNCTION__, '6.3.0' );
/*
* Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
* developer's workflow.
*/
$can_use_cached = ! wp_is_development_mode( 'theme' );
$cache_group = 'theme_json';
$cache_key = 'wp_get_global_styles_svg_filters';
if ( $can_use_cached ) {
$cached = wp_cache_get( $cache_key, $cache_group );
if ( $cached ) {
return $cached;
}
}
$supports_theme_json = wp_theme_has_theme_json();
$origins = array( 'default', 'theme', 'custom' );
if ( ! $supports_theme_json ) {
$origins = array( 'default' );
}
$tree = WP_Theme_JSON_Resolver::get_merged_data();
$svgs = $tree->get_svg_filters( $origins );
if ( $can_use_cached ) {
wp_cache_set( $cache_key, $svgs, $cache_group );
}
return $svgs;
}