diff --git a/lib/outputlib.php b/lib/outputlib.php index e5a8514d980..a51317da4ec 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -245,6 +245,7 @@ function theme_build_css_for_themes($themeconfigs = [], $directions = ['rtl', 'l */ function theme_reset_all_caches() { global $CFG, $PAGE; + require_once("{$CFG->libdir}/filelib.php"); $next = theme_get_next_revision(); theme_set_revision($next); @@ -257,6 +258,12 @@ function theme_reset_all_caches() { // Purge compiled post processed css. cache::make('core', 'postprocessedcss')->purge(); + // Delete all old theme localcaches. + $themecachedirs = glob("{$CFG->localcachedir}/theme/*", GLOB_ONLYDIR); + foreach ($themecachedirs as $localcachedir) { + fulldelete($localcachedir); + } + if ($PAGE) { $PAGE->reload_theme(); } diff --git a/theme/styles.php b/theme/styles.php index 6a4aa9b9d60..9f3ce5fe61e 100644 --- a/theme/styles.php +++ b/theme/styles.php @@ -128,6 +128,7 @@ $cache = true; // likely being regenerated. if ($themerev <= 0 or $themerev != $rev or $themesubrev != $currentthemesubrev) { $rev = $themerev; + $themesubrev = $currentthemesubrev; $cache = false; $candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css"; @@ -216,6 +217,7 @@ if ($sendaftergeneration || $lock) { */ function theme_styles_generate_and_store($theme, $rev, $themesubrev, $candidatedir) { global $CFG; + require_once("{$CFG->libdir}/filelib.php"); // Generate the content first. if (!$csscontent = $theme->get_css_cached_content()) { @@ -260,6 +262,28 @@ function theme_styles_generate_and_store($theme, $rev, $themesubrev, $candidated . theme_styles_get_filename($type, 0, $theme->use_svg_icons()); css_store_css($theme, $fallbacksheet, $csscontent, true, $chunkurl); + // Delete older revisions from localcache. + $themecachedirs = glob("{$CFG->localcachedir}/theme/*", GLOB_ONLYDIR); + foreach ($themecachedirs as $localcachedir) { + $cachedrev = []; + preg_match("/\/theme\/([0-9]+)$/", $localcachedir, $cachedrev); + $cachedrev = isset($cachedrev[1]) ? intval($cachedrev[1]) : 0; + if ($cachedrev > 0 && $cachedrev < $rev) { + fulldelete($localcachedir); + } + } + + // Delete older theme subrevision CSS from localcache. + $subrevfiles = glob("{$CFG->localcachedir}/theme/{$rev}/{$theme->name}/css/*.css"); + foreach ($subrevfiles as $subrevfile) { + $cachedsubrev = []; + preg_match("/_([0-9]+)\.([0-9]+\.)?css$/", $subrevfile, $cachedsubrev); + $cachedsubrev = isset($cachedsubrev[1]) ? intval($cachedsubrev[1]) : 0; + if ($cachedsubrev > 0 && $cachedsubrev < $themesubrev) { + fulldelete($subrevfile); + } + } + return $candidatesheet; }