← All changes
|
minify/class-wp-optimize-minify-cache-functions.php
+165
-66
3.2.5
→
4.7.0
View file →
| @@ -13,15 +13,16 @@ | ||
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * Fix the permission bits on generated files |
| 16 | 16 | * |
| 17 | - * @param String $file - full path to a file | |
| 17 | + * @param string $file - full path to a file | |
| 18 | + * @return bool | |
| 18 | 19 | */ |
| 19 | 20 | public static function fix_permission_bits($file) { |
| 20 | 21 | if (function_exists('stat')) { |
| 21 | 22 | if ($stat = stat(dirname($file))) { |
| 22 | 23 | $perms = $stat['mode'] & 0007777; |
| 23 | - chmod($file, $perms); | |
| 24 | + chmod($file, $perms); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_chmod -- N/A | |
| 24 | 25 | clearstatcache(); |
| 25 | 26 | return true; |
| 26 | 27 | } |
| 27 | 28 | } |
| @@ -34,12 +35,12 @@ | ||
| 34 | 35 | } |
| 35 | 36 | } |
| 36 | 37 | |
| 37 | 38 | if (file_exists($file)) { |
| 38 | - if (($perms & ~umask() != $perms)) { | |
| 39 | + if (($perms & ~umask() !== $perms)) { | |
| 39 | 40 | $folder_parts = explode('/', substr($file, strlen(dirname($file)) + 1)); |
| 40 | 41 | for ($i = 1, $c = count($folder_parts); $i <= $c; $i++) { |
| 41 | - chmod(dirname($file) . '/' . implode('/', array_slice($folder_parts, 0, $i)), $perms); | |
| 42 | + chmod(dirname($file) . '/' . implode('/', array_slice($folder_parts, 0, $i)), $perms); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_chmod -- N/A | |
| 42 | 43 | } |
| 43 | 44 | } |
| 44 | 45 | } |
| 45 | 46 | return true; |
| @@ -47,14 +48,15 @@ | ||
| 47 | 48 | |
| 48 | 49 | /** |
| 49 | 50 | * Get cache directories and urls |
| 50 | 51 | * |
| 51 | - * @return Array | |
| 52 | + * @return array | |
| 52 | 53 | */ |
| 53 | 54 | public static function cache_path() { |
| 54 | 55 | // get latest time stamp |
| 55 | 56 | $cache_time = wp_optimize_minify_config()->get('last-cache-update'); |
| 56 | 57 | |
| 58 | + $cache_base_dir = WPO_CACHE_MIN_FILES_DIR . "/$cache_time"; | |
| 57 | 59 | $cache_dir_url = WPO_CACHE_MIN_FILES_URL . "/$cache_time/assets"; |
| 58 | 60 | $tmp_dir = WPO_CACHE_MIN_FILES_DIR . "/tmp"; |
| 59 | 61 | $header_dir = WPO_CACHE_MIN_FILES_DIR . "/$cache_time/header"; |
| 60 | 62 | $cache_dir = WPO_CACHE_MIN_FILES_DIR . "/$cache_time/assets"; |
| @@ -61,13 +63,13 @@ | ||
| 61 | 63 | |
| 62 | 64 | // Create directories |
| 63 | 65 | $dirs = array($cache_dir, $tmp_dir, $header_dir); |
| 64 | 66 | foreach ($dirs as $target) { |
| 65 | - $enabled = wp_optimize_minify_config()->get('enabled'); | |
| 67 | + $enabled = (bool) wp_optimize_minify_config()->get('enabled'); | |
| 66 | 68 | if (false === $enabled) break; |
| 67 | 69 | |
| 68 | 70 | if (!is_dir($target) && !wp_mkdir_p($target)) { |
| 69 | - error_log('WP_Optimize_Minify_Cache_Functions::cache_path(): The folder "'.$target.'" could not be created.'); | |
| 71 | + error_log('WP_Optimize_Minify_Cache_Functions::cache_path(): The folder "'.$target.'" could not be created.'); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Used for debugging | |
| 70 | 72 | } |
| 71 | 73 | } |
| 72 | 74 | return array( |
| 73 | 75 | 'tmpdir' => $tmp_dir, |
| @@ -72,14 +74,17 @@ | ||
| 72 | 74 | return array( |
| 73 | 75 | 'tmpdir' => $tmp_dir, |
| 74 | 76 | 'cachedir' => $cache_dir, |
| 75 | 77 | 'cachedirurl' => $cache_dir_url, |
| 76 | - 'headerdir' => $header_dir | |
| 78 | + 'headerdir' => $header_dir, | |
| 79 | + 'cachebasedir' => $cache_base_dir | |
| 77 | 80 | ); |
| 78 | 81 | } |
| 79 | 82 | |
| 80 | 83 | /** |
| 81 | 84 | * Increment file names |
| 85 | + * | |
| 86 | + * @return int | |
| 82 | 87 | */ |
| 83 | 88 | public static function cache_increment() { |
| 84 | 89 | $stamp = time(); |
| 85 | 90 | wp_optimize_minify_config()->update(array( |
| @@ -98,9 +103,9 @@ | ||
| 98 | 103 | |
| 99 | 104 | /** |
| 100 | 105 | * Will delete temporary intermediate stuff but leave final css/js alone for compatibility |
| 101 | 106 | * |
| 102 | - * @return Array | |
| 107 | + * @return array | |
| 103 | 108 | */ |
| 104 | 109 | public static function purge_temp_files() { |
| 105 | 110 | // get cache directories and urls |
| 106 | 111 | $cache_path = self::cache_path(); |
| @@ -108,12 +113,12 @@ | ||
| 108 | 113 | $header_dir = $cache_path['headerdir']; |
| 109 | 114 | |
| 110 | 115 | // delete temporary directories only |
| 111 | 116 | if (is_dir($tmp_dir)) { |
| 112 | - wpo_delete_files($tmp_dir, true); | |
| 117 | + wpo_delete_files($tmp_dir); | |
| 113 | 118 | } |
| 114 | 119 | if (is_dir($header_dir)) { |
| 115 | - wpo_delete_files($header_dir, true); | |
| 120 | + wpo_delete_files($header_dir); | |
| 116 | 121 | } |
| 117 | 122 | |
| 118 | 123 | /** |
| 119 | 124 | * Action triggered after purging temporary files |
| @@ -127,9 +132,9 @@ | ||
| 127 | 132 | |
| 128 | 133 | /** |
| 129 | 134 | * Purge supported hosting and plugins |
| 130 | 135 | * |
| 131 | - * @return string | |
| 136 | + * @return array An array of caches purged message | |
| 132 | 137 | */ |
| 133 | 138 | public static function purge_others() { |
| 134 | 139 | /** |
| 135 | 140 | * Action triggered before purging other plugins cache |
| @@ -141,10 +146,14 @@ | ||
| 141 | 146 | wp_cache_flush(); |
| 142 | 147 | } |
| 143 | 148 | |
| 144 | 149 | // Purge WP-Optimize |
| 145 | - WP_Optimize()->get_page_cache()->purge(); | |
| 150 | + $is_cache_purged = WP_Optimize()->get_page_cache()->purge(); | |
| 151 | + if ($is_cache_purged) WP_Optimize()->get_page_cache()->file_log("Full Cache Purge triggered by: ". __METHOD__); | |
| 146 | 152 | |
| 153 | + // Store the messages of purged cache if it was successful | |
| 154 | + $result = array(); | |
| 155 | + | |
| 147 | 156 | // When plugins have a simple method, add them to the array ('Plugin Name' => 'method_name') |
| 148 | 157 | $others = array( |
| 149 | 158 | 'WP Super Cache' => 'wp_cache_clear_cache', |
| 150 | 159 | 'W3 Total Cache' => 'w3tc_pgcache_flush', |
| @@ -161,9 +170,10 @@ | ||
| 161 | 170 | |
| 162 | 171 | foreach ($others as $plugin => $method) { |
| 163 | 172 | if (is_callable($method)) { |
| 164 | 173 | call_user_func($method); |
| 165 | - return sprintf(__('All caches from %s have also been purged.', 'wp-optimize'), '<strong>'.$plugin.'</strong>'); | |
| 174 | + | |
| 175 | + $result[] = self::get_caches_purged_message($plugin); | |
| 166 | 176 | } |
| 167 | 177 | } |
| 168 | 178 | |
| 169 | 179 | // Purge LiteSpeed Cache |
| @@ -168,27 +178,31 @@ | ||
| 168 | 178 | |
| 169 | 179 | // Purge LiteSpeed Cache |
| 170 | 180 | if (is_callable(array('LiteSpeed_Cache_Tags', 'add_purge_tag'))) { |
| 171 | 181 | LiteSpeed_Cache_Tags::add_purge_tag('*'); |
| 172 | - return sprintf(__('All caches from %s have also been purged.', 'wp-optimize'), '<strong>LiteSpeed Cache</strong>'); | |
| 182 | + | |
| 183 | + $result[] = self::get_caches_purged_message('LiteSpeed Cache'); | |
| 173 | 184 | } |
| 174 | 185 | |
| 175 | 186 | // Purge Hyper Cache |
| 176 | 187 | if (class_exists('HyperCache')) { |
| 177 | 188 | do_action('autoptimize_action_cachepurged'); |
| 178 | - return sprintf(__('All caches from %s have also been purged.', 'wp-optimize'), '<strong>Hyper Cache</strong>'); | |
| 189 | + | |
| 190 | + $result[] = self::get_caches_purged_message('Hyper Cache'); | |
| 179 | 191 | } |
| 180 | 192 | |
| 181 | 193 | // Purge Godaddy Managed WordPress Hosting (Varnish + APC) |
| 182 | 194 | if (class_exists('WPaaS\Plugin')) { |
| 183 | 195 | self::godaddy_request('BAN'); |
| 184 | - return sprintf(__('A cache purge request has been sent to %s. Please note that it may not work every time, due to cache rate limiting by your host.', 'wp-optimize'), '<strong>Go Daddy Varnish</strong>'); | |
| 196 | + // translators: %s is a remote cache system name `Go Daddy Varnish` | |
| 197 | + $result[] = sprintf(__('A cache purge request has been sent to %s.', 'wp-optimize'), '<strong>Go Daddy Varnish</strong>') . ' ' . __('Please note that it may not work every time, due to cache rate limiting by your host.', 'wp-optimize'); | |
| 185 | 198 | } |
| 186 | 199 | |
| 187 | 200 | // purge cache enabler |
| 188 | 201 | if (has_action('ce_clear_cache')) { |
| 189 | 202 | do_action('ce_clear_cache'); |
| 190 | - return sprintf(__('All caches from %s have also been purged.', 'wp-optimize'), '<strong>Cache Enabler</strong>'); | |
| 203 | + | |
| 204 | + $result[] = self::get_caches_purged_message('Cache Enabler'); | |
| 191 | 205 | } |
| 192 | 206 | |
| 193 | 207 | // Purge WP Engine |
| 194 | 208 | if (class_exists("WpeCommon")) { |
| @@ -202,9 +216,10 @@ | ||
| 202 | 216 | WpeCommon::purge_varnish_cache(); |
| 203 | 217 | } |
| 204 | 218 | |
| 205 | 219 | if (method_exists('WpeCommon', 'purge_memcached') || method_exists('WpeCommon', 'clear_maxcdn_cache') || method_exists('WpeCommon', 'purge_varnish_cache')) { |
| 206 | - return sprintf(__('A cache purge request has been sent to %s. Please note that it may not work every time, due to cache rate limiting by your host.', 'wp-optimize'), '<strong>WP Engine</strong>'); | |
| 220 | + // translators: %s is a remote cache system name `WP Engine` | |
| 221 | + $result[] = sprintf(__('A cache purge request has been sent to %s.', 'wp-optimize'), '<strong>WP Engine</strong>') . ' ' . __('Please note that it may not work every time, due to cache rate limiting by your host.', 'wp-optimize'); | |
| 207 | 222 | } |
| 208 | 223 | } |
| 209 | 224 | |
| 210 | 225 | // Purge Kinsta |
| @@ -211,9 +226,10 @@ | ||
| 211 | 226 | global $kinsta_cache; |
| 212 | 227 | if (isset($kinsta_cache) && class_exists('\\Kinsta\\CDN_Enabler')) { |
| 213 | 228 | if (!empty($kinsta_cache->kinsta_cache_purge) && is_callable(array($kinsta_cache->kinsta_cache_purge, 'purge_complete_caches'))) { |
| 214 | 229 | $kinsta_cache->kinsta_cache_purge->purge_complete_caches(); |
| 215 | - return sprintf(__('A cache purge request was also sent to %s', 'wp-optimize'), '<strong>Kinsta</strong>'); | |
| 230 | + | |
| 231 | + $result[] = self::get_remote_caches_purged_message('Kinsta'); | |
| 216 | 232 | } |
| 217 | 233 | } |
| 218 | 234 | |
| 219 | 235 | // Purge Pagely |
| @@ -220,27 +236,30 @@ | ||
| 220 | 236 | if (class_exists('PagelyCachePurge')) { |
| 221 | 237 | $purge_pagely = new PagelyCachePurge(); |
| 222 | 238 | if (is_callable(array($purge_pagely, 'purgeAll'))) { |
| 223 | 239 | $purge_pagely->purgeAll(); |
| 224 | - return sprintf(__('A cache purge request was also sent to %s', 'wp-optimize'), '<strong>Pagely</strong>'); | |
| 240 | + | |
| 241 | + $result[] = self::get_remote_caches_purged_message('Pagely'); | |
| 225 | 242 | } |
| 226 | 243 | } |
| 227 | 244 | |
| 228 | - // Purge Pressidum | |
| 245 | + // Purge Pressidium | |
| 229 | 246 | if (defined('WP_NINUKIS_WP_NAME') && class_exists('Ninukis_Plugin') && is_callable(array('Ninukis_Plugin', 'get_instance'))) { |
| 230 | 247 | $purge_pressidum = Ninukis_Plugin::get_instance(); |
| 231 | 248 | if (is_callable(array($purge_pressidum, 'purgeAllCaches'))) { |
| 232 | 249 | $purge_pressidum->purgeAllCaches(); |
| 233 | - return sprintf(__('A cache purge request was also sent to %s', 'wp-optimize'), '<strong>Pressidium</strong>'); | |
| 250 | + | |
| 251 | + $result[] = self::get_remote_caches_purged_message('Pressidium'); | |
| 234 | 252 | } |
| 235 | 253 | } |
| 236 | 254 | |
| 237 | 255 | // Purge Savvii |
| 238 | 256 | if (defined('\Savvii\CacheFlusherPlugin::NAME_DOMAINFLUSH_NOW')) { |
| 239 | - $purge_savvii = new \Savvii\CacheFlusherPlugin(); // phpcs:ignore PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound | |
| 257 | + $purge_savvii = new \Savvii\CacheFlusherPlugin(); | |
| 240 | 258 | if (is_callable(array($purge_savvii, 'domainflush'))) { |
| 241 | 259 | $purge_savvii->domainflush(); |
| 242 | - return sprintf(__('A cache purge request was also sent to %s', 'wp-optimize'), '<strong>Savvii</strong>'); | |
| 260 | + | |
| 261 | + $result[] = self::get_remote_caches_purged_message('Savvii'); | |
| 243 | 262 | } |
| 244 | 263 | } |
| 245 | 264 | |
| 246 | 265 | /** |
| @@ -247,11 +266,46 @@ | ||
| 247 | 266 | * Action triggered when purging other plugins cache, and nothing was triggered |
| 248 | 267 | */ |
| 249 | 268 | do_action('wpo_min_after_purge_others'); |
| 250 | 269 | |
| 270 | + return $result; | |
| 251 | 271 | } |
| 252 | 272 | |
| 253 | 273 | /** |
| 274 | + * Returns the purged cache message for the given plugin name | |
| 275 | + * | |
| 276 | + * @param string $plugin_name Name of the plugin | |
| 277 | + * | |
| 278 | + * @return string | |
| 279 | + */ | |
| 280 | + public static function get_caches_purged_message($plugin_name) { | |
| 281 | + $message = sprintf( | |
| 282 | + // translators: %s is a plugin name | |
| 283 | + __('All caches from %s have also been purged.', 'wp-optimize'), | |
| 284 | + '<strong>' . esc_html($plugin_name) . '</strong>' | |
| 285 | + ); | |
| 286 | + | |
| 287 | + return $message; | |
| 288 | + } | |
| 289 | + | |
| 290 | + /** | |
| 291 | + * Returns remote purged cache message for given host name | |
| 292 | + * | |
| 293 | + * @param string $host_name | |
| 294 | + * | |
| 295 | + * @return string | |
| 296 | + */ | |
| 297 | + public static function get_remote_caches_purged_message($host_name) { | |
| 298 | + $message = sprintf( | |
| 299 | + // translators: %s is a remote cache system name | |
| 300 | + __('A cache purge request has been sent to %s.', 'wp-optimize'), | |
| 301 | + '<strong>' . esc_html($host_name) . '</strong>' | |
| 302 | + ); | |
| 303 | + | |
| 304 | + return $message; | |
| 305 | + } | |
| 306 | + | |
| 307 | + /** | |
| 254 | 308 | * Purge all public files on uninstallation |
| 255 | 309 | * This will break cached pages that ref minified JS/CSS |
| 256 | 310 | * |
| 257 | 311 | * @return Boolean |
| @@ -258,15 +312,15 @@ | ||
| 258 | 312 | */ |
| 259 | 313 | public static function purge() { |
| 260 | 314 | $log = ''; |
| 261 | 315 | if (is_dir(WPO_CACHE_MIN_FILES_DIR)) { |
| 262 | - if (wpo_delete_files(WPO_CACHE_MIN_FILES_DIR, true)) { | |
| 316 | + if (wpo_delete_files(WPO_CACHE_MIN_FILES_DIR)) { | |
| 263 | 317 | $log = "[Minify] files and folders are deleted recursively"; |
| 264 | 318 | } else { |
| 265 | 319 | $log = "[Minify] recursive files and folders deletion unsuccessful"; |
| 266 | 320 | } |
| 267 | 321 | if (wp_optimize_minify_config()->get('debug')) { |
| 268 | - error_log($log); | |
| 322 | + error_log($log); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Used for debugging | |
| 269 | 323 | } |
| 270 | 324 | } |
| 271 | 325 | return true; |
| 272 | 326 | } |
| @@ -277,9 +331,9 @@ | ||
| 277 | 331 | * @return array |
| 278 | 332 | */ |
| 279 | 333 | public static function purge_old() { |
| 280 | 334 | if (!class_exists('WP_Optimize_Minify_Config')) { |
| 281 | - include_once WPO_PLUGIN_MAIN_PATH . '/minify/class-wp-optimize-minify-config.php'; | |
| 335 | + include_once WPO_PLUGIN_MAIN_PATH . 'minify/class-wp-optimize-minify-config.php'; | |
| 282 | 336 | } |
| 283 | 337 | $cache_time = wp_optimize_minify_config()->get('last-cache-update'); |
| 284 | 338 | $cache_lifespan = wp_optimize_minify_config()->get('cache_lifespan'); |
| 285 | 339 | |
| @@ -291,27 +345,30 @@ | ||
| 291 | 345 | $expires = apply_filters('wp_optimize_minify_cache_expiry_time', time() - 86400 * $cache_lifespan); |
| 292 | 346 | $log = array(); |
| 293 | 347 | |
| 294 | 348 | // get all directories that are a direct child of current directory |
| 295 | - if (is_dir(WPO_CACHE_MIN_FILES_DIR) && is_writable(dirname(WPO_CACHE_MIN_FILES_DIR))) { | |
| 349 | + if (is_dir(WPO_CACHE_MIN_FILES_DIR) && wp_is_writable(dirname(WPO_CACHE_MIN_FILES_DIR))) { | |
| 296 | 350 | if ($handle = opendir(WPO_CACHE_MIN_FILES_DIR)) { |
| 297 | 351 | while (false !== ($d = readdir($handle))) { |
| 298 | - if (strcmp($d, '.')==0 || strcmp($d, '..')==0) { | |
| 352 | + if ('.' === $d || '..' === $d) { | |
| 299 | 353 | continue; |
| 300 | 354 | } |
| 301 | 355 | $log[] = "cache expiration time - $expires"; |
| 302 | 356 | $log[] = "checking if cache has expired - $d"; |
| 303 | - if ($d != $cache_time && (is_numeric($d) && $d <= $expires)) { | |
| 304 | - $dir = WPO_CACHE_MIN_FILES_DIR.'/'.$d; | |
| 357 | + | |
| 358 | + $dir = WPO_CACHE_MIN_FILES_DIR.'/'.$d; | |
| 359 | + $is_empty_dir = self::is_empty_minify_subdir($dir); | |
| 360 | + | |
| 361 | + if ($d !== (string) $cache_time && (is_numeric($d) && $d <= $expires || $is_empty_dir)) { | |
| 305 | 362 | if (is_dir($dir)) { |
| 306 | 363 | $log[] = "deleting cache in $dir"; |
| 307 | - if (wpo_delete_files($dir, true)) { | |
| 364 | + if (wpo_delete_files($dir)) { | |
| 308 | 365 | $log[] = "files and folders are deleted recursively - $dir"; |
| 309 | 366 | } else { |
| 310 | 367 | $log[] = "recursive files and folders deletion unsuccessful - $dir"; |
| 311 | 368 | } |
| 312 | 369 | if (file_exists($dir)) { |
| 313 | - if (rmdir($dir)) { | |
| 370 | + if (rmdir($dir)) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- N/A | |
| 314 | 371 | $log[] = "folder deleted successfully - $dir"; |
| 315 | 372 | } else { |
| 316 | 373 | $log[] = "folder deletion unsuccessful - $dir"; |
| 317 | 374 | } |
| @@ -323,9 +380,9 @@ | ||
| 323 | 380 | } |
| 324 | 381 | } |
| 325 | 382 | if (wp_optimize_minify_config()->get('debug')) { |
| 326 | 383 | foreach ($log as $message) { |
| 327 | - error_log($message); | |
| 384 | + error_log($message); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Used for debugging | |
| 328 | 385 | } |
| 329 | 386 | } |
| 330 | 387 | |
| 331 | 388 | return $log; |
| @@ -331,11 +388,22 @@ | ||
| 331 | 388 | return $log; |
| 332 | 389 | } |
| 333 | 390 | |
| 334 | 391 | /** |
| 392 | + * Checks whether the minify cache subdirectory (/ddddddddd/) is empty, i.e. whether the 'assets' and 'header' subdirectories contain no files. | |
| 393 | + * | |
| 394 | + * @param string $dir | |
| 395 | + * @return boolean | |
| 396 | + */ | |
| 397 | + private static function is_empty_minify_subdir($dir) { | |
| 398 | + return wpo_is_empty_dir($dir . '/assets') && wpo_is_empty_dir($dir . '/header'); | |
| 399 | + } | |
| 400 | + | |
| 401 | + /** | |
| 335 | 402 | * Get transients from the disk |
| 336 | 403 | * |
| 337 | - * @return String|Boolean | |
| 404 | + * @param string $key | |
| 405 | + * @return string|boolean | |
| 338 | 406 | */ |
| 339 | 407 | public static function get_transient($key) { |
| 340 | 408 | $cache_path = self::cache_path(); |
| 341 | 409 | $tmp_dir = $cache_path['tmpdir']; |
| @@ -350,12 +418,12 @@ | ||
| 350 | 418 | |
| 351 | 419 | /** |
| 352 | 420 | * Set cache on disk |
| 353 | 421 | * |
| 354 | - * @param String $key | |
| 355 | - * @param Mixed $code | |
| 422 | + * @param string $key | |
| 423 | + * @param mixed $code | |
| 356 | 424 | * |
| 357 | - * @return Boolean | |
| 425 | + * @return boolean | |
| 358 | 426 | */ |
| 359 | 427 | public static function set_transient($key, $code) { |
| 360 | 428 | if (is_null($code) || empty($code)) { |
| 361 | 429 | return false; |
| @@ -371,24 +439,16 @@ | ||
| 371 | 439 | /** |
| 372 | 440 | * Get the cache size and count |
| 373 | 441 | * |
| 374 | 442 | * @param string $folder |
| 375 | - * @return String | |
| 443 | + * @return string | |
| 376 | 444 | */ |
| 377 | 445 | public static function get_cachestats($folder) { |
| 378 | - clearstatcache(); | |
| 379 | - if (is_dir($folder)) { | |
| 380 | - $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder, FilesystemIterator::SKIP_DOTS)); | |
| 381 | - $size = 0; | |
| 382 | - $file_count = 0; | |
| 383 | - foreach ($dir as $file) { | |
| 384 | - $size += $file->getSize(); | |
| 385 | - $file_count++; | |
| 386 | - } | |
| 387 | - return WP_Optimize()->format_size($size) . ' ('.$file_count.' files)'; | |
| 388 | - } else { | |
| 389 | - return sprintf(__('Error: %s is not a directory!', 'wp-optimize'), $folder); | |
| 390 | - } | |
| 446 | + $stats = WP_Optimize_Utils::get_folder_stats($folder); | |
| 447 | + | |
| 448 | + // Translators: %s: number of files. | |
| 449 | + $translated_file_count = sprintf(_n('%s file', '%s files', $stats['file_count'], 'wp-optimize'), number_format_i18n($stats['file_count'])); | |
| 450 | + return WP_Optimize()->format_size($stats['size']) . ' (' . $translated_file_count . ')'; | |
| 391 | 451 | } |
| 392 | 452 | |
| 393 | 453 | /** |
| 394 | 454 | * Purge GoDaddy Managed WordPress Hosting (Varnish) |
| @@ -394,15 +454,15 @@ | ||
| 394 | 454 | * Purge GoDaddy Managed WordPress Hosting (Varnish) |
| 395 | 455 | * |
| 396 | 456 | * Source: https://github.com/wp-media/wp-rocket/blob/master/inc/3rd-party/hosting/godaddy.php |
| 397 | 457 | * |
| 398 | - * @param String $method | |
| 399 | - * @param String|Null $url | |
| 458 | + * @param string $method | |
| 459 | + * @param string|null $url | |
| 400 | 460 | */ |
| 401 | 461 | public static function godaddy_request($method, $url = null) { |
| 402 | 462 | $url = empty($url) ? home_url() : $url; |
| 403 | - $host = parse_url($url, PHP_URL_HOST); | |
| 404 | - $url = set_url_scheme(str_replace($host, WPaas\Plugin::vip(), $url), 'http'); // phpcs:ignore PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound | |
| 463 | + $host = wp_parse_url($url, PHP_URL_HOST); | |
| 464 | + $url = set_url_scheme(str_replace($host, WPaas\Plugin::vip(), $url), 'http'); | |
| 405 | 465 | wp_cache_flush(); |
| 406 | 466 | update_option('gd_system_last_cache_flush', time()); // purge apc |
| 407 | 467 | wp_remote_request(esc_url_raw($url), array('method' => $method, 'blocking' => false, 'headers' => array('Host' => $host))); |
| 408 | 468 | } |
| @@ -422,9 +482,9 @@ | ||
| 422 | 482 | $cache_dir = $cache_path['cachedir']; |
| 423 | 483 | $size = self::get_cachestats($cache_dir); |
| 424 | 484 | $total_size = self::get_cachestats(WPO_CACHE_MIN_FILES_DIR); |
| 425 | 485 | $o = wp_optimize_minify_config()->get(); |
| 426 | - $cache_time = (0 == $o['last-cache-update']) ? __('Never.', 'wp-optimize') : self::format_date_time($o['last-cache-update']); | |
| 486 | + $cache_time = (0 === $o['last-cache-update']) ? __('Never.', 'wp-optimize') : self::format_date_time($o['last-cache-update']); | |
| 427 | 487 | $return = array( |
| 428 | 488 | 'js' => array(), |
| 429 | 489 | 'css' => array(), |
| 430 | 490 | 'stamp' => $stamp, |
| @@ -440,20 +500,17 @@ | ||
| 440 | 500 | while (false !== ($file = readdir($handle))) { |
| 441 | 501 | $file = $cache_dir.'/'.$file; |
| 442 | 502 | $ext = pathinfo($file, PATHINFO_EXTENSION); |
| 443 | 503 | if (in_array($ext, array('js', 'css'))) { |
| 444 | - $log = false; | |
| 445 | - if (file_exists($file.'.json')) { | |
| 446 | - $log = json_decode(file_get_contents($file.'.json')); | |
| 447 | - } | |
| 504 | + $log = self::generate_log($file.'.json' ); | |
| 448 | 505 | $min_css = substr($file, 0, -4).'.min.css'; |
| 449 | 506 | $minjs = substr($file, 0, -3).'.min.js'; |
| 450 | 507 | $file_name = basename($file); |
| 451 | 508 | $file_url = trailingslashit($cache_path['cachedirurl']).$file_name; |
| 452 | - if ('css' == $ext && file_exists($min_css)) { | |
| 509 | + if ('css' === $ext && file_exists($min_css)) { | |
| 453 | 510 | $file_name = basename($min_css); |
| 454 | 511 | } |
| 455 | - if ('js' == $ext && file_exists($minjs)) { | |
| 512 | + if ('js' === $ext && file_exists($minjs)) { | |
| 456 | 513 | $file_name = basename($minjs); |
| 457 | 514 | } |
| 458 | 515 | $file_size = WP_Optimize()->format_size(filesize($file)); |
| 459 | 516 | $uid = hash('adler32', $file_name); |
| @@ -466,10 +523,51 @@ | ||
| 466 | 523 | return $return; |
| 467 | 524 | } |
| 468 | 525 | |
| 469 | 526 | /** |
| 470 | - * Format a timestamp using WP's date_format and time_format | |
| 527 | + * Generate log information from a json file. | |
| 471 | 528 | * |
| 529 | + * @param string $file Full path of log file. | |
| 530 | + * | |
| 531 | + * @return mixed Could be either a 'json_decode' object upon successful parsing of the JSON file, or a stdClass object | |
| 532 | + * upon failure. In the case of stdClass object, $obj->error will contain the error message. | |
| 533 | + */ | |
| 534 | + public static function generate_log($file) { | |
| 535 | + $error_log = new stdClass(); | |
| 536 | + | |
| 537 | + $cache_path = self::cache_path(); | |
| 538 | + $file_name = basename($file); | |
| 539 | + $file_url = trailingslashit($cache_path['cachedirurl']) . $file_name; | |
| 540 | + $file_link_html = '<a href="' . esc_url($file_url) . '" target="_blank">' . $file_name . '</a>'; | |
| 541 | + | |
| 542 | + if (!file_exists($file)) { | |
| 543 | + // translators: %s is a log file link | |
| 544 | + $error_log->error = sprintf(__('Log file %s is missing', 'wp-optimize'), $file_link_html); | |
| 545 | + return $error_log; | |
| 546 | + } | |
| 547 | + | |
| 548 | + $log = json_decode(file_get_contents($file)); | |
| 549 | + | |
| 550 | + $is_valid_json = json_last_error() === JSON_ERROR_NONE; | |
| 551 | + | |
| 552 | + if (!$is_valid_json) { | |
| 553 | + // translators: %1$s is a log file link, %2$s is the error message | |
| 554 | + $error_log->error = sprintf(__('JSON error in file %1$s | Error details: %2$s', 'wp-optimize'), $file_link_html, json_last_error_msg()); | |
| 555 | + return $error_log; | |
| 556 | + } | |
| 557 | + | |
| 558 | + if (!isset($log->header) || !isset($log->files)) { | |
| 559 | + // translators: %s is a log file link | |
| 560 | + $error_log->error = sprintf(__('Some data is missing in the log file %s', 'wp-optimize'), $file_link_html); | |
| 561 | + return $error_log; | |
| 562 | + } | |
| 563 | + | |
| 564 | + return $log; | |
| 565 | + } | |
| 566 | + | |
| 567 | + /** | |
| 568 | + * Format a timestamp using WordPress's date_format and time_format | |
| 569 | + * | |
| 472 | 570 | * @param integer $timestamp - The timestamp |
| 473 | 571 | * @return string |
| 474 | 572 | */ |
| 475 | 573 | public static function format_date_time($timestamp) { |
| @@ -486,9 +584,10 @@ | ||
| 486 | 584 | $files['log'] = WP_Optimize()->include_template( |
| 487 | 585 | 'minify/cached-file-log.php', |
| 488 | 586 | true, |
| 489 | 587 | array( |
| 490 | - 'log' => $files['log'] | |
| 588 | + 'log' => $files['log'], | |
| 589 | + 'minify_config' => wp_optimize_minify_config()->get(), | |
| 491 | 590 | ) |
| 492 | 591 | ); |
| 493 | 592 | return $files; |
| 494 | 593 | } |