PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.5.12
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.5.12
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 4 years ago Notifications.php 4 years ago
NitroPack.php
221 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 getSdk($siteId = null, $siteSecret = null, $urlOverride = NULL, $forwardExceptions = false) {
158 $siteConfig = $this->getSiteConfig();
159
160 $siteId = $siteId ? $siteId : ($siteConfig ? $siteConfig['siteId'] : get_option('nitropack-siteId'));
161 $siteSecret = $siteSecret ? $siteSecret : ($siteConfig ? $siteConfig['siteSecret'] : get_option('nitropack-siteSecret'));
162
163 if ($siteId && $siteSecret) {
164 try {
165 $userAgent = NULL; // It will be automatically detected by the SDK
166 $dataDir = nitropack_trailingslashit(NITROPACK_DATA_DIR) . $siteId; // dir without a trailing slash, because this is how the SDK expects it
167 $cacheKey = "{$siteId}:{$siteSecret}:{$dataDir}";
168
169 if ($urlOverride) {
170 $cacheKey .= ":{$urlOverride}";
171 }
172
173 if (!empty($this->sdkObjects[$cacheKey])) {
174 $nitro = $this->sdkObjects[$cacheKey];
175 } else {
176 if (!defined("NP_COOKIE_FILTER")) {
177 \NitroPack\SDK\NitroPack::addCookieFilter("nitropack_filter_non_original_cookies");
178 define("NP_COOKIE_FILTER", true);
179 }
180 if (!defined("NP_STORAGE_CONFIGURED")) {
181 if (defined("NITROPACK_USE_REDIS") && NITROPACK_USE_REDIS) {
182 \NitroPack\SDK\Filesystem::setStorageDriver(new \NitroPack\SDK\StorageDriver\Redis(
183 NITROPACK_REDIS_HOST,
184 NITROPACK_REDIS_PORT,
185 NITROPACK_REDIS_PASS,
186 NITROPACK_REDIS_DB
187 ));
188 }
189 define("NP_STORAGE_CONFIGURED", true);
190 }
191
192 if (!defined('NP_GEOLOCATION_PREFIX_DEFINED')) {
193 do_action('set_nitropack_geo_cache_prefix');
194 define('NP_GEOLOCATION_PREFIX_DEFINED', true);
195 }
196
197 $nitro = new \NitroPack\SDK\NitroPack($siteId, $siteSecret, $userAgent, $urlOverride, $dataDir);
198 $this->sdkObjects[$cacheKey] = $nitro;
199 }
200 } catch (\Exception $e) {
201 if ($forwardExceptions) {
202 throw $e;
203 }
204 return NULL;
205 }
206
207 return $nitro;
208 }
209
210 return NULL;
211 }
212
213 public function dataDirExists() {
214 return defined("NITROPACK_DATA_DIR") && is_dir(NITROPACK_DATA_DIR); // TODO: Convert this to use the Filesystem abstraction for better Redis support
215 }
216
217 public function initDataDir() {
218 return $this->dataDirExists() || @mkdir(NITROPACK_DATA_DIR, 0755, true); // TODO: Convert this to use the Filesystem abstraction for better Redis support
219 }
220 }
221