PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / trunk
WP STAGING – WordPress Backup, Restore, Migration & Clone vtrunk
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Staging / Service / StagingEngine.php
wp-staging / Staging / Service Last commit date
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