Skip to content
This repository has been archived by the owner on Mar 9, 2024. It is now read-only.

WP-r55888: Script Loader: Improve performance of wp_maybe_inline_styles function. #272

Merged
merged 4 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2128,12 +2128,21 @@ function wp_maybe_inline_styles() {

// Build an array of styles that have a path defined.
foreach ( $wp_styles->queue as $handle ) {
if ( wp_styles()->get_data( $handle, 'path' ) && file_exists( $wp_styles->registered[ $handle ]->extra['path'] ) ) {
if ( ! isset( $wp_styles->registered[ $handle ] ) ) {
continue;
}
$src = $wp_styles->registered[ $handle ]->src;
$path = $wp_styles->get_data( $handle, 'path' );
if ( $path && $src ) {
$size = wp_filesize( $path );
if ( ! $size ) {
continue;
}
$styles[] = array(
'handle' => $handle,
'src' => $wp_styles->registered[ $handle ]->src,
'path' => $wp_styles->registered[ $handle ]->extra['path'],
'size' => filesize( $wp_styles->registered[ $handle ]->extra['path'] ),
'src' => $src,
'path' => $path,
'size' => $size,
);
}
}
Expand Down
115 changes: 115 additions & 0 deletions tests/phpunit/tests/dependencies/styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,119 @@ public function data_styles_with_media() {
),
);
}

/**
* @ticket 58394
*
* @covers ::wp_maybe_inline_styles
*/
public function test_wp_maybe_inline_styles() {
wp_register_style( 'test-handle', '/' . WPINC . '/css/wp-pointer.css' );
wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/wp-pointer.css' );

wp_enqueue_style( 'test-handle' );

wp_maybe_inline_styles();

$this->assertFalse( $GLOBALS['wp_styles']->registered['test-handle']->src, 'Source of style should be reset to false' );

$css = file_get_contents( ABSPATH . WPINC . '/css/wp-pointer.css' );
$this->assertSameSets( $GLOBALS['wp_styles']->registered['test-handle']->extra['after'], array( $css ), 'Source of style should set to after property' );
}

/**
* @ticket 58394
*
* @covers ::wp_maybe_inline_styles
*/
public function test_wp_maybe_inline_styles_dequeue_styles() {
$filter = new MockAction();
add_filter( 'pre_wp_filesize', array( $filter, 'filter' ) );
wp_register_style( 'test-handle', '/' . WPINC . '/css/wp-pointer.css' );
wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/wp-pointer.css' );

wp_enqueue_style( 'test-handle' );

wp_deregister_style( 'test-handle' );

wp_maybe_inline_styles();

$this->assertSame( 0, $filter->get_call_count() );
}

/**
* wp_filesize should be only be called once, as on the second run of wp_maybe_inline_styles,
* src will be set to false and filesize will not be requested.
*
* @ticket 58394
*
* @covers ::wp_maybe_inline_styles
*/
public function test_wp_maybe_inline_styles_multiple_runs() {
$filter = new MockAction();
add_filter( 'pre_wp_filesize', array( $filter, 'filter' ) );
wp_register_style( 'test-handle', '/' . WPINC . '/css/wp-pointer.css' );
wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/wp-pointer.css' );

wp_enqueue_style( 'test-handle' );

wp_maybe_inline_styles();
wp_maybe_inline_styles();

$this->assertSame( 1, $filter->get_call_count() );
}

/**
* @ticket 58394
*
* @covers ::wp_maybe_inline_styles
*/
public function test_test_wp_maybe_inline_styles_missing_file() {
$filter = new MockAction();
add_filter( 'pre_wp_filesize', array( $filter, 'filter' ) );
$url = '/' . WPINC . '/css/invalid.css';
wp_register_style( 'test-handle', $url );
wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/invalid.css' );

wp_enqueue_style( 'test-handle' );

wp_maybe_inline_styles();

$this->assertSame( $GLOBALS['wp_styles']->registered['test-handle']->src, $url, 'Source should not change' );
$this->assertArrayNotHasKey( 'after', $GLOBALS['wp_styles']->registered['test-handle']->extra, 'Source of style not should set to after property' );
$this->assertSame( 1, $filter->get_call_count(), 'wp_filesize should only be called once' );
}

/**
* @ticket 58394
*
* @covers ::wp_maybe_inline_styles
*/
public function test_wp_maybe_inline_styles_no_src() {
wp_register_style( 'test-handle', false );
wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/wp-pointer.css' );

wp_enqueue_style( 'test-handle' );

wp_maybe_inline_styles();

$this->assertFalse( $GLOBALS['wp_styles']->registered['test-handle']->src, 'Source of style should remain false' );
$this->assertArrayNotHasKey( 'after', $GLOBALS['wp_styles']->registered['test-handle']->extra, 'Source of style not should set to after property' );
}

/**
* @ticket 58394
*
* @covers ::wp_maybe_inline_styles
*/
public function test_wp_maybe_inline_styles_no_path() {
$url = '/' . WPINC . '/css/wp-pointer.css';
wp_register_style( 'test-handle', $url );

wp_enqueue_style( 'test-handle' );

wp_maybe_inline_styles();

$this->assertSame( $GLOBALS['wp_styles']->registered['test-handle']->src, $url );
}
}
Loading