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