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