PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.5.7
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.5.7
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 / Integration.php
nitropack / classes Last commit date
Integration 4 years ago Integration.php 4 years ago
Integration.php
180 lines
1 <?php
2
3 namespace NitroPack;
4
5 class Integration {
6 const CRITICAL_INIT_ACTION = "nitropack_integration_critical_init";
7 public static $criticalInitSemaphore = 0;
8
9 private static $instance = NULL;
10 private static $purgeAllPending = false;
11 private static $purgeUrlPending = [];
12 private static $isInitialized = false;
13 private static $isInitializedStage = [];
14 private static $shutdownCallbacks = [];
15 private static $modules = [
16 "NitroPack/Integration/Hosting/Cloudways",
17 "NitroPack/Integration/Hosting/Flywheel",
18 "NitroPack/Integration/Hosting/WPEngine",
19 "NitroPack/Integration/Hosting/SiteGround",
20 "NitroPack/Integration/Hosting/GoDaddyWPaaS",
21 "NitroPack/Integration/Hosting/Kinsta",
22 "NitroPack/Integration/Hosting/Pagely",
23 "NitroPack/Integration/Hosting/Vimexx",
24 "NitroPack/Integration/Server/LiteSpeed",
25 "NitroPack/Integration/Server/Fastly",
26 "NitroPack/Integration/Server/Cloudflare",
27 "NitroPack/Integration/Plugin/NginxHelper",
28 "NitroPack/Integration/Plugin/Cloudflare",
29 "NitroPack/Integration/Plugin/ShortPixel",
30 "NitroPack/Integration/Plugin/WPCacheHelper",
31 "NitroPack/Integration/Plugin/CookieNotice",
32 "NitroPack/Integration/Plugin/BeaverBuilder"
33 ];
34 private static $loadedModules = [];
35 private static $stage = "very_early";
36 private $siteConfig = [];
37 private $purgeUrls = [];
38 private $fullPurge = false;
39
40 public static function getInstance() {
41 if (!self::$instance) {
42 self::$instance = new Integration();
43 }
44 return self::$instance;
45 }
46
47 public static function onCriticalInit($callback) {
48 if (!did_action(self::CRITICAL_INIT_ACTION)) {
49 add_action(self::CRITICAL_INIT_ACTION, $callback);
50 } else {
51 $callback();
52 }
53 }
54
55 public static function onShutdown($callback) {
56 self::$shutdownCallbacks[] = $callback;
57 }
58
59 public static function initSemAcquire() {
60 self::$criticalInitSemaphore++;
61 }
62
63 public static function initSemRelease() {
64 if (--self::$criticalInitSemaphore < 0) {
65 self::$criticalInitSemaphore = 0;
66 }
67 }
68
69 public function __construct() {
70 $this->siteConfig = nitropack_get_site_config();
71 }
72
73 public function getHosting() {
74 return $this->siteConfig && !empty($this->siteConfig["hosting"]) ? $this->siteConfig["hosting"] : "unknown";
75 }
76
77 public function init() {
78 if (self::$isInitialized) return true;
79
80 add_action( 'nitropack_integration_purge_url', [$this, "logUrlPurge"] );
81 add_action( 'nitropack_integration_purge_all', [$this, "logFullPurge"] );
82 self::onShutdown([$this, 'executeIntegrationPurges']);
83 register_shutdown_function(function() {
84 foreach (self::$shutdownCallbacks as $callback) {
85 $callback();
86 }
87 });
88
89 if ($this->siteConfig && !empty($this->siteConfig["isLateIntegrationInitRequired"])) {
90 self::initSemAcquire();
91 }
92
93 $this->initModules(); // very_early init
94
95 $action = $this->getSetupAction(); // can be muplugins_loaded or plugins_loaded
96 if (did_action($action)) {
97 $this->initModules();
98 } else {
99 add_action($action, [$this, 'initModules']);
100 }
101
102 if (did_action('plugins_loaded')) {
103 $this->lateInitModules();
104 } else {
105 add_action('plugins_loaded', [$this, 'lateInitModules']);
106 }
107
108 self::$isInitialized = true;
109 }
110
111 public function logUrlPurge($url) {
112 $this->purgeUrls[] = $url;
113 }
114
115 public function logFullPurge() {
116 $this->fullPurge = true;
117 }
118
119 public function initModules() {
120 if (!empty(self::$isInitializedStage[self::$stage])) return true;
121
122 foreach (self::$modules as $moduleName) {
123 $module = $this->loadModule($moduleName);
124 if ($module && $module->init(self::$stage)) {
125 // Modules which need to be initialized only once return NULL so they don't end up in this array
126 // This array holds only modules which need to have their init method called for each stage
127 self::$loadedModules[$moduleName] = $module;
128 }
129 }
130
131 self::$isInitializedStage[self::$stage] = true;
132
133 if (self::$criticalInitSemaphore < 1 && !did_action(self::CRITICAL_INIT_ACTION)) {
134 do_action(self::CRITICAL_INIT_ACTION);
135 }
136
137 if (self::$stage == "very_early") {
138 self::$stage = "early";
139 }
140 }
141
142 public function lateInitModules() {
143 self::$stage = "late";
144 if ($this->siteConfig && !empty($this->siteConfig["isLateIntegrationInitRequired"])) {
145 self::initSemRelease();
146 }
147 $this->initModules();
148 }
149
150 public function executeIntegrationPurges() {
151 if ($this->fullPurge) {
152 do_action("nitropack_execute_purge_all");
153 } else {
154 foreach (array_unique($this->purgeUrls) as $url) {
155 do_action("nitropack_execute_purge_url", $url);
156 }
157 }
158 }
159
160 private function loadModule($name) {
161 if (isset(self::$loadedModules[$name])) return self::$loadedModules[$name];
162
163 $class = str_replace("/", "\\", $name);
164 if ($class::STAGE == self::$stage) {
165 $module = new $class();
166 return $module;
167 } else {
168 return NULL;
169 }
170 }
171
172 private function getSetupAction() {
173 if ($this->siteConfig && !empty($this->siteConfig["isLateIntegrationInitRequired"])) {
174 return "plugins_loaded";
175 }
176
177 return "muplugins_loaded";
178 }
179 }
180