PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
← All changes | minify/class-wp-optimize-minify-cache-functions.php +68 -49 3.2.21 → 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(
@@ -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
@@ -141,9 +146,10 @@
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
147 153 // Store the messages of purged cache if it was successful
148 154 $result = array();
149 155
@@ -186,8 +192,9 @@
186 192
187 193 // Purge Godaddy Managed WordPress Hosting (Varnish + APC)
188 194 if (class_exists('WPaaS\Plugin')) {
189 195 self::godaddy_request('BAN');
196 + // translators: %s is a remote cache system name `Go Daddy Varnish`
190 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');
191 198 }
192 199
193 200 // purge cache enabler
@@ -209,8 +216,9 @@
209 216 WpeCommon::purge_varnish_cache();
210 217 }
211 218
212 219 if (method_exists('WpeCommon', 'purge_memcached') || method_exists('WpeCommon', 'clear_maxcdn_cache') || method_exists('WpeCommon', 'purge_varnish_cache')) {
220 + // translators: %s is a remote cache system name `WP Engine`
213 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');
214 222 }
215 223 }
216 224
@@ -233,9 +241,9 @@
233 241 $result[] = self::get_remote_caches_purged_message('Pagely');
234 242 }
235 243 }
236 244
237 - // Purge Pressidum
245 + // Purge Pressidium
238 246 if (defined('WP_NINUKIS_WP_NAME') && class_exists('Ninukis_Plugin') && is_callable(array('Ninukis_Plugin', 'get_instance'))) {
239 247 $purge_pressidum = Ninukis_Plugin::get_instance();
240 248 if (is_callable(array($purge_pressidum, 'purgeAllCaches'))) {
241 249 $purge_pressidum->purgeAllCaches();
@@ -270,8 +278,9 @@
270 278 * @return string
271 279 */
272 280 public static function get_caches_purged_message($plugin_name) {
273 281 $message = sprintf(
282 + // translators: %s is a plugin name
274 283 __('All caches from %s have also been purged.', 'wp-optimize'),
275 284 '<strong>' . esc_html($plugin_name) . '</strong>'
276 285 );
277 286
@@ -286,8 +295,9 @@
286 295 * @return string
287 296 */
288 297 public static function get_remote_caches_purged_message($host_name) {
289 298 $message = sprintf(
299 + // translators: %s is a remote cache system name
290 300 __('A cache purge request has been sent to %s.', 'wp-optimize'),
291 301 '<strong>' . esc_html($host_name) . '</strong>'
292 302 );
293 303
@@ -302,15 +312,15 @@
302 312 */
303 313 public static function purge() {
304 314 $log = '';
305 315 if (is_dir(WPO_CACHE_MIN_FILES_DIR)) {
306 - if (wpo_delete_files(WPO_CACHE_MIN_FILES_DIR, true)) {
316 + if (wpo_delete_files(WPO_CACHE_MIN_FILES_DIR)) {
307 317 $log = "[Minify] files and folders are deleted recursively";
308 318 } else {
309 319 $log = "[Minify] recursive files and folders deletion unsuccessful";
310 320 }
311 321 if (wp_optimize_minify_config()->get('debug')) {
312 - error_log($log);
322 + error_log($log); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Used for debugging
313 323 }
314 324 }
315 325 return true;
316 326 }
@@ -335,27 +345,30 @@
335 345 $expires = apply_filters('wp_optimize_minify_cache_expiry_time', time() - 86400 * $cache_lifespan);
336 346 $log = array();
337 347
338 348 // get all directories that are a direct child of current directory
339 - 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))) {
340 350 if ($handle = opendir(WPO_CACHE_MIN_FILES_DIR)) {
341 351 while (false !== ($d = readdir($handle))) {
342 - if (strcmp($d, '.')==0 || strcmp($d, '..')==0) {
352 + if ('.' === $d || '..' === $d) {
343 353 continue;
344 354 }
345 355 $log[] = "cache expiration time - $expires";
346 356 $log[] = "checking if cache has expired - $d";
347 - if ($d != $cache_time && (is_numeric($d) && $d <= $expires)) {
348 - $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)) {
349 362 if (is_dir($dir)) {
350 363 $log[] = "deleting cache in $dir";
351 - if (wpo_delete_files($dir, true)) {
364 + if (wpo_delete_files($dir)) {
352 365 $log[] = "files and folders are deleted recursively - $dir";
353 366 } else {
354 367 $log[] = "recursive files and folders deletion unsuccessful - $dir";
355 368 }
356 369 if (file_exists($dir)) {
357 - if (rmdir($dir)) {
370 + if (rmdir($dir)) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- N/A
358 371 $log[] = "folder deleted successfully - $dir";
359 372 } else {
360 373 $log[] = "folder deletion unsuccessful - $dir";
361 374 }
@@ -367,9 +380,9 @@
367 380 }
368 381 }
369 382 if (wp_optimize_minify_config()->get('debug')) {
370 383 foreach ($log as $message) {
371 - error_log($message);
384 + error_log($message); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Used for debugging
372 385 }
373 386 }
374 387
375 388 return $log;
@@ -375,11 +388,22 @@
375 388 return $log;
376 389 }
377 390
378 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 + /**
379 402 * Get transients from the disk
380 403 *
381 - * @return String|Boolean
404 + * @param string $key
405 + * @return string|boolean
382 406 */
383 407 public static function get_transient($key) {
384 408 $cache_path = self::cache_path();
385 409 $tmp_dir = $cache_path['tmpdir'];
@@ -394,12 +418,12 @@
394 418
395 419 /**
396 420 * Set cache on disk
397 421 *
398 - * @param String $key
399 - * @param Mixed $code
422 + * @param string $key
423 + * @param mixed $code
400 424 *
401 - * @return Boolean
425 + * @return boolean
402 426 */
403 427 public static function set_transient($key, $code) {
404 428 if (is_null($code) || empty($code)) {
405 429 return false;
@@ -415,24 +439,16 @@
415 439 /**
416 440 * Get the cache size and count
417 441 *
418 442 * @param string $folder
419 - * @return String
443 + * @return string
420 444 */
421 445 public static function get_cachestats($folder) {
422 - clearstatcache();
423 - if (is_dir($folder)) {
424 - $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder, FilesystemIterator::SKIP_DOTS));
425 - $size = 0;
426 - $file_count = 0;
427 - foreach ($dir as $file) {
428 - $size += $file->getSize();
429 - $file_count++;
430 - }
431 - return WP_Optimize()->format_size($size) . ' ('.$file_count.' files)';
432 - } else {
433 - return sprintf(__('Error: %s is not a directory!', 'wp-optimize'), $folder);
434 - }
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 . ')';
435 451 }
436 452
437 453 /**
438 454 * Purge GoDaddy Managed WordPress Hosting (Varnish)
@@ -438,14 +454,14 @@
438 454 * Purge GoDaddy Managed WordPress Hosting (Varnish)
439 455 *
440 456 * Source: https://github.com/wp-media/wp-rocket/blob/master/inc/3rd-party/hosting/godaddy.php
441 457 *
442 - * @param String $method
443 - * @param String|Null $url
458 + * @param string $method
459 + * @param string|null $url
444 460 */
445 461 public static function godaddy_request($method, $url = null) {
446 462 $url = empty($url) ? home_url() : $url;
447 - $host = parse_url($url, PHP_URL_HOST);
463 + $host = wp_parse_url($url, PHP_URL_HOST);
448 464 $url = set_url_scheme(str_replace($host, WPaas\Plugin::vip(), $url), 'http');
449 465 wp_cache_flush();
450 466 update_option('gd_system_last_cache_flush', time()); // purge apc
451 467 wp_remote_request(esc_url_raw($url), array('method' => $method, 'blocking' => false, 'headers' => array('Host' => $host)));
@@ -466,9 +482,9 @@
466 482 $cache_dir = $cache_path['cachedir'];
467 483 $size = self::get_cachestats($cache_dir);
468 484 $total_size = self::get_cachestats(WPO_CACHE_MIN_FILES_DIR);
469 485 $o = wp_optimize_minify_config()->get();
470 - $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']);
471 487 $return = array(
472 488 'js' => array(),
473 489 'css' => array(),
474 490 'stamp' => $stamp,
@@ -489,12 +505,12 @@
489 505 $min_css = substr($file, 0, -4).'.min.css';
490 506 $minjs = substr($file, 0, -3).'.min.js';
491 507 $file_name = basename($file);
492 508 $file_url = trailingslashit($cache_path['cachedirurl']).$file_name;
493 - if ('css' == $ext && file_exists($min_css)) {
509 + if ('css' === $ext && file_exists($min_css)) {
494 510 $file_name = basename($min_css);
495 511 }
496 - if ('js' == $ext && file_exists($minjs)) {
512 + if ('js' === $ext && file_exists($minjs)) {
497 513 $file_name = basename($minjs);
498 514 }
499 515 $file_size = WP_Optimize()->format_size(filesize($file));
500 516 $uid = hash('adler32', $file_name);
@@ -511,9 +527,9 @@
511 527 * Generate log information from a json file.
512 528 *
513 529 * @param string $file Full path of log file.
514 530 *
515 - * @return object Could be either a 'json_decode' object upon successful parsing of the JSON file, or a stdClass object
531 + * @return mixed Could be either a 'json_decode' object upon successful parsing of the JSON file, or a stdClass object
516 532 * upon failure. In the case of stdClass object, $obj->error will contain the error message.
517 533 */
518 534 public static function generate_log($file) {
519 535 $error_log = new stdClass();
@@ -523,8 +539,9 @@
523 539 $file_url = trailingslashit($cache_path['cachedirurl']) . $file_name;
524 540 $file_link_html = '<a href="' . esc_url($file_url) . '" target="_blank">' . $file_name . '</a>';
525 541
526 542 if (!file_exists($file)) {
543 + // translators: %s is a log file link
527 544 $error_log->error = sprintf(__('Log file %s is missing', 'wp-optimize'), $file_link_html);
528 545 return $error_log;
529 546 }
530 547
@@ -529,16 +546,18 @@
529 546 }
530 547
531 548 $log = json_decode(file_get_contents($file));
532 549
533 - $is_valid_json = json_last_error() === JSON_ERROR_NONE ? true : false;
550 + $is_valid_json = json_last_error() === JSON_ERROR_NONE;
534 551
535 552 if (!$is_valid_json) {
536 - $error_log->error = sprintf(__('JSON error in file %s | Error details: %s', 'wp-optimize'), $file_link_html, json_last_error_msg());
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());
537 555 return $error_log;
538 556 }
539 557
540 558 if (!isset($log->header) || !isset($log->files)) {
559 + // translators: %s is a log file link
541 560 $error_log->error = sprintf(__('Some data is missing in the log file %s', 'wp-optimize'), $file_link_html);
542 561 return $error_log;
543 562 }
544 563
@@ -545,9 +564,9 @@
545 564 return $log;
546 565 }
547 566
548 567 /**
549 - * Format a timestamp using WP's date_format and time_format
568 + * Format a timestamp using WordPress's date_format and time_format
550 569 *
551 570 * @param integer $timestamp - The timestamp
552 571 * @return string
553 572 */