函数文档

wp_kses_xml_named_entities()

💡 云策文档标注

概述

wp_kses_xml_named_entities() 是 wp_kses_normalize_entities() 正则表达式的回调函数,用于处理 XML 命名实体引用。它确保只接受有效的命名实体,并转换为代码点。

关键要点

  • 作为 wp_kses_normalize_entities() 的回调函数,处理正则匹配结果
  • 只接受有限、区分大小写且经过 XML 验证器严格审查的命名实体引用
  • 将 HTML 命名实体引用转换为对应的代码点
  • 参数 $matches 是 preg_replace_callback() 的匹配数组,必需
  • 返回正确编码的实体字符串
  • 函数内部使用全局变量 $allowedentitynames 和 $allowedxmlentitynames 进行验证
  • 如果匹配为空,返回空字符串;优先检查 XML 实体,其次 HTML 实体,否则返回原始实体

代码示例

function wp_kses_xml_named_entities( $matches ) {
    global $allowedentitynames, $allowedxmlentitynames;

    if ( empty( $matches[1] ) ) {
        return '';
    }

    $i = $matches[1];

    if ( in_array( $i, $allowedxmlentitynames, true ) ) {
        return "&$i;";
    } elseif ( in_array( $i, $allowedentitynames, true ) ) {
        return html_entity_decode( "&$i;", ENT_HTML5 );
    }

    return "&$i;";
}

注意事项

  • 该函数从 WordPress 5.5.0 版本引入
  • 依赖于全局变量 $allowedentitynames 和 $allowedxmlentitynames,确保在正确上下文中使用
  • 处理实体时区分 XML 和 HTML 实体,使用 ENT_HTML5 常量解码 HTML 实体

📄 原文内容

Callback for wp_kses_normalize_entities() regular expression.

Description

This function only accepts valid named entity references, which are finite, case-sensitive, and highly scrutinized by XML validators. HTML named entity references are converted to their code points.

Parameters

$matchesarrayrequired
preg_replace_callback() matches array.

Return

string Correctly encoded entity.

Source

function wp_kses_xml_named_entities( $matches ) {
	global $allowedentitynames, $allowedxmlentitynames;

	if ( empty( $matches[1] ) ) {
		return '';
	}

	$i = $matches[1];

	if ( in_array( $i, $allowedxmlentitynames, true ) ) {
		return "&$i;";
	} elseif ( in_array( $i, $allowedentitynames, true ) ) {
		return html_entity_decode( "&$i;", ENT_HTML5 );
	}

	return "&$i;";
}

Changelog

Version Description
5.5.0 Introduced.