wp_kses_one_attr

函数
wp_kses_one_attr ( $string, $element )
参数
  • (string) $string The 'whole' attribute, including name and value.
    Required:
  • (string) $element The HTML element name to which the attribute belongs.
    Required:
返回值
  • (string) Filtered attribute.
定义位置
相关方法
wp_kses_attrwp_kses_check_attr_valwp_kses_attr_parsewp_kses_hairwp_kses_data
引入
4.2.3
弃用
-

wp_kses_one_attr: 这个函数用于通过删除任何潜在的恶意或不允许的内容来净化一个单一的HTML属性。

过滤一个HTML属性并确保其值是允许的。

这个函数可以在某些情况下逃避数据,因为`wp_kses()`必须剥离整个属性。

function wp_kses_one_attr( $string, $element ) {
	$uris              = wp_kses_uri_attributes();
	$allowed_html      = wp_kses_allowed_html( 'post' );
	$allowed_protocols = wp_allowed_protocols();
	$string            = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) );

	// Preserve leading and trailing whitespace.
	$matches = array();
	preg_match( '/^s*/', $string, $matches );
	$lead = $matches[0];
	preg_match( '/s*$/', $string, $matches );
	$trail = $matches[0];
	if ( empty( $trail ) ) {
		$string = substr( $string, strlen( $lead ) );
	} else {
		$string = substr( $string, strlen( $lead ), -strlen( $trail ) );
	}

	// Parse attribute name and value from input.
	$split = preg_split( '/s*=s*/', $string, 2 );
	$name  = $split[0];
	if ( count( $split ) == 2 ) {
		$value = $split[1];

		// Remove quotes surrounding $value.
		// Also guarantee correct quoting in $string for this one attribute.
		if ( '' === $value ) {
			$quote = '';
		} else {
			$quote = $value[0];
		}
		if ( '"' === $quote || "'" === $quote ) {
			if ( substr( $value, -1 ) != $quote ) {
				return '';
			}
			$value = substr( $value, 1, -1 );
		} else {
			$quote = '"';
		}

		// Sanitize quotes, angle braces, and entities.
		$value = esc_attr( $value );

		// Sanitize URI values.
		if ( in_array( strtolower( $name ), $uris, true ) ) {
			$value = wp_kses_bad_protocol( $value, $allowed_protocols );
		}

		$string = "$name=$quote$value$quote";
		$vless  = 'n';
	} else {
		$value = '';
		$vless = 'y';
	}

	// Sanitize attribute by name.
	wp_kses_attr_check( $name, $value, $string, $vless, $element, $allowed_html );

	// Restore whitespace.
	return $lead . $string . $trail;
}

常见问题

FAQs
查看更多 >