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.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

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
Moderate

Only touches the object cache, via wp_cache_get().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
12–97

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

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

  • cacheobject cachewp_cache_get()one call below wp_render_block_visibility_support()

Further down the call graph this can also reach hook. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

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_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–38::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)54–58::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), wp_get_global_settings(), ::WP_Theme_JSON()
$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)67–72::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), wp_get_global_settings(), ::WP_Theme_JSON(), 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()74–79::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), wp_get_global_settings(), ::WP_Theme_JSON(), 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)88–93::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), wp_get_global_settings(), ::WP_Theme_JSON(), 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)92–97::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), wp_get_global_settings(), ::WP_Theme_JSON(), 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-dev13012–9722
8.513012–9722
8.413012–97223 fewer instructions than PHP 8.3
8.313312–10022
8.213312–10022
8.113312–10022
7.413312–10022

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

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_settings      = wp_get_global_settings( array( 'viewport' ) );		$viewport_media_queries = WP_Theme_JSON::get_viewport_media_queries(			$viewport_settings,			array(				'include_desktop' => true,			)		); 		/*		 * Viewport media queries are keyed by style-state names (`@mobile`,		 * `@tablet`, and `@desktop`). Block visibility metadata and generated		 * classes use plain viewport names, so map the keys at this boundary.		 */		$block_visibility_media_queries = array();		foreach ( $viewport_media_queries as $viewport_state => $media_query ) {			$block_visibility_media_queries[ ltrim( $viewport_state, '@' ) ] = $media_query;		}		$viewport_media_queries = $block_visibility_media_queries; 		$hidden_on = array(); 		// Collect which viewport the block is hidden on (only known viewport sizes).		foreach ( $viewport_config as $viewport_config_size => $is_visible ) {			if ( false === $is_visible && isset( $viewport_media_queries[ $viewport_config_size ] ) ) {				$hidden_on[] = $viewport_config_size;			}		} 		// If no viewport sizes have visibility set to false, return unchanged.		if ( empty( $hidden_on ) ) {			return $block_content;		} 		// Maintain consistent order of viewport sizes for class name generation.		sort( $hidden_on ); 		$css_rules   = array();		$class_names = array(); 		foreach ( $hidden_on as $hidden_viewport_size ) {			/*			 * If these values ever become user-defined,			 * they should be sanitized and kebab-cased.			 */			$visibility_class = 'wp-block-hidden-' . $hidden_viewport_size;			$class_names[]    = $visibility_class;			$css_rules[]      = array(				'selector'     => '.' . $visibility_class,				'declarations' => array(					'display' => 'none !important',				),				'rules_group'  => $viewport_media_queries[ $hidden_viewport_size ],			);

Changelog

Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.

  1. 6.9.7
  2. 7.0.4
  3. 7.1.0

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.1.0 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.