WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 11 months ago

#34591 new defect (bug)

BugFix to WP_Scripts::do_item(), remove doubled "//"

Reported by: jan.mazanek Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.3.1
Component: Script Loader Keywords: reporter-feedback needs-patch needs-unit-tests
Focuses: Cc:

Description (last modified by johnbillion)

Current code in do_item() of class.wp-script.php on line 172:

$src = $this->base_url . $src;

may produce duplicate slashes "//", resulting in problems.

This might be fixed with code:

$src = $this->base_url . $src;

Currently:

  • WP_Scripts contains: "public $base_url; // Full URL with trailing slash" and
  • script-loader.php contains calls $scripts->add() with initial "/" in relative paths

Together this produces doubled slashes "//". For example:
http://www.ocelovehaly.cz/ll//wp-includes/js/jquery/jquery.js?ver=1.11.3

This makes W3TC include script in minified version, but not to remove it from the original HTML.

Including jQuery twice makes LayerSlider not to work.

Please, could you bugfix class.wp-script.php on line 172?

Thank you :-)

Change History (5)

#1 in reply to: ↑ description @jan.mazanek
2 years ago

To be able to update Wordpress, untill this fix is included, it should be more secure to solve this with filter:

function abeo_script_loader_src($src, $handle) {
	$path = parse_url($src, PHP_URL_PATH);
	if(!empty($path)) {
		$replaced = 0;
		$pathReplaced = str_replace('//', '/', $path, $replaced);
		if($replaced) {
			$src = str_replace($path, $pathReplaced, $src);
		}
	}
	return $src;
}

add_filter('script_loader_src', 'abeo_script_loader_src', 10, 2);
Last edited 2 years ago by jan.mazanek (previous) (diff)

#2 @johnbillion
2 years ago

  • Description modified (diff)
  • Keywords reporter-feedback added

Thanks for the report, @jan.mazanek.

  • Can you explain a little more about why you think this double slash is appearing?
  • What's the value of the home option in the wp_options table in your database?
  • Does the bug remain if you disable all the plugins on your site and switch to one of the default themes?
  • Is there anything unusual about the configuration of your site?

#3 @jan.mazanek
2 years ago

John, thank for reply.

My configuration is highly unusual to get high speed, shared data and use multiple host names without domain preffixes or suffixes. I developed a Child Theme based on Avada, I use plugins W3 Total Cache and A3 Lazy Load and some other.

The key info for this ticket might be, that I do override options siteurl, home, page_on_front, blogname, blogdescription with filters:
pre_option_siteurl
pre_option_home
pre_option_blogname
pre_option_blogdescription

If I remember correct, I did remove trailing slash from siteurl and home options afer writting this ticket.

But to simplify things. wp-includes/class.wp-scripts.php contains:
1) line 20:

public $base_url; // Full URL with trailing slash

2) line 172:

$src = $this->base_url . $src;

So together: if I do respect comment and $this->base_url contains trailing slash
and if $src starts with slash (as is now used in script-loader.php)
then together this will produce two slashes.

Possible solutions - alternatives:
1) Change documentation, that $this->base_url should not contain trailing slash
2) Remove initial slash from script-loader.php calls to $scripts->add()
3) My original suggestion (or any other code) which will prevent doubled slashes

Currently I solved the problem for myself and I am not going to test, if "Does the bug remain if you disable all the plugins on your site and switch to one of the default themes?", but I am almost sure, that the problem would disappear (which doesn't mean, that the code is safe and should not be changed).

#4 @jan.mazanek
2 years ago

johnbillion, your update of description is not incorrect

$src = $this->base_url . $src;

Doesn't fix the problem.

To fix the problem, there must be some code which ensures, there is exactly one slash on the border of $this->base_url and $src. Such code is for example:

$src = rtrim($this->base_url, '/'). '/' . ltrim($src, '/');

#5 @swissspidy
11 months ago

  • Keywords needs-patch needs-unit-tests added
Note: See TracTickets for help on using tickets.