nitropack
Last commit date
nitropack-sdk
5 years ago
view
5 years ago
advanced-cache.php
5 years ago
cf-helper.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
406 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 ($siteConfig && empty($siteConfig["isLateIntegrationInitRequired"])) { |
| 50 | do_action(NITROPACK_INTEGRATIONS_ACTION); |
| 51 | } |
| 52 | |
| 53 | // This is needed in order to load non-cache-related integrations like the one with ShortPixel and WooCommerce Geo Location. |
| 54 | if (did_action('plugins_loaded')) { |
| 55 | nitropack_init_late_integrations(); |
| 56 | } else { |
| 57 | add_action('plugins_loaded', 'nitropack_init_late_integrations'); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | function nitropack_init_late_integrations() { |
| 62 | if (defined("NITROPACK_LATE_INTEGRATIONS")) return; |
| 63 | define("NITROPACK_LATE_INTEGRATIONS", true); |
| 64 | |
| 65 | // Cache related integrations |
| 66 | if (nitropack_is_nginx_helper_active()) { |
| 67 | add_action('nitropack_integration_purge_url', 'nitropack_nginx_helper_purge_url'); |
| 68 | add_action('nitropack_integration_purge_all', 'nitropack_nginx_helper_purge_all'); |
| 69 | } |
| 70 | |
| 71 | if (nitropack_is_apo_active()) { |
| 72 | add_action('nitropack_integration_purge_url', 'nitropack_apo_purge_url'); |
| 73 | add_action('nitropack_integration_purge_all', 'nitropack_apo_purge_all'); |
| 74 | } |
| 75 | |
| 76 | // Non cache related integrations |
| 77 | if (defined('SHORTPIXEL_AI_VERSION')) { // ShortPixel |
| 78 | if (nitropack_is_ajax()) { |
| 79 | if (version_compare(SHORTPIXEL_AI_VERSION, "2", ">=")) { // ShortPixel AI 2.x |
| 80 | remove_action('wp_enqueue_scripts', array(ShortPixelAI::_(), 'enqueue_script')); |
| 81 | remove_action('init', array(ShortPixelAI::_(), 'init_ob'), 1); |
| 82 | remove_filter('script_loader_tag', array(ShortPixelAI::_(), 'disable_rocket-Loader'), 10); |
| 83 | } else { // ShortPixel AI 1.x |
| 84 | remove_action('wp_enqueue_scripts', array(ShortPixelAI::instance(SHORTPIXEL_AI_PLUGIN_FILE), 'enqueue_script'), 11); |
| 85 | remove_action('init', array(ShortPixelAI::instance(SHORTPIXEL_AI_PLUGIN_FILE), 'init_ob'), 1); |
| 86 | remove_filter('rocket_css_content', array(ShortPixelAI::instance(SHORTPIXEL_AI_PLUGIN_FILE), 'parse_cached_css'), 10); |
| 87 | remove_filter('script_loader_tag', array(ShortPixelAI::instance(SHORTPIXEL_AI_PLUGIN_FILE), 'disable_rocket-Loader'), 10); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (class_exists("WC_Cache_Helper")) { |
| 93 | remove_action('template_redirect', array('WC_Cache_Helper', 'geolocation_ajax_redirect')); |
| 94 | } |
| 95 | |
| 96 | $siteConfig = nitropack_get_site_config(); |
| 97 | if ($siteConfig && !empty($siteConfig["isLateIntegrationInitRequired"])) { |
| 98 | do_action(NITROPACK_INTEGRATIONS_ACTION); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** WP Engine **/ |
| 103 | function nitropack_wpe_purge_url($url) { |
| 104 | try { |
| 105 | $handler = function($paths) use($url) { |
| 106 | $wpe_path = parse_url($url, PHP_URL_PATH); |
| 107 | $wpe_query = parse_url($url, PHP_URL_QUERY); |
| 108 | $varnish_path = $wpe_path; |
| 109 | if (!empty($wpe_query)) { |
| 110 | $varnish_path .= '?' . $wpe_query; |
| 111 | } |
| 112 | if ($url && count($paths) == 1 && $paths[0] == ".*") { |
| 113 | return array($varnish_path); |
| 114 | } |
| 115 | return $paths; |
| 116 | }; |
| 117 | add_filter( 'wpe_purge_varnish_cache_paths', $handler ); |
| 118 | if (class_exists("WpeCommon")) { // We need to have this check for clients that switch hosts |
| 119 | WpeCommon::purge_varnish_cache(); |
| 120 | } |
| 121 | remove_filter( 'wpe_purge_varnish_cache_paths', $handler ); |
| 122 | } catch (\Exception $e) { |
| 123 | // WPE exception |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | function nitropack_wpe_purge_all() { |
| 128 | try { |
| 129 | if (class_exists("WpeCommon")) { // We need to have this check for clients that switch hosts |
| 130 | WpeCommon::purge_varnish_cache(); |
| 131 | } |
| 132 | } catch (\Exception $e) { |
| 133 | // WPE exception |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** Cloudways' Breeze plugin **/ |
| 138 | function nitropack_cloudways_purge_url($url) { |
| 139 | try { |
| 140 | $purger = new \NitroPack\SDK\Integrations\Varnish(array("127.0.0.1"), "URLPURGE"); |
| 141 | $purger->purge($url); |
| 142 | } catch (\Exception $e) { |
| 143 | // Breeze exception |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | function nitropack_cloudways_purge_all() { |
| 148 | try { |
| 149 | $homepage = home_url().'/.*'; |
| 150 | $purger = new \NitroPack\SDK\Integrations\Varnish(array("127.0.0.1"), "PURGE"); |
| 151 | $purger->purge($homepage); |
| 152 | } catch (\Exception $e) { |
| 153 | // Exception |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** SiteGround - Even though they use Nginx we can communicate with it as if it was Varnish **/ |
| 158 | function nitropack_siteground_purge_url($url) { |
| 159 | $url = preg_replace("/^https?:\/\//", "", $url); |
| 160 | $url = preg_replace("/^www\./", "", $url); |
| 161 | $url = "http://" . $url; |
| 162 | |
| 163 | try { |
| 164 | $hosts = ['127.0.0.1']; |
| 165 | $purger = new \NitroPack\SDK\Integrations\Varnish($hosts, 'PURGE'); |
| 166 | $purger->purge($url); |
| 167 | } catch (\Exception $e) {} |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | function nitropack_siteground_purge_all() { |
| 173 | $siteConfig = nitropack_get_site_config(); |
| 174 | if ($siteConfig && !empty($siteConfig["home_url"])) { |
| 175 | return nitropack_siteground_purge_url($siteConfig["home_url"]); |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | /** GoDaddy WPaaS - Even though they use ApacheTrafficServer we can communicate with it as if it was Varnish **/ |
| 181 | function nitropack_wpaas_purge_url($url) { |
| 182 | if (class_exists('\WPaaS\Plugin')) { |
| 183 | update_option( 'gd_system_last_cache_flush', time() ); |
| 184 | $hosts = [\WPaaS\Plugin::vip()]; |
| 185 | $url = preg_replace("/^https:\/\//", "http://", $url); |
| 186 | $purger = new \NitroPack\SDK\Integrations\Varnish($hosts, 'BAN'); |
| 187 | $purger->purge($url); |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | function nitropack_wpaas_purge_all() { |
| 195 | $siteConfig = nitropack_get_site_config(); |
| 196 | if ($siteConfig && !empty($siteConfig["home_url"])) { |
| 197 | return nitropack_wpaas_purge_url($siteConfig["home_url"]); |
| 198 | } |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | /** Kinsta **/ |
| 203 | function nitropack_kinsta_purge_url($url) { |
| 204 | try { |
| 205 | $data = array( |
| 206 | 'single|nitropack' => preg_replace( '@^https?://@', '', $url) |
| 207 | ); |
| 208 | $httpClient = new \NitroPack\HttpClient("https://localhost/kinsta-clear-cache/v2/immediate"); |
| 209 | $httpClient->setPostData($data); |
| 210 | $httpClient->fetch(true, "POST"); |
| 211 | return true; |
| 212 | } catch (\Exception $e) { |
| 213 | } |
| 214 | |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | function nitropack_kinsta_purge_all() { |
| 219 | try { |
| 220 | $httpClient = new \NitroPack\HttpClient("https://localhost/kinsta-clear-cache-all"); |
| 221 | $httpClient->timeout = 5; |
| 222 | $httpClient->fetch(); |
| 223 | return true; |
| 224 | } catch (\Exception $e) { |
| 225 | } |
| 226 | |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | /** Flywheel Varnish **/ |
| 231 | function nitropack_flywheel_varnish_instance($type) { |
| 232 | return new \NitroPack\SDK\Integrations\Varnish(array('127.0.0.1'), 'PURGE'); |
| 233 | } |
| 234 | |
| 235 | /** Generic Varnish **/ |
| 236 | function nitropack_varnish_generic_instance($type) { |
| 237 | $varnishConfig = nitropack_get_varnish_settings(); |
| 238 | $purgeMethod = ($type == 'single') ? $varnishConfig->PurgeSingleMethod : $varnishConfig->PurgeAllMethod; |
| 239 | if (empty($purgeMethod)) $purgeMethod = 'PURGE'; |
| 240 | return new \NitroPack\SDK\Integrations\Varnish($varnishConfig->Servers, $purgeMethod); |
| 241 | } |
| 242 | |
| 243 | function nitropack_varnish_purge_url($url) { |
| 244 | try { |
| 245 | $purger = apply_filters('nitropack_varnish_purger', 'single'); |
| 246 | $purger->purge($url); |
| 247 | } catch (\Exception $e) { |
| 248 | // Exception encountered while trying to purge varnish cache |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | function nitropack_varnish_purge_all() { |
| 253 | try { |
| 254 | $purger = apply_filters('nitropack_varnish_purger', 'all'); |
| 255 | if (function_exists("get_home_url")) { |
| 256 | $home = get_home_url(); |
| 257 | } else { |
| 258 | $siteConfig = nitropack_get_site_config(); |
| 259 | $home = "/"; |
| 260 | if ($siteConfig && !empty($siteConfig["home_url"])) { |
| 261 | $home = $siteConfig["home_url"]; |
| 262 | } |
| 263 | } |
| 264 | $purger->purge($home); |
| 265 | } catch (\Exception $e) { |
| 266 | // Exception encountered while trying to purge varnish cache |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | function nitropack_get_varnish_settings() { |
| 271 | if (null !== $nitro = get_nitropack_sdk()) { |
| 272 | $config = $nitro->getConfig(); |
| 273 | return !empty($config->CacheIntegrations) && $empty($config->CacheIntegrations->Varnish) ? $config->CacheIntegrations->Varnish : null; |
| 274 | } |
| 275 | |
| 276 | return null; |
| 277 | } |
| 278 | |
| 279 | // Nginx Helper integration |
| 280 | function nitropack_is_nginx_helper_active() { |
| 281 | return defined('NGINX_HELPER_BASEPATH'); |
| 282 | } |
| 283 | |
| 284 | function nitropack_nginx_helper_purge_url($url) { |
| 285 | global $nginx_purger; |
| 286 | if ($nginx_purger) { |
| 287 | $nginx_purger->purge_url($url); |
| 288 | } |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | function nitropack_nginx_helper_purge_all() { |
| 293 | global $nginx_purger; |
| 294 | if ($nginx_purger) { |
| 295 | $nginx_purger->purge_all(); |
| 296 | } |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | // Cloudflare APO integration |
| 301 | function nitropack_is_apo_active() { |
| 302 | if (defined('CLOUDFLARE_PLUGIN_DIR')) { |
| 303 | require_once NITROPACK_PLUGIN_DIR . 'cf-helper.php'; |
| 304 | $cfHelper = new NitroPack_CF_Helper(); |
| 305 | return $cfHelper->isApoEnabled(); |
| 306 | } else { |
| 307 | return false; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | function nitropack_apo_purge_url($url) { |
| 312 | if (defined('CLOUDFLARE_PLUGIN_DIR')) { |
| 313 | require_once NITROPACK_PLUGIN_DIR . 'cf-helper.php'; |
| 314 | $cfHelper = new NitroPack_CF_Helper(); |
| 315 | return $cfHelper->purgeUrl($url); |
| 316 | } else { |
| 317 | return false; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | function nitropack_apo_purge_all() { |
| 322 | if (defined('CLOUDFLARE_PLUGIN_DIR')) { |
| 323 | require_once NITROPACK_PLUGIN_DIR . 'cf-helper.php'; |
| 324 | $cfHelper = new NitroPack_CF_Helper(); |
| 325 | return $cfHelper->purgeCacheEverything(); |
| 326 | } else { |
| 327 | return false; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /** Pagely **/ |
| 332 | function nitropack_pagely_purge_url($url) { |
| 333 | try { |
| 334 | $path = parse_url($url, PHP_URL_PATH); |
| 335 | if (class_exists("PagelyCachePurge")) { // We need to have this check for clients that switch hosts |
| 336 | $pagely = new \PagelyCachePurge(); |
| 337 | $pagely->purgePath($path . "(.*)"); |
| 338 | } |
| 339 | } catch (\Exception $e) { |
| 340 | // Pagely exception |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | function nitropack_pagely_purge_all() { |
| 345 | try { |
| 346 | if (class_exists("PagelyCachePurge")) { // We need to have this check for clients that switch hosts |
| 347 | $pagely = new \PagelyCachePurge(); |
| 348 | $pagely->purgeAll(); |
| 349 | } |
| 350 | } catch (\Exception $e) { |
| 351 | // Pagely exception |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // Ezoic integration |
| 356 | function nitropack_is_ezoic_active() { |
| 357 | return defined('EZOIC_INTEGRATION_VERSION'); |
| 358 | } |
| 359 | |
| 360 | function nitropack_disable_ezoic() { |
| 361 | global $wp_filter; |
| 362 | $hook = "shutdown"; |
| 363 | |
| 364 | if ( isset( $wp_filter[$hook]->callbacks ) ) { |
| 365 | array_walk( $wp_filter[$hook]->callbacks, function( $callbacks, $priority ) use ( &$hooks ) { |
| 366 | foreach ( $callbacks as $id => $callback ) { |
| 367 | $cb = $callback["function"]; |
| 368 | if (is_callable($cb) && is_array($cb) && $cb[1] == "ez_buffer_end") { |
| 369 | remove_filter("shutdown", $cb, $priority); |
| 370 | register_shutdown_function('ob_end_flush'); |
| 371 | } |
| 372 | } |
| 373 | }); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | function nitropack_ezoic_home_url($url) { |
| 378 | $siteConfig = nitropack_get_site_config(); |
| 379 | if ( $siteConfig && null !== $nitro = get_nitropack_sdk($siteConfig["siteId"], $siteConfig["siteSecret"]) ) { |
| 380 | $nitroUrl = $nitro->getUrl(); |
| 381 | $queryStart = strpos($nitroUrl, "?"); |
| 382 | if ($queryStart !== false) { |
| 383 | return substr($nitroUrl, 0, $queryStart); |
| 384 | } else { |
| 385 | return $nitroUrl; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return $url; |
| 390 | } |
| 391 | |
| 392 | // Download Manager integration |
| 393 | function nitropack_is_dlm_active() { |
| 394 | return defined('DLM_VERSION'); |
| 395 | } |
| 396 | |
| 397 | function nitropack_dlm_downloading_url() { |
| 398 | $downloadingPage = get_option("dlm_dp_downloading_page"); |
| 399 | return $downloadingPage ? get_permalink($downloadingPage) : NULL; |
| 400 | } |
| 401 | |
| 402 | function nitropack_dlm_download_endpoint() { |
| 403 | $downloadEndpoint = get_option("dlm_download_endpoint"); |
| 404 | return $downloadEndpoint ? nitropack_trailingslashit(get_home_url()) . $downloadEndpoint : NULL; |
| 405 | } |
| 406 |