wppaste
WordPress

WP_Customize_Custom_CSS_Setting::validate( string $value ): true|WP_Error

Since
4.7.0, 4.9.0, 5.9.0, 7.0.0
Source
wp-includes/customize/class-wp-customize-custom-css-setting.php:165
Validate a received value for being valid CSS.

Description

Checks for imbalanced braces, brackets, and comments.
Notifications are rendered when the customizer state is saved.

Compatibility

WordPress
since 7.0.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

$valuestring
CSS to validate.

Return value

true|WP_Error
True if the input was validated, otherwise WP_Error.

Performance profile

How much work a call to WP_Customize_Custom_CSS_Setting::validate() 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
17–60

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()one call below WP_Customize_Custom_CSS_Setting::validate()

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 · 6 distinct outcomes

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

WhenInstructionsCalls it makes
!$at && ->has_errors()17strcspn(), ->has_errors()
!$at && !->has_errors()21strcspn(), ->has_errors(), ::validate()
$at && $remaining_strlen && ->has_errors()46strcspn(), substr_compare(), __(), esc_html(), sprintf(), ->add(), ->has_errors()
$at && $remaining_strlen && !->has_errors()50strcspn(), substr_compare(), __(), esc_html(), sprintf(), ->add(), ->has_errors(), ::validate()
$at && !$remaining_strlen && ->has_errors()56strcspn(), substr_compare(), strspn(), __(), esc_html(), sprintf(), ->add(), ->has_errors()
$at && !$remaining_strlen && !->has_errors()60strcspn(), substr_compare(), strspn(), __(), esc_html(), sprintf(), ->add(), ->has_errors(), ::validate()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8317–605
8.58317–605
8.48317–6059 fewer instructions than PHP 8.3
8.39217–665
8.29217–665
8.19217–665
7.49217–665

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 · 4

Source code

	public function validate( $value ) {		// Restores the more descriptive, specific name for use within this method.		$css = $value; 		$validity = new WP_Error(); 		$length = strlen( $css );		for (			$at = strcspn( $css, '<' );			$at < $length;			$at += strcspn( $css, '<', ++$at )		) {			$remaining_strlen = $length - $at;			/**			 * Custom CSS text is expected to render inside an HTML STYLE element.			 * A STYLE closing tag must not appear within the CSS text because it			 * would close the element prematurely.			 *			 * The text must also *not* end with a partial closing tag (e.g., `<`,			 * `</`, … `</style`) because subsequent styles which are concatenated			 * could complete it, forming a valid `</style>` tag.			 *			 * Example:			 *			 *     $style_a = 'p { font-weight: bold; </sty';			 *     $style_b = 'le> gotcha!';			 *     $combined = "{$style_a}{$style_b}";			 *			 *     $style_a = 'p { font-weight: bold; </style';			 *     $style_b = 'p > b { color: red; }';			 *     $combined = "{$style_a}\n{$style_b}";			 *			 * Note how in the second example, both of the style contents are benign			 * when analyzed on their own. The first style was likely the result of			 * improper truncation, while the second is perfectly sound. It was only			 * through concatenation that these two styles combined to form content			 * that would have broken out of the containing STYLE element, thus			 * corrupting the page and potentially introducing security issues.			 *			 * @see https://html.spec.whatwg.org/multipage/parsing.html#rawtext-end-tag-name-state			 */			$possible_style_close_tag = 0 === substr_compare(				$css,				'</style',				$at,				min( 7, $remaining_strlen ),				true			);			if ( $possible_style_close_tag ) {				if ( $remaining_strlen < 8 ) {					$validity->add(						'illegal_markup',						sprintf(							/* translators: %s is the CSS that was provided. */							__( 'The CSS must not end in "%s".' ),							esc_html( substr( $css, $at ) )						)					);					break;				} 				if ( 1 === strspn( $css, " \t\f\r\n/>", $at + 7, 1 ) ) {					$validity->add(						'illegal_markup',						sprintf(							/* translators: %s is the CSS that was provided. */							__( 'The CSS must not contain "%s".' ),							esc_html( substr( $css, $at, 8 ) )						)					);					break;				}			}		} 		if ( ! $validity->has_errors() ) {			$validity = parent::validate( $css );		}		return $validity;	}

Changelog

Introduced in 4.7.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.

7.0.0
Only restricts contents which risk prematurely closing the STYLE element, either through a STYLE end tag or a prefix of one which might become a full end tag when combined with the contents of other styles.from the docblock
5.9.0
Renamed $css to $value for PHP 8 named parameter support.from the docblock
4.9.0
Checking for balanced characters has been moved client-side via linting in code editor.from the docblock
4.7.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/customize/class-wp-customize-custom-css-setting.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.