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