NitroPack.php
333 lines
| 1 | <?php |
| 2 | namespace NitroPack\WordPress; |
| 3 | |
| 4 | class NitroPack { |
| 5 | private static $instance = NULL; |
| 6 | public static $preUpdatePosts = array(); |
| 7 | public static $preUpdateTaxonomies = array(); |
| 8 | public static $preUpdateMeta = array(); |
| 9 | public static $ignoreUpdatePostIDs = array(); |
| 10 | public static $optionsToCache = [ |
| 11 | 'cache_handler_cache_handler', |
| 12 | 'woocommerce_default_customer_address', |
| 13 | [ "wc_aelia_currency_switcher" => "ipgeolocation_enabled"] |
| 14 | ]; |
| 15 | |
| 16 | public static function getInstance() { |
| 17 | if (!self::$instance) { |
| 18 | self::$instance = new NitroPack(); |
| 19 | } |
| 20 | |
| 21 | return self::$instance; |
| 22 | } |
| 23 | |
| 24 | private $sdkObjects; |
| 25 | private $disabledReason; |
| 26 | private $pageType; |
| 27 | |
| 28 | public $Config; |
| 29 | public $Notifications; |
| 30 | |
| 31 | public function __construct() { |
| 32 | $this->Config = new Config($this); |
| 33 | $this->Notifications = new Notifications($this); |
| 34 | $this->sdkObjects = array(); |
| 35 | $this->disabledReason = NULL; |
| 36 | $this->pageType = NULL; |
| 37 | } |
| 38 | |
| 39 | public function getSiteConfig() { |
| 40 | $siteConfig = null; |
| 41 | $npConfig = $this->Config->get(); |
| 42 | $currentUrl = $this -> getCurrentUrl(); |
| 43 | |
| 44 | $matchLength = 0; |
| 45 | |
| 46 | foreach ($npConfig as $siteUrl => $config) { |
| 47 | if (stripos($siteUrl, "www.") === 0) { |
| 48 | $siteUrl = substr($siteUrl, 4); |
| 49 | } |
| 50 | |
| 51 | if (stripos($currentUrl, $siteUrl) === 0 && strlen($siteUrl) > $matchLength) { |
| 52 | $siteConfig = $config; |
| 53 | $matchLength = strlen($siteUrl); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if (!$siteConfig) { |
| 58 | $matchLength = 0; |
| 59 | foreach ($npConfig as $siteUrl => $config) { |
| 60 | if (isset($config['additional_domains'])) { |
| 61 | foreach ($config['additional_domains'] as $additionalDomain) { |
| 62 | if (stripos($additionalDomain, "www.") === 0) { |
| 63 | $additionalDomain = substr($additionalDomain, 4); |
| 64 | } |
| 65 | |
| 66 | if (stripos($currentUrl, $additionalDomain) === 0 && strlen($additionalDomain) > $matchLength) { |
| 67 | $siteConfig = $config; |
| 68 | $matchLength = strlen($additionalDomain); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return $siteConfig; |
| 76 | } |
| 77 | |
| 78 | public function getSiteId() { |
| 79 | $siteConfig = $this->getSiteConfig(); |
| 80 | return $siteConfig ? $siteConfig["siteId"] : NULL; |
| 81 | } |
| 82 | |
| 83 | public function getSiteSecret() { |
| 84 | $siteConfig = $this->getSiteConfig(); |
| 85 | return $siteConfig ? $siteConfig["siteSecret"] : NULL; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Bear in mind that get_home_url() is not defined in the context of advanced_cache.php |
| 90 | * so this will throw a fatal error if you call it at that point! |
| 91 | */ |
| 92 | public static function getConfigKey() { |
| 93 | return preg_replace("/^https?:\/\/(.*)/", "$1", get_home_url()); |
| 94 | } |
| 95 | |
| 96 | public function getAdditionalDomains($siteId, $siteSecret) { |
| 97 | if (null !== $nitro = $this->getSdk($siteId, $siteSecret)) { |
| 98 | $config = $nitro->getConfig(); |
| 99 | if (!property_exists($config->AdditionalDomains, 'Domains')) { |
| 100 | $nitro->fetchConfig(); |
| 101 | } |
| 102 | return $config->AdditionalDomains->Domains; |
| 103 | } |
| 104 | |
| 105 | return []; |
| 106 | } |
| 107 | |
| 108 | public function isConnected() { |
| 109 | return !empty($this->getSiteId()) && !empty($this->getSiteSecret()); |
| 110 | } |
| 111 | |
| 112 | public function updateCurrentBlogConfig($siteId, $siteSecret, $blogId, $enableCompression = null) { |
| 113 | if ($enableCompression === null) { |
| 114 | $enableCompression = (get_option('nitropack-enableCompression') == 1); |
| 115 | } |
| 116 | |
| 117 | $webhookToken = get_option('nitropack-webhookToken'); |
| 118 | $hosting = nitropack_detect_hosting(); |
| 119 | |
| 120 | $home_url = get_home_url(); |
| 121 | $admin_url = admin_url(); |
| 122 | $alwaysBuffer = defined("NITROPACK_ALWAYS_BUFFER") ? NITROPACK_ALWAYS_BUFFER : true; |
| 123 | $configKey = self::getConfigKey(); |
| 124 | $staticConfig = $this->Config->get(); |
| 125 | $staticConfig[$configKey] = array( |
| 126 | "siteId" => $siteId, |
| 127 | "siteSecret" => $siteSecret, |
| 128 | "blogId" => $blogId, |
| 129 | "compression" => $enableCompression, |
| 130 | "webhookToken" => $webhookToken, |
| 131 | "home_url" => $home_url, |
| 132 | "admin_url" => $admin_url, |
| 133 | "hosting" => $hosting, |
| 134 | "alwaysBuffer" => $alwaysBuffer, |
| 135 | "isEzoicActive" => \NitroPack\Integration\Plugin\Ezoic::isActive(), |
| 136 | "isApoActive" => \NitroPack\Integration\Plugin\Cloudflare::isApoActive(), |
| 137 | "isNginxHelperActive" => \NitroPack\Integration\Plugin\NginxHelper::isActive(), |
| 138 | "isLateIntegrationInitRequired" => nitropack_is_late_integration_init_required(), |
| 139 | "isDlmActive" => \NitroPack\Integration\Plugin\DownloadManager::isActive(), |
| 140 | "isWoocommerceCacheHandlerActive" => \NitroPack\Integration\Plugin\WoocommerceCacheHandler::isActive(), |
| 141 | "isWoocommerceActive" => \NitroPack\Integration\Plugin\Woocommerce::isActive(), |
| 142 | "isAeliaCurrencySwitcherActive" => \NitroPack\Integration\Plugin\AeliaCurrencySwitcher::isActive(), |
| 143 | "isGeoTargetingWPActive" => \NitroPack\Integration\Plugin\GeoTargetingWP::isActive(), |
| 144 | "dlm_downloading_url" => \NitroPack\Integration\Plugin\DownloadManager::isActive() ? \NitroPack\Integration\Plugin\DownloadManager::downloadingUrl() : NULL, |
| 145 | "dlm_download_endpoint" => \NitroPack\Integration\Plugin\DownloadManager::isActive() ? \NitroPack\Integration\Plugin\DownloadManager::downloadEndpoint() : NULL, |
| 146 | "pluginVersion" => NITROPACK_VERSION, |
| 147 | "options_cache" => [], |
| 148 | "additional_domains" => $this->getAdditionalDomains($siteId, $siteSecret), |
| 149 | ); |
| 150 | foreach (self::$optionsToCache as $opt) { |
| 151 | if (is_array($opt)) { |
| 152 | foreach($opt as $option => $suboption) { |
| 153 | if (empty($staticConfig[$configKey]["options_cache"][$option])) { |
| 154 | $staticConfig[$configKey]["options_cache"][$option] = []; |
| 155 | } |
| 156 | $optionValue = get_option($option); |
| 157 | if (!empty($optionValue)) { |
| 158 | $staticConfig[$configKey]["options_cache"][$option][$suboption] = $optionValue[$suboption]; |
| 159 | } else { |
| 160 | $staticConfig[$configKey]["options_cache"][$option][$suboption] = null; |
| 161 | } |
| 162 | } |
| 163 | } else { |
| 164 | $staticConfig[$configKey]["options_cache"][$opt] = get_option($opt); |
| 165 | } |
| 166 | } |
| 167 | $configSetResult = $this->Config->set($staticConfig); |
| 168 | |
| 169 | if (\NitroPack\Integration\Plugin\AeliaCurrencySwitcher::isActive()) { |
| 170 | try { |
| 171 | \NitroPack\Integration\Plugin\AeliaCurrencySwitcher::configureVariationCookies(); |
| 172 | } catch (\Exception $e) { |
| 173 | // TODO: Log this error |
| 174 | } |
| 175 | } |
| 176 | if (\NitroPack\Integration\Plugin\GeoTargetingWP::isActive()) { |
| 177 | try { |
| 178 | \NitroPack\Integration\Plugin\GeoTargetingWP::configureVariationCookies(); |
| 179 | } catch (\Exception $e) { |
| 180 | // TODO: Log this error |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return $configSetResult; |
| 185 | } |
| 186 | |
| 187 | public function unsetCurrentBlogConfig() { |
| 188 | $configKey = self::getConfigKey(); |
| 189 | $staticConfig = $this->Config->get(); |
| 190 | if (!empty($staticConfig[$configKey])) { |
| 191 | unset($staticConfig[$configKey]); |
| 192 | return $this->Config->set($staticConfig); |
| 193 | } |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | public function resetSdkInstances() { |
| 199 | $this->sdkObjects = []; |
| 200 | } |
| 201 | |
| 202 | public function getSdk($siteId = null, $siteSecret = null, $urlOverride = NULL, $forwardExceptions = false) { |
| 203 | $siteConfig = $this->getSiteConfig(); |
| 204 | |
| 205 | $siteId = $siteId ?: (!empty($siteConfig) ? $siteConfig['siteId'] : NULL); |
| 206 | $siteSecret = $siteSecret ?: (!empty($siteConfig) ? $siteConfig['siteSecret'] : NULL); |
| 207 | |
| 208 | if ($siteId && $siteSecret) { |
| 209 | try { |
| 210 | $userAgent = NULL; // It will be automatically detected by the SDK |
| 211 | $dataDir = nitropack_trailingslashit(NITROPACK_DATA_DIR) . $siteId; // dir without a trailing slash, because this is how the SDK expects it |
| 212 | $cacheKey = "{$siteId}:{$siteSecret}:{$dataDir}"; |
| 213 | |
| 214 | if ($urlOverride) { |
| 215 | $cacheKey .= ":{$urlOverride}"; |
| 216 | } |
| 217 | |
| 218 | if (!empty($this->sdkObjects[$cacheKey])) { |
| 219 | $nitro = $this->sdkObjects[$cacheKey]; |
| 220 | } else { |
| 221 | if (!defined("NP_COOKIE_FILTER")) { |
| 222 | \NitroPack\SDK\NitroPack::addCookieFilter("nitropack_filter_non_original_cookies"); |
| 223 | define("NP_COOKIE_FILTER", true); |
| 224 | do_action('np_set_cookie_filter'); |
| 225 | } |
| 226 | if (!defined("NP_STORAGE_CONFIGURED")) { |
| 227 | if (defined("NITROPACK_USE_REDIS") && NITROPACK_USE_REDIS) { |
| 228 | \NitroPack\SDK\Filesystem::setStorageDriver(new \NitroPack\SDK\StorageDriver\Redis( |
| 229 | NITROPACK_REDIS_HOST, |
| 230 | NITROPACK_REDIS_PORT, |
| 231 | NITROPACK_REDIS_PASS, |
| 232 | NITROPACK_REDIS_DB |
| 233 | )); |
| 234 | } |
| 235 | define("NP_STORAGE_CONFIGURED", true); |
| 236 | } |
| 237 | |
| 238 | if (!defined('NP_GEOLOCATION_PREFIX_DEFINED')) { |
| 239 | do_action('set_nitropack_geo_cache_prefix'); |
| 240 | define('NP_GEOLOCATION_PREFIX_DEFINED', true); |
| 241 | } |
| 242 | |
| 243 | if (defined("NITROPACK_CUSTOM_CACHE_PREFIX") && !defined('NP_CUSTOM_CACHE_PREFIX_SET')) { |
| 244 | \NitroPack\SDK\NitroPack::addCustomCachePrefix(NITROPACK_CUSTOM_CACHE_PREFIX); |
| 245 | define('NP_CUSTOM_CACHE_PREFIX_SET', true); |
| 246 | } |
| 247 | |
| 248 | $nitro = new \NitroPack\SDK\NitroPack($siteId, $siteSecret, $userAgent, $urlOverride, $dataDir); |
| 249 | $this->sdkObjects[$cacheKey] = $nitro; |
| 250 | } |
| 251 | } catch (\Exception $e) { |
| 252 | if ($forwardExceptions) { |
| 253 | throw $e; |
| 254 | } |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | return $nitro; |
| 259 | } |
| 260 | |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | public function dataDirExists() { |
| 265 | return defined("NITROPACK_DATA_DIR") && is_dir(NITROPACK_DATA_DIR); // TODO: Convert this to use the Filesystem abstraction for better Redis support |
| 266 | } |
| 267 | |
| 268 | public function initDataDir() { |
| 269 | return $this->dataDirExists() || @mkdir(NITROPACK_DATA_DIR, 0755, true); // TODO: Convert this to use the Filesystem abstraction for better Redis support |
| 270 | } |
| 271 | |
| 272 | public function setDisabledReason($reason) { |
| 273 | $this->disabledReason = $reason; |
| 274 | nitropack_header("X-Nitro-Disabled-Reason: $reason"); |
| 275 | } |
| 276 | |
| 277 | public function getDisabledReason() { |
| 278 | return $this->disabledReason; |
| 279 | } |
| 280 | |
| 281 | public function setPageType($type) { |
| 282 | $this->pageType = $type; |
| 283 | } |
| 284 | |
| 285 | public function getPageType() { |
| 286 | return $this->pageType; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Get current url |
| 291 | * |
| 292 | * @return string The current url |
| 293 | */ |
| 294 | public function getCurrentUrl() { |
| 295 | |
| 296 | if ( defined('NITROPACK_HOST') ) { |
| 297 | |
| 298 | return NITROPACK_HOST; |
| 299 | } |
| 300 | |
| 301 | if (! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] )) { |
| 302 | $host = $_SERVER['HTTP_X_FORWARDED_HOST']; |
| 303 | } else { |
| 304 | $host = !empty($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : ""; |
| 305 | } |
| 306 | |
| 307 | $uri = !empty($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : ""; |
| 308 | $currentUrl = $host . $uri; |
| 309 | |
| 310 | if (empty($currentUrl) || (defined( 'WP_CLI' ) && WP_CLI && trim($currentUrl) == "localhost")) { |
| 311 | |
| 312 | if (function_exists('get_site_url')) { |
| 313 | $host = apply_filters('nitropack_current_host', get_site_url()); |
| 314 | } elseif (function_exists('get_option')) { |
| 315 | $host = apply_filters('nitropack_current_host', get_option('siteurl')); |
| 316 | } |
| 317 | |
| 318 | if ($host != '') { |
| 319 | $site_url = parse_url($host); |
| 320 | if (is_array($site_url) && isset($site_url["host"]) && !empty($site_url["host"])) { |
| 321 | $currentUrl = $site_url["host"]; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (stripos($currentUrl, "www.") === 0) { |
| 327 | $currentUrl = substr($currentUrl, 4); |
| 328 | } |
| 329 | |
| 330 | return $currentUrl; |
| 331 | } |
| 332 | } |
| 333 |