PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.18.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.18.0
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 / ModuleHandler.php
nitropack / classes Last commit date
Feature 1 year ago Integration 1 year ago Interfaces 2 years ago Util 2 years ago WordPress 1 year ago CLI.php 1 year ago ModuleHandler.php 1 year ago PluginStateHandler.php 1 year ago
ModuleHandler.php
214 lines
1 <?php
2
3 namespace NitroPack;
4
5 class ModuleHandler {
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/Hosting/Pressable",
25 "NitroPack/Integration/Hosting/RocketNet",
26 "NitroPack/Integration/Hosting/Savvii",
27 "NitroPack/Integration/Hosting/DreamHost",
28 "NitroPack/Integration/Hosting/WPX",
29 "NitroPack/Integration/Hosting/WPmudev",
30 "NitroPack/Integration/Hosting/Raidboxes",
31 "NitroPack/Integration/Hosting/Pantheon",
32 "NitroPack/Integration/Hosting/SpinupWp",
33 "NitroPack/Integration/Server/LiteSpeed",
34 "NitroPack/Integration/Server/Fastly",
35 "NitroPack/Integration/Server/Cloudflare",
36 "NitroPack/Integration/Server/Sucuri",
37 "NitroPack/Integration/Server/NginxFastCgi",
38 Integration\Server\NestifyCDN::class,
39 "NitroPack/Integration/Plugin/NginxHelper",
40 "NitroPack/Integration/Plugin/Cloudflare",
41 "NitroPack/Integration/Plugin/ShortPixel",
42 "NitroPack/Integration/Plugin/WPCacheHelper",
43 "NitroPack/Integration/Plugin/CookieNotice",
44 "NitroPack/Integration/Plugin/BeaverBuilder",
45 "NitroPack/Integration/Plugin/FusionBuilder",
46 "NitroPack/Integration/Plugin/ThriveTheme",
47 "NitroPack/Integration/Plugin/AeliaCurrencySwitcher",
48 Integration\Plugin\CURCY_MultiCurrency::class,
49 Integration\Plugin\WooCommerce::class,
50 "NitroPack/Integration/Plugin/WoocommerceCacheHandler",
51 "NitroPack/Integration/Plugin/AdvancedMathCaptcha",
52 "NitroPack/Integration/Plugin/TheEventsCalendar",
53 "NitroPack/Integration/Plugin/WCML",
54 "NitroPack/Integration/Plugin/YoastSEO",
55 "NitroPack/Integration/Plugin/JetPackNP",
56 "NitroPack/Integration/Plugin/SquirrlySEO",
57 "NitroPack/Integration/Plugin/RankMathNP",
58 "NitroPack/Integration/Plugin/WPBakeryNP",
59 //"NitroPack/Integration/Plugin/GravityForms",
60 "NitroPack/Integration/Plugin/Elementor",
61 "NitroPack/Integration/Plugin/WPForms",
62 "NitroPack/Integration/Plugin/GeoTargetingWP",
63 "NitroPack/Integration/Plugin/WPML",
64 Integration\Plugin\ACF::class,
65 "NitroPack/Feature/SubrequestCache",
66 Feature\AjaxShortcodes::class,
67 ];
68 private static $loadedModules = [];
69 private static $stage = "very_early";
70 private $siteConfig = [];
71 private $purgeUrls = [];
72 private $fullPurge = false;
73
74 public static function getInstance() {
75 if (!self::$instance) {
76 self::$instance = new ModuleHandler();
77 }
78 return self::$instance;
79 }
80
81 public static function onCriticalInit($callback) {
82 if (!did_action(self::CRITICAL_INIT_ACTION)) {
83 add_action(self::CRITICAL_INIT_ACTION, $callback);
84 } else {
85 $callback();
86 }
87 }
88
89 public static function onShutdown($callback) {
90 self::$shutdownCallbacks[] = $callback;
91 }
92
93 public static function initSemAcquire() {
94 self::$criticalInitSemaphore++;
95 }
96
97 public static function initSemRelease() {
98 if (--self::$criticalInitSemaphore < 0) {
99 self::$criticalInitSemaphore = 0;
100 }
101 }
102
103 public function __construct() {
104 $this->siteConfig = nitropack_get_site_config();
105 }
106
107 public function getHosting() {
108 return $this->siteConfig && !empty($this->siteConfig["hosting"]) ? $this->siteConfig["hosting"] : "unknown";
109 }
110
111 public function init() {
112 if (self::$isInitialized) return true;
113
114 add_action( 'nitropack_integration_purge_url', [$this, "logUrlPurge"] );
115 add_action( 'nitropack_integration_purge_all', [$this, "logFullPurge"] );
116 self::onShutdown([$this, 'executeIntegrationPurges']);
117 register_shutdown_function(function() {
118 foreach (self::$shutdownCallbacks as $callback) {
119 $callback();
120 }
121 });
122
123 if ($this->siteConfig && !empty($this->siteConfig["isLateIntegrationInitRequired"])) {
124 self::initSemAcquire();
125 }
126
127 $this->initModules(); // very_early init
128
129 $action = $this->getSetupAction(); // can be muplugins_loaded or plugins_loaded
130 if (did_action($action)) {
131 $this->initModules();
132 } else {
133 add_action($action, [$this, 'initModules']);
134 }
135
136 if (did_action('plugins_loaded')) {
137 $this->lateInitModules();
138 } else {
139 add_action('plugins_loaded', [$this, 'lateInitModules']);
140 }
141
142 self::$isInitialized = true;
143 }
144
145 public function logUrlPurge($url) {
146 $this->purgeUrls[] = $url;
147 }
148
149 public function logFullPurge() {
150 $this->fullPurge = true;
151 }
152
153 public function initModules() {
154 if (!empty(self::$isInitializedStage[self::$stage])) return true;
155
156 foreach (self::$modules as $moduleName) {
157 $module = $this->loadModule($moduleName);
158 if ($module && $module->init(self::$stage)) {
159 // Modules which need to be initialized only once return NULL so they don't end up in this array
160 // This array holds only modules which need to have their init method called for each stage
161 self::$loadedModules[$moduleName] = $module;
162 }
163 }
164
165 self::$isInitializedStage[self::$stage] = true;
166
167 if (self::$criticalInitSemaphore < 1 && !did_action(self::CRITICAL_INIT_ACTION)) {
168 do_action(self::CRITICAL_INIT_ACTION);
169 }
170
171 if (self::$stage == "very_early") {
172 self::$stage = "early";
173 }
174 }
175
176 public function lateInitModules() {
177 self::$stage = "late";
178 if ($this->siteConfig && !empty($this->siteConfig["isLateIntegrationInitRequired"])) {
179 self::initSemRelease();
180 }
181 $this->initModules();
182 }
183
184 public function executeIntegrationPurges() {
185 if ($this->fullPurge) {
186 do_action("nitropack_execute_purge_all");
187 } else {
188 foreach (array_unique($this->purgeUrls) as $url) {
189 do_action("nitropack_execute_purge_url", $url);
190 }
191 }
192 }
193
194 private function loadModule($name) {
195 if (isset(self::$loadedModules[$name])) return self::$loadedModules[$name];
196
197 $class = str_replace("/", "\\", $name);
198 if ($class::STAGE == self::$stage) {
199 $module = new $class();
200 return $module;
201 } else {
202 return NULL;
203 }
204 }
205
206 private function getSetupAction() {
207 if ($this->siteConfig && !empty($this->siteConfig["isLateIntegrationInitRequired"])) {
208 return "plugins_loaded";
209 }
210
211 return "muplugins_loaded";
212 }
213 }
214