Make WordPress Core


Ignore:
Timestamp:
05/28/2024 06:04:37 AM (2 years ago)
Author:
noisysocks
Message:

Block Themes: Allow setting site-wide background images in theme.json

Syncs the necessary changes from Gutenberg to allow setting site-wide
background images using the top-level styles.background key in theme.json.

Props ramonopoly.
Fixes #61123.

File:
1 edited

Legend:

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

    r57365 r58222  
    4242 * @since 6.4.0
    4343 * @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 *
    4446 * @access private
    4547 *
     
    5557        if (
    5658                ! $has_background_image_support ||
    57                 wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' )
     59                wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' ) ||
     60                ! isset( $block_attributes['style']['background'] )
    5861        ) {
    5962                return $block_content;
    6063        }
    6164
    62         $background_image_source = isset( $block_attributes['style']['background']['backgroundImage']['source'] )
    63                 ? $block_attributes['style']['background']['backgroundImage']['source']
    64                 : null;
    65         $background_image_url    = isset( $block_attributes['style']['background']['backgroundImage']['url'] )
    66                 ? $block_attributes['style']['background']['backgroundImage']['url']
    67                 : null;
     65        $background_styles                    = array();
     66        $background_styles['backgroundImage'] = isset( $block_attributes['style']['background']['backgroundImage'] ) ? $block_attributes['style']['background']['backgroundImage'] : array();
    6867
    69         if ( ! $background_image_source && ! $background_image_url ) {
    70                 return $block_content;
    71         }
    72 
    73         $background_size     = isset( $block_attributes['style']['background']['backgroundSize'] )
    74                 ? $block_attributes['style']['background']['backgroundSize']
    75                 : '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;
    82 
    83         $background_block_styles = array();
    84 
    85         if (
    86                 'file' === $background_image_source &&
    87                 $background_image_url
    88         ) {
    89                 // Set file based background URL.
    90                 $background_block_styles['backgroundImage']['url'] = $background_image_url;
    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;
     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;
    9572
    9673                // 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';
     74                if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
     75                        $background_styles['backgroundPosition'] = 'center';
    9976                }
    10077        }
    10178
    102         $styles = wp_style_engine_get_styles( array( 'background' => $background_block_styles ) );
     79        $styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) );
    10380
    10481        if ( ! empty( $styles['css'] ) ) {
Note: See TracChangeset for help on using the changeset viewer.