| @@ -10,8 +10,15 @@ | ||
| 10 | 10 | */ |
| 11 | 11 | declare (strict_types=1); |
| 12 | 12 | namespace WindPress\WindPress\Utils; |
| 13 | 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; | |
| 14 | 21 | /** |
| 15 | 22 | * Cache utility functions for the plugin. |
| 16 | 23 | * |
| 17 | 24 | * @since 3.0.0 |
| @@ -31,9 +38,9 @@ | ||
| 31 | 38 | /** |
| 32 | 39 | * WP Rocket |
| 33 | 40 | * @see https://docs.wp-rocket.me/article/92-rocketcleandomain |
| 34 | 41 | */ |
| 35 | - if (\function_exists('rocket_clean_domain')) { | |
| 42 | + if (function_exists('rocket_clean_domain')) { | |
| 36 | 43 | \rocket_clean_domain(); |
| 37 | 44 | } |
| 38 | 45 | /** |
| 39 | 46 | * WP Super Cache |
| @@ -38,9 +45,9 @@ | ||
| 38 | 45 | /** |
| 39 | 46 | * WP Super Cache |
| 40 | 47 | * @see https://github.com/Automattic/wp-super-cache/blob/a0872032b1b3fc6847f490eadfabf74c12ad0135/wp-cache-phase2.php#L3013 |
| 41 | 48 | */ |
| 42 | - if (\function_exists('wp_cache_clear_cache')) { | |
| 49 | + if (function_exists('wp_cache_clear_cache')) { | |
| 43 | 50 | \wp_cache_clear_cache(); |
| 44 | 51 | } |
| 45 | 52 | /** |
| 46 | 53 | * W3 Total Cache |
| @@ -45,9 +52,9 @@ | ||
| 45 | 52 | /** |
| 46 | 53 | * W3 Total Cache |
| 47 | 54 | * @see https://github.com/BoldGrid/w3-total-cache/blob/3a094493064ea60d727b3389dee813639860ef49/w3-total-cache-api.php#L259 |
| 48 | 55 | */ |
| 49 | - if (\function_exists('w3tc_flush_all')) { | |
| 56 | + if (function_exists('w3tc_flush_all')) { | |
| 50 | 57 | \w3tc_flush_all(); |
| 51 | 58 | } |
| 52 | 59 | /** |
| 53 | 60 | * WP Fastest Cache |
| @@ -52,9 +59,9 @@ | ||
| 52 | 59 | /** |
| 53 | 60 | * WP Fastest Cache |
| 54 | 61 | * @see https://www.wpfastestcache.com/tutorial/delete-the-cache-by-calling-the-function/ |
| 55 | 62 | */ |
| 56 | - if (\function_exists('wpfc_clear_all_cache')) { | |
| 63 | + if (function_exists('wpfc_clear_all_cache')) { | |
| 57 | 64 | \wpfc_clear_all_cache(\true); |
| 58 | 65 | } |
| 59 | 66 | /** |
| 60 | 67 | * LiteSpeed Cache |
| @@ -59,16 +66,97 @@ | ||
| 59 | 66 | /** |
| 60 | 67 | * LiteSpeed Cache |
| 61 | 68 | * @see https://docs.litespeedtech.com/lscache/lscwp/api/#purge-all-existing-caches |
| 62 | 69 | */ |
| 63 | - \do_action('litespeed_purge_all'); | |
| 70 | + do_action('litespeed_purge_all'); | |
| 64 | 71 | /** |
| 65 | 72 | * SG Optimizer |
| 66 | 73 | * @see https://plugins.trac.wordpress.org/browser/sg-cachepress/trunk/core/Supercacher/Supercacher.php |
| 67 | 74 | * @see https://plugins.trac.wordpress.org/browser/sg-cachepress/trunk/core/File_Cacher/File_Cacher.php |
| 68 | 75 | */ |
| 69 | - if (\class_exists('\\SiteGround_Optimizer\\Supercacher\\Supercacher')) { | |
| 76 | + if (class_exists('\SiteGround_Optimizer\Supercacher\Supercacher')) { | |
| 70 | 77 | \SiteGround_Optimizer\Supercacher\Supercacher::purge_cache(); |
| 71 | 78 | \SiteGround_Optimizer\File_Cacher\File_Cacher::get_instance()->purge_everything(); |
| 72 | 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 | + }); | |
| 73 | 161 | } |
| 74 | 162 | } |