Make WordPress Core

Changeset 56614


Ignore:
Timestamp:
09/19/2023 01:27:43 AM (12 months ago)
Author:
isabel_brison
Message:

Editor: add background image support.

Adds a new background block support with the ability to set a background image on blocks that opt into it.

Props andrewserong, mukesh27.
Fixes #59357.

Location:
trunk
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json.php

    r56605 r56614  
    343343     *              `position.fixed` and `position.sticky`.
    344344     * @since 6.3.0 Added support for `typography.textColumns`, removed `layout.definitions`.
    345      * @since 6.4.0 Added `layout.allowEditing` and `typography.writingMode`.
    346      *
     345     * @since 6.4.0 Added support for `layout.allowEditing`, `background.backgroundImage`,
     346     *              and `typography.writingMode`.
    347347     * @var array
    348348     */
     
    350350        'appearanceTools'               => null,
    351351        'useRootPaddingAwareAlignments' => null,
     352        'background'                    => array(
     353            'backgroundImage' => null,
     354        ),
    352355        'border'                        => array(
    353356            'color'  => null,
     
    564567     * @since 6.0.0
    565568     * @since 6.2.0 Added `dimensions.minHeight` and `position.sticky`.
     569     * @since 6.4.0 Added `background.backgroundImage`.
    566570     * @var array
    567571     */
    568572    const APPEARANCE_TOOLS_OPT_INS = array(
     573        array( 'background', 'backgroundImage' ),
    569574        array( 'border', 'color' ),
    570575        array( 'border', 'radius' ),
  • trunk/src/wp-includes/style-engine/class-wp-style-engine.php

    r56604 r56614  
    2323 * @since 6.1.0
    2424 * @since 6.3.0 Added support for text-columns.
     25 * @since 6.4.0 Added support for background.backgroundImage.
    2526 */
    2627#[AllowDynamicProperties]
     
    5253     */
    5354    const BLOCK_STYLE_DEFINITIONS_METADATA = array(
     55        'background' => array(
     56            'backgroundImage' => array(
     57                'property_keys' => array(
     58                    'default' => 'background-image',
     59                ),
     60                'value_func'    => array( self::class, 'get_url_or_value_css_declaration' ),
     61                'path'          => array( 'background', 'backgroundImage' ),
     62            ),
     63            'backgroundSize'  => array(
     64                'property_keys' => array(
     65                    'default' => 'background-size',
     66                ),
     67                'path'          => array( 'background', 'backgroundSize' ),
     68            ),
     69        ),
    5470        'color'      => array(
    5571            'text'       => array(
     
    591607
    592608    /**
     609     * Style value parser that constructs a CSS definition array comprising a single CSS property and value.
     610     * If the provided value is an array containing a `url` property, the function will return a CSS definition array
     611     * with a single property and value, with `url` escaped and injected into a CSS `url()` function,
     612     * e.g., array( 'background-image' => "url( '...' )" ).
     613     *
     614     * @since 6.4.0
     615     *
     616     * @param array $style_value      A single raw style value from $block_styles array.
     617     * @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
     618     * @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
     619     */
     620    protected static function get_url_or_value_css_declaration( $style_value, $style_definition ) {
     621        if ( empty( $style_value ) ) {
     622            return array();
     623        }
     624
     625        $css_declarations = array();
     626
     627        if ( isset( $style_definition['property_keys']['default'] ) ) {
     628            $value = null;
     629
     630            if ( ! empty( $style_value['url'] ) ) {
     631                $value = "url('" . $style_value['url'] . "')";
     632            } elseif ( is_string( $style_value ) ) {
     633                $value = $style_value;
     634            }
     635
     636            if ( null !== $value ) {
     637                $css_declarations[ $style_definition['property_keys']['default'] ] = $value;
     638            }
     639        }
     640
     641        return $css_declarations;
     642    }
     643
     644    /**
    593645     * Returns compiled CSS from CSS declarations.
    594646     *
  • trunk/src/wp-settings.php

    r56500 r56614  
    341341require ABSPATH . WPINC . '/block-supports/utils.php';
    342342require ABSPATH . WPINC . '/block-supports/align.php';
     343require ABSPATH . WPINC . '/block-supports/background.php';
    343344require ABSPATH . WPINC . '/block-supports/border.php';
    344345require ABSPATH . WPINC . '/block-supports/colors.php';
  • trunk/tests/phpunit/tests/style-engine/styleEngine.php

    r56574 r56614  
    510510                ),
    511511            ),
     512
     513            'inline_background_image_url_with_background_size' => array(
     514                'block_styles'    => array(
     515                    'background' => array(
     516                        'backgroundImage' => array(
     517                            'url' => 'https://example.com/image.jpg',
     518                        ),
     519                        'backgroundSize'  => 'cover',
     520                    ),
     521                ),
     522                'options'         => array(),
     523                'expected_output' => array(
     524                    'css'          => "background-image:url('https://example.com/image.jpg');background-size:cover;",
     525                    'declarations' => array(
     526                        'background-image' => "url('https://example.com/image.jpg')",
     527                        'background-size'  => 'cover',
     528                    ),
     529                ),
     530            ),
    512531        );
    513532    }
  • trunk/tests/phpunit/tests/theme/wpThemeJson.php

    r56548 r56614  
    263263        $actual   = $theme_json->get_settings();
    264264        $expected = array(
     265            'background' => array(
     266                'backgroundImage' => true,
     267            ),
    265268            'border'     => array(
    266269                'width'  => true,
     
    296299                ),
    297300                'core/group'     => array(
     301                    'background' => array(
     302                        'backgroundImage' => true,
     303                    ),
    298304                    'border'     => array(
    299305                        'width'  => true,
Note: See TracChangeset for help on using the changeset viewer.