WP_REST_Global_Styles_Controller::validate_custom_css( mixed $css ): true|WP_Error
- Since
- 6.2.0, 6.4.0, 7.0.0, 7.1.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php:683
Description
Currently just checks that CSS will not break an HTML STYLE tag.
Compatibility
- WordPress
- since 7.1.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
$cssmixed- 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_REST_Global_Styles_Controller::validate_custom_css() 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
- 12–51
- Plugin surface
- None
- Called by
- 1
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 85.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below WP_REST_Global_Styles_Controller::validate_custom_css()
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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Global_Styles_Controller::validate_custom_css() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_string($css) && !$at | 12 | strcspn() |
!is_string($css) | 12 | __() |
is_string($css) && $at && $remaining_strlen | 41 | strcspn(), substr_compare(), __(), esc_html(), sprintf() |
is_string($css) && $at && !$remaining_strlen | 51 | strcspn(), substr_compare(), strspn(), __(), esc_html(), sprintf() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 85 | 12–51 | 5 | |
| 8.5 | 85 | 12–51 | 5 | |
| 8.4 | 85 | 12–51 | 5 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 94 | 12–57 | 5 | |
| 8.2 | 94 | 12–57 | 5 | |
| 8.1 | 94 | 12–57 | 5 | |
| 7.4 | 94 | 12–57 | 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 · 3
- __()Retrieves the translation of $text.
- esc_html()Escaping for HTML blocks.
- WP_Error::__construct()Initializes the error.
Used by · 1
- WP_REST_Global_Styles_Controller::prepare_item_for_database()Prepares a single global styles config for update.
Source code
protected function validate_custom_css( $css ) { if ( ! is_string( $css ) ) { return new WP_Error( 'rest_custom_css_invalid_type', __( 'CSS must be a string.' ), array( 'status' => 400 ) ); } $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. * * @link 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 ) { return new WP_Error( 'rest_custom_css_illegal_markup', sprintf( /* translators: %s is the CSS that was provided. */ __( 'The CSS must not end in "%s".' ), esc_html( substr( $css, $at ) ) ), array( 'status' => 400 ) ); } if ( 1 === strspn( $css, " \t\f\r\n/>", $at + 7, 1 ) ) { return new WP_Error( 'rest_custom_css_illegal_markup', sprintf( /* translators: %s is the CSS that was provided. */ __( 'The CSS must not contain "%s".' ), esc_html( substr( $css, $at, 8 ) ) ), array( 'status' => 400 ) ); } } } return true; }Changelog
Introduced in 6.2.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$css retyped from string to mixed.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.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.