[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Dimensions block support flag.
   4   *
   5   * This does not include the `spacing` block support even though that visually
   6   * appears under the "Dimensions" panel in the editor. It remains in its
   7   * original `spacing.php` file for compatibility with core.
   8   *
   9   * @package WordPress
  10   * @since 5.9.0
  11   */
  12  
  13  /**
  14   * Registers the style block attribute for block types that support it.
  15   *
  16   * @since 5.9.0
  17   * @access private
  18   *
  19   * @param WP_Block_Type $block_type Block Type.
  20   */
  21  function wp_register_dimensions_support( $block_type ) {
  22      // Setup attributes and styles within that if needed.
  23      if ( ! $block_type->attributes ) {
  24          $block_type->attributes = array();
  25      }
  26  
  27      // Check for existing style attribute definition e.g. from block.json.
  28      if ( array_key_exists( 'style', $block_type->attributes ) ) {
  29          return;
  30      }
  31  
  32      $has_dimensions_support = block_has_support( $block_type, 'dimensions', false );
  33  
  34      if ( $has_dimensions_support ) {
  35          $block_type->attributes['style'] = array(
  36              'type' => 'object',
  37          );
  38      }
  39  }
  40  
  41  /**
  42   * Adds CSS classes for block dimensions to the incoming attributes array.
  43   * This will be applied to the block markup in the front-end.
  44   *
  45   * @since 5.9.0
  46   * @since 6.2.0 Added `minHeight` support.
  47   * @access private
  48   *
  49   * @param WP_Block_Type $block_type       Block Type.
  50   * @param array         $block_attributes Block attributes.
  51   * @return array Block dimensions CSS classes and inline styles.
  52   */
  53  function wp_apply_dimensions_support( $block_type, $block_attributes ) {
  54      if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) {
  55          return array();
  56      }
  57  
  58      $attributes = array();
  59  
  60      // Width support to be added in near future.
  61  
  62      $has_min_height_support = block_has_support( $block_type, array( 'dimensions', 'minHeight' ), false );
  63      $block_styles           = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null;
  64  
  65      if ( ! $block_styles ) {
  66          return $attributes;
  67      }
  68  
  69      $skip_min_height                      = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'minHeight' );
  70      $dimensions_block_styles              = array();
  71      $dimensions_block_styles['minHeight'] = null;
  72      if ( $has_min_height_support && ! $skip_min_height ) {
  73          $dimensions_block_styles['minHeight'] = isset( $block_styles['dimensions']['minHeight'] )
  74              ? $block_styles['dimensions']['minHeight']
  75              : null;
  76      }
  77      $styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
  78  
  79      if ( ! empty( $styles['css'] ) ) {
  80          $attributes['style'] = $styles['css'];
  81      }
  82  
  83      return $attributes;
  84  }
  85  
  86  /**
  87   * Renders server-side dimensions styles to the block wrapper.
  88   * This block support uses the `render_block` hook to ensure that
  89   * it is also applied to non-server-rendered blocks.
  90   *
  91   * @since 6.5.0
  92   * @access private
  93   *
  94   * @param  string $block_content Rendered block content.
  95   * @param  array  $block         Block object.
  96   * @return string                Filtered block content.
  97   */
  98  function wp_render_dimensions_support( $block_content, $block ) {
  99      $block_type               = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
 100      $block_attributes         = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
 101      $has_aspect_ratio_support = block_has_support( $block_type, array( 'dimensions', 'aspectRatio' ), false );
 102  
 103      if (
 104          ! $has_aspect_ratio_support ||
 105          wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'aspectRatio' )
 106      ) {
 107          return $block_content;
 108      }
 109  
 110      $dimensions_block_styles                = array();
 111      $dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null;
 112  
 113      // To ensure the aspect ratio does not get overridden by `minHeight` unset any existing rule.
 114      if (
 115          isset( $dimensions_block_styles['aspectRatio'] )
 116      ) {
 117          $dimensions_block_styles['minHeight'] = 'unset';
 118      } elseif (
 119          isset( $block_attributes['style']['dimensions']['minHeight'] ) ||
 120          isset( $block_attributes['minHeight'] )
 121      ) {
 122          $dimensions_block_styles['aspectRatio'] = 'unset';
 123      }
 124  
 125      $styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
 126  
 127      if ( ! empty( $styles['css'] ) ) {
 128          // Inject dimensions styles to the first element, presuming it's the wrapper, if it exists.
 129          $tags = new WP_HTML_Tag_Processor( $block_content );
 130  
 131          if ( $tags->next_tag() ) {
 132              $existing_style = $tags->get_attribute( 'style' );
 133              $updated_style  = '';
 134  
 135              if ( ! empty( $existing_style ) ) {
 136                  $updated_style = $existing_style;
 137                  if ( ! str_ends_with( $existing_style, ';' ) ) {
 138                      $updated_style .= ';';
 139                  }
 140              }
 141  
 142              $updated_style .= $styles['css'];
 143              $tags->set_attribute( 'style', $updated_style );
 144  
 145              if ( ! empty( $styles['classnames'] ) ) {
 146                  foreach ( explode( ' ', $styles['classnames'] ) as $class_name ) {
 147                      if (
 148                          str_contains( $class_name, 'aspect-ratio' ) &&
 149                          ! isset( $block_attributes['style']['dimensions']['aspectRatio'] )
 150                      ) {
 151                          continue;
 152                      }
 153                      $tags->add_class( $class_name );
 154                  }
 155              }
 156          }
 157  
 158          return $tags->get_updated_html();
 159      }
 160  
 161      return $block_content;
 162  }
 163  
 164  add_filter( 'render_block', 'wp_render_dimensions_support', 10, 2 );
 165  
 166  // Register the block support.
 167  WP_Block_Supports::get_instance()->register(
 168      'dimensions',
 169      array(
 170          'register_attribute' => 'wp_register_dimensions_support',
 171          'apply'              => 'wp_apply_dimensions_support',
 172      )
 173  );


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref