PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.6.1
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.6.1
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
325 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 "isGeoTargetingWPActive" => \NitroPack\Integration\Plugin\GeoTargetingWP::isActive(),
142 "dlm_downloading_url" => \NitroPack\Integration\Plugin\DownloadManager::isActive() ? \NitroPack\Integration\Plugin\DownloadManager::downloadingUrl() : NULL,
143 "dlm_download_endpoint" => \NitroPack\Integration\Plugin\DownloadManager::isActive() ? \NitroPack\Integration\Plugin\DownloadManager::downloadEndpoint() : NULL,
144 "pluginVersion" => NITROPACK_VERSION,
145 "options_cache" => [],
146 "additional_domains" => $this->getAdditionalDomains($siteId, $siteSecret),
147 );
148 foreach (self::$optionsToCache as $opt) {
149 if (is_array($opt)) {
150 foreach($opt as $option => $suboption) {
151 if (empty($staticConfig[$configKey]["options_cache"][$option])) {
152 $staticConfig[$configKey]["options_cache"][$option] = [];
153 }
154 $optionValue = get_option($option);
155 if (!empty($optionValue)) {
156 $staticConfig[$configKey]["options_cache"][$option][$suboption] = $optionValue[$suboption];
157 } else {
158 $staticConfig[$configKey]["options_cache"][$option][$suboption] = null;
159 }
160 }
161 } else {
162 $staticConfig[$configKey]["options_cache"][$opt] = get_option($opt);
163 }
164 }
165 $configSetResult = $this->Config->set($staticConfig);
166
167 if (\NitroPack\Integration\Plugin\AeliaCurrencySwitcher::isActive()) {
168 try {
169 \NitroPack\Integration\Plugin\AeliaCurrencySwitcher::configureVariationCookies();
170 } catch (\Exception $e) {
171 // TODO: Log this error
172 }
173 }
174 if (\NitroPack\Integration\Plugin\GeoTargetingWP::isActive()) {
175 try {
176 \NitroPack\Integration\Plugin\GeoTargetingWP::configureVariationCookies();
177 } catch (\Exception $e) {
178 // TODO: Log this error
179 }
180 }
181
182 return $configSetResult;
183 }
184
185 public function unsetCurrentBlogConfig() {
186 $configKey = self::getConfigKey();
187 $staticConfig = $this->Config->get();
188 if (!empty($staticConfig[$configKey])) {
189 unset($staticConfig[$configKey]);
190 return $this->Config->set($staticConfig);
191 }
192
193 return true;
194 }
195
196 public function resetSdkInstances() {
197 $this->sdkObjects = [];
198 }
199
200 public function getSdk($siteId = null, $siteSecret = null, $urlOverride = NULL, $forwardExceptions = false) {
201 $siteConfig = $this->getSiteConfig();
202
203 $siteId = $siteId ?: (!empty($siteConfig) ? $siteConfig['siteId'] : NULL);
204 $siteSecret = $siteSecret ?: (!empty($siteConfig) ? $siteConfig['siteSecret'] : NULL);
205
206 if ($siteId && $siteSecret) {
207 try {
208 $userAgent = NULL; // It will be automatically detected by the SDK
209 $dataDir = nitropack_trailingslashit(NITROPACK_DATA_DIR) . $siteId; // dir without a trailing slash, because this is how the SDK expects it
210 $cacheKey = "{$siteId}:{$siteSecret}:{$dataDir}";
211
212 if ($urlOverride) {
213 $cacheKey .= ":{$urlOverride}";
214 }
215
216 if (!empty($this->sdkObjects[$cacheKey])) {
217 $nitro = $this->sdkObjects[$cacheKey];
218 } else {
219 if (!defined("NP_COOKIE_FILTER")) {
220 \NitroPack\SDK\NitroPack::addCookieFilter("nitropack_filter_non_original_cookies");
221 define("NP_COOKIE_FILTER", true);
222 do_action('np_set_cookie_filter');
223 }
224 if (!defined("NP_STORAGE_CONFIGURED")) {
225 if (defined("NITROPACK_USE_REDIS") && NITROPACK_USE_REDIS) {
226 \NitroPack\SDK\Filesystem::setStorageDriver(new \NitroPack\SDK\StorageDriver\Redis(
227 NITROPACK_REDIS_HOST,
228 NITROPACK_REDIS_PORT,
229 NITROPACK_REDIS_PASS,
230 NITROPACK_REDIS_DB
231 ));
232 }
233 define("NP_STORAGE_CONFIGURED", true);
234 }
235
236 if (!defined('NP_GEOLOCATION_PREFIX_DEFINED')) {
237 do_action('set_nitropack_geo_cache_prefix');
238 define('NP_GEOLOCATION_PREFIX_DEFINED', true);
239 }
240
241 if (defined("NITROPACK_CUSTOM_CACHE_PREFIX") && !defined('NP_CUSTOM_CACHE_PREFIX_SET')) {
242 \NitroPack\SDK\NitroPack::addCustomCachePrefix(NITROPACK_CUSTOM_CACHE_PREFIX);
243 define('NP_CUSTOM_CACHE_PREFIX_SET', true);
244 }
245
246 $nitro = new \NitroPack\SDK\NitroPack($siteId, $siteSecret, $userAgent, $urlOverride, $dataDir);
247 $this->sdkObjects[$cacheKey] = $nitro;
248 }
249 } catch (\Exception $e) {
250 if ($forwardExceptions) {
251 throw $e;
252 }
253 return NULL;
254 }
255
256 return $nitro;
257 }
258
259 return NULL;
260 }
261
262 public function dataDirExists() {
263 return defined("NITROPACK_DATA_DIR") && is_dir(NITROPACK_DATA_DIR); // TODO: Convert this to use the Filesystem abstraction for better Redis support
264 }
265
266 public function initDataDir() {
267 return $this->dataDirExists() || @mkdir(NITROPACK_DATA_DIR, 0755, true); // TODO: Convert this to use the Filesystem abstraction for better Redis support
268 }
269
270 public function setDisabledReason($reason) {
271 $this->disabledReason = $reason;
272 nitropack_header("X-Nitro-Disabled-Reason: $reason");
273 }
274
275 public function getDisabledReason() {
276 return $this->disabledReason;
277 }
278
279 public function setPageType($type) {
280 $this->pageType = $type;
281 }
282
283 public function getPageType() {
284 return $this->pageType;
285 }
286
287 /**
288 * Get current url
289 *
290 * @return string The current url
291 */
292 public function getCurrentUrl() {
293 if (! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] )) {
294 $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
295 } else {
296 $host = !empty($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : "";
297 }
298
299 $uri = !empty($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : "";
300 $currentUrl = $host . $uri;
301
302 if (empty($currentUrl)) {
303
304 if (function_exists('get_site_url')) {
305 $host = apply_filters('nitropack_current_host', get_site_url());
306 } elseif (function_exists('get_option')) {
307 $host = apply_filters('nitropack_current_host', get_option('siteurl'));
308 }
309
310 if ($host != '') {
311 $site_url = parse_url($host);
312 if (is_array($site_url) && isset($site_url["host"]) && !empty($site_url["host"])) {
313 $currentUrl = $site_url["host"];
314 }
315 }
316 }
317
318 if (stripos($currentUrl, "www.") === 0) {
319 $currentUrl = substr($currentUrl, 4);
320 }
321
322 return $currentUrl;
323 }
324 }
325