Make WordPress Core

Changeset 56183


Ignore:
Timestamp:
07/10/2023 06:34:37 AM (17 months ago)
Author:
audrasjb
Message:

Editor: Ensure blocks registered within parent theme are available when child theme is activated.

This changeset fixes both register_block_script_handle() and register_block_style_handle() functions, allowing for child themes to access and utilise
blocks defined in parent themes.

Previously these functions could not handle blocks registered in Parent themes. This changeset fixes the issue by determining whether the given asset path
belongs to either of the template or stylesheet directories (Parent and Child themes respectively) as opposed to just checking using
get_theme_file_path('').

Props jacknotman, Levdbas, audrasjb, guillaumeturpin, leprincenoir, whaze, isabel_brison.
Fixes #57566.

--Cette ligne, et les suivantes ci-dessous, seront ignorées--

M trunk/src/wp-includes/blocks.php

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r56109 r56183  
    135135    }
    136136
    137     $theme_path_norm  = wp_normalize_path( get_theme_file_path() );
     137    // Cache $template_path_norm and $stylesheet_path_norm to avoid unnecessary additional calls.
     138    static $template_path_norm   = '';
     139    static $stylesheet_path_norm = '';
     140    if ( ! $template_path_norm || ! $stylesheet_path_norm ) {
     141        $template_path_norm   = wp_normalize_path( get_template_directory() );
     142        $stylesheet_path_norm = wp_normalize_path( get_stylesheet_directory() );
     143    }
     144
    138145    $script_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $script_path ) );
    139146
    140     $is_core_block  = isset( $metadata['file'] ) && str_starts_with( $metadata['file'], $wpinc_path_norm );
    141     $is_theme_block = str_starts_with( $script_path_norm, $theme_path_norm );
     147    $is_core_block = isset( $metadata['file'] ) && str_starts_with( $metadata['file'], $wpinc_path_norm );
     148
     149    /*
     150     * Determine if the block script was registered in a theme, by checking if the script path starts with either
     151     * the parent (template) or child (stylesheet) directory path.
     152     */
     153    $is_parent_theme_block = str_starts_with( $script_path_norm, $template_path_norm );
     154    $is_child_theme_block  = str_starts_with( $script_path_norm, $stylesheet_path_norm );
     155    $is_theme_block        = ( $is_parent_theme_block || $is_child_theme_block );
    142156
    143157    $script_uri = plugins_url( $script_path, $metadata['file'] );
     
    145159        $script_uri = includes_url( str_replace( $wpinc_path_norm, '', $script_path_norm ) );
    146160    } elseif ( $is_theme_block ) {
    147         $script_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $script_path_norm ) );
     161        // Get the script path deterministically based on whether or not it was registered in a parent or child theme.
     162        $script_uri = $is_parent_theme_block
     163            ? get_theme_file_uri( str_replace( $template_path_norm, '', $script_path_norm ) )
     164            : get_theme_file_uri( str_replace( $stylesheet_path_norm, '', $script_path_norm ) );
    148165    }
    149166
     
    235252        $style_uri = plugins_url( $style_path, $metadata['file'] );
    236253
    237         // Cache $theme_path_norm to avoid calling get_theme_file_path() multiple times.
    238         static $theme_path_norm = '';
    239         if ( ! $theme_path_norm ) {
    240             $theme_path_norm = wp_normalize_path( get_theme_file_path() );
    241         }
    242 
    243         $is_theme_block = str_starts_with( $style_path_norm, $theme_path_norm );
    244 
    245         if ( $is_theme_block ) {
    246             $style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
    247         } elseif ( $is_core_block ) {
     254        // Cache $template_path_norm and $stylesheet_path_norm to avoid unnecessary additional calls.
     255        static $template_path_norm   = '';
     256        static $stylesheet_path_norm = '';
     257        if ( ! $template_path_norm || ! $stylesheet_path_norm ) {
     258            $template_path_norm   = wp_normalize_path( get_template_directory() );
     259            $stylesheet_path_norm = wp_normalize_path( get_stylesheet_directory() );
     260        }
     261
     262        // Determine if the block style was registered in a theme, by checking if the script path starts with either
     263        // the parent (template) or child (stylesheet) directory path.
     264        $is_parent_theme_block = str_starts_with( $style_path_norm, $template_path_norm );
     265        $is_child_theme_block  = str_starts_with( $style_path_norm, $stylesheet_path_norm );
     266        $is_theme_block        = ( $is_parent_theme_block || $is_child_theme_block );
     267
     268        if ( $is_core_block ) {
    248269            // All possible $style_path variants for core blocks are hard-coded above.
    249270            $style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . '/' . $style_path );
     271        } elseif ( $is_theme_block ) {
     272            // Get the script path deterministically based on whether or not it was registered in a parent or child theme.
     273            $style_uri = $is_parent_theme_block
     274                ? get_theme_file_uri( str_replace( $template_path_norm, '', $style_path_norm ) )
     275                : get_theme_file_uri( str_replace( $stylesheet_path_norm, '', $style_path_norm ) );
    250276        }
    251277    } else {
Note: See TracChangeset for help on using the changeset viewer.