[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/block-supports/ -> block-visibility.php (source)

   1  <?php
   2  /**
   3   * Block visibility block support flag.
   4   *
   5   * @package WordPress
   6   * @since 6.9.0
   7   */
   8  
   9  /**
  10   * Render nothing if the block is hidden, or add viewport visibility styles.
  11   *
  12   * @since 6.9.0
  13   * @since 7.0.0 Added support for viewport visibility.
  14   * @access private
  15   *
  16   * @param string $block_content Rendered block content.
  17   * @param array  $block         Block object.
  18   * @return string Filtered block content.
  19   */
  20  function wp_render_block_visibility_support( $block_content, $block ) {
  21      $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
  22  
  23      if ( ! $block_type ) {
  24          return $block_content;
  25      }
  26  
  27      $block_visibility = $block['attrs']['metadata']['blockVisibility'] ?? null;
  28  
  29      // Hide the block whenever the value is boolean false, regardless of the
  30      // block's current visibility support. This prevents blocks that previously
  31      // supported visibility from unintentionally appearing on the front end
  32      // after their support was disabled.
  33      if ( false === $block_visibility ) {
  34          return '';
  35      }
  36  
  37      if ( ! block_has_support( $block_type, 'visibility', true ) ) {
  38          return $block_content;
  39      }
  40  
  41      if ( is_array( $block_visibility ) && ! empty( $block_visibility ) ) {
  42          $viewport_config = $block_visibility['viewport'] ?? null;
  43  
  44          if ( ! is_array( $viewport_config ) || empty( $viewport_config ) ) {
  45              return $block_content;
  46          }
  47          $viewport_settings      = wp_get_global_settings( array( 'viewport' ) );
  48          $viewport_media_queries = WP_Theme_JSON::get_viewport_media_queries(
  49              $viewport_settings,
  50              array(
  51                  'include_desktop' => true,
  52              )
  53          );
  54  
  55          /*
  56           * Viewport media queries are keyed by style-state names (`@mobile`,
  57           * `@tablet`, and `@desktop`). Block visibility metadata and generated
  58           * classes use plain viewport names, so map the keys at this boundary.
  59           */
  60          $block_visibility_media_queries = array();
  61          foreach ( $viewport_media_queries as $viewport_state => $media_query ) {
  62              $block_visibility_media_queries[ ltrim( $viewport_state, '@' ) ] = $media_query;
  63          }
  64          $viewport_media_queries = $block_visibility_media_queries;
  65  
  66          $hidden_on = array();
  67  
  68          // Collect which viewport the block is hidden on (only known viewport sizes).
  69          foreach ( $viewport_config as $viewport_config_size => $is_visible ) {
  70              if ( false === $is_visible && isset( $viewport_media_queries[ $viewport_config_size ] ) ) {
  71                  $hidden_on[] = $viewport_config_size;
  72              }
  73          }
  74  
  75          // If no viewport sizes have visibility set to false, return unchanged.
  76          if ( empty( $hidden_on ) ) {
  77              return $block_content;
  78          }
  79  
  80          // Maintain consistent order of viewport sizes for class name generation.
  81          sort( $hidden_on );
  82  
  83          $css_rules   = array();
  84          $class_names = array();
  85  
  86          foreach ( $hidden_on as $hidden_viewport_size ) {
  87              /*
  88               * If these values ever become user-defined,
  89               * they should be sanitized and kebab-cased.
  90               */
  91              $visibility_class = 'wp-block-hidden-' . $hidden_viewport_size;
  92              $class_names[]    = $visibility_class;
  93              $css_rules[]      = array(
  94                  'selector'     => '.' . $visibility_class,
  95                  'declarations' => array(
  96                      'display' => 'none !important',
  97                  ),
  98                  'rules_group'  => $viewport_media_queries[ $hidden_viewport_size ],
  99              );
 100          }
 101  
 102          wp_style_engine_get_stylesheet_from_css_rules(
 103              $css_rules,
 104              array(
 105                  'context'  => 'block-supports',
 106                  'prettify' => false,
 107              )
 108          );
 109  
 110          if ( ! empty( $block_content ) ) {
 111              $processor = new WP_HTML_Tag_Processor( $block_content );
 112              if ( $processor->next_tag() ) {
 113                  $processor->add_class( implode( ' ', $class_names ) );
 114  
 115                  /*
 116                   * Set all IMG tags to be `fetchpriority=auto` so that wp_get_loading_optimization_attributes() won't add
 117                   * `fetchpriority=high` or increment the media count to affect whether subsequent IMG tags get `loading=lazy`.
 118                   */
 119                  do {
 120                      if ( 'IMG' === $processor->get_tag() ) {
 121                          $processor->set_attribute( 'fetchpriority', 'auto' );
 122                      }
 123                  } while ( $processor->next_tag() );
 124                  $block_content = $processor->get_updated_html();
 125              }
 126          }
 127      }
 128  
 129      return $block_content;
 130  }
 131  
 132  add_filter( 'render_block', 'wp_render_block_visibility_support', 10, 2 );


Generated : Sat Jul 25 08:20:20 2026 Cross-referenced by PHPXref