Make WordPress Core

Opened 6 weeks ago

Closed 4 weeks ago

#64372 closed defect (bug) (fixed)

Additional URL args supplied via enqueued script/style handle cause malformed ver query param when null

Reported by: westonruter's profile westonruter Owned by: westonruter's profile westonruter
Milestone: 7.0 Priority: low
Severity: normal Version: 2.6
Component: Script Loader Keywords: has-patch has-unit-tests
Focuses: Cc:

Description (last modified by westonruter)

When working on fixing PHPStan issues (#64238) for WP_Scripts (r61358), I stumbled across a capability I was previously unaware of: you can pass additional query parameters to an enqueued script/style URL by adding them to the handle. For example:

<?php
wp_enqueue_script( 'foo?bar=baz&baz=quux', 'https://example.com/test.js', array(), '1.0' );

This results in the following script being printed:

<script type="text/javascript" src="https://example.com/test.js?ver=1.0&amp;bar=baz&amp;baz=quux" id="foo-js"></script

This capability is not included in any unit test, and it may be a vestige of something no longer used. Naturally, a better way to do this now would seem to be to leverage the script_loader_src filter, or rather, to add the query parameters to the URL being registered in the first place. I can find one plugin (Map Navigator) which seems to be using this intentionally.

In any case, I found that this capability is broken when using null as a version:

<?php
wp_enqueue_script( 'foo?bar=baz&baz=quux', 'https://example.com/test.js', array(), null );

This results in:

<script type="text/javascript" src="https://example.com/test.js?ver=bar=baz&amp;baz=quux" id="foo-js"></script>

Note the query string: ver=bar=baz&baz=quux.

The "ver=" shouldn't have been included here.

Change History (5)

This ticket was mentioned in PR #10608 on WordPress/wordpress-develop by @westonruter.


6 weeks ago
#1

  • Keywords has-patch has-unit-tests added

#2 @westonruter
6 weeks ago

  • Description modified (diff)

#3 @westonruter
6 weeks ago

  • Owner set to westonruter
  • Status changed from new to accepted

@westonruter commented on PR #10608:


5 weeks ago
#4

@peterwilsoncc I added your example code as a test case in 624ea26959281a29bbb0f4f475dc3b4b49521ee2.

I then added ba88780 to ensure this PR outputs the same markup, except for what is supposed to be fixed of course.

#5 @westonruter
4 weeks ago

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

In 61397:

Script Loader: Fix adding default version to script/style URL when args are supplied via enqueued handle.

Also fixes phpdoc for some member variables of WP_Scripts and WP_Styles.

Developed in https://github.com/WordPress/wordpress-develop/pull/10608

Follow-up to [61358].

Props westonruter, peterwilsoncc.
See #64224, #64238.
Fixes #64372.

Note: See TracTickets for help on using tickets.