Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#55311 closed defect (bug) (fixed)

Style and JS URLs for some blocks are broken when using WP outside of the current prefix

Reported by: pgpagely's profile pgpagely Owned by: audrasjb's profile audrasjb
Milestone: 5.9.3 Priority: normal
Severity: major Version: 5.9.1
Component: Script Loader Keywords: has-patch fixed-major
Focuses: Cc:

Description

Both CSS and JS URLs are totally broken for some of the newer blocks (the most obvious one being the navigation block in the 2022 theme) when WP core is outside of the current directory in a different prefix.

The issue happens because of the use of realpath() when trying to detect these paths which results in $is_core_block being false and then generating a weird URL because of that.

The simplest reproduction case is as follows:

  • Install WP 5.9.1
  • Execute mv wp-includes ../wp-includes-test
  • Execute ln -s ../wp-includes-test wp-includes
  • The navigation styles/JS will now be broken on the 2022 theme

For us, we are running WP inside docker, and our WP core files exist at /wordpress-versions/5.9/ in the container, but the web root is at /httpdocs, we have the use the following to make things work:

add_filter("block_type_metadata", function ($meta) {
    if (!empty($meta["file"])) {
        $meta["file"] = preg_replace("#/wordpress-versions/[0-9.]+/#", ABSPATH, $meta["file"]);
    }
    return $meta;
});

If we don't do this then an example URI that is generated for one of the styles is this:

/wp-content/plugins/wordpress-versions/5.9/wp-includes/blocks/navigation/view.min.js?ver=3776ea67846b3bb10fe8f7cdd486b0ba

Attachments (1)

55311.diff (1.3 KB) - added by gziolo 3 years ago.

Download all attachments as: .zip

Change History (17)

#1 @pgpagely
3 years ago

  • Component changed from General to Script Loader

#2 @pgpagely
3 years ago

An additional finding is that if the gutenberg plugin is enabled then the following hack is needed so that the view.min.js URL is not broken (with gutenberg disabled only the hack in the original message is needed):

add_filter("script_loader_src", function ($src) {
    return preg_replace("#/wp-includes/wordpress-versions/[0-9.]+/#", "/", $src);
});

#3 @Mamaduka
3 years ago

I was able to reproduce the issue.

Based on my quick tests, the issue is fixed if I update the following code to use realpath.

$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );

Cc. @aristath and @gziolo, since you're more familiar with this part of the codebase.

#4 @pgpagely
3 years ago

I can confirm this looks like it fixes the issue on our side when applied like this as well. It would be great to get this out in the next maintenance release if it is passing tests because right now it is a breaking change as far as I can tell.

@gziolo
3 years ago

#5 @gziolo
3 years ago

@Mamaduka, it looks like there are 3 places where we should wrap filepaths with realpath.

Let's test the patch attached and plan to land it soon.

#6 @gziolo
3 years ago

  • Version changed from 5.9.1 to 5.9.2

#7 @gziolo
3 years ago

  • Keywords has-patch added

#8 @gziolo
3 years ago

  • Version changed from 5.9.2 to 5.9.1

This ticket was mentioned in PR #2417 on WordPress/wordpress-develop by gziolo.


3 years ago
#9

…rectory

Trac ticket: [](https://core.trac.wordpress.org/ticket/55311)

This ticket was mentioned in PR #2417 on WordPress/wordpress-develop by gziolo.


3 years ago
#10

…rectory

Trac ticket: [](https://core.trac.wordpress.org/ticket/55311)

#12 @kirasong
3 years ago

Just tested and can verify this improves things when applied to the 5.9 branch.

Do you think it'd be a good candidate for backport for the 5.9.3 release?

Last edited 3 years ago by kirasong (previous) (diff)

This ticket was mentioned in Slack in #core-editor by gziolo. View the logs.


3 years ago

#14 @SergeyBiryukov
3 years ago

  • Milestone changed from Awaiting Review to 5.9.3

Looks like this was committed in [52939], though the commit is not linked here, probably due to a Trac glitch.

Moving for 5.9.3 consideration, per comment:12.

#15 @audrasjb
3 years ago

  • Keywords fixed-major added
  • Owner set to audrasjb
  • Status changed from new to accepted

Alrighty, I'm going to backport this one to branch 5.9 now.

#16 @audrasjb
3 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 53019:

Editor: Fix broken asset URLs when using WP outside of the regular directory

Both CSS and JS URLs were totally broken for some of the newer blocks (e.g. the navigation block in the 2022 theme) when WP core is outside of the current directory using a different prefix.

Props gziolo, SergeyBiryukov, pgpagely, Mamaduka.
Merges [52939] to the 5.9 branch.
Fixes #55311.

Note: See TracTickets for help on using tickets.