| @@ -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 |
| @@ -69,6 +76,87 @@ | ||
| 69 | 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 | } |