40 * it is also applied to non-server-rendered blocks. |
40 * it is also applied to non-server-rendered blocks. |
41 * |
41 * |
42 * @since 6.4.0 |
42 * @since 6.4.0 |
43 * @since 6.5.0 Added support for `backgroundPosition` and `backgroundRepeat` output. |
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. |
44 * @since 6.6.0 Removed requirement for `backgroundImage.source`. A file/url is the default. |
|
45 * @since 6.7.0 Added support for `backgroundAttachment` output. |
45 * |
46 * |
46 * @access private |
47 * @access private |
47 * |
48 * |
48 * @param string $block_content Rendered block content. |
49 * @param string $block_content Rendered block content. |
49 * @param array $block Block object. |
50 * @param array $block Block object. |
60 ! isset( $block_attributes['style']['background'] ) |
61 ! isset( $block_attributes['style']['background'] ) |
61 ) { |
62 ) { |
62 return $block_content; |
63 return $block_content; |
63 } |
64 } |
64 |
65 |
65 $background_styles = array(); |
66 $background_styles = array(); |
66 $background_styles['backgroundImage'] = isset( $block_attributes['style']['background']['backgroundImage'] ) ? $block_attributes['style']['background']['backgroundImage'] : array(); |
67 $background_styles['backgroundImage'] = $block_attributes['style']['background']['backgroundImage'] ?? null; |
|
68 $background_styles['backgroundSize'] = $block_attributes['style']['background']['backgroundSize'] ?? null; |
|
69 $background_styles['backgroundPosition'] = $block_attributes['style']['background']['backgroundPosition'] ?? null; |
|
70 $background_styles['backgroundRepeat'] = $block_attributes['style']['background']['backgroundRepeat'] ?? null; |
|
71 $background_styles['backgroundAttachment'] = $block_attributes['style']['background']['backgroundAttachment'] ?? null; |
67 |
72 |
68 if ( ! empty( $background_styles['backgroundImage'] ) ) { |
73 if ( ! empty( $background_styles['backgroundImage'] ) ) { |
69 $background_styles['backgroundSize'] = isset( $block_attributes['style']['background']['backgroundSize'] ) ? $block_attributes['style']['background']['backgroundSize'] : 'cover'; |
74 $background_styles['backgroundSize'] = $background_styles['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 |
75 |
73 // If the background size is set to `contain` and no position is set, set the position to `center`. |
76 // 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'] ) { |
77 if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) { |
75 $background_styles['backgroundPosition'] = 'center'; |
78 $background_styles['backgroundPosition'] = '50% 50%'; |
76 } |
79 } |
77 } |
80 } |
78 |
81 |
79 $styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) ); |
82 $styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) ); |
80 |
83 |