Make WordPress Core

Opened 18 months ago

Last modified 18 months ago

#56859 new defect (bug)

Blocks: register_block_script_handle() does not account for symlinks

Reported by: cr0ybot's profile cr0ybot Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.0.3
Component: Editor Keywords:
Focuses: Cc:

Description

I love the fix added by #55513 and #54647 to detect if a block is being registered from a theme and to alter the enqueued URI appropriately. However, the way that a theme block is detected does not take symlinks into account. I have all of my custom themes/plugins in one location in my file system, and I symlink the folders I'm working on into a local WP install. I imagine there are other scenarios where a symlink might be used for theme folders as well.

The output of wp_normalize_path( get_theme_file_path() ) is the location of my local WP install:

/Users/coryhughart/Dev/web/local/website/app/public/wp-content/themes/website-theme

The output of wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $script_path ) ) is the original path to the theme outside of the WP install:

/Users/coryhughart/Dev/Blackbird/Client/website.com/website-theme/dist/blocks/hero/script.js

Obviously symlink resolution is part of what realpath does. Is there a similar function/solution that could expand the path without resolving symlinks? Or is symlinking outside the scope of what is expected in a WordPress environment?

Change History (1)

#1 @cr0ybot
18 months ago

Prior to the update that allows block registration in themes, I had been filtering plugins_url but also checking for symlinks with is_link() and readlink(). This doesn't really solve for expanding the path though.

Here's that filter for reference. It's a bit of a blunt instrument, but it works.

<?php
/**
 * While block registration from themes is now supported, registration from
 * themes that are symlinked, like you might do when developing locally, is not.
 *
 * @link https://core.trac.wordpress.org/ticket/56859
 *
 * Used in register_blocks().
 */
function block_plugins_url( $url, $path ) {
        // Handle symlinked paths (generally only during local development).
        $dir = get_template_directory();
        if ( is_link( $dir ) ) {
                $dir = readlink( $dir );
        }
        // Split the $url by the current theme slug and get just the file path.
        $file = explode( $dir, $url )[1];
        // For some reason this is getting applied to the gutenberg plugin urls, so we check if the string explode above worked.
        if ( empty( $file ) ) {
                return $url;
        }
        return untrailingslashit( get_template_directory_uri() ) . $file;
}
Note: See TracTickets for help on using tickets.