Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear CPT custom cache files correctly #7277

Merged
merged 9 commits into from
Feb 17, 2025
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
6 changes: 5 additions & 1 deletion inc/Engine/Preload/Controller/ClearCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public function __construct( Cache $query ) {
*/
public function partial_clean( array $urls ) {
foreach ( $urls as $url ) {

// if it has a regex, remove the regex part completely.
if ( str_contains( $url, '*' ) ) {
$regex_part = basename( $url );
$url = str_replace( $regex_part, '', $url );
}
if ( ! $this->is_excluded_by_filter( $url ) ) {
$this->query->create_or_update(
[
Expand Down
9 changes: 1 addition & 8 deletions inc/common/purge.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,8 @@ function rocket_get_purge_urls( $post_id, $post ) {
if ( 'post' !== $post_type ) {
$post_type_archive = get_post_type_archive_link( $post_type );
if ( $post_type_archive ) {
// Rename the caching filename for SSL URLs.
$filename = 'index';
if ( is_ssl() ) {
$filename .= '-https';
}

$post_type_archive = trailingslashit( $post_type_archive );
$purge_urls[] = $post_type_archive . $filename . '.html';
$purge_urls[] = $post_type_archive . $filename . '.html_gzip';
$purge_urls[] = $post_type_archive . 'index(.*).html';
$purge_urls[] = $post_type_archive . $GLOBALS['wp_rewrite']->pagination_base;
}
}
Expand Down
18 changes: 17 additions & 1 deletion inc/functions/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ function rocket_clean_files( $urls, $filesystem = null, $run_actions = true ) {
do_action( 'before_rocket_clean_files', $urls ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
}

foreach ( $urls as $url ) {
foreach ( $urls as $url_key => $url ) {
if ( $run_actions ) {
/**
* Fires before the cache file is deleted.
Expand Down Expand Up @@ -612,6 +612,22 @@ function rocket_clean_files( $urls, $filesystem = null, $run_actions = true ) {

$entry = $dir . $parsed_url['path'];

// For regex we use it for file names only, and it should include the * character.
if ( str_contains( $entry, '*' ) ) {
$regex_part = basename( $entry );
$search_dir = str_replace( $regex_part, '', $entry );
$matched_files = _rocket_get_dir_files_by_regex( $search_dir, '#' . $regex_part . '#i' );
foreach ( $matched_files as $item ) {
$current_file = $item->getPath() . DIRECTORY_SEPARATOR . $item->getFilename();
if ( $filesystem->exists( $current_file ) ) {
$filesystem->delete( $current_file );
}
}
// Remove the regex part from the url.
$url = str_replace( $regex_part, '', $url );
$urls[ $url_key ] = $url;
}

// Skip if the dir/file does not exist.
if ( ! $filesystem->exists( $entry ) ) {
continue;
Expand Down
3 changes: 1 addition & 2 deletions tests/Fixtures/inc/common/rocketGetPurgeUrls.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@
],
'expected' => [
'http://example.org/custompost/test_custom_post/',
'http://example.org/custompost/index.html',
'http://example.org/custompost/index.html_gzip',
'http://example.org/custompost/index(.*).html',
'http://example.org/custompost/page',
'http://example.org/author/author_name/',
'http://example.org/test_parent/',
Expand Down
4 changes: 0 additions & 4 deletions tests/Unit/inc/common/rocketGetPurgeUrls.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ public function testShouldReturnUrls( $config, $expected ) {
->andReturn( $archive_url );

if ( $archive_url ) {
Functions\expect('is_ssl')
->once()
->andReturn( $config['is_ssl'] );

$GLOBALS['wp_rewrite'] = (object) [
'pagination_base' => 'page'
];
Expand Down
Loading