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
6 years ago
integrations.php
332 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 | $nginx_purger->purge_url($url); |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | function nitropack_nginx_helper_purge_all() { |
| 267 | global $nginx_purger; |
| 268 | $nginx_purger->purge_all(); |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | /** Pagely **/ |
| 273 | function nitropack_pagely_purge_url($url) { |
| 274 | try { |
| 275 | $path = parse_url($url, PHP_URL_PATH); |
| 276 | if (class_exists("PagelyCachePurge")) { // We need to have this check for clients that switch hosts |
| 277 | $pagely = new \PagelyCachePurge(); |
| 278 | $pagely->purgePath($path . "(.*)"); |
| 279 | } |
| 280 | } catch (\Exception $e) { |
| 281 | // Pagely exception |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | function nitropack_pagely_purge_all() { |
| 286 | try { |
| 287 | if (class_exists("PagelyCachePurge")) { // We need to have this check for clients that switch hosts |
| 288 | $pagely = new \PagelyCachePurge(); |
| 289 | $pagely->purgeAll(); |
| 290 | } |
| 291 | } catch (\Exception $e) { |
| 292 | // Pagely exception |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Ezoic integration |
| 297 | function nitropack_is_ezoic_active() { |
| 298 | return defined('EZOIC_INTEGRATION_VERSION'); |
| 299 | } |
| 300 | |
| 301 | function nitropack_disable_ezoic() { |
| 302 | global $wp_filter; |
| 303 | $hook = "shutdown"; |
| 304 | |
| 305 | if ( isset( $wp_filter[$hook]->callbacks ) ) { |
| 306 | array_walk( $wp_filter[$hook]->callbacks, function( $callbacks, $priority ) use ( &$hooks ) { |
| 307 | foreach ( $callbacks as $id => $callback ) { |
| 308 | $cb = $callback["function"]; |
| 309 | if (is_callable($cb) && is_array($cb) && $cb[1] == "ez_buffer_end") { |
| 310 | remove_filter("shutdown", $cb, $priority); |
| 311 | register_shutdown_function('ob_end_flush'); |
| 312 | } |
| 313 | } |
| 314 | }); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | function nitropack_ezoic_home_url($url) { |
| 319 | $siteConfig = nitropack_get_site_config(); |
| 320 | if ( $siteConfig && null !== $nitro = get_nitropack_sdk($siteConfig["siteId"], $siteConfig["siteSecret"]) ) { |
| 321 | $nitroUrl = $nitro->getUrl(); |
| 322 | $queryStart = strpos($nitroUrl, "?"); |
| 323 | if ($queryStart !== false) { |
| 324 | return substr($nitroUrl, 0, $queryStart); |
| 325 | } else { |
| 326 | return $nitroUrl; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return $url; |
| 331 | } |
| 332 |