Created
September 7, 2023 08:39
-
-
Save zeopix/55f4689978f07d92998cc33f576e3843 to your computer and use it in GitHub Desktop.
[WP Rocket] Clear cache once for certain hooks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'after_rocket_clean_domain_outside_the_loop' => 'clean_full_cache', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function rocket_clean_domain( $lang = '', $filesystem = null ) { | |
$urls = ( ! $lang || is_object( $lang ) || is_array( $lang ) || is_int( $lang ) ) | |
? (array) get_rocket_i18n_uri() | |
: (array) get_rocket_i18n_home_url( $lang ); | |
/** | |
* Filter URLs to delete all caching files from a domain. | |
* | |
* @since 2.6.4 | |
* | |
* @param array URLs that will be returned. | |
* @param string The language code. | |
*/ | |
$urls = (array) apply_filters( 'rocket_clean_domain_urls', $urls, $lang ); | |
$urls = array_filter( $urls ); | |
if ( empty( $urls ) ) { | |
return false; | |
} | |
/** This filter is documented in inc/front/htaccess.php */ | |
$url_no_dots = (bool) apply_filters( 'rocket_url_no_dots', false ); | |
$cache_path = _rocket_get_wp_rocket_cache_path(); | |
$dirs_to_preserve = get_rocket_i18n_to_preserve( $lang, $cache_path ); | |
if ( empty( $filesystem ) ) { | |
$filesystem = rocket_direct_filesystem(); | |
} | |
foreach ( $urls as $url ) { | |
$parsed_url = get_rocket_parse_url( $url ); | |
if ( $url_no_dots ) { | |
$parsed_url['host'] = str_replace( '.', '_', $parsed_url['host'] ); | |
} | |
$root = $cache_path . $parsed_url['host'] . $parsed_url['path']; | |
/** | |
* Fires before all cache files are deleted. | |
* | |
* @since 1.0 | |
* | |
* @param string $root The path of home cache file. | |
* @param string $lang The current lang to purge. | |
* @param string $url The home url. | |
*/ | |
do_action( 'before_rocket_clean_domain', $root, $lang, $url ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals | |
foreach ( _rocket_get_cache_dirs( $parsed_url['host'], $cache_path ) as $dir ) { | |
$entry = $dir . $parsed_url['path']; | |
// Skip if the dir/file does not exist. | |
if ( ! $filesystem->exists( $entry ) ) { | |
continue; | |
} | |
if ( $filesystem->is_dir( $entry ) ) { | |
rocket_rrmdir( $entry, $dirs_to_preserve, $filesystem ); | |
} else { | |
$filesystem->delete( $entry ); | |
} | |
} | |
/** | |
* Fires after all cache files was deleted. | |
* | |
* @since 1.0 | |
* | |
* @param string $root The path of home cache file. | |
* @param string $lang The current lang to purge. | |
* @param string $url The home url. | |
*/ | |
do_action( 'after_rocket_clean_domain', $root, $lang, $url ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals | |
} | |
/** | |
* This is custom hook introduced by WPML team tok | |
*/ | |
do_action( 'after_rocket_clean_domain_outside_the_loop' ); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment