Database
2 weeks ago
AbstractStagingSetup.php
2 weeks ago
DirectoryScanner.php
2 weeks ago
FileCopier.php
1 week ago
LegacyOptionsCache.php
2 weeks ago
StagingEngine.php
1 day ago
StagingSetup.php
2 weeks ago
TableScanner.php
1 week ago
StagingEngine.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Staging\Service; |
| 4 | |
| 5 | /** |
| 6 | * Stores and validates the preferred staging engine for staging jobs. |
| 7 | */ |
| 8 | class StagingEngine |
| 9 | { |
| 10 | /** @var string */ |
| 11 | const OPTION_NAME = 'wpstg_staging_engine_preference'; |
| 12 | |
| 13 | /** @var string */ |
| 14 | const LEGACY_OPTION_NAME = 'wpstg_staging_engine_preferences'; |
| 15 | |
| 16 | /** @var string */ |
| 17 | const ENGINE_LEGACY = 'legacy'; |
| 18 | |
| 19 | /** @var string */ |
| 20 | const ENGINE_NEXT_GEN = 'next_gen'; |
| 21 | |
| 22 | /** |
| 23 | * Master switch for the Next-Gen engine. Temporarily disabled while the |
| 24 | * data-corruption issue #5346 is fixed. Flip to true to re-enable it |
| 25 | * everywhere (UI, runtime routing and the prepare AJAX endpoints). |
| 26 | * |
| 27 | * @var bool |
| 28 | */ |
| 29 | const NEXT_GEN_ENABLED = false; |
| 30 | |
| 31 | /** @var string[] */ |
| 32 | const ENGINES = [ |
| 33 | self::ENGINE_LEGACY, |
| 34 | self::ENGINE_NEXT_GEN, |
| 35 | ]; |
| 36 | |
| 37 | public function isNextGenEnabled(): bool |
| 38 | { |
| 39 | return self::NEXT_GEN_ENABLED; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Effective engine used at runtime. Coerces Next-Gen to Classic while the |
| 44 | * Next-Gen engine is disabled so all routing falls back to the Classic path. |
| 45 | */ |
| 46 | public function getEngine(): string |
| 47 | { |
| 48 | $engine = $this->getStoredEngine(); |
| 49 | if ($engine === self::ENGINE_NEXT_GEN && !$this->isNextGenEnabled()) { |
| 50 | return self::ENGINE_LEGACY; |
| 51 | } |
| 52 | |
| 53 | return $engine; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Raw engine preference as saved by the user, ignoring the master switch. |
| 58 | * Used by the upgrade routine to detect Next-Gen users before reverting them. |
| 59 | */ |
| 60 | public function getStoredEngine(): string |
| 61 | { |
| 62 | $stored = get_option(self::OPTION_NAME, null); |
| 63 | if ($stored === null) { |
| 64 | $stored = get_option(self::LEGACY_OPTION_NAME, self::ENGINE_LEGACY); |
| 65 | } |
| 66 | |
| 67 | return $this->resolveEngine($stored); |
| 68 | } |
| 69 | |
| 70 | public function saveEngine(string $engine): bool |
| 71 | { |
| 72 | if (!$this->isValidEngine($engine)) { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | if (get_option(self::OPTION_NAME, null) === $engine) { |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | return update_option(self::OPTION_NAME, $engine, false); |
| 81 | } |
| 82 | |
| 83 | public function isValidEngine($engine): bool |
| 84 | { |
| 85 | return is_string($engine) && in_array($engine, self::ENGINES, true); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param mixed $stored |
| 90 | */ |
| 91 | private function resolveEngine($stored): string |
| 92 | { |
| 93 | if ($this->isValidEngine($stored)) { |
| 94 | return $stored; |
| 95 | } |
| 96 | |
| 97 | if (!is_array($stored)) { |
| 98 | return self::ENGINE_LEGACY; |
| 99 | } |
| 100 | |
| 101 | $legacyActions = ['create', 'update', 'reset', 'push']; |
| 102 | foreach ($legacyActions as $action) { |
| 103 | if (isset($stored[$action]) && $stored[$action] === self::ENGINE_NEXT_GEN) { |
| 104 | return self::ENGINE_NEXT_GEN; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | foreach ($legacyActions as $action) { |
| 109 | if (isset($stored[$action]) && $this->isValidEngine($stored[$action])) { |
| 110 | return $stored[$action]; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return self::ENGINE_LEGACY; |
| 115 | } |
| 116 | } |
| 117 |