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