[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Background block support flag.
   4   *
   5   * @package WordPress
   6   * @since 6.4.0
   7   */
   8  
   9  /**
  10   * Registers the style block attribute for block types that support it.
  11   *
  12   * @since 6.4.0
  13   * @access private
  14   *
  15   * @param WP_Block_Type $block_type Block Type.
  16   */
  17  function wp_register_background_support( $block_type ) {
  18      // Setup attributes and styles within that if needed.
  19      if ( ! $block_type->attributes ) {
  20          $block_type->attributes = array();
  21      }
  22  
  23      // Check for existing style attribute definition e.g. from block.json.
  24      if ( array_key_exists( 'style', $block_type->attributes ) ) {
  25          return;
  26      }
  27  
  28      $has_background_support = block_has_support( $block_type, array( 'background' ), false );
  29  
  30      if ( $has_background_support ) {
  31          $block_type->attributes['style'] = array(
  32              'type' => 'object',
  33          );
  34      }
  35  }
  36  
  37  /**
  38   * Renders the background styles to the block wrapper.
  39   * This block support uses the `render_block` hook to ensure that
  40   * it is also applied to non-server-rendered blocks.
  41   *
  42   * @since 6.4.0
  43   * @since 6.5.0 Added support for `backgroundPosition` and `backgroundRepeat` output.
  44   * @since 6.6.0 Removed requirement for `backgroundImage.source`. A file/url is the default.
  45   *
  46   * @access private
  47   *
  48   * @param  string $block_content Rendered block content.
  49   * @param  array  $block         Block object.
  50   * @return string Filtered block content.
  51   */
  52  function wp_render_background_support( $block_content, $block ) {
  53      $block_type                   = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
  54      $block_attributes             = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
  55      $has_background_image_support = block_has_support( $block_type, array( 'background', 'backgroundImage' ), false );
  56  
  57      if (
  58          ! $has_background_image_support ||
  59          wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' ) ||
  60          ! isset( $block_attributes['style']['background'] )
  61      ) {
  62          return $block_content;
  63      }
  64  
  65      $background_styles                    = array();
  66      $background_styles['backgroundImage'] = isset( $block_attributes['style']['background']['backgroundImage'] ) ? $block_attributes['style']['background']['backgroundImage'] : array();
  67  
  68      if ( ! empty( $background_styles['backgroundImage'] ) ) {
  69          $background_styles['backgroundSize']     = isset( $block_attributes['style']['background']['backgroundSize'] ) ? $block_attributes['style']['background']['backgroundSize'] : 'cover';
  70          $background_styles['backgroundPosition'] = isset( $block_attributes['style']['background']['backgroundPosition'] ) ? $block_attributes['style']['background']['backgroundPosition'] : null;
  71          $background_styles['backgroundRepeat']   = isset( $block_attributes['style']['background']['backgroundRepeat'] ) ? $block_attributes['style']['background']['backgroundRepeat'] : null;
  72  
  73          // If the background size is set to `contain` and no position is set, set the position to `center`.
  74          if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
  75              $background_styles['backgroundPosition'] = 'center';
  76          }
  77      }
  78  
  79      $styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) );
  80  
  81      if ( ! empty( $styles['css'] ) ) {
  82          // Inject background styles to the first element, presuming it's the wrapper, if it exists.
  83          $tags = new WP_HTML_Tag_Processor( $block_content );
  84  
  85          if ( $tags->next_tag() ) {
  86              $existing_style = $tags->get_attribute( 'style' );
  87              $updated_style  = '';
  88  
  89              if ( ! empty( $existing_style ) ) {
  90                  $updated_style = $existing_style;
  91                  if ( ! str_ends_with( $existing_style, ';' ) ) {
  92                      $updated_style .= ';';
  93                  }
  94              }
  95  
  96              $updated_style .= $styles['css'];
  97              $tags->set_attribute( 'style', $updated_style );
  98              $tags->add_class( 'has-background' );
  99          }
 100  
 101          return $tags->get_updated_html();
 102      }
 103  
 104      return $block_content;
 105  }
 106  
 107  // Register the block support.
 108  WP_Block_Supports::get_instance()->register(
 109      'background',
 110      array(
 111          'register_attribute' => 'wp_register_background_support',
 112      )
 113  );
 114  
 115  add_filter( 'render_block', 'wp_render_background_support', 10, 2 );


Generated : Mon Jul 15 08:20:02 2024 Cross-referenced by PHPXref