Make WordPress Core


Ignore:
Timestamp:
07/24/2024 12:24:31 AM (2 years ago)
Author:
noisysocks
Message:

Block themes: Enable block-level background image styles

Allows defining background images for blocks in theme.json.

Syncs PHP changes from https://github.com/WordPress/gutenberg/pull/60100.

Props ramonopoly, aaronrobertshaw.
Fixes #61588.

File:
1 edited

Legend:

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

    r58468 r58797  
    849849         *
    850850         * @since 6.6.0
     851         * @since 6.7.0 Resolve relative paths in block styles.
    851852         *
    852853         * @param WP_Theme_JSON $theme_json A theme json instance.
     
    861862
    862863                $theme_json_data = $theme_json->get_raw_data();
    863 
    864                 // Top level styles.
    865                 $background_image_url = isset( $theme_json_data['styles']['background']['backgroundImage']['url'] ) ? $theme_json_data['styles']['background']['backgroundImage']['url'] : null;
    866 
    867864                /*
    868865                 * The same file convention when registering web fonts.
     
    870867                 */
    871868                $placeholder = 'file:./';
     869
     870                // Top level styles.
     871                $background_image_url = $theme_json_data['styles']['background']['backgroundImage']['url'] ?? null;
    872872                if (
    873873                        isset( $background_image_url ) &&
     
    889889                }
    890890
     891                // Block styles.
     892                if ( ! empty( $theme_json_data['styles']['blocks'] ) ) {
     893                        foreach ( $theme_json_data['styles']['blocks'] as $block_name => $block_styles ) {
     894                                if ( ! isset( $block_styles['background']['backgroundImage']['url'] ) ) {
     895                                        continue;
     896                                }
     897                                $background_image_url = $block_styles['background']['backgroundImage']['url'];
     898                                if (
     899                                        is_string( $background_image_url ) &&
     900                                        // Skip if the src doesn't start with the placeholder, as there's nothing to replace.
     901                                        str_starts_with( $background_image_url, $placeholder )
     902                                ) {
     903                                        $file_type          = wp_check_filetype( $background_image_url );
     904                                        $src_url            = str_replace( $placeholder, '', $background_image_url );
     905                                        $resolved_theme_uri = array(
     906                                                'name'   => $background_image_url,
     907                                                'href'   => sanitize_url( get_theme_file_uri( $src_url ) ),
     908                                                'target' => "styles.blocks.{$block_name}.background.backgroundImage.url",
     909                                        );
     910                                        if ( isset( $file_type['type'] ) ) {
     911                                                $resolved_theme_uri['type'] = $file_type['type'];
     912                                        }
     913                                        $resolved_theme_uris[] = $resolved_theme_uri;
     914                                }
     915                        }
     916                }
     917
    891918                return $resolved_theme_uris;
    892919        }
Note: See TracChangeset for help on using the changeset viewer.