wppaste
WordPress

wp_render_block_visibility_support( string $block_content, array $block ): string

Since
6.9.0, 7.0.0
Source
wp-includes/block-supports/block-visibility.php:20
Render nothing if the block is hidden, or add viewport visibility styles.

Compatibility

WordPress
since 7.0.0
PHP
7.4–8.6-dev
  • 6.7.10
  • 6.8.11
  • 6.9.10
  • 7.0.7
  • 7.1.3

Present in 3 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$block_contentstring
Rendered block content.
$blockarray
Block object.

Return value

string
Filtered block content.

Performance profile

How much work a call to wp_render_block_visibility_support() 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
12–88

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

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

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

WhenInstructionsCalls it makes
always12–20::WP_Block_Type_Registry(), ->get_registered()
$block_visibility !== false25–49::WP_Block_Type_Registry(), ->get_registered(), block_has_support()
$block_visibility !== false && block_has_support() && is_array($block_visibility) && !empty($block_visibility) && is_array($viewport_config) && !empty($viewport_config) && !empty($hidden_on) && empty($block_content)58–63::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), sort(), wp_style_engine_get_stylesheet_from_css_rules()
$block_visibility !== false && block_has_support() && is_array($block_visibility) && !empty($block_visibility) && is_array($viewport_config) && !empty($viewport_config) && !empty($hidden_on) && !empty($block_content) && !->next_tag()65–70::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), sort(), wp_style_engine_get_stylesheet_from_css_rules(), selector(), ->next_tag()
$block_visibility !== false && block_has_support() && is_array($block_visibility) && !empty($block_visibility) && is_array($viewport_config) && !empty($viewport_config) && !empty($hidden_on) && !empty($block_content)79–84::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), sort(), wp_style_engine_get_stylesheet_from_css_rules(), selector(), ->next_tag(), ->add_class(), ->get_tag(), ->next_tag(), ->get_updated_html()
$block_visibility !== false && block_has_support() && is_array($block_visibility) && !empty($block_visibility) && is_array($viewport_config) && !empty($viewport_config) && !empty($hidden_on) && !empty($block_content)83–88::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), sort(), wp_style_engine_get_stylesheet_from_css_rules(), selector(), ->next_tag(), ->add_class(), ->get_tag(), ->set_attribute(), ->next_tag(), ->get_updated_html()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14712–8826
8.514712–8826
8.414712–88263 fewer instructions than PHP 8.3
8.315012–9126
8.215012–9126
8.115012–9126
7.415012–9126

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

Source code

function wp_render_block_visibility_support( $block_content, $block ) {	$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); 	if ( ! $block_type ) {		return $block_content;	} 	$block_visibility = $block['attrs']['metadata']['blockVisibility'] ?? null; 	// Hide the block whenever the value is boolean false, regardless of the	// block's current visibility support. This prevents blocks that previously	// supported visibility from unintentionally appearing on the front end	// after their support was disabled.	if ( false === $block_visibility ) {		return '';	} 	if ( ! block_has_support( $block_type, 'visibility', true ) ) {		return $block_content;	} 	if ( is_array( $block_visibility ) && ! empty( $block_visibility ) ) {		$viewport_config = $block_visibility['viewport'] ?? null; 		if ( ! is_array( $viewport_config ) || empty( $viewport_config ) ) {			return $block_content;		}		/*		 * Viewport size definitions are in several places in WordPress packages.		 * The following are taken from: https://github.com/WordPress/gutenberg/blob/trunk/packages/base-styles/_breakpoints.scss		 * The array is in a future, potential JSON format, and will be centralized		 * as the feature is developed.		 *		 * Viewport sizes as array items are defined sequentially. The first item's size is the max value.		 * Each subsequent item starts after the previous size (using > operator), and its size is the max.		 * The last item starts after the previous size (using > operator), and it has no max.		 */		$viewport_sizes = array(			array(				'name' => 'Mobile',				'slug' => 'mobile',				'size' => '480px',			),			array(				'name' => 'Tablet',				'slug' => 'tablet',				'size' => '782px',			),			array(				'name' => 'Desktop',				'slug' => 'desktop',				/*				 * Note: the last item in the $viewport_sizes array does not technically require a 'size' key,				 * as the last item's media query is calculated using `width > previous size`.				 * The last item is present for validating the attribute values, and in order to indicate				 * that this is the final viewport size, and to calculate the previous media query accordingly.				 */			),		); 		/*		 * Build media queries from viewport size definitions using the CSS range syntax.		 * Could be absorbed into the style engine,		 * as well as classname building, and declaration of the display property, if required.		 */		$viewport_media_queries = array();		$previous_size          = null;		foreach ( $viewport_sizes as $index => $viewport_size ) {			// First item: width <= size.			if ( 0 === $index ) {				$viewport_media_queries[ $viewport_size['slug'] ] = "@media (width <= {$viewport_size['size']})";			} elseif ( count( $viewport_sizes ) - 1 === $index && $previous_size ) {				// Last item: width > previous size.				$viewport_media_queries[ $viewport_size['slug'] ] = "@media (width > $previous_size)";			} else {				// Middle items: previous size < width <= size.				$viewport_media_queries[ $viewport_size['slug'] ] = "@media ({$previous_size} < width <= {$viewport_size['size']})";			} 			$previous_size = $viewport_size['size'] ?? null;

Changelog

Introduced in 6.9.0. Unchanged from 6.9.10 through 7.1.3.

  1. 6.9.10
  2. 7.0.7
  3. 7.1.3

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

7.0.0
Added support for viewport visibility.from the docblock
6.9.0
Introduced.from the docblock

About this page

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