PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.4.1
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.4.1
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / integrations.php
nitropack Last commit date
nitropack-sdk 5 years ago view 5 years ago advanced-cache.php 5 years ago constants.php 5 years ago diagnostics.php 5 years ago functions.php 5 years ago integrations.php 5 years ago main.php 5 years ago readme.txt 5 years ago uninstall.php 6 years ago wp-cli.php 5 years ago
integrations.php
351 lines
1 <?php
2
3 if (nitropack_is_wpe()) {
4 define("NITROPACK_USE_MICROTIMEOUT", 20000);
5 }
6
7 function nitropack_check_and_init_integrations() {
8 $siteConfig = nitropack_get_site_config();
9 if ($siteConfig && !empty($siteConfig["hosting"])) {
10 $hosting = $siteConfig["hosting"];
11 } else {
12 $hosting = nitropack_detect_hosting();
13 }
14
15 switch ($hosting) {
16 case "cloudways":
17 add_action('nitropack_integration_purge_url', 'nitropack_cloudways_purge_url');
18 add_action('nitropack_integration_purge_all', 'nitropack_cloudways_purge_all');
19 break;
20 case "flywheel":
21 add_filter('nitropack_varnish_purger', 'nitropack_flywheel_varnish_instance');
22 add_action('nitropack_integration_purge_url', 'nitropack_varnish_purge_url');
23 add_action('nitropack_integration_purge_all', 'nitropack_varnish_purge_all');
24 break;
25 case "wpengine":
26 add_action('nitropack_integration_purge_url', 'nitropack_wpe_purge_url');
27 add_action('nitropack_integration_purge_all', 'nitropack_wpe_purge_all');
28 break;
29 case "siteground":
30 add_action('nitropack_integration_purge_url', 'nitropack_siteground_purge_url');
31 add_action('nitropack_integration_purge_all', 'nitropack_siteground_purge_all');
32 break;
33 case "godaddy_wpaas":
34 add_action('nitropack_integration_purge_url', 'nitropack_wpaas_purge_url');
35 add_action('nitropack_integration_purge_all', 'nitropack_wpaas_purge_all');
36 break;
37 case "kinsta":
38 add_action('nitropack_integration_purge_url', 'nitropack_kinsta_purge_url');
39 add_action('nitropack_integration_purge_all', 'nitropack_kinsta_purge_all');
40 break;
41 case "pagely":
42 add_action('nitropack_integration_purge_url', 'nitropack_pagely_purge_url');
43 add_action('nitropack_integration_purge_all', 'nitropack_pagely_purge_all');
44 break;
45 default:
46 break;
47 }
48
49 if (!empty($siteConfig["isNginxHelperActive"])) {
50 add_action('nitropack_integration_purge_url', 'nitropack_nginx_helper_purge_url');
51 add_action('nitropack_integration_purge_all', 'nitropack_nginx_helper_purge_all');
52 }
53
54 add_action('plugins_loaded', 'nitropack_init_late_integrations');
55 }
56
57 function nitropack_init_late_integrations() {
58 if (defined('SHORTPIXEL_AI_VERSION')) { // ShortPixel
59 if (nitropack_is_ajax()) {
60 if (version_compare(SHORTPIXEL_AI_VERSION, "2", ">=")) { // ShortPixel AI 2.x
61 remove_action('wp_enqueue_scripts', array(ShortPixelAI::_(), 'enqueue_script'));
62 remove_action('init', array(ShortPixelAI::_(), 'init_ob'), 1);
63 remove_filter('script_loader_tag', array(ShortPixelAI::_(), 'disable_rocket-Loader'), 10);
64 } else { // ShortPixel AI 1.x
65 remove_action('wp_enqueue_scripts', array(ShortPixelAI::instance(SHORTPIXEL_AI_PLUGIN_FILE), 'enqueue_script'), 11);
66 remove_action('init', array(ShortPixelAI::instance(SHORTPIXEL_AI_PLUGIN_FILE), 'init_ob'), 1);
67 remove_filter('rocket_css_content', array(ShortPixelAI::instance(SHORTPIXEL_AI_PLUGIN_FILE), 'parse_cached_css'), 10);
68 remove_filter('script_loader_tag', array(ShortPixelAI::instance(SHORTPIXEL_AI_PLUGIN_FILE), 'disable_rocket-Loader'), 10);
69 }
70 }
71 }
72
73 if (class_exists("WC_Cache_Helper")) {
74 remove_action('template_redirect', array('WC_Cache_Helper', 'geolocation_ajax_redirect'));
75 }
76 }
77
78 /** WP Engine **/
79 function nitropack_wpe_purge_url($url) {
80 try {
81 $handler = function($paths) use($url) {
82 $wpe_path = parse_url($url, PHP_URL_PATH);
83 $wpe_query = parse_url($url, PHP_URL_QUERY);
84 $varnish_path = $wpe_path;
85 if (!empty($wpe_query)) {
86 $varnish_path .= '?' . $wpe_query;
87 }
88 if ($url && count($paths) == 1 && $paths[0] == ".*") {
89 return array($varnish_path);
90 }
91 return $paths;
92 };
93 add_filter( 'wpe_purge_varnish_cache_paths', $handler );
94 if (class_exists("WpeCommon")) { // We need to have this check for clients that switch hosts
95 WpeCommon::purge_varnish_cache();
96 }
97 remove_filter( 'wpe_purge_varnish_cache_paths', $handler );
98 } catch (\Exception $e) {
99 // WPE exception
100 }
101 }
102
103 function nitropack_wpe_purge_all() {
104 try {
105 if (class_exists("WpeCommon")) { // We need to have this check for clients that switch hosts
106 WpeCommon::purge_varnish_cache();
107 }
108 } catch (\Exception $e) {
109 // WPE exception
110 }
111 }
112
113 /** Cloudways' Breeze plugin **/
114 function nitropack_cloudways_purge_url($url) {
115 try {
116 $purger = new \NitroPack\SDK\Integrations\Varnish(array("127.0.0.1"), "URLPURGE");
117 $purger->purge($url);
118 } catch (\Exception $e) {
119 // Breeze exception
120 }
121 }
122
123 function nitropack_cloudways_purge_all() {
124 try {
125 $homepage = home_url().'/.*';
126 $purger = new \NitroPack\SDK\Integrations\Varnish(array("127.0.0.1"), "PURGE");
127 $purger->purge($homepage);
128 } catch (\Exception $e) {
129 // Exception
130 }
131 }
132
133 /** SiteGround - Even though they use Nginx we can communicate with it as if it was Varnish **/
134 function nitropack_siteground_purge_url($url) {
135 $url = preg_replace("/^https?:\/\//", "", $url);
136 $url = preg_replace("/^www\./", "", $url);
137 $url = "http://" . $url;
138
139 try {
140 $hosts = ['127.0.0.1'];
141 $purger = new \NitroPack\SDK\Integrations\Varnish($hosts, 'PURGE');
142 $purger->purge($url);
143 } catch (\Exception $e) {}
144
145 return true;
146 }
147
148 function nitropack_siteground_purge_all() {
149 $siteConfig = nitropack_get_site_config();
150 if ($siteConfig && !empty($siteConfig["home_url"])) {
151 return nitropack_siteground_purge_url($siteConfig["home_url"]);
152 }
153 return false;
154 }
155
156 /** GoDaddy WPaaS - Even though they use ApacheTrafficServer we can communicate with it as if it was Varnish **/
157 function nitropack_wpaas_purge_url($url) {
158 if (class_exists('\WPaaS\Plugin')) {
159 update_option( 'gd_system_last_cache_flush', time() );
160 $hosts = [\WPaaS\Plugin::vip()];
161 $url = preg_replace("/^https:\/\//", "http://", $url);
162 $purger = new \NitroPack\SDK\Integrations\Varnish($hosts, 'BAN');
163 $purger->purge($url);
164 return true;
165 }
166
167 return false;
168 }
169
170 function nitropack_wpaas_purge_all() {
171 $siteConfig = nitropack_get_site_config();
172 if ($siteConfig && !empty($siteConfig["home_url"])) {
173 return nitropack_wpaas_purge_url($siteConfig["home_url"]);
174 }
175 return false;
176 }
177
178 /** Kinsta **/
179 function nitropack_kinsta_purge_url($url) {
180 try {
181 $data = array(
182 'single|nitropack' => preg_replace( '@^https?://@', '', $url)
183 );
184 $httpClient = new \NitroPack\HttpClient("https://localhost/kinsta-clear-cache/v2/immediate");
185 $httpClient->setPostData($data);
186 $httpClient->fetch(true, "POST");
187 return true;
188 } catch (\Exception $e) {
189 }
190
191 return false;
192 }
193
194 function nitropack_kinsta_purge_all() {
195 try {
196 $httpClient = new \NitroPack\HttpClient("https://localhost/kinsta-clear-cache-all");
197 $httpClient->timeout = 5;
198 $httpClient->fetch();
199 return true;
200 } catch (\Exception $e) {
201 }
202
203 return false;
204 }
205
206 /** Flywheel Varnish **/
207 function nitropack_flywheel_varnish_instance($type) {
208 return new \NitroPack\SDK\Integrations\Varnish(array('127.0.0.1'), 'PURGE');
209 }
210
211 /** Generic Varnish **/
212 function nitropack_varnish_generic_instance($type) {
213 $varnishConfig = nitropack_get_varnish_settings();
214 $purgeMethod = ($type == 'single') ? $varnishConfig->PurgeSingleMethod : $varnishConfig->PurgeAllMethod;
215 if (empty($purgeMethod)) $purgeMethod = 'PURGE';
216 return new \NitroPack\SDK\Integrations\Varnish($varnishConfig->Servers, $purgeMethod);
217 }
218
219 function nitropack_varnish_purge_url($url) {
220 try {
221 $purger = apply_filters('nitropack_varnish_purger', 'single');
222 $purger->purge($url);
223 } catch (\Exception $e) {
224 // Exception encountered while trying to purge varnish cache
225 }
226 }
227
228 function nitropack_varnish_purge_all() {
229 try {
230 $purger = apply_filters('nitropack_varnish_purger', 'all');
231 if (function_exists("get_home_url")) {
232 $home = get_home_url();
233 } else {
234 $siteConfig = nitropack_get_site_config();
235 $home = "/";
236 if ($siteConfig && !empty($siteConfig["home_url"])) {
237 $home = $siteConfig["home_url"];
238 }
239 }
240 $purger->purge($home);
241 } catch (\Exception $e) {
242 // Exception encountered while trying to purge varnish cache
243 }
244 }
245
246 function nitropack_get_varnish_settings() {
247 if (null !== $nitro = get_nitropack_sdk()) {
248 $config = $nitro->getConfig();
249 return !empty($config->CacheIntegrations) && $empty($config->CacheIntegrations->Varnish) ? $config->CacheIntegrations->Varnish : null;
250 }
251
252 return null;
253 }
254
255 // Nginx Helper integration
256 function nitropack_is_nginx_helper_active() {
257 return defined('NGINX_HELPER_BASEPATH');
258 }
259
260 function nitropack_nginx_helper_purge_url($url) {
261 global $nginx_purger;
262 if ($nginx_purger) {
263 $nginx_purger->purge_url($url);
264 }
265 return true;
266 }
267
268 function nitropack_nginx_helper_purge_all() {
269 global $nginx_purger;
270 if ($nginx_purger) {
271 $nginx_purger->purge_all();
272 }
273 return true;
274 }
275
276 /** Pagely **/
277 function nitropack_pagely_purge_url($url) {
278 try {
279 $path = parse_url($url, PHP_URL_PATH);
280 if (class_exists("PagelyCachePurge")) { // We need to have this check for clients that switch hosts
281 $pagely = new \PagelyCachePurge();
282 $pagely->purgePath($path . "(.*)");
283 }
284 } catch (\Exception $e) {
285 // Pagely exception
286 }
287 }
288
289 function nitropack_pagely_purge_all() {
290 try {
291 if (class_exists("PagelyCachePurge")) { // We need to have this check for clients that switch hosts
292 $pagely = new \PagelyCachePurge();
293 $pagely->purgeAll();
294 }
295 } catch (\Exception $e) {
296 // Pagely exception
297 }
298 }
299
300 // Ezoic integration
301 function nitropack_is_ezoic_active() {
302 return defined('EZOIC_INTEGRATION_VERSION');
303 }
304
305 function nitropack_disable_ezoic() {
306 global $wp_filter;
307 $hook = "shutdown";
308
309 if ( isset( $wp_filter[$hook]->callbacks ) ) {
310 array_walk( $wp_filter[$hook]->callbacks, function( $callbacks, $priority ) use ( &$hooks ) {
311 foreach ( $callbacks as $id => $callback ) {
312 $cb = $callback["function"];
313 if (is_callable($cb) && is_array($cb) && $cb[1] == "ez_buffer_end") {
314 remove_filter("shutdown", $cb, $priority);
315 register_shutdown_function('ob_end_flush');
316 }
317 }
318 });
319 }
320 }
321
322 function nitropack_ezoic_home_url($url) {
323 $siteConfig = nitropack_get_site_config();
324 if ( $siteConfig && null !== $nitro = get_nitropack_sdk($siteConfig["siteId"], $siteConfig["siteSecret"]) ) {
325 $nitroUrl = $nitro->getUrl();
326 $queryStart = strpos($nitroUrl, "?");
327 if ($queryStart !== false) {
328 return substr($nitroUrl, 0, $queryStart);
329 } else {
330 return $nitroUrl;
331 }
332 }
333
334 return $url;
335 }
336
337 // Download Manager integration
338 function nitropack_is_dlm_active() {
339 return defined('DLM_VERSION');
340 }
341
342 function nitropack_dlm_downloading_url() {
343 $downloadingPage = get_option("dlm_dp_downloading_page");
344 return $downloadingPage ? get_permalink($downloadingPage) : NULL;
345 }
346
347 function nitropack_dlm_download_endpoint() {
348 $downloadEndpoint = get_option("dlm_download_endpoint");
349 return $downloadEndpoint ? nitropack_trailingslashit(get_home_url()) . $downloadEndpoint : NULL;
350 }
351