Make WordPress Core


Ignore:
Timestamp:
06/07/2023 06:54:18 AM (9 months ago)
Author:
spacedmonkey
Message:

Script Loader: Improve performance of wp_maybe_inline_styles function.

The wp_maybe_inline_styles function is called twice on the average page load. On it's second run however, it did not check to see if the style had already been processed on the first run. This resulted in calling filesize and get_file_contents unnecessarily, which was bad for performance. Now, the loop around the queued styles, checks to see if the source is set to false, meaning it has already been processed. This change also replaces calls to filesize with the core function wp_filesize, which improves extensibility.

Props spacedmonkey, flixos90, peterwilsoncc, joemcgill.
Fixes #58394.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/script-loader.php

    r55870 r55888  
    28732873    // Build an array of styles that have a path defined.
    28742874    foreach ( $wp_styles->queue as $handle ) {
    2875         if ( wp_styles()->get_data( $handle, 'path' ) && file_exists( $wp_styles->registered[ $handle ]->extra['path'] ) ) {
     2875        $src  = $wp_styles->registered[ $handle ]->src;
     2876        $path = wp_styles()->get_data( $handle, 'path' );
     2877        if ( $path && $src ) {
     2878            $size = wp_filesize( $path );
     2879            if ( ! $size ) {
     2880                continue;
     2881            }
    28762882            $styles[] = array(
    28772883                'handle' => $handle,
    2878                 'src'    => $wp_styles->registered[ $handle ]->src,
    2879                 'path'   => $wp_styles->registered[ $handle ]->extra['path'],
    2880                 'size'   => filesize( $wp_styles->registered[ $handle ]->extra['path'] ),
     2884                'src'    => $src,
     2885                'path'   => $path,
     2886                'size'   => $size,
    28812887            );
    28822888        }
Note: See TracChangeset for help on using the changeset viewer.