Make WordPress Core

Changeset 56683


Ignore:
Timestamp:
09/25/2023 05:47:27 PM (3 years ago)
Author:
spacedmonkey
Message:

Editor: Introduce get_block_asset_url Utility Function.

This commit introduces a valuable utility function, get_block_asset_url, designed to simplify the retrieval of block asset URLs, such as those for CSS and JavaScript files. This utility eliminates redundancy in both register_block_script_handle and register_block_style_handle. Additionally, get_block_asset_url incorporates an early exit mechanism to optimize performance.

This update includes comprehensive unit tests, covering various scenarios, including asset registration from core (wp-includes), themes, child themes, plugins, and mu-plugins.

Props spacedmonkey, joemcgill, flixos90, gziolo.
Fixes #58525.

Location:
trunk
Files:
12 added
1 edited

Legend:

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

    r56680 r56683  
    7575
    7676/**
     77 * Gets the URL to a block asset.
     78 *
     79 * @since 6.4.0
     80 *
     81 * @param string $path A normalized path to a block asset.
     82 * @return string|false The URL to the block asset or false on failure.
     83 */
     84function get_block_asset_url( $path ) {
     85        if ( empty( $path ) ) {
     86                return false;
     87        }
     88
     89        // Path needs to be normalized to work in Windows env.
     90        static $wpinc_path_norm = '';
     91        if ( ! $wpinc_path_norm ) {
     92                $wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
     93        }
     94
     95        if ( str_starts_with( $path, $wpinc_path_norm ) ) {
     96                return includes_url( str_replace( $wpinc_path_norm, '', $path ) );
     97        }
     98
     99        static $template_paths_norm = array();
     100
     101        $template = get_template();
     102        if ( ! isset( $template_paths_norm[ $template ] ) ) {
     103                $template_paths_norm[ $template ] = wp_normalize_path( get_template_directory() );
     104        }
     105
     106        if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $template ] ) ) ) {
     107                return get_theme_file_uri( str_replace( $template_paths_norm[ $template ], '', $path ) );
     108        }
     109
     110        if ( is_child_theme() ) {
     111                $stylesheet = get_stylesheet();
     112                if ( ! isset( $template_paths_norm[ $stylesheet ] ) ) {
     113                        $template_paths_norm[ $stylesheet ] = wp_normalize_path( get_stylesheet_directory() );
     114                }
     115
     116                if ( str_starts_with( $path, trailingslashit( $template_paths_norm[ $stylesheet ] ) ) ) {
     117                        return get_theme_file_uri( str_replace( $template_paths_norm[ $stylesheet ], '', $path ) );
     118                }
     119        }
     120
     121        return plugins_url( basename( $path ), $path );
     122}
     123
     124/**
    77125 * Finds a script handle for the selected block metadata field. It detects
    78126 * when a path to file was provided and finds a corresponding asset file
     
    108156        }
    109157
    110         $script_asset_raw_path = dirname( $metadata['file'] ) . '/' . substr_replace( $script_path, '.asset.php', - strlen( '.js' ) );
     158        $path                  = dirname( $metadata['file'] );
     159        $script_asset_raw_path = $path . '/' . substr_replace( $script_path, '.asset.php', - strlen( '.js' ) );
    111160        $script_handle         = generate_block_asset_handle( $metadata['name'], $field_name, $index );
    112161        $script_asset_path     = wp_normalize_path(
     
    129178        }
    130179
    131         // Path needs to be normalized to work in Windows env.
    132         static $wpinc_path_norm = '';
    133         if ( ! $wpinc_path_norm ) {
    134                 $wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
    135         }
    136 
    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 
    145         $script_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $script_path ) );
    146 
    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, trailingslashit( $template_path_norm ) );
    154         $is_child_theme_block  = str_starts_with( $script_path_norm, trailingslashit( $stylesheet_path_norm ) );
    155         $is_theme_block        = ( $is_parent_theme_block || $is_child_theme_block );
    156 
    157         $script_uri = '';
    158         if ( $is_core_block ) {
    159                 $script_uri = includes_url( str_replace( $wpinc_path_norm, '', $script_path_norm ) );
    160         } elseif ( $is_theme_block ) {
    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 ) );
    165         } else {
    166                 // Fallback to plugins_url().
    167                 $script_uri = plugins_url( $script_path, $metadata['file'] );
    168         }
     180        $script_path_norm = wp_normalize_path( realpath( $path . '/' . $script_path ) );
     181        $script_uri       = get_block_asset_url( $script_path_norm );
    169182
    170183        $script_args = array();
     
    256269
    257270        $style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
    258         $has_style_file  = '' !== $style_path_norm;
    259 
    260         if ( $has_style_file ) {
    261                 $style_uri = plugins_url( $style_path, $metadata['file'] );
    262 
    263                 // Cache $template_path_norm and $stylesheet_path_norm to avoid unnecessary additional calls.
    264                 static $template_path_norm   = '';
    265                 static $stylesheet_path_norm = '';
    266                 if ( ! $template_path_norm || ! $stylesheet_path_norm ) {
    267                         $template_path_norm   = wp_normalize_path( get_template_directory() );
    268                         $stylesheet_path_norm = wp_normalize_path( get_stylesheet_directory() );
    269                 }
    270 
    271                 // Determine if the block style was registered in a theme, by checking if the script path starts with either
    272                 // the parent (template) or child (stylesheet) directory path.
    273                 $is_parent_theme_block = str_starts_with( $style_path_norm, trailingslashit( $template_path_norm ) );
    274                 $is_child_theme_block  = str_starts_with( $style_path_norm, trailingslashit( $stylesheet_path_norm ) );
    275                 $is_theme_block        = ( $is_parent_theme_block || $is_child_theme_block );
    276 
    277                 if ( $is_core_block ) {
    278                         // All possible $style_path variants for core blocks are hard-coded above.
    279                         $style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . '/' . $style_path );
    280                 } elseif ( $is_theme_block ) {
    281                         // Get the script path deterministically based on whether or not it was registered in a parent or child theme.
    282                         $style_uri = $is_parent_theme_block
    283                                 ? get_theme_file_uri( str_replace( $template_path_norm, '', $style_path_norm ) )
    284                                 : get_theme_file_uri( str_replace( $stylesheet_path_norm, '', $style_path_norm ) );
    285                 }
    286         } else {
    287                 $style_uri = false;
    288         }
     271        $style_uri       = get_block_asset_url( $style_path_norm );
    289272
    290273        $version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
     
    299282        }
    300283
    301         if ( $has_style_file ) {
     284        if ( $style_uri ) {
    302285                wp_style_add_data( $style_handle_name, 'path', $style_path_norm );
    303286
Note: See TracChangeset for help on using the changeset viewer.