_wp_filter_post_meta_footnotes()
云策文档标注
概述
_wp_filter_post_meta_footnotes() 函数用于处理脚注数据,移除脚注内容中的所有 HTML 标签,并对 ID 进行清理。它期望输入为已转义的 JSON 字符串,并返回过滤后的 JSON 数据。
关键要点
- 函数接收一个 JSON 编码的字符串参数 $footnotes,包含脚注内容和 ID 的数组。
- 使用 json_decode() 解码 JSON 字符串,并检查是否为数组,否则返回空字符串。
- 遍历脚注数组,对每个脚注的 ID 应用 sanitize_key() 进行清理,对内容应用 wp_slash()、wp_filter_post_kses() 和 wp_unslash() 来移除 HTML 标签并处理转义。
- 返回处理后的脚注数组的 JSON 编码字符串,确保数据安全且格式正确。
代码示例
function _wp_filter_post_meta_footnotes( $footnotes ) {
$footnotes_decoded = json_decode( $footnotes, true );
if ( ! is_array( $footnotes_decoded ) ) {
return '';
}
$footnotes_sanitized = array();
foreach ( $footnotes_decoded as $footnote ) {
if ( ! empty( $footnote['content'] ) && ! empty( $footnote['id'] ) ) {
$footnotes_sanitized[] = array(
'id' => sanitize_key( $footnote['id'] ),
'content' => wp_unslash( wp_filter_post_kses( wp_slash( $footnote['content'] ) ) ),
);
}
}
return wp_json_encode( $footnotes_sanitized );
}
原文内容
Strips all HTML from the content of footnotes, and sanitizes the ID.
Description
This function expects slashed data on the footnotes content.
Parameters
$footnotesstringrequired-
JSON-encoded string of an array containing the content and ID of each footnote.
Source
function _wp_filter_post_meta_footnotes( $footnotes ) {
$footnotes_decoded = json_decode( $footnotes, true );
if ( ! is_array( $footnotes_decoded ) ) {
return '';
}
$footnotes_sanitized = array();
foreach ( $footnotes_decoded as $footnote ) {
if ( ! empty( $footnote['content'] ) && ! empty( $footnote['id'] ) ) {
$footnotes_sanitized[] = array(
'id' => sanitize_key( $footnote['id'] ),
'content' => wp_unslash( wp_filter_post_kses( wp_slash( $footnote['content'] ) ) ),
);
}
}
return wp_json_encode( $footnotes_sanitized );
}
Changelog
| Version | Description |
|---|---|
| 6.3.2 | Introduced. |