Database
1 week ago
AbstractStagingSetup.php
1 week ago
DirectoryScanner.php
1 week ago
FileCopier.php
1 week ago
LegacyOptionsCache.php
1 week ago
StagingEngine.php
1 week ago
StagingSetup.php
1 week ago
TableScanner.php
1 week ago
StagingEngine.php
85 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 | /** @var string[] */ |
| 23 | const ENGINES = [ |
| 24 | self::ENGINE_LEGACY, |
| 25 | self::ENGINE_NEXT_GEN, |
| 26 | ]; |
| 27 | |
| 28 | public function getEngine(): string |
| 29 | { |
| 30 | $stored = get_option(self::OPTION_NAME, null); |
| 31 | if ($stored === null) { |
| 32 | $stored = get_option(self::LEGACY_OPTION_NAME, self::ENGINE_LEGACY); |
| 33 | } |
| 34 | |
| 35 | return $this->resolveEngine($stored); |
| 36 | } |
| 37 | |
| 38 | public function saveEngine(string $engine): bool |
| 39 | { |
| 40 | if (!$this->isValidEngine($engine)) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | if (get_option(self::OPTION_NAME, null) === $engine) { |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | return update_option(self::OPTION_NAME, $engine, false); |
| 49 | } |
| 50 | |
| 51 | public function isValidEngine($engine): bool |
| 52 | { |
| 53 | return is_string($engine) && in_array($engine, self::ENGINES, true); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param mixed $stored |
| 58 | */ |
| 59 | private function resolveEngine($stored): string |
| 60 | { |
| 61 | if ($this->isValidEngine($stored)) { |
| 62 | return $stored; |
| 63 | } |
| 64 | |
| 65 | if (!is_array($stored)) { |
| 66 | return self::ENGINE_LEGACY; |
| 67 | } |
| 68 | |
| 69 | $legacyActions = ['create', 'update', 'reset', 'push']; |
| 70 | foreach ($legacyActions as $action) { |
| 71 | if (isset($stored[$action]) && $stored[$action] === self::ENGINE_NEXT_GEN) { |
| 72 | return self::ENGINE_NEXT_GEN; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | foreach ($legacyActions as $action) { |
| 77 | if (isset($stored[$action]) && $this->isValidEngine($stored[$action])) { |
| 78 | return $stored[$action]; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return self::ENGINE_LEGACY; |
| 83 | } |
| 84 | } |
| 85 |