wppaste
WordPress

wp_kses_split2( string $content, array[]|string $allowed_html, string[] $allowed_protocols ): string

Since
1.0.0, 6.6.0
Source
wp-includes/kses.php:1396
Callback for wp_kses_split() for fixing malformed HTML tags.

Description

This function does a lot of work. It rejects some very malformed things like <:::>. It returns an empty string, if the element isn't allowed (look ma, no strip_tags()!). Otherwise it splits the tag into an element and an attribute list.

After the tag is split into an element and an attribute list, it is run through another filter which will remove illegal attributes and once that is completed, will be returned.

Compatibility

WordPress
since 6.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$contentstring
Content to filter.
$allowed_htmlarray[]|string
An array of allowed HTML elements and attributes, or a context name such as 'post'. See wp_kses_allowed_html() for the list of accepted context names.
$allowed_protocolsstring[]
Array of allowed URL protocols.

Return value

string
Fixed HTML element

Performance profile

How much work a call to wp_kses_split2() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
10–44

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 95.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below wp_kses_split2()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 7 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_kses_split2() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$content10wp_kses_stripslashes()
!preg_match()21wp_kses_stripslashes(), preg_match()
$content29–38wp_kses_stripslashes(), wp_kses()
preg_match() && is_array($allowed_html)32–37wp_kses_stripslashes(), preg_match(), strtolower()
preg_match() && !is_array($allowed_html)36–41wp_kses_stripslashes(), preg_match(), wp_kses_allowed_html(), strtolower()
preg_match() && is_array($allowed_html) && isset($allowed_html) && $slash === ""40wp_kses_stripslashes(), preg_match(), strtolower(), wp_kses_attr()
preg_match() && !is_array($allowed_html) && isset($allowed_html) && $slash === ""44wp_kses_stripslashes(), preg_match(), wp_kses_allowed_html(), strtolower(), wp_kses_attr()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9510–4410
8.59510–4410
8.49510–441024 fewer instructions than PHP 8.3
8.311913–5610
8.211913–5610
8.111913–5610
7.411913–5610

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 5

Used by · 1

Source code

function wp_kses_split2( $content, $allowed_html, $allowed_protocols ) {	$content = wp_kses_stripslashes( $content ); 	/*	 * The regex pattern used to split HTML into chunks attempts	 * to split on HTML token boundaries. This function should	 * thus receive chunks that _either_ start with meaningful	 * syntax tokens, like a tag `<div>` or a comment `<!-- ... -->`.	 *	 * If the first character of the `$content` chunk _isn't_ one	 * of these syntax elements, which always starts with `<`, then	 * the match had to be for the final alternation of `>`. In such	 * case, it's probably standing on its own and could be encoded	 * with a character reference to remove ambiguity.	 *	 * In other words, if this chunk isn't from a match of a syntax	 * token, it's just a plaintext greater-than (`>`) sign.	 */	if ( ! str_starts_with( $content, '<' ) ) {		return '&gt;';	} 	/*	 * When certain invalid syntax constructs appear, the HTML parser	 * shifts into what's called the "bogus comment state." This is a	 * plaintext state that consumes everything until the nearest `>`	 * and then transforms the entire span into an HTML comment.	 *	 * Preserve these comments and do not treat them like tags.	 *	 * @see https://html.spec.whatwg.org/#bogus-comment-state	 */	if ( 1 === preg_match( '~^(?:</[^a-zA-Z][^>]*>|<![a-z][^>]*>)$~', $content ) ) {		/**		 * Since the pattern matches `</…>` and also `<!…>`, this will		 * preserve the type of the cleaned-up token in the output.		 */		$opener  = $content[1];		$content = substr( $content, 2, -1 ); 		do {			$prev    = $content;			$content = wp_kses( $content, $allowed_html, $allowed_protocols );		} while ( $prev !== $content ); 		// Recombine the modified inner content with the original token structure.		return "<{$opener}{$content}>";	} 	/*	 * Normative HTML comments should be handled separately as their	 * parsing rules differ from those for tags and text nodes.	 */	if ( str_starts_with( $content, '<!--' ) ) {		$content = str_replace( array( '<!--', '-->' ), '', $content ); 		while ( ( $newstring = wp_kses( $content, $allowed_html, $allowed_protocols ) ) !== $content ) {			$content = $newstring;		} 		if ( '' === $content ) {			return '';		} 		// Prevent multiple dashes in comments.		$content = preg_replace( '/--+/', '-', $content );		// Prevent three dashes closing a comment.		$content = preg_replace( '/-$/', '', $content ); 		return "<!--{$content}-->";	} 	// It's seriously malformed.	if ( ! preg_match( '%^<\s*(/\s*)?([a-zA-Z0-9-]+)([^>]*)>?$%', $content, $matches ) ) {		return '';	} 	$slash    = trim( $matches[1] );	$elem     = $matches[2];	$attrlist = $matches[3];

Changelog

Introduced in 1.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.6.0
Recognize additional forms of invalid HTML which convert into comments.from the docblock
1.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/kses.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.