wp_filter_global_styles_post()
云策文档标注
概述
wp_filter_global_styles_post() 函数用于过滤全局样式用户内容,移除不安全规则。它处理 JSON 格式的帖子数据,确保主题 JSON 的安全性。
关键要点
- 函数接收一个字符串参数 $data,表示要过滤的帖子内容。
- 通过 JSON 解码和验证,检查是否为全局样式用户主题 JSON。
- 使用 WP_Theme_JSON::remove_insecure_properties() 移除不安全属性。
- 返回过滤后的 JSON 编码字符串,保留 isGlobalStylesUserThemeJSON 标志。
代码示例
function wp_filter_global_styles_post( $data ) {
$decoded_data = json_decode( wp_unslash( $data ), true );
$json_decoding_error = json_last_error();
if (
JSON_ERROR_NONE === $json_decoding_error &&
is_array( $decoded_data ) &&
isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
$decoded_data['isGlobalStylesUserThemeJSON']
) {
unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
$data_to_encode = WP_Theme_JSON::remove_insecure_properties( $decoded_data, 'custom' );
$data_to_encode['isGlobalStylesUserThemeJSON'] = true;
return wp_slash( wp_json_encode( $data_to_encode ) );
}
return $data;
}注意事项
- 函数仅在数据为有效的全局样式用户主题 JSON 时进行过滤,否则返回原始数据。
- 依赖 WP_Theme_JSON 类和相关辅助函数如 wp_unslash() 和 wp_slash()。
- 自 WordPress 5.9.0 版本引入。
原文内容
Sanitizes global styles user content removing unsafe rules.
Parameters
$datastringrequired-
Post content to filter.
Source
function wp_filter_global_styles_post( $data ) {
$decoded_data = json_decode( wp_unslash( $data ), true );
$json_decoding_error = json_last_error();
if (
JSON_ERROR_NONE === $json_decoding_error &&
is_array( $decoded_data ) &&
isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
$decoded_data['isGlobalStylesUserThemeJSON']
) {
unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
$data_to_encode = WP_Theme_JSON::remove_insecure_properties( $decoded_data, 'custom' );
$data_to_encode['isGlobalStylesUserThemeJSON'] = true;
return wp_slash( wp_json_encode( $data_to_encode ) );
}
return $data;
}
Changelog
| Version | Description |
|---|---|
| 5.9.0 | Introduced. |