PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.5.18
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.5.18
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
257 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
42 if (! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] )) {
43 $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
44 } else {
45 $host = !empty($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : "";
46 }
47
48 $uri = !empty($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : "";
49 $currentUrl = $host . $uri;
50 $matchLength = 0;
51
52 if (stripos($currentUrl, "www.") === 0) {
53 $currentUrl = substr($currentUrl, 4);
54 }
55
56 foreach ($npConfig as $siteUrl => $config) {
57 if (stripos($siteUrl, "www.") === 0) {
58 $siteUrl = substr($siteUrl, 4);
59 }
60
61 if (stripos($currentUrl, $siteUrl) === 0 && strlen($siteUrl) > $matchLength) {
62 $siteConfig = $config;
63 $matchLength = strlen($siteUrl);
64 }
65 }
66 return $siteConfig;
67 }
68
69 public function getSiteId() {
70 $siteConfig = $this->getSiteConfig();
71 return $siteConfig ? $siteConfig["siteId"] : NULL;
72 }
73
74 public function getSiteSecret() {
75 $siteConfig = $this->getSiteConfig();
76 return $siteConfig ? $siteConfig["siteSecret"] : NULL;
77 }
78
79 /**
80 * Bear in mind that get_home_url() is not defined in the context of advanced_cache.php
81 * so this will throw a fatal error if you call it at that point!
82 */
83 public static function getConfigKey() {
84 return preg_replace("/^https?:\/\/(.*)/", "$1", get_home_url());
85 }
86
87 public function isConnected() {
88 return !empty($this->getSiteId()) && !empty($this->getSiteSecret());
89 }
90
91 public function updateCurrentBlogConfig($siteId, $siteSecret, $blogId, $enableCompression = null) {
92 if ($enableCompression === null) {
93 $enableCompression = (get_option('nitropack-enableCompression') == 1);
94 }
95
96 $webhookToken = get_option('nitropack-webhookToken');
97 $hosting = nitropack_detect_hosting();
98
99 $home_url = get_home_url();
100 $admin_url = admin_url();
101 $alwaysBuffer = defined("NITROPACK_ALWAYS_BUFFER") ? NITROPACK_ALWAYS_BUFFER : true;
102 $configKey = self::getConfigKey();
103 $staticConfig = $this->Config->get();
104 $staticConfig[$configKey] = array(
105 "siteId" => $siteId,
106 "siteSecret" => $siteSecret,
107 "blogId" => $blogId,
108 "compression" => $enableCompression,
109 "webhookToken" => $webhookToken,
110 "home_url" => $home_url,
111 "admin_url" => $admin_url,
112 "hosting" => $hosting,
113 "alwaysBuffer" => $alwaysBuffer,
114 "isEzoicActive" => \NitroPack\Integration\Plugin\Ezoic::isActive(),
115 "isApoActive" => \NitroPack\Integration\Plugin\Cloudflare::isApoActive(),
116 "isLateIntegrationInitRequired" => nitropack_is_late_integration_init_required(),
117 "isDlmActive" => \NitroPack\Integration\Plugin\DownloadManager::isActive(),
118 "isWoocommerceCacheHandlerActive" => \NitroPack\Integration\Plugin\WoocommerceCacheHandler::isActive(),
119 "isWoocommerceActive" => \NitroPack\Integration\Plugin\Woocommerce::isActive(),
120 "isAeliaCurrencySwitcherActive" => \NitroPack\Integration\Plugin\AeliaCurrencySwitcher::isActive(),
121 "dlm_downloading_url" => \NitroPack\Integration\Plugin\DownloadManager::isActive() ? \NitroPack\Integration\Plugin\DownloadManager::downloadingUrl() : NULL,
122 "dlm_download_endpoint" => \NitroPack\Integration\Plugin\DownloadManager::isActive() ? \NitroPack\Integration\Plugin\DownloadManager::downloadEndpoint() : NULL,
123 "pluginVersion" => NITROPACK_VERSION,
124 "options_cache" => [],
125 );
126 foreach (self::$optionsToCache as $opt) {
127 if (is_array($opt)) {
128 foreach($opt as $option => $suboption) {
129 if (empty($staticConfig[$configKey]["options_cache"][$option])) {
130 $staticConfig[$configKey]["options_cache"][$option] = [];
131 }
132 $optionValue = get_option($option);
133 if (!empty($optionValue)) {
134 $staticConfig[$configKey]["options_cache"][$option][$suboption] = $optionValue[$suboption];
135 } else {
136 $staticConfig[$configKey]["options_cache"][$option][$suboption] = null;
137 }
138 }
139 } else {
140 $staticConfig[$configKey]["options_cache"][$opt] = get_option($opt);
141 }
142 }
143 $configSetResult = $this->Config->set($staticConfig);
144
145 if (\NitroPack\Integration\Plugin\AeliaCurrencySwitcher::isActive()) {
146 try {
147 \NitroPack\Integration\Plugin\AeliaCurrencySwitcher::configureVariationCookies();
148 } catch (\Exception $e) {
149 // TODO: Log this error
150 }
151 }
152
153 return $configSetResult;
154 }
155
156 public function unsetCurrentBlogConfig() {
157 $configKey = self::getConfigKey();
158 $staticConfig = $this->Config->get();
159 if (!empty($staticConfig[$configKey])) {
160 unset($staticConfig[$configKey]);
161 return $this->Config->set($staticConfig);
162 }
163
164 return true;
165 }
166
167 public function resetSdkInstances() {
168 $this->sdkObjects = [];
169 }
170
171 public function getSdk($siteId = null, $siteSecret = null, $urlOverride = NULL, $forwardExceptions = false) {
172 $siteConfig = $this->getSiteConfig();
173
174 $siteId = $siteId ?: (!empty($siteConfig) ? $siteConfig['siteId'] : NULL);
175 $siteSecret = $siteSecret ?: (!empty($siteConfig) ? $siteConfig['siteSecret'] : NULL);
176
177 if ($siteId && $siteSecret) {
178 try {
179 $userAgent = NULL; // It will be automatically detected by the SDK
180 $dataDir = nitropack_trailingslashit(NITROPACK_DATA_DIR) . $siteId; // dir without a trailing slash, because this is how the SDK expects it
181 $cacheKey = "{$siteId}:{$siteSecret}:{$dataDir}";
182
183 if ($urlOverride) {
184 $cacheKey .= ":{$urlOverride}";
185 }
186
187 if (!empty($this->sdkObjects[$cacheKey])) {
188 $nitro = $this->sdkObjects[$cacheKey];
189 } else {
190 if (!defined("NP_COOKIE_FILTER")) {
191 \NitroPack\SDK\NitroPack::addCookieFilter("nitropack_filter_non_original_cookies");
192 define("NP_COOKIE_FILTER", true);
193 }
194 if (!defined("NP_STORAGE_CONFIGURED")) {
195 if (defined("NITROPACK_USE_REDIS") && NITROPACK_USE_REDIS) {
196 \NitroPack\SDK\Filesystem::setStorageDriver(new \NitroPack\SDK\StorageDriver\Redis(
197 NITROPACK_REDIS_HOST,
198 NITROPACK_REDIS_PORT,
199 NITROPACK_REDIS_PASS,
200 NITROPACK_REDIS_DB
201 ));
202 }
203 define("NP_STORAGE_CONFIGURED", true);
204 }
205
206 if (!defined('NP_GEOLOCATION_PREFIX_DEFINED')) {
207 do_action('set_nitropack_geo_cache_prefix');
208 define('NP_GEOLOCATION_PREFIX_DEFINED', true);
209 }
210
211 if (defined("NITROPACK_CUSTOM_CACHE_PREFIX") && !defined('NP_CUSTOM_CACHE_PREFIX_SET')) {
212 \NitroPack\SDK\NitroPack::addCustomCachePrefix(NITROPACK_CUSTOM_CACHE_PREFIX);
213 define('NP_CUSTOM_CACHE_PREFIX_SET', true);
214 }
215
216 $nitro = new \NitroPack\SDK\NitroPack($siteId, $siteSecret, $userAgent, $urlOverride, $dataDir);
217 $this->sdkObjects[$cacheKey] = $nitro;
218 }
219 } catch (\Exception $e) {
220 if ($forwardExceptions) {
221 throw $e;
222 }
223 return NULL;
224 }
225
226 return $nitro;
227 }
228
229 return NULL;
230 }
231
232 public function dataDirExists() {
233 return defined("NITROPACK_DATA_DIR") && is_dir(NITROPACK_DATA_DIR); // TODO: Convert this to use the Filesystem abstraction for better Redis support
234 }
235
236 public function initDataDir() {
237 return $this->dataDirExists() || @mkdir(NITROPACK_DATA_DIR, 0755, true); // TODO: Convert this to use the Filesystem abstraction for better Redis support
238 }
239
240 public function setDisabledReason($reason) {
241 $this->disabledReason = $reason;
242 nitropack_header("X-Nitro-Disabled-Reason: $reason");
243 }
244
245 public function getDisabledReason() {
246 return $this->disabledReason;
247 }
248
249 public function setPageType($type) {
250 $this->pageType = $type;
251 }
252
253 public function getPageType() {
254 return $this->pageType;
255 }
256 }
257