PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.5.10
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.5.10
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
172 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 return !empty($this->getSiteId()) && !empty($this->getSiteSecret());
66 }
67
68 public function updateCurrentBlogConfig($siteId, $siteSecret, $blogId, $enableCompression = null) {
69 if ($enableCompression === null) {
70 $enableCompression = (get_option('nitropack-enableCompression') == 1);
71 }
72
73 $webhookToken = get_option('nitropack-webhookToken');
74 $hosting = nitropack_detect_hosting();
75
76 $home_url = get_home_url();
77 $admin_url = admin_url();
78 $alwaysBuffer = defined("NITROPACK_ALWAYS_BUFFER") ? NITROPACK_ALWAYS_BUFFER : true;
79 $configKey = preg_replace("/^https?:\/\/(.*)/", "$1", $home_url);
80 $staticConfig = $this->Config->get();
81 $staticConfig[$configKey] = array(
82 "siteId" => $siteId,
83 "siteSecret" => $siteSecret,
84 "blogId" => $blogId,
85 "compression" => $enableCompression,
86 "webhookToken" => $webhookToken,
87 "home_url" => $home_url,
88 "admin_url" => $admin_url,
89 "hosting" => $hosting,
90 "alwaysBuffer" => $alwaysBuffer,
91 "isEzoicActive" => \NitroPack\Integration\Plugin\Ezoic::isActive(),
92 "isApoActive" => \NitroPack\Integration\Plugin\Cloudflare::isApoActive(),
93 "isLateIntegrationInitRequired" => nitropack_is_late_integration_init_required(),
94 "isDlmActive" => \NitroPack\Integration\Plugin\DownloadManager::isActive(),
95 "dlm_downloading_url" => \NitroPack\Integration\Plugin\DownloadManager::isActive() ? \NitroPack\Integration\Plugin\DownloadManager::downloadingUrl() : NULL,
96 "dlm_download_endpoint" => \NitroPack\Integration\Plugin\DownloadManager::isActive() ? \NitroPack\Integration\Plugin\DownloadManager::downloadEndpoint() : NULL,
97 "pluginVersion" => NITROPACK_VERSION
98 );
99 return $this->Config->set($staticConfig);
100 }
101
102 public function unsetCurrentBlogConfig() {
103 $home_url = get_home_url();
104 $configKey = preg_replace("/^https?:\/\/(.*)/", "$1", $home_url);
105 $staticConfig = $this->Config->get();
106 if (!empty($staticConfig[$configKey])) {
107 unset($staticConfig[$configKey]);
108 return $this->Config->set($staticConfig);
109 }
110
111 return true;
112 }
113
114 public function getSdk($siteId = null, $siteSecret = null, $urlOverride = NULL, $forwardExceptions = false) {
115 $siteConfig = $this->getSiteConfig();
116
117 $siteId = $siteId ? $siteId : ($siteConfig ? $siteConfig['siteId'] : get_option('nitropack-siteId'));
118 $siteSecret = $siteSecret ? $siteSecret : ($siteConfig ? $siteConfig['siteSecret'] : get_option('nitropack-siteSecret'));
119
120 if ($siteId && $siteSecret) {
121 try {
122 $userAgent = NULL; // It will be automatically detected by the SDK
123 $dataDir = nitropack_trailingslashit(NITROPACK_DATA_DIR) . $siteId; // dir without a trailing slash, because this is how the SDK expects it
124 $cacheKey = "{$siteId}:{$siteSecret}:{$dataDir}";
125
126 if ($urlOverride) {
127 $cacheKey .= ":{$urlOverride}";
128 }
129
130 if (!empty($this->sdkObjects[$cacheKey])) {
131 $nitro = $this->sdkObjects[$cacheKey];
132 } else {
133 if (!defined("NP_COOKIE_FILTER")) {
134 \NitroPack\SDK\NitroPack::addCookieFilter("nitropack_filter_non_original_cookies");
135 define("NP_COOKIE_FILTER", true);
136 }
137 if (!defined("NP_STORAGE_CONFIGURED")) {
138 if (defined("NITROPACK_USE_REDIS") && NITROPACK_USE_REDIS) {
139 \NitroPack\SDK\Filesystem::setStorageDriver(new \NitroPack\SDK\StorageDriver\Redis(
140 NITROPACK_REDIS_HOST,
141 NITROPACK_REDIS_PORT,
142 NITROPACK_REDIS_PASS,
143 NITROPACK_REDIS_DB
144 ));
145 }
146 define("NP_STORAGE_CONFIGURED", true);
147 }
148 $nitro = new \NitroPack\SDK\NitroPack($siteId, $siteSecret, $userAgent, $urlOverride, $dataDir);
149 $this->sdkObjects[$cacheKey] = $nitro;
150 }
151 } catch (\Exception $e) {
152 if ($forwardExceptions) {
153 throw $e;
154 }
155 return NULL;
156 }
157
158 return $nitro;
159 }
160
161 return NULL;
162 }
163
164 public function dataDirExists() {
165 return defined("NITROPACK_DATA_DIR") && is_dir(NITROPACK_DATA_DIR); // TODO: Convert this to use the Filesystem abstraction for better Redis support
166 }
167
168 public function initDataDir() {
169 return $this->dataDirExists() || @mkdir(NITROPACK_DATA_DIR, 0755, true); // TODO: Convert this to use the Filesystem abstraction for better Redis support
170 }
171 }
172