Make WordPress Core

Changeset 53091


Ignore:
Timestamp:
04/07/2022 11:57:16 AM (4 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.

Location:
trunk
Files:
12 added
2 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                                }
  • trunk/tests/phpunit/tests/blocks/register.php

    r52699 r53091  
    254254
    255255        /**
     256         * @ticket 55513
     257         */
     258        public function test_success_register_block_script_handle_in_theme() {
     259                switch_theme( 'block-theme' );
     260
     261                $metadata = array(
     262                        'file'       => wp_normalize_path( get_theme_file_path( 'blocks/example-block/block.json' ) ),
     263                        'name'       => 'block-theme/example-block',
     264                        'viewScript' => 'file:./view.js',
     265                );
     266                $result   = register_block_script_handle( $metadata, 'viewScript' );
     267
     268                $expected_script_handle = 'block-theme-example-block-view-script';
     269                $this->assertSame( $expected_script_handle, $result );
     270        }
     271
     272        /**
    256273         * @ticket 50263
    257274         */
     
    304321                        wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-style', 'path' ) )
    305322                );
     323        }
     324
     325        /**
     326         * @ticket 55513
     327         */
     328        public function test_success_register_block_style_handle_in_theme() {
     329                switch_theme( 'block-theme' );
     330
     331                $metadata = array(
     332                        'file'        => wp_normalize_path( get_theme_file_path( 'blocks/example-block/block.json' ) ),
     333                        'name'        => 'block-theme/example-block',
     334                        'editorStyle' => 'file:./editor-style.css',
     335                );
     336                $result   = register_block_style_handle( $metadata, 'editorStyle' );
     337
     338                $expected_style_handle = 'block-theme-example-block-editor-style';
     339                $this->assertSame( $expected_style_handle, $result );
     340                $this->assertSame( 'replace', wp_styles()->get_data( $expected_style_handle, 'rtl' ) );
    306341        }
    307342
Note: See TracChangeset for help on using the changeset viewer.