PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.9.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.9.2
4.9.2 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 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