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
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
- Scaling
- Scales with input
- Instructions
- 17–60
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 83.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
!$at && ->has_errors() | 17 | strcspn(), ->has_errors() |
!$at && !->has_errors() | 21 | strcspn(), ->has_errors(), ::validate() |
$at && $remaining_strlen && ->has_errors() | 46 | strcspn(), substr_compare(), __(), esc_html(), sprintf(), ->add(), ->has_errors() |
$at && $remaining_strlen && !->has_errors() | 50 | strcspn(), substr_compare(), __(), esc_html(), sprintf(), ->add(), ->has_errors(), ::validate() |
$at && !$remaining_strlen && ->has_errors() | 56 | strcspn(), substr_compare(), strspn(), __(), esc_html(), sprintf(), ->add(), ->has_errors() |
$at && !$remaining_strlen && !->has_errors() | 60 | strcspn(), substr_compare(), strspn(), __(), esc_html(), sprintf(), ->add(), ->has_errors(), ::validate() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 83 | 17–60 | 5 | |
| 8.5 | 83 | 17–60 | 5 | |
| 8.4 | 83 | 17–60 | 5 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 92 | 17–66 | 5 | |
| 8.2 | 92 | 17–66 | 5 | |
| 8.1 | 92 | 17–66 | 5 | |
| 7.4 | 92 | 17–66 | 5 |
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
- __()Retrieves the translation of $text.
- esc_html()Escaping for HTML blocks.
- WP_Error::__construct()Initializes the error.
- WP_Customize_Setting::validate()Validates an input.
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.
Signature, return type and hooks compared across 5 parsed releases.
$css to $value for PHP 8 named parameter support.from the docblockAbout 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.