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