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