Make WordPress Core


Ignore:
Timestamp:
10/10/2025 04:17:22 AM (2 months ago)
Author:
westonruter
Message:

Script Loader: Use original stylesheet URL for sourceURL when inlined.

For inline styles which had been inlined from registered external stylesheets via wp_maybe_inline_styles(), this defers to using the original stylesheet URL for the sourceURL as opposed to fabricating one from the stylesheet handle. This makes the sourceURL much more useful for debugging, as it indicates where the stylesheet is located. This allows a developer to make a change to the CSS more easily.

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

Follow-up to [50836].

Props westonruter, mukesh27, gziolo.
See #50328, #52620.
Fixes #63887.

File:
1 edited

Legend:

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

    r60719 r60920  
    334334        $output = $this->get_data( $handle, 'after' );
    335335
    336         if ( empty( $output ) ) {
     336        if ( empty( $output ) || ! is_array( $output ) ) {
    337337            return false;
    338338        }
    339339
    340340        if ( ! $this->do_concat ) {
     341
     342            // Obtain the original `src` for a stylesheet possibly inlined by wp_maybe_inline_styles().
     343            $inlined_src = $this->get_data( $handle, 'inlined_src' );
     344
     345            // If there's only one `after` inline style, and that inline style had been inlined, then use the $inlined_src
     346            // as the sourceURL. Otherwise, if there is more than one inline `after` style associated with the handle,
     347            // then resort to using the handle to construct the sourceURL since there isn't a single source.
     348            if ( count( $output ) === 1 && is_string( $inlined_src ) && strlen( $inlined_src ) > 0 ) {
     349                $source_url = esc_url_raw( $inlined_src );
     350            } else {
     351                $source_url = rawurlencode( "{$handle}-inline-css" );
     352            }
     353
    341354            $output[] = sprintf(
    342355                '/*# sourceURL=%s */',
    343                 rawurlencode( "{$handle}-inline-css" )
     356                $source_url
    344357            );
    345358        }
Note: See TracChangeset for help on using the changeset viewer.