Make WordPress Core


Ignore:
Timestamp:
01/09/2024 06:10:09 AM (2 years ago)
Author:
isabel_brison
Message:

Editor: add size and repeat to background image support.

Adds background size and background repeat style processing to the background image block support and WP_Style_Engine definitions.

Props andrewserong, mukesh27.
Fixes #60175.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/background.php

    r56731 r57254  
    4141 *
    4242 * @since 6.4.0
     43 * @since 6.5.0 Added support for `backgroundPosition` and `backgroundRepeat` output.
    4344 * @access private
    4445 *
     
    6566        ? $block_attributes['style']['background']['backgroundImage']['url']
    6667        : null;
     68
     69    if ( ! $background_image_source && ! $background_image_url ) {
     70        return $block_content;
     71    }
     72
    6773    $background_size         = isset( $block_attributes['style']['background']['backgroundSize'] )
    6874        ? $block_attributes['style']['background']['backgroundSize']
    6975        : 'cover';
     76    $background_position     = isset( $block_attributes['style']['background']['backgroundPosition'] )
     77        ? $block_attributes['style']['background']['backgroundPosition']
     78        : null;
     79    $background_repeat       = isset( $block_attributes['style']['background']['backgroundRepeat'] )
     80        ? $block_attributes['style']['background']['backgroundRepeat']
     81        : null;
    7082
    7183    $background_block_styles = array();
     
    7789        // Set file based background URL.
    7890        $background_block_styles['backgroundImage']['url'] = $background_image_url;
    79         // Only output the background size when an image url is set.
    80         $background_block_styles['backgroundSize'] = $background_size;
     91        // Only output the background size and repeat when an image url is set.
     92        $background_block_styles['backgroundSize']     = $background_size;
     93        $background_block_styles['backgroundRepeat']   = $background_repeat;
     94        $background_block_styles['backgroundPosition'] = $background_position;
     95
     96        // If the background size is set to `contain` and no position is set, set the position to `center`.
     97        if ( 'contain' === $background_size && ! isset( $background_position ) ) {
     98            $background_block_styles['backgroundPosition'] = 'center';
     99        }
    81100    }
    82101
     
    100119            $updated_style .= $styles['css'];
    101120            $tags->set_attribute( 'style', $updated_style );
     121            $tags->add_class( 'has-background' );
    102122        }
    103123
Note: See TracChangeset for help on using the changeset viewer.