Make WordPress Core


Ignore:
Timestamp:
03/11/2026 01:30:24 AM (2 weeks ago)
Author:
westonruter
Message:

Script Loader: Preserve duplicate URL query params in enqueued scripts and styles.

Previously in r61397, add_query_arg() was used to append versions or handle-specific query arguments. This resulted in stripping any existing duplicate query variables in the source URL (common in Google Fonts URLs). This change refactors WP_Styles::_css_href() and WP_Scripts::do_item() to manually append these parameters to the URL string. This ensures all original query variables are preserved exactly as provided. It also improves fragment handling by ensuring query parameters are inserted before any '#' anchor while maintaining the anchor's presence.

The URL encoding changes in tests/phpunit/tests/dependencies/scripts.php are reversions of what had previously been done in r61397.

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

Follow-up to r61397, r61358.

Props westonruter, jonsurrell.
Fixes #64372.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-scripts.php

    r61699 r61927  
    418418        }
    419419
    420         $query_args = array();
     420        $ver_to_add = '';
    421421        if ( empty( $obj->ver ) && null !== $obj->ver && is_string( $this->default_version ) ) {
    422             $query_args['ver'] = $this->default_version;
     422            $ver_to_add = $this->default_version;
    423423        } elseif ( is_scalar( $obj->ver ) ) {
    424             $query_args['ver'] = (string) $obj->ver;
    425         }
    426         if ( isset( $this->args[ $handle ] ) ) {
    427             parse_str( $this->args[ $handle ], $parsed_args );
    428             if ( $parsed_args ) {
    429                 $query_args = array_merge( $query_args, $parsed_args );
    430             }
    431         }
    432         $src = add_query_arg( rawurlencode_deep( $query_args ), $src );
     424            $ver_to_add = (string) $obj->ver;
     425        }
     426
     427        $added_args = (string) ( $this->args[ $handle ] ?? '' );
     428
     429        if ( '' !== $ver_to_add || '' !== $added_args ) {
     430            $fragment = strstr( $src, '#' );
     431            if ( false !== $fragment ) {
     432                $src = substr( $src, 0, -strlen( $fragment ) );
     433            }
     434
     435            if ( '' !== $ver_to_add ) {
     436                $src .= ( str_contains( $src, '?' ) ? '&' : '?' ) . 'ver=' . rawurlencode( $ver_to_add );
     437            }
     438            if ( '' !== $added_args ) {
     439                $src .= ( str_contains( $src, '?' ) ? '&' : '?' ) . $added_args;
     440            }
     441
     442            if ( false !== $fragment ) {
     443                $src .= $fragment;
     444            }
     445        }
    433446
    434447        /** This filter is documented in wp-includes/class-wp-scripts.php */
Note: See TracChangeset for help on using the changeset viewer.