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