| @@ -1,193 +1,572 @@ | ||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
| 2 | +/** | |
| 3 | + * Handles disk-cache-related operations. | |
| 4 | + */ | |
| 3 | 5 | |
| 4 | -class autoptimizeCache { | |
| 6 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 7 | + exit; | |
| 8 | +} | |
| 9 | + | |
| 10 | +class autoptimizeCache | |
| 11 | +{ | |
| 12 | + /** | |
| 13 | + * Cache filename. | |
| 14 | + * | |
| 15 | + * @var string | |
| 16 | + */ | |
| 5 | 17 | private $filename; |
| 6 | - private $mime; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Cache directory path (with a trailing slash). | |
| 21 | + * | |
| 22 | + * @var string | |
| 23 | + */ | |
| 7 | 24 | private $cachedir; |
| 8 | - private $delayed; | |
| 9 | - | |
| 10 | - public function __construct($md5,$ext='php') { | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Whether gzipping is done by the web server or us. | |
| 28 | + * True => we don't gzip, the web server does it. | |
| 29 | + * False => we do it ourselves. | |
| 30 | + * | |
| 31 | + * @var bool | |
| 32 | + */ | |
| 33 | + private $nogzip; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Ctor. | |
| 37 | + * | |
| 38 | + * @param string $md5 Hash. | |
| 39 | + * @param string $ext Extension. | |
| 40 | + */ | |
| 41 | + public function __construct( $md5, $ext = 'php' ) | |
| 42 | + { | |
| 43 | + $_min_ext = ''; | |
| 44 | + if ( apply_filters( 'autoptimize_filter_cache_url_add_min_ext', false ) ) { | |
| 45 | + $_min_ext = '.min'; | |
| 46 | + } | |
| 47 | + | |
| 11 | 48 | $this->cachedir = AUTOPTIMIZE_CACHE_DIR; |
| 12 | - $this->delayed = AUTOPTIMIZE_CACHE_DELAY; | |
| 13 | - $this->nogzip = AUTOPTIMIZE_CACHE_NOGZIP; | |
| 14 | - if($this->nogzip == false) { | |
| 15 | - $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.php'; | |
| 49 | + $this->nogzip = AUTOPTIMIZE_CACHE_NOGZIP; | |
| 50 | + if ( ! $this->nogzip ) { | |
| 51 | + $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.php'; | |
| 16 | 52 | } else { |
| 17 | - if (in_array($ext, array("js","css"))) { | |
| 18 | - $this->filename = $ext.'/'.AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.'.$ext; | |
| 53 | + if ( in_array( $ext, array( 'js', 'css' ) ) ) { | |
| 54 | + $this->filename = $ext . '/' . AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.' . $ext; | |
| 19 | 55 | } else { |
| 20 | - $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.'.$ext; | |
| 56 | + $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.' . $ext; | |
| 21 | 57 | } |
| 22 | 58 | } |
| 23 | 59 | } |
| 24 | - | |
| 25 | - public function check() { | |
| 26 | - if(!file_exists($this->cachedir.$this->filename)) { | |
| 27 | - // No cached file, sorry | |
| 28 | - return false; | |
| 29 | - } | |
| 30 | - // Cache exists! | |
| 31 | - return true; | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Returns true if the cached file exists on disk. | |
| 63 | + * | |
| 64 | + * @return bool | |
| 65 | + */ | |
| 66 | + public function check() | |
| 67 | + { | |
| 68 | + return file_exists( $this->cachedir . $this->filename ); | |
| 32 | 69 | } |
| 33 | - | |
| 34 | - public function retrieve() { | |
| 35 | - if($this->check()) { | |
| 36 | - if($this->nogzip == false) { | |
| 37 | - return file_get_contents($this->cachedir.$this->filename.'.none'); | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Returns cache contents if they exist, false otherwise. | |
| 73 | + * | |
| 74 | + * @return string|false | |
| 75 | + */ | |
| 76 | + public function retrieve() | |
| 77 | + { | |
| 78 | + if ( $this->check() ) { | |
| 79 | + if ( false == $this->nogzip ) { | |
| 80 | + return file_get_contents( $this->cachedir . $this->filename . '.none' ); | |
| 38 | 81 | } else { |
| 39 | - return file_get_contents($this->cachedir.$this->filename); | |
| 82 | + return file_get_contents( $this->cachedir . $this->filename ); | |
| 40 | 83 | } |
| 41 | 84 | } |
| 42 | 85 | return false; |
| 43 | 86 | } |
| 44 | - | |
| 45 | - public function cache($code,$mime) { | |
| 46 | - if($this->nogzip == false) { | |
| 47 | - $file = ($this->delayed ? 'delayed.php' : 'default.php'); | |
| 48 | - $phpcode = file_get_contents(AUTOPTIMIZE_PLUGIN_DIR.'/config/'.$file); | |
| 49 | - $phpcode = str_replace(array('%%CONTENT%%','exit;'),array($mime,''),$phpcode); | |
| 50 | - file_put_contents($this->cachedir.$this->filename,$phpcode, LOCK_EX); | |
| 51 | - file_put_contents($this->cachedir.$this->filename.'.none',$code, LOCK_EX); | |
| 52 | - if(!$this->delayed) { | |
| 53 | - // Compress now! | |
| 54 | - file_put_contents($this->cachedir.$this->filename.'.deflate',gzencode($code,9,FORCE_DEFLATE), LOCK_EX); | |
| 55 | - file_put_contents($this->cachedir.$this->filename.'.gzip',gzencode($code,9,FORCE_GZIP), LOCK_EX); | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Stores given $data in cache. | |
| 90 | + * | |
| 91 | + * @param string $data Data to cache. | |
| 92 | + * @param string $mime Mimetype. | |
| 93 | + * | |
| 94 | + * @return void | |
| 95 | + */ | |
| 96 | + public function cache( $data, $mime ) | |
| 97 | + { | |
| 98 | + // readonly FS explicitly OK'ed by developer, so just pretend all is OK. | |
| 99 | + if ( defined( 'AUTOPTIMIZE_CACHE_READONLY' ) ) { | |
| 100 | + return true; | |
| 101 | + } | |
| 102 | + | |
| 103 | + // off by default; check if cachedirs exist every time before caching | |
| 104 | + // | |
| 105 | + // to be activated for users that experience these ugly errors; | |
| 106 | + // PHP Warning: file_put_contents failed to open stream: No such file or directory. | |
| 107 | + if ( apply_filters( 'autoptimize_filter_cache_checkdirs_on_write', false ) ) { | |
| 108 | + $this->check_and_create_dirs(); | |
| 109 | + } | |
| 110 | + | |
| 111 | + if ( false === $this->nogzip ) { | |
| 112 | + // We handle gzipping ourselves. | |
| 113 | + $file = 'default.php'; | |
| 114 | + $phpcode = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $file ); | |
| 115 | + $phpcode = str_replace( array( '%%CONTENT%%', 'exit;' ), array( $mime, '' ), $phpcode ); | |
| 116 | + | |
| 117 | + file_put_contents( $this->cachedir . $this->filename, $phpcode ); | |
| 118 | + file_put_contents( $this->cachedir . $this->filename . '.none', $data ); | |
| 119 | + } else { | |
| 120 | + // Write code to cache without doing anything else. | |
| 121 | + file_put_contents( $this->cachedir . $this->filename, $data ); | |
| 122 | + | |
| 123 | + // save fallback .js or .css file if filter true (to be false by default) but not if snippet or single. | |
| 124 | + if ( self::do_fallback() && strpos( $this->filename, '_snippet_' ) === false && strpos( $this->filename, '_single_' ) === false ) { | |
| 125 | + $_extension = pathinfo( $this->filename, PATHINFO_EXTENSION ); | |
| 126 | + $_fallback_file = AUTOPTIMIZE_CACHEFILE_PREFIX . 'fallback.' . $_extension; | |
| 127 | + if ( ( 'css' === $_extension || 'js' === $_extension ) && ! file_exists( $this->cachedir . $_extension . '/' . $_fallback_file ) ) { | |
| 128 | + file_put_contents( $this->cachedir . $_extension . '/' . $_fallback_file, $data ); | |
| 129 | + } | |
| 56 | 130 | } |
| 57 | - } else { | |
| 58 | - // Write code to cache without doing anything else | |
| 59 | - file_put_contents($this->cachedir.$this->filename,$code, LOCK_EX); | |
| 60 | - if (apply_filters('autoptimize_filter_cache_create_static_gzip', false)) { | |
| 61 | - // Create an additional cached gzip file | |
| 62 | - file_put_contents($this->cachedir.$this->filename.'.gz', gzencode($code,9,FORCE_GZIP), LOCK_EX); | |
| 131 | + | |
| 132 | + if ( apply_filters( 'autoptimize_filter_cache_create_static_gzip', false ) ) { | |
| 133 | + // Create an additional cached gzip file. | |
| 134 | + file_put_contents( $this->cachedir . $this->filename . '.gz', gzencode( $data, 9, FORCE_GZIP ) ); | |
| 135 | + // If PHP Brotli extension is installed, create an additional cached Brotli file. | |
| 136 | + if ( function_exists( 'brotli_compress' ) ) { | |
| 137 | + file_put_contents( $this->cachedir . $this->filename . '.br', brotli_compress( $data, 11, BROTLI_GENERIC ) ); | |
| 138 | + } | |
| 63 | 139 | } |
| 64 | 140 | } |
| 141 | + | |
| 142 | + // Provide 3rd party action hook for every cache file that is created. | |
| 143 | + // This hook can for example be used to inject a copy of the created cache file to a other domain. | |
| 144 | + do_action( 'autoptimize_action_cache_file_created', $this->cachedir . $this->filename ); | |
| 65 | 145 | } |
| 66 | - | |
| 67 | - public function getname() { | |
| 68 | - apply_filters('autoptimize_filter_cache_getname',AUTOPTIMIZE_CACHE_URL.$this->filename); | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * Get cache filename. | |
| 149 | + * | |
| 150 | + * @return string | |
| 151 | + */ | |
| 152 | + public function getname() | |
| 153 | + { | |
| 154 | + // NOTE: This could've maybe been a do_action() instead, however, | |
| 155 | + // that ship has sailed. | |
| 156 | + // The original idea here was to provide 3rd party code a hook so that | |
| 157 | + // it can "listen" to all the complete autoptimized-urls that the page | |
| 158 | + // will emit... Or something to that effect I think? | |
| 159 | + apply_filters( 'autoptimize_filter_cache_getname', AUTOPTIMIZE_CACHE_URL . $this->filename ); | |
| 160 | + | |
| 69 | 161 | return $this->filename; |
| 70 | 162 | } |
| 71 | - | |
| 72 | - static function clearall() { | |
| 73 | - if(!autoptimizeCache::cacheavail()) { | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * Returns true if given `$file` is considered a valid Autoptimize cache file, | |
| 166 | + * false otherwise. | |
| 167 | + * | |
| 168 | + * @param string $dir Directory name (with a trailing slash). | |
| 169 | + * @param string $file Filename. | |
| 170 | + * @return bool | |
| 171 | + */ | |
| 172 | + protected static function is_valid_cache_file( $dir, $file ) | |
| 173 | + { | |
| 174 | + if ( '.' !== $file && '..' !== $file && | |
| 175 | + false !== strpos( $file, AUTOPTIMIZE_CACHEFILE_PREFIX ) && | |
| 176 | + is_file( $dir . $file ) ) { | |
| 177 | + | |
| 178 | + // It's a valid file! | |
| 179 | + return true; | |
| 180 | + } | |
| 181 | + | |
| 182 | + // Everything else is considered invalid! | |
| 183 | + return false; | |
| 184 | + } | |
| 185 | + | |
| 186 | + /** | |
| 187 | + * Clears contents of AUTOPTIMIZE_CACHE_DIR. | |
| 188 | + * | |
| 189 | + * @return void | |
| 190 | + */ | |
| 191 | + protected static function clear_cache_classic() | |
| 192 | + { | |
| 193 | + $contents = self::get_cache_contents(); | |
| 194 | + foreach ( $contents as $name => $files ) { | |
| 195 | + $dir = rtrim( AUTOPTIMIZE_CACHE_DIR . $name, '/' ) . '/'; | |
| 196 | + foreach ( $files as $file ) { | |
| 197 | + if ( self::is_valid_cache_file( $dir, $file ) ) { | |
| 198 | + @unlink( $dir . $file ); // @codingStandardsIgnoreLine | |
| 199 | + } | |
| 200 | + } | |
| 201 | + } | |
| 202 | + | |
| 203 | + @unlink( AUTOPTIMIZE_CACHE_DIR . '/.htaccess' ); // @codingStandardsIgnoreLine | |
| 204 | + } | |
| 205 | + | |
| 206 | + /** | |
| 207 | + * Recursively deletes the specified pathname (file/directory) if possible. | |
| 208 | + * Returns true on success, false otherwise. | |
| 209 | + * | |
| 210 | + * @param string $pathname Pathname to remove. | |
| 211 | + * | |
| 212 | + * @return bool | |
| 213 | + */ | |
| 214 | + protected static function rmdir( $pathname ) | |
| 215 | + { | |
| 216 | + $files = self::get_dir_contents( $pathname ); | |
| 217 | + foreach ( $files as $file ) { | |
| 218 | + $path = $pathname . '/' . $file; | |
| 219 | + if ( is_dir( $path ) ) { | |
| 220 | + self::rmdir( $path ); | |
| 221 | + } else { | |
| 222 | + unlink( $path ); | |
| 223 | + } | |
| 224 | + } | |
| 225 | + | |
| 226 | + return rmdir( $pathname ); | |
| 227 | + } | |
| 228 | + | |
| 229 | + /** | |
| 230 | + * Clears contents of AUTOPTIMIZE_CACHE_DIR by renaming the current | |
| 231 | + * cache directory into a new one with a unique name and then | |
| 232 | + * re-creating the default (empty) cache directory. | |
| 233 | + * | |
| 234 | + * Important/ Fixme: this does not take multisite into account, so | |
| 235 | + * if advanced_cache_clear_enabled is true (it is not by default) | |
| 236 | + * then the content for all subsites is zapped! | |
| 237 | + * | |
| 238 | + * @return bool Returns true when everything is done successfully, false otherwise. | |
| 239 | + */ | |
| 240 | + protected static function clear_cache_via_rename() | |
| 241 | + { | |
| 242 | + $ok = false; | |
| 243 | + $dir = self::get_pathname_base(); | |
| 244 | + $new_name = self::get_unique_name(); | |
| 245 | + | |
| 246 | + // Makes sure the new pathname is on the same level... | |
| 247 | + $new_pathname = dirname( $dir ) . '/' . $new_name; | |
| 248 | + $renamed = @rename( $dir, $new_pathname ); // @codingStandardsIgnoreLine | |
| 249 | + | |
| 250 | + // When renamed, re-create the default cache directory back so it's | |
| 251 | + // available again... | |
| 252 | + if ( $renamed ) { | |
| 253 | + $ok = self::cacheavail(); | |
| 254 | + } | |
| 255 | + | |
| 256 | + return $ok; | |
| 257 | + } | |
| 258 | + | |
| 259 | + /** | |
| 260 | + * Returns true when advanced cache clearing is enabled. | |
| 261 | + * | |
| 262 | + * @return bool | |
| 263 | + */ | |
| 264 | + public static function advanced_cache_clear_enabled() | |
| 265 | + { | |
| 266 | + return apply_filters( 'autoptimize_filter_cache_clear_advanced', false ); | |
| 267 | + } | |
| 268 | + | |
| 269 | + /** | |
| 270 | + * Returns a (hopefully) unique new cache folder name for renaming purposes. | |
| 271 | + * | |
| 272 | + * @return string | |
| 273 | + */ | |
| 274 | + protected static function get_unique_name() | |
| 275 | + { | |
| 276 | + $prefix = self::get_advanced_cache_clear_prefix(); | |
| 277 | + $new_name = uniqid( $prefix, true ); | |
| 278 | + | |
| 279 | + return $new_name; | |
| 280 | + } | |
| 281 | + | |
| 282 | + /** | |
| 283 | + * Get cache prefix name used in advanced cache clearing mode. | |
| 284 | + * | |
| 285 | + * @return string | |
| 286 | + */ | |
| 287 | + protected static function get_advanced_cache_clear_prefix() | |
| 288 | + { | |
| 289 | + $pathname = self::get_pathname_base(); | |
| 290 | + $basename = basename( $pathname ); | |
| 291 | + $prefix = $basename . '-artifact-'; | |
| 292 | + | |
| 293 | + return $prefix; | |
| 294 | + } | |
| 295 | + | |
| 296 | + /** | |
| 297 | + * Returns an array of file and directory names found within | |
| 298 | + * the given $pathname without '.' and '..' elements. | |
| 299 | + * | |
| 300 | + * @param string $pathname Pathname. | |
| 301 | + * | |
| 302 | + * @return array | |
| 303 | + */ | |
| 304 | + protected static function get_dir_contents( $pathname ) | |
| 305 | + { | |
| 306 | + return array_slice( scandir( $pathname ), 2 ); | |
| 307 | + } | |
| 308 | + | |
| 309 | + /** | |
| 310 | + * Wipes directories which were created as part of the fast cache clearing | |
| 311 | + * routine (which renames the current cache directory into a new one with | |
| 312 | + * a custom-prefixed unique name). | |
| 313 | + * | |
| 314 | + * @return bool | |
| 315 | + */ | |
| 316 | + public static function delete_advanced_cache_clear_artifacts() | |
| 317 | + { | |
| 318 | + // Don't go through these motions (called from the cachechecker) if advanced cache clear isn't even active. | |
| 319 | + if ( ! self::advanced_cache_clear_enabled() ) { | |
| 74 | 320 | return false; |
| 75 | 321 | } |
| 76 | - | |
| 77 | - // scan the cachedirs | |
| 78 | - foreach (array("","js","css") as $scandirName) { | |
| 79 | - $scan[$scandirName] = scandir(AUTOPTIMIZE_CACHE_DIR.$scandirName); | |
| 80 | - } | |
| 81 | - | |
| 82 | - // clear the cachedirs | |
| 83 | - foreach ($scan as $scandirName=>$scanneddir) { | |
| 84 | - $thisAoCacheDir=rtrim(AUTOPTIMIZE_CACHE_DIR.$scandirName,"/")."/"; | |
| 85 | - foreach($scanneddir as $file) { | |
| 86 | - if(!in_array($file,array('.','..')) && strpos($file,AUTOPTIMIZE_CACHEFILE_PREFIX) !== false && is_file($thisAoCacheDir.$file)) { | |
| 87 | - @unlink($thisAoCacheDir.$file); | |
| 322 | + | |
| 323 | + $dir = self::get_pathname_base(); | |
| 324 | + $prefix = self::get_advanced_cache_clear_prefix(); | |
| 325 | + $parent = dirname( $dir ); | |
| 326 | + $ok = false; | |
| 327 | + | |
| 328 | + // Returns the list of files without '.' and '..' elements. | |
| 329 | + $files = self::get_dir_contents( $parent ); | |
| 330 | + if ( is_array( $files ) && ! empty( $files ) ) { | |
| 331 | + foreach ( $files as $file ) { | |
| 332 | + $path = $parent . '/' . $file; | |
| 333 | + $prefixed = ( false !== strpos( $path, $prefix ) ); | |
| 334 | + // Removing only our own (prefixed) directories... | |
| 335 | + if ( is_dir( $path ) && $prefixed ) { | |
| 336 | + $ok = self::rmdir( $path ); | |
| 88 | 337 | } |
| 89 | 338 | } |
| 90 | 339 | } |
| 91 | 340 | |
| 92 | - @unlink(AUTOPTIMIZE_CACHE_DIR."/.htaccess"); | |
| 93 | - delete_transient("autoptimize_stats"); | |
| 341 | + return $ok; | |
| 342 | + } | |
| 94 | 343 | |
| 95 | - // add cachepurged action | |
| 96 | - if (!function_exists('autoptimize_do_cachepurged_action')) { | |
| 97 | - function autoptimize_do_cachepurged_action() { | |
| 98 | - do_action("autoptimize_action_cachepurged"); | |
| 344 | + /** | |
| 345 | + * Returns the cache directory pathname used. | |
| 346 | + * Done as a function so we canSlightly different | |
| 347 | + * if multisite is used and `autoptimize_separate_blog_caches` filter | |
| 348 | + * is used. | |
| 349 | + * | |
| 350 | + * @return string | |
| 351 | + */ | |
| 352 | + public static function get_pathname() | |
| 353 | + { | |
| 354 | + $pathname = self::get_pathname_base(); | |
| 355 | + | |
| 356 | + if ( is_multisite() && apply_filters( 'autoptimize_separate_blog_caches', true ) ) { | |
| 357 | + $blog_id = get_current_blog_id(); | |
| 358 | + $pathname .= $blog_id . '/'; | |
| 359 | + } | |
| 360 | + | |
| 361 | + return $pathname; | |
| 362 | + } | |
| 363 | + | |
| 364 | + /** | |
| 365 | + * Returns the base path of our cache directory. | |
| 366 | + * | |
| 367 | + * @return string | |
| 368 | + */ | |
| 369 | + protected static function get_pathname_base() | |
| 370 | + { | |
| 371 | + $pathname = WP_CONTENT_DIR . AUTOPTIMIZE_CACHE_CHILD_DIR; | |
| 372 | + | |
| 373 | + return $pathname; | |
| 374 | + } | |
| 375 | + | |
| 376 | + /** | |
| 377 | + * Deletes everything from the cache directories. | |
| 378 | + * | |
| 379 | + * @param bool $propagate Whether to trigger additional actions when cache is purged. | |
| 380 | + * | |
| 381 | + * @return bool | |
| 382 | + */ | |
| 383 | + public static function clearall( $propagate = true ) | |
| 384 | + { | |
| 385 | + if ( defined( 'ET_CORE_VERSION' ) && 'Divi' === get_template() ) { | |
| 386 | + // see https://blog.futtta.be/2018/11/17/warning-divi-purging-autoptimizes-cache/ . | |
| 387 | + $dbt = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 ); | |
| 388 | + $caller = isset( $dbt[1]['function'] ) ? $dbt[1]['function'] : null; | |
| 389 | + if ( $caller === 'et_core_clear_wp_cache' ) { | |
| 390 | + _doing_it_wrong( 'autoptimizeCache::clearall', 'Divi devs: please don\'t clear Autoptimize\'s cache, it is unneeded and can break sites. You can contact me at futtta@gmail.com to discuss.', 'Autoptimize 2.9.6' ); | |
| 391 | + return false; | |
| 99 | 392 | } |
| 100 | 393 | } |
| 101 | - add_action("shutdown","autoptimize_do_cachepurged_action",11); | |
| 102 | 394 | |
| 103 | - // try to purge caching plugins cache-files? | |
| 104 | - include_once(AUTOPTIMIZE_PLUGIN_DIR.'classlesses/autoptimizePageCacheFlush.php'); | |
| 105 | - add_action("autoptimize_action_cachepurged","autoptimize_flush_pagecache",10,0); | |
| 395 | + if ( ! self::cacheavail() ) { | |
| 396 | + return false; | |
| 397 | + } | |
| 106 | 398 | |
| 107 | - // warm cache (part of speedupper)? | |
| 108 | - if ( apply_filters('autoptimize_filter_speedupper', true) ) { | |
| 109 | - $warmCacheUrl = site_url()."/?ao_speedup_cachebuster=".rand(1,100000); | |
| 110 | - $warmCache = @wp_remote_get($warmCacheUrl); | |
| 111 | - unset($warmCache); | |
| 399 | + // TODO/FIXME: If cache is big, switch to advanced/new cache clearing automatically? | |
| 400 | + if ( self::advanced_cache_clear_enabled() ) { | |
| 401 | + self::clear_cache_via_rename(); | |
| 402 | + } else { | |
| 403 | + self::clear_cache_classic(); | |
| 112 | 404 | } |
| 113 | 405 | |
| 406 | + // Remove 404 handler if required. | |
| 407 | + if ( self::do_fallback() ) { | |
| 408 | + $_fallback_php = trailingslashit( WP_CONTENT_DIR ) . 'autoptimize_404_handler.php'; | |
| 409 | + @unlink( $_fallback_php ); // @codingStandardsIgnoreLine | |
| 410 | + } | |
| 411 | + | |
| 412 | + // Remove the transient so it gets regenerated... | |
| 413 | + delete_transient( 'autoptimize_stats' ); | |
| 414 | + | |
| 415 | + // Cache was just purged, clear page cache and allow others to hook into our purging... | |
| 416 | + if ( true === $propagate ) { | |
| 417 | + if ( ! function_exists( 'autoptimize_do_cachepurged_action' ) ) { | |
| 418 | + function autoptimize_do_cachepurged_action() { | |
| 419 | + do_action( 'autoptimize_action_cachepurged' ); | |
| 420 | + } | |
| 421 | + } | |
| 422 | + add_action( 'shutdown', 'autoptimize_do_cachepurged_action', 11 ); | |
| 423 | + add_action( 'autoptimize_action_cachepurged', array( 'autoptimizeCache', 'flushPageCache' ), 10, 0 ); | |
| 424 | + } | |
| 425 | + | |
| 426 | + // Warm cache (part of speedupper)! | |
| 427 | + if ( apply_filters( 'autoptimize_filter_speedupper', true ) && false == get_transient( 'autoptimize_cache_warmer_protector' ) ) { | |
| 428 | + set_transient( 'autoptimize_cache_warmer_protector', 'I shall not warm cache for another 10 minutes.', 60 * 10 ); | |
| 429 | + $url = site_url() . '/?ao_speedup_cachebuster=' . rand( 1, 100000 ); | |
| 430 | + $url = apply_filters( 'autoptimize_filter_cache_warmer_url', $url ); | |
| 431 | + $cache = @wp_remote_get( $url ); // @codingStandardsIgnoreLine | |
| 432 | + unset( $cache ); | |
| 433 | + } | |
| 434 | + | |
| 114 | 435 | return true; |
| 115 | 436 | } |
| 116 | 437 | |
| 117 | - static function stats() { | |
| 118 | - $AOstats=get_transient("autoptimize_stats"); | |
| 438 | + /** | |
| 439 | + * Wrapper for clearall but with false param | |
| 440 | + * to ensure the event is not propagated to others | |
| 441 | + * through our own hooks (to avoid infinite loops). | |
| 442 | + * | |
| 443 | + * @return bool | |
| 444 | + */ | |
| 445 | + public static function clearall_actionless() | |
| 446 | + { | |
| 447 | + return self::clearall( false ); | |
| 448 | + } | |
| 119 | 449 | |
| 120 | - if (empty($AOstats)) { | |
| 121 | - // Cache not available :( | |
| 122 | - if(!autoptimizeCache::cacheavail()) { | |
| 450 | + /** | |
| 451 | + * Returns the contents of our cache dirs. | |
| 452 | + * | |
| 453 | + * @return array | |
| 454 | + */ | |
| 455 | + protected static function get_cache_contents() | |
| 456 | + { | |
| 457 | + $contents = array(); | |
| 458 | + | |
| 459 | + foreach ( array( '', 'js', 'css' ) as $dir ) { | |
| 460 | + $contents[ $dir ] = scandir( AUTOPTIMIZE_CACHE_DIR . $dir ); | |
| 461 | + } | |
| 462 | + | |
| 463 | + return $contents; | |
| 464 | + } | |
| 465 | + | |
| 466 | + /** | |
| 467 | + * Returns stats about cached contents. | |
| 468 | + * | |
| 469 | + * @return array | |
| 470 | + */ | |
| 471 | + public static function stats() | |
| 472 | + { | |
| 473 | + $stats = get_transient( 'autoptimize_stats' ); | |
| 474 | + | |
| 475 | + // If no transient, do the actual scan! | |
| 476 | + if ( ! is_array( $stats ) ) { | |
| 477 | + if ( ! self::cacheavail() ) { | |
| 123 | 478 | return 0; |
| 124 | 479 | } |
| 125 | - | |
| 126 | - // Count cached info | |
| 127 | - $count = 0; | |
| 128 | - $size = 0; | |
| 129 | - | |
| 130 | - // scan the cachedirs | |
| 131 | - foreach (array("","js","css") as $scandirName) { | |
| 132 | - $scan[$scandirName] = scandir(AUTOPTIMIZE_CACHE_DIR.$scandirName); | |
| 480 | + $stats = self::stats_scan(); | |
| 481 | + $count = $stats[0]; | |
| 482 | + if ( $count > 100 ) { | |
| 483 | + // Store results in transient. | |
| 484 | + set_transient( | |
| 485 | + 'autoptimize_stats', | |
| 486 | + $stats, | |
| 487 | + apply_filters( 'autoptimize_filter_cache_statsexpiry', HOUR_IN_SECONDS ) | |
| 488 | + ); | |
| 133 | 489 | } |
| 134 | - | |
| 135 | - foreach ($scan as $scandirName=>$scanneddir) { | |
| 136 | - $thisAoCacheDir=rtrim(AUTOPTIMIZE_CACHE_DIR.$scandirName,"/")."/"; | |
| 137 | - foreach($scanneddir as $file) { | |
| 138 | - if(!in_array($file,array('.','..')) && strpos($file,AUTOPTIMIZE_CACHEFILE_PREFIX) !== false) { | |
| 139 | - if(is_file($thisAoCacheDir.$file)) { | |
| 140 | - if(AUTOPTIMIZE_CACHE_NOGZIP && (strpos($file,'.js') !== false || strpos($file,'.css') !== false || strpos($file,'.img') !== false || strpos($file,'.txt') !== false )) { | |
| 141 | - $count++; | |
| 142 | - } elseif(!AUTOPTIMIZE_CACHE_NOGZIP && strpos($file,'.none') !== false) { | |
| 143 | - $count++; | |
| 144 | - } | |
| 145 | - $size+=filesize($thisAoCacheDir.$file); | |
| 146 | - } | |
| 490 | + } | |
| 491 | + | |
| 492 | + return $stats; | |
| 493 | + } | |
| 494 | + | |
| 495 | + /** | |
| 496 | + * Performs a scan of cache directory contents and returns an array | |
| 497 | + * with 3 values: count, size, timestamp. | |
| 498 | + * count = total number of found files | |
| 499 | + * size = total filesize (in bytes) of found files | |
| 500 | + * timestamp = unix timestamp when the scan was last performed/finished. | |
| 501 | + * | |
| 502 | + * @return array | |
| 503 | + */ | |
| 504 | + protected static function stats_scan() | |
| 505 | + { | |
| 506 | + $count = 0; | |
| 507 | + $size = 0; | |
| 508 | + | |
| 509 | + // Scan everything in our cache directories. | |
| 510 | + foreach ( self::get_cache_contents() as $name => $files ) { | |
| 511 | + $dir = rtrim( AUTOPTIMIZE_CACHE_DIR . $name, '/' ) . '/'; | |
| 512 | + foreach ( $files as $file ) { | |
| 513 | + if ( self::is_valid_cache_file( $dir, $file ) ) { | |
| 514 | + if ( AUTOPTIMIZE_CACHE_NOGZIP && | |
| 515 | + ( | |
| 516 | + false !== strpos( $file, '.js' ) || | |
| 517 | + false !== strpos( $file, '.css' ) || | |
| 518 | + false !== strpos( $file, '.img' ) || | |
| 519 | + false !== strpos( $file, '.txt' ) | |
| 520 | + ) | |
| 521 | + ) { | |
| 522 | + // Web server is gzipping, we count .js|.css|.img|.txt files. | |
| 523 | + $count++; | |
| 524 | + } elseif ( ! AUTOPTIMIZE_CACHE_NOGZIP && false !== strpos( $file, '.none' ) ) { | |
| 525 | + // We are gzipping ourselves via php, counting only .none files. | |
| 526 | + $count++; | |
| 147 | 527 | } |
| 528 | + $size += filesize( $dir . $file ); | |
| 148 | 529 | } |
| 149 | 530 | } |
| 150 | - $AOstats=array($count,$size,time()); | |
| 151 | - if ($count>100) { | |
| 152 | - set_transient( "autoptimize_stats", $AOstats, apply_filters( 'autoptimize_filter_cache_statsexpiry', HOUR_IN_SECONDS ) ); | |
| 153 | - } | |
| 154 | 531 | } |
| 155 | - // print the number of instances | |
| 156 | - return $AOstats; | |
| 157 | - } | |
| 158 | 532 | |
| 159 | - static function cacheavail() { | |
| 160 | - if(!defined('AUTOPTIMIZE_CACHE_DIR')) { | |
| 161 | - // We didn't set a cache | |
| 533 | + $stats = array( $count, $size, time() ); | |
| 534 | + | |
| 535 | + return $stats; | |
| 536 | + } | |
| 537 | + | |
| 538 | + /** | |
| 539 | + * Ensures the cache directory exists, is writeable and contains the | |
| 540 | + * required .htaccess files. | |
| 541 | + * Returns false in case it fails to ensure any of those things. | |
| 542 | + * | |
| 543 | + * @return bool | |
| 544 | + */ | |
| 545 | + public static function cacheavail() | |
| 546 | + { | |
| 547 | + // readonly FS explicitly OK'ed by dev, let's assume the cache dirs are there! | |
| 548 | + if ( defined( 'AUTOPTIMIZE_CACHE_READONLY' ) ) { | |
| 549 | + return true; | |
| 550 | + } | |
| 551 | + | |
| 552 | + if ( false === autoptimizeCache::check_and_create_dirs() ) { | |
| 162 | 553 | return false; |
| 163 | 554 | } |
| 164 | - | |
| 165 | - foreach (array("","js","css") as $checkDir) { | |
| 166 | - if(!autoptimizeCache::checkCacheDir(AUTOPTIMIZE_CACHE_DIR.$checkDir)) { | |
| 167 | - return false; | |
| 168 | - } | |
| 169 | - } | |
| 170 | - | |
| 171 | - /** write index.html here to avoid prying eyes */ | |
| 172 | - $indexFile=AUTOPTIMIZE_CACHE_DIR.'/index.html'; | |
| 173 | - if(!is_file($indexFile)) { | |
| 174 | - @file_put_contents($indexFile,'<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>'); | |
| 175 | - } | |
| 176 | 555 | |
| 177 | - /** write .htaccess here to overrule wp_super_cache */ | |
| 178 | - $htAccess=AUTOPTIMIZE_CACHE_DIR.'/.htaccess'; | |
| 179 | - if(!is_file($htAccess)) { | |
| 180 | - /** | |
| 181 | - * create wp-content/AO_htaccess_tmpl with | |
| 556 | + // Using .htaccess inside our cache folder to overrule wp-super-cache. | |
| 557 | + $htaccess = AUTOPTIMIZE_CACHE_DIR . '/.htaccess'; | |
| 558 | + if ( ! is_file( $htaccess ) ) { | |
| 559 | + /** | |
| 560 | + * Create `wp-content/AO_htaccess_tmpl` file with | |
| 182 | 561 | * whatever htaccess rules you might need |
| 183 | 562 | * if you want to override default AO htaccess |
| 184 | 563 | */ |
| 185 | - $htaccess_tmpl=WP_CONTENT_DIR."/AO_htaccess_tmpl"; | |
| 186 | - if (is_file($htaccess_tmpl)) { | |
| 187 | - $htAccessContent=file_get_contents($htaccess_tmpl); | |
| 188 | - } else if (is_multisite() || AUTOPTIMIZE_CACHE_NOGZIP == false) { | |
| 189 | - $htAccessContent='<IfModule mod_expires.c> | |
| 564 | + $htaccess_tmpl = WP_CONTENT_DIR . '/AO_htaccess_tmpl'; | |
| 565 | + if ( is_file( $htaccess_tmpl ) ) { | |
| 566 | + $content = file_get_contents( $htaccess_tmpl ); | |
| 567 | + } elseif ( is_multisite() || ! AUTOPTIMIZE_CACHE_NOGZIP ) { | |
| 568 | + $content = '<IfModule mod_expires.c> | |
| 190 | 569 | ExpiresActive On |
| 191 | 570 | ExpiresByType text/css A30672000 |
| 192 | 571 | ExpiresByType text/javascript A30672000 |
| 193 | 572 | ExpiresByType application/javascript A30672000 |
| @@ -211,9 +590,9 @@ | ||
| 211 | 590 | Allow from all |
| 212 | 591 | </Files> |
| 213 | 592 | </IfModule>'; |
| 214 | 593 | } else { |
| 215 | - $htAccessContent='<IfModule mod_expires.c> | |
| 594 | + $content = '<IfModule mod_expires.c> | |
| 216 | 595 | ExpiresActive On |
| 217 | 596 | ExpiresByType text/css A30672000 |
| 218 | 597 | ExpiresByType text/javascript A30672000 |
| 219 | 598 | ExpiresByType application/javascript A30672000 |
| @@ -237,34 +616,238 @@ | ||
| 237 | 616 | Deny from all |
| 238 | 617 | </Files> |
| 239 | 618 | </IfModule>'; |
| 240 | 619 | } |
| 241 | - @file_put_contents($htAccess,$htAccessContent); | |
| 620 | + | |
| 621 | + if ( self::do_fallback() === true ) { | |
| 622 | + $content .= "\nErrorDocument 404 " . trailingslashit( parse_url( content_url(), PHP_URL_PATH ) ) . 'autoptimize_404_handler.php'; | |
| 623 | + } | |
| 624 | + @file_put_contents( $htaccess, $content ); // @codingStandardsIgnoreLine | |
| 242 | 625 | } |
| 243 | 626 | |
| 244 | - // All OK | |
| 627 | + if ( self::do_fallback() ) { | |
| 628 | + self::check_fallback_php(); | |
| 629 | + } | |
| 630 | + | |
| 631 | + // All OK! | |
| 245 | 632 | return true; |
| 246 | 633 | } |
| 247 | 634 | |
| 248 | - static function checkCacheDir($dir) { | |
| 249 | - // Check and create if not exists | |
| 250 | - if(!file_exists($dir)) { | |
| 251 | - @mkdir($dir,0775,true); | |
| 252 | - if(!file_exists($dir)) { | |
| 635 | + /** | |
| 636 | + * Checks if fallback-php file exists and create it if not. | |
| 637 | + * | |
| 638 | + * Return bool | |
| 639 | + */ | |
| 640 | + public static function check_fallback_php() { | |
| 641 | + $_fallback_filename = 'autoptimize_404_handler.php'; | |
| 642 | + $_fallback_php = trailingslashit( WP_CONTENT_DIR ) . $_fallback_filename; | |
| 643 | + $_fallback_status = true; | |
| 644 | + | |
| 645 | + if ( ! file_exists( $_fallback_php ) && is_writable( WP_CONTENT_DIR ) ) { | |
| 646 | + $_fallback_php_contents = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $_fallback_filename ); | |
| 647 | + $_fallback_php_contents = str_replace( '<?php exit;', '<?php', $_fallback_php_contents ); | |
| 648 | + $_fallback_php_contents = str_replace( '<!--ao-cache-dir-->', AUTOPTIMIZE_CACHE_DIR, $_fallback_php_contents ); | |
| 649 | + $_fallback_php_contents = str_replace( '<!--ao-cachefile-prefix-->', AUTOPTIMIZE_CACHEFILE_PREFIX, $_fallback_php_contents ); | |
| 650 | + if ( is_multisite() ) { | |
| 651 | + $_fallback_php_contents = str_replace( '$multisite = false;', '$multisite = true;', $_fallback_php_contents ); | |
| 652 | + } | |
| 653 | + if ( apply_filters( 'autoptimize_filter_cache_fallback_log_errors', false ) ) { | |
| 654 | + $_fallback_php_contents = str_replace( '// error_log', 'error_log', $_fallback_php_contents ); | |
| 655 | + } | |
| 656 | + $_fallback_status = file_put_contents( $_fallback_php, $_fallback_php_contents ); | |
| 657 | + } | |
| 658 | + | |
| 659 | + return $_fallback_status; | |
| 660 | + } | |
| 661 | + | |
| 662 | + /** | |
| 663 | + * Tells if AO should try to avoid 404's by creating fallback filesize | |
| 664 | + * and create a php 404 handler and tell .htaccess to redirect to said handler | |
| 665 | + * and hook into WordPress to redirect 404 to said handler as well. NGINX users | |
| 666 | + * are smart enough to get this working, no? ;-) | |
| 667 | + * | |
| 668 | + * Return bool | |
| 669 | + */ | |
| 670 | + public static function do_fallback() { | |
| 671 | + static $_do_fallback = null; | |
| 672 | + | |
| 673 | + if ( null === $_do_fallback ) { | |
| 674 | + $_do_fallback = (bool) apply_filters( 'autoptimize_filter_cache_do_fallback', autoptimizeOptionWrapper::get_option( 'autoptimize_cache_fallback', '1' ) ); | |
| 675 | + } | |
| 676 | + | |
| 677 | + return $_do_fallback; | |
| 678 | + } | |
| 679 | + | |
| 680 | + /** | |
| 681 | + * Hooks into template_redirect, will act on 404-ing requests for | |
| 682 | + * Autoptimized files and redirects to the fallback CSS/ JS if available | |
| 683 | + * and 410'ing ("Gone") if fallback not available. | |
| 684 | + */ | |
| 685 | + public static function wordpress_notfound_fallback() { | |
| 686 | + $original_request = strtok( $_SERVER['REQUEST_URI'], '?' ); | |
| 687 | + if ( strpos( $original_request, wp_basename( WP_CONTENT_DIR ) . AUTOPTIMIZE_CACHE_CHILD_DIR ) !== false && is_404() ) { | |
| 688 | + // make sure this is not considered a 404. | |
| 689 | + global $wp_query; | |
| 690 | + $wp_query->is_404 = false; | |
| 691 | + | |
| 692 | + // set fallback path. | |
| 693 | + $js_or_css = pathinfo( $original_request, PATHINFO_EXTENSION ); | |
| 694 | + $fallback_path = AUTOPTIMIZE_CACHE_DIR . $js_or_css . '/autoptimize_fallback.' . $js_or_css; | |
| 695 | + | |
| 696 | + // prepare for Shakeeb's Unused CSS files to be 404-handled as well. | |
| 697 | + if ( strpos( $original_request, 'uucss/uucss-' ) !== false ) { | |
| 698 | + $original_request = preg_replace( '/uucss\/uucss-[a-z0-9]{32}-/', 'css/', $original_request ); | |
| 699 | + } | |
| 700 | + | |
| 701 | + // set fallback URL. | |
| 702 | + $fallback_target = preg_replace( '/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request ); | |
| 703 | + | |
| 704 | + // redirect to fallback if possible. | |
| 705 | + if ( $original_request !== $fallback_target && file_exists( $fallback_path ) ) { | |
| 706 | + // redirect to fallback. | |
| 707 | + wp_redirect( $fallback_target, 302 ); | |
| 708 | + } else { | |
| 709 | + // return HTTP 410 (gone) reponse. | |
| 710 | + status_header( 410 ); | |
| 711 | + } | |
| 712 | + } | |
| 713 | + } | |
| 714 | + | |
| 715 | + /** | |
| 716 | + * Checks if cache dirs exist and create if not. | |
| 717 | + * Returns false if not succesful. | |
| 718 | + * | |
| 719 | + * @return bool | |
| 720 | + */ | |
| 721 | + public static function check_and_create_dirs() { | |
| 722 | + if ( ! defined( 'AUTOPTIMIZE_CACHE_DIR' ) ) { | |
| 723 | + // We didn't set a cache. | |
| 724 | + return false; | |
| 725 | + } | |
| 726 | + | |
| 727 | + foreach ( array( '', 'js', 'css' ) as $dir ) { | |
| 728 | + if ( ! self::check_cache_dir( AUTOPTIMIZE_CACHE_DIR . $dir ) ) { | |
| 253 | 729 | return false; |
| 254 | 730 | } |
| 255 | 731 | } |
| 732 | + return true; | |
| 733 | + } | |
| 256 | 734 | |
| 257 | - // check if we can now write | |
| 258 | - if(!is_writable($dir)) { | |
| 735 | + /** | |
| 736 | + * Ensures the specified `$dir` exists and is writeable. | |
| 737 | + * Returns false if that's not the case. | |
| 738 | + * | |
| 739 | + * @param string $dir Directory to check/create. | |
| 740 | + * | |
| 741 | + * @return bool | |
| 742 | + */ | |
| 743 | + protected static function check_cache_dir( $dir ) | |
| 744 | + { | |
| 745 | + // Try creating the dir if it doesn't exist. | |
| 746 | + if ( ! file_exists( $dir ) ) { | |
| 747 | + @mkdir( $dir, 0775, true ); // @codingStandardsIgnoreLine | |
| 748 | + if ( ! file_exists( $dir ) ) { | |
| 749 | + return false; | |
| 750 | + } | |
| 751 | + } | |
| 752 | + | |
| 753 | + // If we still cannot write, bail. | |
| 754 | + if ( ! is_writable( $dir ) ) { | |
| 259 | 755 | return false; |
| 260 | 756 | } |
| 261 | 757 | |
| 262 | - // and write index.html here to avoid prying eyes | |
| 263 | - $indexFile=$dir.'/index.html'; | |
| 264 | - if(!is_file($indexFile)) { | |
| 265 | - @file_put_contents($indexFile,'<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>'); | |
| 758 | + // Create an index.html in there to avoid prying eyes! | |
| 759 | + $idx_file = rtrim( $dir, '/\\' ) . '/index.html'; | |
| 760 | + if ( ! is_file( $idx_file ) ) { | |
| 761 | + @file_put_contents( $idx_file, '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>' ); // @codingStandardsIgnoreLine | |
| 266 | 762 | } |
| 267 | - | |
| 763 | + | |
| 268 | 764 | return true; |
| 269 | 765 | } |
| 766 | + | |
| 767 | + /** | |
| 768 | + * Flushes as many page cache plugin's caches as possible. | |
| 769 | + * | |
| 770 | + * @return void | |
| 771 | + */ | |
| 772 | + // @codingStandardsIgnoreStart | |
| 773 | + public static function flushPageCache() | |
| 774 | + { | |
| 775 | + if ( function_exists( 'wp_cache_clear_cache' ) ) { | |
| 776 | + if ( is_multisite() ) { | |
| 777 | + $blog_id = get_current_blog_id(); | |
| 778 | + wp_cache_clear_cache( $blog_id ); | |
| 779 | + } else { | |
| 780 | + wp_cache_clear_cache(); | |
| 781 | + } | |
| 782 | + } elseif ( has_action( 'cachify_flush_cache' ) ) { | |
| 783 | + do_action( 'cachify_flush_cache' ); | |
| 784 | + } elseif ( function_exists( 'w3tc_pgcache_flush' ) ) { | |
| 785 | + w3tc_pgcache_flush(); | |
| 786 | + } elseif ( function_exists( 'wp_fast_cache_bulk_delete_all' ) ) { | |
| 787 | + wp_fast_cache_bulk_delete_all(); | |
| 788 | + } elseif ( function_exists( 'rapidcache_clear_cache' ) ) { | |
| 789 | + rapidcache_clear_cache(); | |
| 790 | + } elseif ( class_exists( 'Swift_Performance_Cache' ) ) { | |
| 791 | + Swift_Performance_Cache::clear_all_cache(); | |
| 792 | + } elseif ( class_exists( 'WpFastestCache' ) ) { | |
| 793 | + $wpfc = new WpFastestCache(); | |
| 794 | + $wpfc->deleteCache(); | |
| 795 | + } elseif ( class_exists( 'c_ws_plugin__qcache_purging_routines' ) ) { | |
| 796 | + c_ws_plugin__qcache_purging_routines::purge_cache_dir(); // quick cache | |
| 797 | + } elseif ( class_exists( 'zencache' ) ) { | |
| 798 | + zencache::clear(); | |
| 799 | + } elseif ( class_exists( 'comet_cache' ) ) { | |
| 800 | + comet_cache::clear(); | |
| 801 | + } elseif ( class_exists( 'WpeCommon' ) ) { | |
| 802 | + // WPEngine cache purge/flush methods to call by default | |
| 803 | + $wpe_methods = array( | |
| 804 | + 'purge_varnish_cache', | |
| 805 | + ); | |
| 806 | + | |
| 807 | + // More agressive clear/flush/purge behind a filter | |
| 808 | + if ( apply_filters( 'autoptimize_flush_wpengine_aggressive', false ) ) { | |
| 809 | + $wpe_methods = array_merge( $wpe_methods, array( 'purge_memcached', 'clear_maxcdn_cache' ) ); | |
| 810 | + } | |
| 811 | + | |
| 812 | + // Filtering the entire list of WpeCommon methods to be called (for advanced usage + easier testing) | |
| 813 | + $wpe_methods = apply_filters( 'autoptimize_flush_wpengine_methods', $wpe_methods ); | |
| 814 | + | |
| 815 | + foreach ( $wpe_methods as $wpe_method ) { | |
| 816 | + if ( method_exists( 'WpeCommon', $wpe_method ) ) { | |
| 817 | + WpeCommon::$wpe_method(); | |
| 818 | + } | |
| 819 | + } | |
| 820 | + } elseif ( function_exists( 'sg_cachepress_purge_cache' ) ) { | |
| 821 | + sg_cachepress_purge_cache(); | |
| 822 | + } elseif ( array_key_exists( 'KINSTA_CACHE_ZONE', $_SERVER ) ) { | |
| 823 | + $_kinsta_clear_cache_url = 'https://localhost/kinsta-clear-cache-all'; | |
| 824 | + $_kinsta_response = wp_remote_get( | |
| 825 | + $_kinsta_clear_cache_url, | |
| 826 | + array( | |
| 827 | + 'sslverify' => false, | |
| 828 | + 'timeout' => 5, | |
| 829 | + ) | |
| 830 | + ); | |
| 831 | + } elseif ( class_exists( 'RaidboxesNginxCacheFunctions' ) ) { | |
| 832 | + $rb_cache_helper = new RaidboxesNginxCacheFunctions(); | |
| 833 | + $rb_cache_helper->purge_cache(); | |
| 834 | + } elseif ( defined('NGINX_HELPER_BASENAME') ) { | |
| 835 | + do_action( 'rt_nginx_helper_purge_all' ); | |
| 836 | + } elseif ( file_exists( WP_CONTENT_DIR . '/wp-cache-config.php' ) && function_exists( 'prune_super_cache' ) ) { | |
| 837 | + // fallback for WP-Super-Cache | |
| 838 | + global $cache_path; | |
| 839 | + if ( is_multisite() ) { | |
| 840 | + $blog_id = get_current_blog_id(); | |
| 841 | + prune_super_cache( get_supercache_dir( $blog_id ), true ); | |
| 842 | + prune_super_cache( $cache_path . 'blogs/', true ); | |
| 843 | + } else { | |
| 844 | + prune_super_cache( $cache_path . 'supercache/', true ); | |
| 845 | + prune_super_cache( $cache_path, true ); | |
| 846 | + } | |
| 847 | + } elseif ( class_exists( 'NginxCache' ) ) { | |
| 848 | + $nginx_cache = new NginxCache(); | |
| 849 | + $nginx_cache->purge_zone_once(); | |
| 850 | + } | |
| 851 | + } | |
| 852 | + // @codingStandardsIgnoreEnd | |
| 270 | 853 | } |