Skip to content

Commit

Permalink
Fixes regex and optimizes rocket_clean_minify() (PR #2574)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tonya Mork committed Apr 23, 2020
1 parent c42f139 commit 14f6669
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 187 deletions.
65 changes: 34 additions & 31 deletions inc/functions/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,77 +421,80 @@ function set_rocket_wp_cache_define( $turn_it_on ) { // phpcs:ignore WordPress.N
}

/**
* Delete all minify cache files
* Delete all minify cache files.
*
* @since 3.5.3 Replaces glob.
* @since 2.1
*
* @param string|array $extensions (default: array('js','css') File extensions to minify.
* @return void
* @param string|array $extensions Optional. File extensions to minify. Default: js and css.
*/
function rocket_clean_minify( $extensions = [ 'js', 'css' ] ) {
$extensions = is_string( $extensions ) ? (array) $extensions : $extensions;

try {
$dir = new RecursiveDirectoryIterator( WP_ROCKET_MINIFY_CACHE_PATH . get_current_blog_id(), FilesystemIterator::SKIP_DOTS );
} catch ( \UnexpectedValueException $e ) {
// No logging yet.
// Bails out if there are no extensions to target.
if ( empty( $extensions ) ) {
return;
}

try {
$iterator = new RecursiveIteratorIterator( $dir, RecursiveIteratorIterator::CHILD_FIRST );
} catch ( \Exception $e ) {
// No logging yet.
if ( is_string( $extensions ) ) {
$extensions = (array) $extensions;
}

$min_cache_path = rocket_get_constant( 'WP_ROCKET_MINIFY_CACHE_PATH' );
$min_path = $min_cache_path . get_current_blog_id() . '/';
$iterator = _rocket_get_cache_path_iterator( $min_path );
if ( false === $iterator ) {
return;
}

$filesystem = rocket_direct_filesystem();
$min_path_regex = str_replace( '/', '\/', $min_path );

foreach ( $extensions as $ext ) {
/**
* Fires before the minify cache files are deleted
* Fires before the minify cache files are deleted.
*
* @since 2.1
*
* @param string $ext File extensions to minify.
*/
*/
do_action( 'before_rocket_clean_minify', $ext ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals

try {
$files = new RegexIterator( $iterator, '#.*\.' . $ext . '#', RegexIterator::GET_MATCH );
foreach ( $files as $file ) {
rocket_direct_filesystem()->delete( $file[0] );
}
} catch ( \InvalidArgumentException $e ) {
// No logging yet.
$entries = new RegexIterator( $iterator, "/{$min_path_regex}.*\.{$ext}/" );
} catch ( Exception $e ) {
return;
}

foreach ( $entries as $entry ) {
$filesystem->delete( $entry->getPathname() );
}

/**
* Fires after the minify cache files was deleted
* Fires after the minify cache files was deleted.
*
* @since 2.1
*
* @param string $ext File extensions to minify.
*/
*/
do_action( 'after_rocket_clean_minify', $ext ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
}

// Delete any directories.
foreach ( $iterator as $item ) {
if ( rocket_direct_filesystem()->is_dir( $item ) ) {
rocket_direct_filesystem()->delete( $item );
if ( $filesystem->is_dir( $item ) ) {
$filesystem->delete( $item );
}
}

$third_party = WP_ROCKET_MINIFY_CACHE_PATH . '3rd-party';

// Clean the cache/min/3rd-party items.
try {
$files = new FilesystemIterator( $third_party );
$files = new FilesystemIterator( "{$min_cache_path}3rd-party" );

foreach ( $files as $file ) {
if ( rocket_direct_filesystem()->is_file( $file ) ) {
rocket_direct_filesystem()->delete( $file );
if ( $filesystem->is_file( $file ) ) {
$filesystem->delete( $file );
}
}
} catch ( \UnexpectedValueException $e ) {
} catch ( UnexpectedValueException $e ) {
// No logging yet.
return;
}
Expand Down
209 changes: 154 additions & 55 deletions tests/Fixtures/inc/functions/rocketCleanMinify.php
Original file line number Diff line number Diff line change
@@ -1,67 +1,166 @@
<?php

return [
'vfs_dir' => 'wp-content/cache/min/',

// Virtual filesystem structure.
'structure' => [
'wp-content' => [
'cache' => [
'min' => [
'1' => [
'5c795b0e3a1884eec34a989485f863ff.js' => '',
'5c795b0e3a1884eec34a989485f863ff.js.gz' => '',
'fa2965d41f1515951de523cecb81f85e.css' => '',
'fa2965d41f1515951de523cecb81f85e.css.gz' => '',
],
'3rd-party' => [
'2n7x3vd41f1515951de523cecb81f85e.css' => '',
'2n7x3vd41f1515951de523cecb81f85e.css.gz' => '',
'bt937b0e3a1884eec34a989485f863ff.js' => '',
'bt937b0e3a1884eec34a989485f863ff.js.gz' => '',
],
'vfs_dir' => 'wp-content/cache/min/',

'structure' => require WP_ROCKET_TESTS_FIXTURES_DIR . '/vfs-structure/default.php',

// Test data.
'test_data' => [
'shouldNotCleanWhenNoExtensionsGiven' => [
'extensions' => '',
'expected' => [
'cleaned' => [],
'non_cleaned' => [
'vfs://public/wp-content/cache/min/1/' => true,
'vfs://public/wp-content/cache/min/2/' => true,
'vfs://public/wp-content/cache/min/3rd-party/' => true,
],
],
],
],
'shouldNotCleanWhenExtensionDoesNotExist' => [
'extensions' => [ 'php', 'html' ],
'expected' => [
'cleaned' => [],
'non_cleaned' => [
'vfs://public/wp-content/cache/min/1/' => true,
'vfs://public/wp-content/cache/min/2/' => true,
'vfs://public/wp-content/cache/min/3rd-party/' => true,
],
],
],
'shouldClean_css' => [
'extensions' => 'css',
'expected' => [
'cleaned' => [
'vfs://public/wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css' => null,
'vfs://public/wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css.gz' => null,

// Test data.
// The virtual filesystem does not work with glob. Therefore, we have to specify all of the file extensions.
'test_data' => [
[
[ 'css', 'css.gz' ],
[
'wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css',
'wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css.gz',
'wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css',
'wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css.gz',
'wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js',
'wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js.gz',
]
'vfs://public/wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css' => null,
'vfs://public/wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css.gz' => null,
],
'non_cleaned' => [
'vfs://public/wp-content/cache/min/1/' => false,
'vfs://public/wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js' => false,
'vfs://public/wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js.gz' => false,

'vfs://public/wp-content/cache/min/2/' => true,

'vfs://public/wp-content/cache/min/3rd-party/' => false,
'vfs://public/wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js' => false,
'vfs://public/wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js.gz' => false,
],
],
],
[
[ 'js', 'js.gz' ],
[
'wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js',
'wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js.gz',
'wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css',
'wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css.gz',
'wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js',
'wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js.gz',
]
'shouldClean_css.gz' => [
'extensions' => 'css.gz',
'expected' => [
'cleaned' => [
'vfs://public/wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css.gz' => null,
'vfs://public/wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css.gz' => null,
],
'non_cleaned' => [
'vfs://public/wp-content/cache/min/1/' => false,
'vfs://public/wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css' => false,
'vfs://public/wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js' => false,
'vfs://public/wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js.gz' => false,

'vfs://public/wp-content/cache/min/2/' => true,

'vfs://public/wp-content/cache/min/3rd-party/' => false,
'vfs://public/wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css' => false,
'vfs://public/wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js' => false,
'vfs://public/wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js.gz' => false,
],
],
],
[
[ 'css', 'css.gz', 'js', 'js.gz' ],
[
'wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css',
'wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css.gz',
'wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js',
'wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js.gz',
'wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css',
'wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css.gz',
'wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js',
'wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js.gz',
]
'shouldClean_js' => [
'extensions' => 'js',
'expected' => [
'cleaned' => [
'vfs://public/wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js' => null,
'vfs://public/wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js.gz' => null,

'vfs://public/wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js' => null,
'vfs://public/wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js.gz' => null,
],
'non_cleaned' => [
'vfs://public/wp-content/cache/min/1/' => false,
'vfs://public/wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css' => false,
'vfs://public/wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css.gz' => false,

'vfs://public/wp-content/cache/min/2/' => true,

'vfs://public/wp-content/cache/min/3rd-party/' => false,
'vfs://public/wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css' => false,
'vfs://public/wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css.gz' => false,
],
],
],
'shouldClean_js.gz' => [
'extensions' => 'js.gz',
'expected' => [
'cleaned' => [
'vfs://public/wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js.gz' => null,
'vfs://public/wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js.gz' => null,
],
'non_cleaned' => [
'vfs://public/wp-content/cache/min/1/' => false,
'vfs://public/wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js' => false,
'vfs://public/wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css' => false,
'vfs://public/wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css.gz' => false,

'vfs://public/wp-content/cache/min/2/' => true,

'vfs://public/wp-content/cache/min/3rd-party/' => false,
'vfs://public/wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js' => false,
'vfs://public/wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css' => false,
'vfs://public/wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css.gz' => false,
],
],
],
'shouldCleanCssAndJs' => [
'extensions' => [ 'css', 'js' ],
'expected' => [
'cleaned' => [
'vfs://public/wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js' => null,
'vfs://public/wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js.gz' => null,
'vfs://public/wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css' => null,
'vfs://public/wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css.gz' => null,

'vfs://public/wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js' => null,
'vfs://public/wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js.gz' => null,
'vfs://public/wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css' => null,
'vfs://public/wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css.gz' => null,
],
'non_cleaned' => [
'vfs://public/wp-content/cache/min/1/' => false,
'vfs://public/wp-content/cache/min/2/' => true,
'vfs://public/wp-content/cache/min/3rd-party/' => false,
],
],
],
'shouldClean_.gz' => [
'extensions' => 'gz',
'expected' => [
'cleaned' => [
'vfs://public/wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css.gz' => null,
'vfs://public/wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js.gz' => null,
'vfs://public/wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css.gz' => null,
'vfs://public/wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js.gz' => null,
],
'non_cleaned' => [
'vfs://public/wp-content/cache/min/1/' => false,
'vfs://public/wp-content/cache/min/1/5c795b0e3a1884eec34a989485f863ff.js' => false,
'vfs://public/wp-content/cache/min/1/fa2965d41f1515951de523cecb81f85e.css' => false,

'vfs://public/wp-content/cache/min/2/' => true,

'vfs://public/wp-content/cache/min/3rd-party/' => false,
'vfs://public/wp-content/cache/min/3rd-party/bt937b0e3a1884eec34a989485f863ff.js' => false,
'vfs://public/wp-content/cache/min/3rd-party/2n7x3vd41f1515951de523cecb81f85e.css' => false,
],
],
],
],
];
4 changes: 2 additions & 2 deletions tests/Fixtures/inc/functions/rocketRrmdir.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@
'vfs://public/wp-content/cache/wp-rocket/example.org(.*)/fr',
],
'expected' => [
'before_rocket_rrmdir' => 19,
'after_rocket_rrmdir' => 19,
'before_rocket_rrmdir' => 21,
'after_rocket_rrmdir' => 21,
'removed' => [
'vfs://public/wp-content/cache/min/' => null,
'vfs://public/wp-content/cache/busting' => null,
Expand Down
22 changes: 18 additions & 4 deletions tests/Fixtures/vfs-structure/default.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

return [
'wp-content' => [
'wp-content' => [
'cache' => [
'wp-rocket' => [
'index.html' => '',
Expand Down Expand Up @@ -81,9 +81,23 @@
],
],
'min' => [
'1' => [
'123456.css' => '',
'123456.js' => '',
'1' => [
'5c795b0e3a1884eec34a989485f863ff.js' => '',
'5c795b0e3a1884eec34a989485f863ff.js.gz' => '',
'fa2965d41f1515951de523cecb81f85e.css' => '',
'fa2965d41f1515951de523cecb81f85e.css.gz' => '',
],
'2' => [
'34a989485f863ff5c795b0e3a1884eec.js' => '',
'34a989485f863ff5c795b0e3a1884eec.js.gz' => '',
'523cecb81f85efa2965d41f1515951de.css' => '',
'523cecb81f85efa2965d41f1515951de.css.gz' => '',
],
'3rd-party' => [
'2n7x3vd41f1515951de523cecb81f85e.css' => '',
'2n7x3vd41f1515951de523cecb81f85e.css.gz' => '',
'bt937b0e3a1884eec34a989485f863ff.js' => '',
'bt937b0e3a1884eec34a989485f863ff.js.gz' => '',
],
],
'busting' => [
Expand Down

0 comments on commit 14f6669

Please sign in to comment.