[wp-trac] [WordPress Trac] #58664: Eliminate manual construction of script tags in WP_Scripts

WordPress Trac noreply at wordpress.org
Mon Sep 25 22:09:24 UTC 2023


#58664: Eliminate manual construction of script tags in WP_Scripts
-------------------------------------------------+-------------------------
 Reporter:  westonruter                          |       Owner:
                                                 |  westonruter
     Type:  defect (bug)                         |      Status:  closed
 Priority:  normal                               |   Milestone:  6.4
Component:  Script Loader                        |     Version:  6.3
 Severity:  normal                               |  Resolution:  fixed
 Keywords:  has-patch has-unit-tests needs-dev-  |     Focuses:  javascript
  note                                           |
-------------------------------------------------+-------------------------

Comment (by westonruter):

 Given aforementioned plugin code for injecting `defer` via the `clean_url`
 filter:

 {{{#!php
 <?php
 function defer_parsing_of_js ( $url ) {
         if ( FALSE === strpos( $url, '.js' ) ) return $url;
         if ( strpos( $url, 'jquery.js' ) ) return $url;
         return "$url' defer ";
 }
 add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
 }}}

 This is resulting in the following being rendered for `underscore`:

 {{{#!html
 <script src="http://localhost:8889/wp-
 includes/js/underscore.js?ver=1.13.4' defer " id="underscore-
 js"></script>
 }}}

 Since the script has `ver` query parameter, the resulting URL is parsed by
 the browser to be:

 {{{
 http://localhost:8889/wp-includes/js/underscore.js?ver=1.13.4%27%20defer
 }}}

 And this does not result in a 404. So I'd say that's graceful degradation.

 However, if a script lacked a `ver` query parameter (which is unusual)
 then this could indeed result in a 404. If we were concerned about that,
 we could add backwards-compatibility with something like this:

 {{{#!diff
 --- a/src/wp-includes/class-wp-scripts.php
 +++ b/src/wp-includes/class-wp-scripts.php
 @@ -373,6 +373,11 @@ class WP_Scripts extends WP_Dependencies {
                 /** This filter is documented in wp-includes/class-wp-
 scripts.php */
                 $src = esc_url_raw( apply_filters( 'script_loader_src',
 $src, $handle ) );

 +               if ( preg_match( "/^(.+?)' (.*)$/", $src, $matches ) ) {
 +                       _deprecated_hook( 'clean_url', '6.3', 'script
 loading strategies', __( 'Do not use clean_url filter to inject script tag
 attributes.' ) );
 +                       $src = $matches[1];
 +               }
 +
                 if ( ! $src ) {
                         return true;
                 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58664#comment:29>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list