PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 1.4.9
Backup Migration v1.4.9
2.1.6 2.1.5.2 trunk 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.6.1 1.4.7 1.4.8 1.4.9 1.4.9.1 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.5.1
backup-backup / includes / class-backup-method-mananger.php
backup-backup / includes Last commit date
banner 11 months ago bodies 11 months ago check 11 months ago cli 11 months ago cron 11 months ago dashboard 11 months ago database 11 months ago external 11 months ago extracter 11 months ago htaccess 11 months ago notices 11 months ago progress 11 months ago scanner 11 months ago staging 11 months ago traits 11 months ago uploader 11 months ago zipper 11 months ago .htaccess 11 months ago activation.php 11 months ago ajax.php 11 months ago ajax_offline.php 11 months ago analyst.php 11 months ago backup-process.php 11 months ago class-backup-method-mananger.php 11 months ago cli-handler.php 11 months ago compatibility.php 11 months ago config.php 11 months ago constants.php 11 months ago initializer.php 11 months ago logger.php 11 months ago offline.php 11 months ago
class-backup-method-mananger.php
213 lines
1 <?php
2
3
4 namespace BMI\Plugin;
5 use BMI\Plugin\Traits\LoggerTrait;
6 use BMI\Plugin\Dashboard as Dashboard;
7 use BMI\Plugin\Checker\System_Info as SI;
8
9
10 require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'traits' . DIRECTORY_SEPARATOR . 'logger-trait.php';
11
12
13 /**
14 * Class BMI_BackupMethodManager
15 *
16 * This class is responsible for managing the backup method used by the plugin.
17 * It able to change the backup method during runtime or globally by changing the plugin settings.
18 *
19 * @package BMI\Plugin
20 * @since 1.4.6
21 *
22 */
23 class BMI_BackupMethodManager {
24
25 use LoggerTrait;
26
27 private $method;
28 private $error;
29 private $oldMethod;
30
31 /**
32 * BMI_BackupMethodManager constructor.
33 *
34 * It determines the current backup method used by the plugin.
35 */
36 public function __construct() {
37 $this->method = $this->determineBackupMethod();
38 }
39
40 /**
41 * Determine the current backup method used by the plugin.
42 *
43 * @return string|null The current backup method used by the plugin.
44 */
45 private function determineBackupMethod() {
46 if (Dashboard\bmi_get_config('OTHER:EXPERIMENT:TIMEOUT')) {
47 return BMI_METHOD_CURL;
48 } elseif (Dashboard\bmi_get_config('OTHER:USE:TIMEOUT:NORMAL')) {
49 return BMI_METHOD_DEFAULT;
50 } elseif (Dashboard\bmi_get_config('OTHER:EXPERIMENT:TIMEOUT:HARD')) {
51 return BMI_METHOD_BROWSER;
52 }
53 return null;
54 }
55
56 /**
57 * Change the backup method used by the plugin.
58 *
59 * @param string $method The new backup method to use.
60 * @param bool $global Whether to change the backup method globally or not. (Will force change the method globally if method is cURL or browser-method)
61 * @return bool True if the backup method was changed successfully, false otherwise.
62 */
63 public function changeBackupMethod($method, $global = true) {
64 if (!in_array($method, [BMI_METHOD_CURL, BMI_METHOD_DEFAULT, BMI_METHOD_BROWSER])) {
65 return false;
66 }
67
68 if ($this->method === $method) {
69 return true;
70 }
71
72 if (false === $this->isMethodChangable($method)) {
73 // $this->log("Changing backup to {$method} method is not possible");
74 return false;
75 }
76
77 if ($method === BMI_METHOD_BROWSER || $method === BMI_METHOD_CURL || $global) {
78 // $this->log("Changing backup to {$method} method with global settings");
79 if (!$this->applyMethodSettings($method, true)) {
80 // $this->log("Changing backup to {$method} method failed");
81 return false;
82 }
83 }
84
85 if ($this->applyMethodSettings($method)) {
86 $this->log("Backup method changed to {$method}");
87 $this->oldMethod = $this->method;
88 $this->method = $method;
89 return true;
90 }
91
92 return false;
93 }
94
95 /**
96 * Check if the backup method can be changed to the specified method.
97 *
98 * @param string $method The backup method to check.
99 * @return bool
100 */
101 public function isMethodChangable($method) {
102 switch ($method) {
103 case BMI_METHOD_CURL:
104 require_once BMI_INCLUDES . DIRECTORY_SEPARATOR . 'check' . DIRECTORY_SEPARATOR . 'system_info.php';
105 return SI::is_curl_work();
106 case BMI_METHOD_DEFAULT:
107 case BMI_METHOD_BROWSER:
108 return true;
109 default:
110 return false;
111 }
112 }
113
114 /**
115 * Apply the settings for the specified backup method.
116 *
117 * @param string $method The backup method to apply settings for.
118 * @param bool $global Whether to apply the settings globally or not.
119 * @return bool True if the settings were applied successfully, false otherwise.
120 */
121 private function applyMethodSettings($method, $global = false) {
122 if ($global) {
123 $settings = [
124 BMI_METHOD_CURL => ['OTHER:EXPERIMENT:TIMEOUT' => true, 'OTHER:USE:TIMEOUT:NORMAL' => false, 'OTHER:EXPERIMENT:TIMEOUT:HARD' => false],
125 BMI_METHOD_DEFAULT => ['OTHER:EXPERIMENT:TIMEOUT' => false, 'OTHER:USE:TIMEOUT:NORMAL' => true, 'OTHER:EXPERIMENT:TIMEOUT:HARD' => false],
126 BMI_METHOD_BROWSER => ['OTHER:EXPERIMENT:TIMEOUT' => false, 'OTHER:USE:TIMEOUT:NORMAL' => false, 'OTHER:EXPERIMENT:TIMEOUT:HARD' => true],
127 ];
128
129 } else {
130 $settings = [
131 BMI_METHOD_CURL => ['bmi_legacy_version' => false, 'bmi_function_normal' => false, 'bmi_legacy_hard_version' => true],
132 BMI_METHOD_DEFAULT => ['bmi_legacy_version' => true, 'bmi_function_normal' => true, 'bmi_legacy_hard_version' => true],
133 BMI_METHOD_BROWSER => ['bmi_legacy_version' => true, 'bmi_function_normal' => false, 'bmi_legacy_hard_version' => false],
134 ];
135 }
136
137 return $this->configureTimeouts($settings[$method], $global);
138 }
139
140 /**
141 * Configure the settings for the specified backup method.
142 *
143 * @param array $settings The settings to configure.
144 * @param bool $global Whether to configure the settings globally or not.
145 * @return bool
146 */
147 private function configureTimeouts($settings, $global) {
148 $this->configureConstantFilters($settings);
149 return $global ? $this->configurePluginSettings($settings) : true;
150 }
151
152 /**
153 * Configure the plugin settings.
154 *
155 * @param array $settings The settings to configure.
156 * @return bool
157 */
158 private function configurePluginSettings($settings) {
159 $result = true;
160 foreach ($settings as $key => $value) {
161 if (!Dashboard\bmi_set_config($key, $value)) {
162 $result = false;
163 }
164 }
165 return $result;
166 }
167
168 /**
169 * Configure the constant filters.
170 *
171 * @param array $settings The settings to configure.
172 * @return bool always return true.
173 */
174 private function configureConstantFilters($settings) {
175 foreach ($settings as $filterName => $returnValue) {
176 add_filter($filterName, '__return_' . ($returnValue ? 'true' : 'false'));
177 }
178 return true;
179 }
180
181 /**
182 * Get the current backup method used by the plugin.
183 *
184 * @return string The current backup method used by the plugin.
185 */
186 public function currentMethod() {
187 return $this->method;
188 }
189
190 /**
191 * Get the old backup method used by the plugin.
192 *
193 * @return string|null The old backup method used by the plugin.
194 */
195 public function oldMethod() {
196 return $this->oldMethod;
197 }
198
199 /**
200 * Temporary disable cli during current request.
201 *
202 * @param bool $global Whether to disable the cli globally or not.
203 * @return bool
204 */
205 public function disableCLI($global = true) {
206 add_filter('bmi_cli_enabled', '__return_false');
207 if ($global) {
208 return Dashboard\bmi_set_config('OTHER:CLI:DISABLE', true);
209 }
210 return true;
211 }
212
213 }