| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <suabahasa@gmail.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress\Utils; |
| 13 |
|
| 14 |
use RecursiveIteratorIterator; |
| 15 |
use RecursiveDirectoryIterator; |
| 16 |
use RegexIterator; |
| 17 |
use WIND_PRESS; |
| 18 |
use WindPress\WindPress\Core\Cache as CoreCache; |
| 19 |
use WindPress\WindPress\Core\Runtime as CoreRuntime; |
| 20 |
use WindPress\WindPress\Core\Volume as CoreVolume; |
| 21 |
/** |
| 22 |
* Cache utility functions for the plugin. |
| 23 |
* |
| 24 |
* @since 3.0.0 |
| 25 |
*/ |
| 26 |
class Cache |
| 27 |
{ |
| 28 |
/** |
| 29 |
* Clear the cache from various cache plugins. |
| 30 |
*/ |
| 31 |
public static function flush_cache_plugin() |
| 32 |
{ |
| 33 |
/** |
| 34 |
* WordPress Object Cache |
| 35 |
* @see https://developer.wordpress.org/reference/classes/wp_object_cache/ |
| 36 |
*/ |
| 37 |
\wp_cache_flush(); |
| 38 |
/** |
| 39 |
* WP Rocket |
| 40 |
* @see https://docs.wp-rocket.me/article/92-rocketcleandomain |
| 41 |
*/ |
| 42 |
if (function_exists('rocket_clean_domain')) { |
| 43 |
\rocket_clean_domain(); |
| 44 |
} |
| 45 |
/** |
| 46 |
* WP Super Cache |
| 47 |
* @see https://github.com/Automattic/wp-super-cache/blob/a0872032b1b3fc6847f490eadfabf74c12ad0135/wp-cache-phase2.php#L3013 |
| 48 |
*/ |
| 49 |
if (function_exists('wp_cache_clear_cache')) { |
| 50 |
\wp_cache_clear_cache(); |
| 51 |
} |
| 52 |
/** |
| 53 |
* W3 Total Cache |
| 54 |
* @see https://github.com/BoldGrid/w3-total-cache/blob/3a094493064ea60d727b3389dee813639860ef49/w3-total-cache-api.php#L259 |
| 55 |
*/ |
| 56 |
if (function_exists('w3tc_flush_all')) { |
| 57 |
\w3tc_flush_all(); |
| 58 |
} |
| 59 |
/** |
| 60 |
* WP Fastest Cache |
| 61 |
* @see https://www.wpfastestcache.com/tutorial/delete-the-cache-by-calling-the-function/ |
| 62 |
*/ |
| 63 |
if (function_exists('wpfc_clear_all_cache')) { |
| 64 |
\wpfc_clear_all_cache(\true); |
| 65 |
} |
| 66 |
/** |
| 67 |
* LiteSpeed Cache |
| 68 |
* @see https://docs.litespeedtech.com/lscache/lscwp/api/#purge-all-existing-caches |
| 69 |
*/ |
| 70 |
do_action('litespeed_purge_all'); |
| 71 |
/** |
| 72 |
* SG Optimizer |
| 73 |
* @see https://plugins.trac.wordpress.org/browser/sg-cachepress/trunk/core/Supercacher/Supercacher.php |
| 74 |
* @see https://plugins.trac.wordpress.org/browser/sg-cachepress/trunk/core/File_Cacher/File_Cacher.php |
| 75 |
*/ |
| 76 |
if (class_exists('\SiteGround_Optimizer\Supercacher\Supercacher')) { |
| 77 |
\SiteGround_Optimizer\Supercacher\Supercacher::purge_cache(); |
| 78 |
\SiteGround_Optimizer\File_Cacher\File_Cacher::get_instance()->purge_everything(); |
| 79 |
} |
| 80 |
} |
| 81 |
public static function exclude_optimization() |
| 82 |
{ |
| 83 |
self::sgoptimizer_exclude(); |
| 84 |
self::wprocket_exclude(); |
| 85 |
} |
| 86 |
/** |
| 87 |
* SG Optimizer |
| 88 |
* @see https://www.siteground.com/tutorials/wordpress/speed-optimizer/custom-filters/ |
| 89 |
*/ |
| 90 |
private static function sgoptimizer_exclude() |
| 91 |
{ |
| 92 |
add_action('wp_print_scripts', function () { |
| 93 |
/** |
| 94 |
* @var \WP_Scripts $wp_scripts |
| 95 |
*/ |
| 96 |
global $wp_scripts; |
| 97 |
$handles = []; |
| 98 |
/** @var \WP_Scripts $scripts */ |
| 99 |
$scripts = clone $wp_scripts; |
| 100 |
$scripts->all_deps($scripts->queue); |
| 101 |
foreach ($scripts->to_do as $handle) { |
| 102 |
// if $handle contains 'windpress' then add to $handles |
| 103 |
if (strpos($handle, 'windpress') !== \false) { |
| 104 |
$handles[] = $handle; |
| 105 |
} |
| 106 |
} |
| 107 |
add_filter('sgo_js_minify_exclude', fn($exclude_list) => array_merge($exclude_list, $handles)); |
| 108 |
add_filter('sgo_javascript_combine_exclude', fn($exclude_list) => array_merge($exclude_list, $handles)); |
| 109 |
add_filter('sgo_js_async_exclude', fn($exclude_list) => array_merge($exclude_list, $handles)); |
| 110 |
}, 19); |
| 111 |
} |
| 112 |
/** |
| 113 |
* WP Rocket |
| 114 |
* |
| 115 |
* @see https://docs.wp-rocket.me/article/976-exclude-files-from-defer-js#exclude-inline-scripts |
| 116 |
* @see https://github.com/wp-media/wp-rocket-helpers |
| 117 |
*/ |
| 118 |
private static function wprocket_exclude() |
| 119 |
{ |
| 120 |
$wp_root = strval(substr(\ABSPATH, 0, -1)); |
| 121 |
$cache_dir = CoreCache::get_cache_path(); |
| 122 |
$data_dir = CoreVolume::data_dir_path(); |
| 123 |
$plugin_asset_dir = dirname(WIND_PRESS::FILE) . '/assets/dist'; |
| 124 |
$excluded_folders = [$cache_dir, $data_dir, $plugin_asset_dir]; |
| 125 |
$excluded_files = []; |
| 126 |
$inline_patterns = ['windpress', substr(CoreRuntime::get_instance()->getVFSHtml(), 45, 7)]; |
| 127 |
foreach ($excluded_folders as $folder) { |
| 128 |
if (file_exists($folder)) { |
| 129 |
$allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder)); |
| 130 |
$staticFiles = new RegexIterator($allFiles, '/\.(css|js)$/i'); |
| 131 |
foreach ($staticFiles as $staticFile) { |
| 132 |
$file = str_replace($wp_root, '', $staticFile->getPathname()); |
| 133 |
array_push($excluded_files, $file); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
add_filter('rocket_exclude_async_css', function ($exclude_list) use ($excluded_files) { |
| 138 |
$css_files = array_filter($excluded_files, function ($file) { |
| 139 |
return str_ends_with($file, '.css'); |
| 140 |
}); |
| 141 |
return array_merge($exclude_list, $css_files); |
| 142 |
}); |
| 143 |
add_filter('rocket_delay_js_exclusions', function ($exclude_list) use ($excluded_files) { |
| 144 |
$js_files = array_filter($excluded_files, function ($file) { |
| 145 |
return str_ends_with($file, '.js'); |
| 146 |
}); |
| 147 |
return array_merge($exclude_list, $js_files); |
| 148 |
}); |
| 149 |
add_filter('rocket_exclude_defer_js', function ($exclude_list) use ($excluded_files) { |
| 150 |
$js_files = array_filter($excluded_files, function ($file) { |
| 151 |
return str_ends_with($file, '.js'); |
| 152 |
}); |
| 153 |
return array_merge($exclude_list, $js_files); |
| 154 |
}); |
| 155 |
add_filter('rocket_defer_inline_exclusions', function ($exclude_list) use ($inline_patterns) { |
| 156 |
return array_merge($exclude_list, $inline_patterns); |
| 157 |
}); |
| 158 |
add_filter('rocket_excluded_inline_js_content', function ($exclude_list) use ($inline_patterns) { |
| 159 |
return array_merge($exclude_list, $inline_patterns); |
| 160 |
}); |
| 161 |
} |
| 162 |
} |
| 163 |
|