1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
<?php
namespace SIW\Modules;
use SIW\i18n;
use SIW\Util;
class Cache_Rebuild {
const TS_CACHE_REBUILD = '05:00';
const HOOK = 'siw_rebuild_cache';
public static function init() {
if ( ! defined( 'WP_ROCKET_VERSION' ) ) {
return false;
}
$self = new self();
add_action( 'siw_update_plugin', [ $self, 'schedule_cache_rebuild' ] );
add_action( self::HOOK, [ $self, 'rebuild_cache' ] );
add_filter( 'rocket_sitemap_preload_list', [ $self, 'set_sitemaps_for_preload' ] );
}
public function schedule_cache_rebuild() {
$cache_rebuild_ts = strtotime( 'tomorrow ' . self::TS_CACHE_REBUILD );
$cache_rebuild_ts_gmt = Util::convert_timestamp_to_gmt( $cache_rebuild_ts );
if ( wp_next_scheduled( self::HOOK ) ) {
wp_clear_scheduled_hook( self::HOOK );
}
wp_schedule_event( $cache_rebuild_ts_gmt, 'daily', self::HOOK );
}
public function rebuild_cache() {
rocket_clean_domain();
rocket_clean_minify();
rocket_clean_cache_busting();
run_rocket_sitemap_preload();
}
public function set_sitemaps_for_preload( $sitemaps ) {
if ( ! class_exists( '\The_SEO_Framework\Bridges\Sitemap' ) ) {
return $sitemaps;
}
if ( get_rocket_option( 'tsf_xml_sitemap', false ) ) {
$languages = i18n::get_active_languages();
$sitemap_url = \The_SEO_Framework\Bridges\Sitemap::get_instance()->get_expected_sitemap_endpoint_url();
foreach ( $languages as $language ) {
$sitemaps[] = i18n::get_translated_permalink( $sitemap_url, $language['code'] );
}
}
return $sitemaps;
}
}