wppaste
WordPress

wp_render_custom_css_support_styles( array $parsed_block ): array

Since
7.0.0
Source
wp-includes/block-supports/custom-css.php:41
Render the custom CSS stylesheet and add class name to block as required.

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 2 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$parsed_blockarray
The parsed block.

Return value

array
The same parsed block with custom CSS class name added if appropriate.

Performance profile

How much work a call to wp_render_custom_css_support_styles() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
9–85

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

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 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_render_custom_css_support_styles() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always9–13none
is_string($custom_css)26–29::WP_Block_Type_Registry(), ->get_registered(), block_has_support()
is_string($custom_css) && block_has_support() && !$custom_css && empty($processed_css)52–57::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), wp_unique_id_from_values(), ::WP_Theme_JSON()
is_string($custom_css) && block_has_support() && !$custom_css && !empty($processed_css) && wp_style_is() && $processed_css70–76::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), wp_unique_id_from_values(), ::WP_Theme_JSON(), wp_style_is(), wp_styles(), ->get_data()
is_string($custom_css) && block_has_support() && !$custom_css && !empty($processed_css) && wp_style_is() && !$processed_css74–80::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), wp_unique_id_from_values(), ::WP_Theme_JSON(), wp_style_is(), wp_styles(), ->get_data(), wp_add_inline_style()
is_string($custom_css) && block_has_support() && !$custom_css && !empty($processed_css) && !wp_style_is() && $processed_css75–81::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), wp_unique_id_from_values(), ::WP_Theme_JSON(), wp_style_is(), wp_register_style(), wp_styles(), ->get_data()
is_string($custom_css) && block_has_support() && !$custom_css && !empty($processed_css) && !wp_style_is() && !$processed_css79–85::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), wp_unique_id_from_values(), ::WP_Theme_JSON(), wp_style_is(), wp_register_style(), wp_styles(), ->get_data(), wp_add_inline_style()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev899–8511
8.5899–8511
8.4899–85118 fewer instructions than PHP 8.3
8.3979–9311
8.2979–9311
8.1979–93111 fewer instruction than PHP 7.4
7.4989–9411

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

Source code

function wp_render_custom_css_support_styles( $parsed_block ) {	$custom_css = $parsed_block['attrs']['style']['css'] ?? null;	if ( ! is_string( $custom_css ) || '' === trim( $custom_css ) ) {		return $parsed_block;	} 	$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] );	if ( ! block_has_support( $block_type, 'customCSS', true ) ) {		return $parsed_block;	} 	// Validate CSS doesn't contain HTML markup (same validation as global styles REST API).	if ( preg_match( '#</?\w+#', $custom_css ) ) {		return $parsed_block;	} 	// Generate a unique class name for this block instance.	$class_name          = wp_unique_id_from_values( $parsed_block, 'wp-custom-css-' );	$existing_class_name = $parsed_block['attrs']['className'] ?? null;	$updated_class_name  = is_string( $existing_class_name )		? "$existing_class_name $class_name"		: $class_name; 	$parsed_block['attrs']['className'] = $updated_class_name; 	// Process the custom CSS using the same method as global styles.	$selector      = '.' . $class_name;	$processed_css = WP_Theme_JSON::process_blocks_custom_css( $custom_css, $selector ); 	if ( ! empty( $processed_css ) ) {		/**		 * Reuse one handle so identical custom CSS is enqueued only once via		 * {@see wp_unique_id_from_values()}. Explicitly declare the `wp-block-library`		 * dependency so `global-styles` is guaranteed to print after it, preventing		 * block default styles from unintentionally overriding global styles.		 */		$handle = 'wp-block-custom-css';		if ( ! wp_style_is( $handle, 'registered' ) ) {			wp_register_style( $handle, false, array( 'wp-block-library', 'global-styles' ) );		}		$after_styles = wp_styles()->get_data( $handle, 'after' );		if ( ! is_array( $after_styles ) ) {			$after_styles = array();		}		if ( ! in_array( $processed_css, $after_styles, true ) ) {			wp_add_inline_style( $handle, $processed_css );		}	} 	return $parsed_block;}

Changelog

Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

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

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/block-supports/custom-css.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.