Make WordPress Core


Ignore:
Timestamp:
09/19/2023 01:27:43 AM (20 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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     *
Note: See TracChangeset for help on using the changeset viewer.