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/class-wp-theme-json.php

    r58171 r58222  
    214214         * @since 6.4.0 Added `writing-mode` property.
    215215         * @since 6.5.0 Added `aspect-ratio` property.
     216         * @since 6.6.0 Added `background-[image|position|repeat|size]` properties.
    216217         *
    217218         * @var array
     
    221222                'background'                        => array( 'color', 'gradient' ),
    222223                'background-color'                  => array( 'color', 'background' ),
     224                'background-image'                  => array( 'background', 'backgroundImage' ),
     225                'background-position'               => array( 'background', 'backgroundPosition' ),
     226                'background-repeat'                 => array( 'background', 'backgroundRepeat' ),
     227                'background-size'                   => array( 'background', 'backgroundSize' ),
    223228                'border-radius'                     => array( 'border', 'radius' ),
    224229                'border-top-left-radius'            => array( 'border', 'radius', 'topLeft' ),
     
    284289         * Indirect properties are not output directly by `compute_style_properties`,
    285290         * but are used elsewhere in the processing of global styles. The indirect
    286          * property is used to validate whether or not a style value is allowed.
     291         * property is used to validate whether a style value is allowed.
    287292         *
    288293         * @since 6.2.0
     294         * @since 6.6.0 Added background-image properties.
    289295         *
    290296         * @var array
     
    303309                        array( 'layout', 'contentSize' ),
    304310                        array( 'layout', 'wideSize' ),
     311                ),
     312                'background-image' => array(
     313                        array( 'background', 'backgroundImage', 'url' ),
    305314                ),
    306315        );
     
    483492         * @since 6.3.0 Added support for `typography.textColumns`.
    484493         * @since 6.5.0 Added support for `dimensions.aspectRatio`.
     494         * @since 6.6.0 Added `background` sub properties to top-level only.
    485495         *
    486496         * @var array
    487497         */
    488498        const VALID_STYLES = array(
     499                'background' => array(
     500                        'backgroundImage'    => 'top',
     501                        'backgroundPosition' => 'top',
     502                        'backgroundRepeat'   => 'top',
     503                        'backgroundSize'     => 'top',
     504                ),
    489505                'border'     => array(
    490506                        'color'  => null,
     
    20522068         * @since 6.1.0 Added `$theme_json`, `$selector`, and `$use_root_padding` parameters.
    20532069         * @since 6.5.0 Output a `min-height: unset` rule when `aspect-ratio` is set.
    2054          * @since 6.6.0 Passing current theme JSON settings to wp_get_typography_font_size_value().
     2070         * @since 6.6.0 Pass current theme JSON settings to wp_get_typography_font_size_value(), and process background properties.
    20552071         *
    20562072         * @param array   $styles Styles to process.
     
    21042120                                        continue;
    21052121                                }
     2122                        }
     2123
     2124                        // Processes background styles.
     2125                        if ( 'background' === $value_path[0] && isset( $styles['background'] ) ) {
     2126                                $background_styles = wp_style_engine_get_styles( array( 'background' => $styles['background'] ) );
     2127                                $value             = isset( $background_styles['declarations'][ $css_property ] ) ? $background_styles['declarations'][ $css_property ] : $value;
    21062128                        }
    21072129
     
    24852507         *
    24862508         * @since 6.1.0
     2509         * @since 6.6.0 Setting a min-height of HTML when root styles have a background gradient or image.
    24872510         *
    24882511         * @param array $block_metadata Metadata about the block to get styles for.
     
    24962519                $settings             = isset( $this->theme_json['settings'] ) ? $this->theme_json['settings'] : array();
    24972520                $feature_declarations = static::get_feature_declarations_for_node( $block_metadata, $node );
     2521                $is_root_selector     = static::ROOT_BLOCK_SELECTOR === $selector;
    24982522
    24992523                // If there are style variations, generate the declarations for them, including any feature selectors the block may have.
     
    25782602
    25792603                /*
    2580                  * 1. Separate the declarations that use the general selector
     2604                 * 1. Bespoke declaration modifiers:
     2605                 * - 'filter': Separate the declarations that use the general selector
    25812606                 * from the ones using the duotone selector.
     2607                 * - 'background|background-image': set the html min-height to 100%
     2608                 * to ensure the background covers the entire viewport.
    25822609                 */
    2583                 $declarations_duotone = array();
     2610                $declarations_duotone       = array();
     2611                $should_set_root_min_height = false;
     2612
    25842613                foreach ( $declarations as $index => $declaration ) {
    25852614                        if ( 'filter' === $declaration['name'] ) {
     2615                                /*
     2616                                 * 'unset' filters happen when a filter is unset
     2617                                 * in the site-editor UI. Because the 'unset' value
     2618                                 * in the user origin overrides the value in the
     2619                                 * theme origin, we can skip rendering anything
     2620                                 * here as no filter needs to be applied anymore.
     2621                                 * So only add declarations to with values other
     2622                                 * than 'unset'.
     2623                                 */
     2624                                if ( 'unset' !== $declaration['value'] ) {
     2625                                        $declarations_duotone[] = $declaration;
     2626                                }
    25862627                                unset( $declarations[ $index ] );
    2587                                 $declarations_duotone[] = $declaration;
    2588                         }
     2628                        }
     2629
     2630                        if ( $is_root_selector && ( 'background-image' === $declaration['name'] || 'background' === $declaration['name'] ) ) {
     2631                                $should_set_root_min_height = true;
     2632                        }
     2633                }
     2634
     2635                /*
     2636                 * If root styles has a background-image or a background (gradient) set,
     2637                 * set the min-height to '100%'. Minus `--wp-admin--admin-bar--height` for logged-in view.
     2638                 * Setting the CSS rule on the HTML tag ensures background gradients and images behave similarly,
     2639                 * and matches the behavior of the site editor.
     2640                 */
     2641                if ( $should_set_root_min_height ) {
     2642                        $block_rules .= static::to_ruleset(
     2643                                'html',
     2644                                array(
     2645                                        array(
     2646                                                'name'  => 'min-height',
     2647                                                'value' => 'calc(100% - var(--wp-admin--admin-bar--height, 0px))',
     2648                                        ),
     2649                                )
     2650                        );
    25892651                }
    25902652
     
    26042666                // 4. Generate Layout block gap styles.
    26052667                if (
    2606                         static::ROOT_BLOCK_SELECTOR !== $selector &&
     2668                        ! $is_root_selector &&
    26072669                        ! empty( $block_metadata['name'] )
    26082670                ) {
Note: See TracChangeset for help on using the changeset viewer.