Integration.php
146 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack; |
| 4 | |
| 5 | class Integration { |
| 6 | private static $instance = NULL; |
| 7 | private static $purgeAllPending = false; |
| 8 | private static $purgeUrlPending = []; |
| 9 | private static $isInitialized = false; |
| 10 | private static $isInitializedStage = []; |
| 11 | private static $modules = [ |
| 12 | "NitroPack/Integration/Hosting/Cloudways", |
| 13 | "NitroPack/Integration/Hosting/Flywheel", |
| 14 | "NitroPack/Integration/Hosting/WPEngine", |
| 15 | "NitroPack/Integration/Hosting/SiteGround", |
| 16 | "NitroPack/Integration/Hosting/GoDaddyWPaaS", |
| 17 | "NitroPack/Integration/Hosting/Kinsta", |
| 18 | "NitroPack/Integration/Hosting/Pagely", |
| 19 | "NitroPack/Integration/Server/LiteSpeed", |
| 20 | "NitroPack/Integration/Server/Fastly", |
| 21 | "NitroPack/Integration/Server/Cloudflare", |
| 22 | "NitroPack/Integration/Plugin/NginxHelper", |
| 23 | "NitroPack/Integration/Plugin/Cloudflare", |
| 24 | "NitroPack/Integration/Plugin/ShortPixel", |
| 25 | "NitroPack/Integration/Plugin/WPCacheHelper", |
| 26 | "NitroPack/Integration/Plugin/CookieNotice", |
| 27 | "NitroPack/Integration/Plugin/BeaverBuilder" |
| 28 | ]; |
| 29 | private static $loadedModules = []; |
| 30 | private static $stage = "very_early"; |
| 31 | private $siteConfig = []; |
| 32 | private $purgeUrls = []; |
| 33 | private $fullPurge = false; |
| 34 | |
| 35 | public static function getInstance() { |
| 36 | if (!self::$instance) { |
| 37 | self::$instance = new Integration(); |
| 38 | } |
| 39 | return self::$instance; |
| 40 | } |
| 41 | |
| 42 | public function __construct() { |
| 43 | $this->siteConfig = nitropack_get_site_config(); |
| 44 | } |
| 45 | |
| 46 | public function getHosting() { |
| 47 | return $this->siteConfig && !empty($this->siteConfig["hosting"]) ? $this->siteConfig["hosting"] : "unknown"; |
| 48 | } |
| 49 | |
| 50 | public function init() { |
| 51 | if (self::$isInitialized) return true; |
| 52 | |
| 53 | add_action( 'nitropack_integration_purge_url', [$this, "logUrlPurge"] ); |
| 54 | add_action( 'nitropack_integration_purge_all', [$this, "logFullPurge"] ); |
| 55 | add_action( 'shutdown', [$this, 'executeIntegrationPurges'], -999 ); |
| 56 | |
| 57 | $this->initModules(); // very_early init |
| 58 | |
| 59 | $action = $this->getSetupAction(); |
| 60 | if (did_action($action)) { |
| 61 | $this->initModules(); |
| 62 | } else { |
| 63 | add_action($action, [$this, 'initModules']); |
| 64 | } |
| 65 | |
| 66 | self::$isInitialized = true; |
| 67 | } |
| 68 | |
| 69 | public function logUrlPurge($url) { |
| 70 | $this->purgeUrls[] = $url; |
| 71 | } |
| 72 | |
| 73 | public function logFullPurge() { |
| 74 | $this->fullPurge = true; |
| 75 | } |
| 76 | |
| 77 | public function initModules() { |
| 78 | if (!empty(self::$isInitializedStage[self::$stage])) return true; |
| 79 | |
| 80 | foreach (self::$modules as $moduleName) { |
| 81 | $module = $this->loadModule($moduleName); |
| 82 | if ($module && $module->init(self::$stage)) { |
| 83 | // Modules which need to be initialized only once return NULL so they don't end up in this array |
| 84 | // This array holds only modules which need to have their init method called for each stage |
| 85 | self::$loadedModules[$moduleName] = $module; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | self::$isInitializedStage[self::$stage] = true; |
| 90 | |
| 91 | if (self::$stage == "very_early") { |
| 92 | self::$stage = "early"; |
| 93 | } else if (self::$stage == "early") { |
| 94 | if ($this->siteConfig && empty($this->siteConfig["isLateIntegrationInitRequired"])) { |
| 95 | do_action(NITROPACK_INTEGRATIONS_ACTION); |
| 96 | } |
| 97 | |
| 98 | // This is needed in order to load non-cache-related integrations like the one with ShortPixel and WooCommerce Geo Location. |
| 99 | if (did_action('plugins_loaded')) { |
| 100 | $this->lateInitModules(); |
| 101 | } else { |
| 102 | add_action('plugins_loaded', [$this, 'lateInitModules']); |
| 103 | } |
| 104 | } else { |
| 105 | if ($this->siteConfig && !empty($this->siteConfig["isLateIntegrationInitRequired"])) { |
| 106 | do_action(NITROPACK_INTEGRATIONS_ACTION); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public function lateInitModules() { |
| 112 | self::$stage = "late"; |
| 113 | $this->initModules(); |
| 114 | } |
| 115 | |
| 116 | public function executeIntegrationPurges() { |
| 117 | if ($this->fullPurge) { |
| 118 | do_action("nitropack_execute_purge_all"); |
| 119 | } else { |
| 120 | foreach (array_unique($this->purgeUrls) as $url) { |
| 121 | do_action("nitropack_execute_purge_url", $url); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private function loadModule($name) { |
| 127 | if (isset(self::$loadedModules[$name])) return self::$loadedModules[$name]; |
| 128 | |
| 129 | $class = str_replace("/", "\\", $name); |
| 130 | if ($class::STAGE == self::$stage) { |
| 131 | $module = new $class(); |
| 132 | return $module; |
| 133 | } else { |
| 134 | return NULL; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private function getSetupAction() { |
| 139 | if ($this->siteConfig && !empty($this->siteConfig["isLateIntegrationInitRequired"])) { |
| 140 | return "plugins_loaded"; |
| 141 | } |
| 142 | |
| 143 | return "muplugins_loaded"; |
| 144 | } |
| 145 | } |
| 146 |