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