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