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