Make WordPress Core


Ignore:
Timestamp:
04/07/2022 11:57:16 AM (3 years ago)
Author:
gziolo
Message:

Editor: Allow registration of blocks that include assets from within a theme

Fixes the issue when you register blocks with block.json in your theme. There is no longer an assets's URL error because it resolves correctly in relation to the theme where it is located.

Props fabiankaegy, ocean90, whoisnegrello, audrasjb, peterwilsoncc,
Fixes #54647, #55513.

File:
1 edited

Legend:

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

    r52939 r53091  
    109109    // Path needs to be normalized to work in Windows env.
    110110    $wpinc_path_norm  = wp_normalize_path( realpath( ABSPATH . WPINC ) );
     111    $theme_path_norm  = wp_normalize_path( get_theme_file_path() );
    111112    $script_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $script_path ) );
    112113    $is_core_block    = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
    113 
    114     $script_uri          = $is_core_block ?
    115         includes_url( str_replace( $wpinc_path_norm, '', $script_path_norm ) ) :
    116         plugins_url( $script_path, $metadata['file'] );
     114    $is_theme_block   = 0 === strpos( $script_path_norm, $theme_path_norm );
     115
     116    $script_uri;
     117    if ( $is_core_block ) {
     118        $script_uri = includes_url( str_replace( $wpinc_path_norm, '', $script_path_norm ) );
     119    } elseif ( $is_theme_block ) {
     120        $script_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $script_path_norm ) );
     121    } else {
     122        $script_uri = plugins_url( $script_path, $metadata['file'] );
     123    }
     124
    117125    $script_asset        = require $script_asset_path;
    118126    $script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
     
    151159    }
    152160    $wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
     161    $theme_path_norm = wp_normalize_path( get_theme_file_path() );
    153162    $is_core_block   = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
    154163    if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
     
    170179        $style_path = "style$suffix.css";
    171180        $style_uri  = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
     181    }
     182
     183    $style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
     184    $is_theme_block  = 0 === strpos( $style_path_norm, $theme_path_norm );
     185
     186    if ( $is_theme_block ) {
     187        $style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
    172188    }
    173189
     
    13161332                $args = array( 'handle' => $handle );
    13171333                if ( 0 === strpos( $handle, 'file:' ) && isset( $metadata['file'] ) ) {
    1318                     $style_path = remove_block_asset_path_prefix( $handle );
     1334                    $style_path      = remove_block_asset_path_prefix( $handle );
     1335                    $theme_path_norm = wp_normalize_path( get_theme_file_path() );
     1336                    $style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
     1337                    $is_theme_block  = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $theme_path_norm );
     1338
     1339                    $style_uri = plugins_url( $style_path, $metadata['file'] );
     1340
     1341                    if ( $is_theme_block ) {
     1342                        $style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
     1343                    }
     1344
    13191345                    $args       = array(
    13201346                        'handle' => sanitize_key( "{$metadata['name']}-{$style_path}" ),
    1321                         'src'    => plugins_url( $style_path, $metadata['file'] ),
     1347                        'src'    => $style_uri,
    13221348                    );
    13231349                }
Note: See TracChangeset for help on using the changeset viewer.