PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 3.0.2
WP STAGING – WordPress Backups, Restore, Migration & Clone v3.0.2
4.11.2 4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 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 / Backend / Modules / Jobs / Job.php
wp-staging / Backend / Modules / Jobs Last commit date
Cleaners 5 years ago Exceptions 5 years ago Cancel.php 3 years ago CancelUpdate.php 4 years ago Cloning.php 3 years ago CloningProcess.php 3 years ago Data.php 3 years ago Database.php 3 years ago Delete.php 3 years ago Directories.php 3 years ago Files.php 3 years ago Finish.php 3 years ago Job.php 3 years ago JobExecutable.php 3 years ago Logs.php 3 years ago PreserveDataFirstStep.php 3 years ago PreserveDataSecondStep.php 3 years ago ProcessLock.php 3 years ago Scan.php 3 years ago SearchReplace.php 3 years ago TotalStepsAreNumberOfTables.php 5 years ago Updating.php 3 years ago Verify.php 3 years ago
Job.php
347 lines
1 <?php
2
3 namespace WPStaging\Backend\Modules\Jobs;
4
5 use DateInterval;
6 use DateTime;
7 use Exception;
8 use stdClass;
9 use WPStaging\Core\DTO\Settings;
10 use WPStaging\Core\Utils\Cache;
11 use WPStaging\Core\Utils\Logger;
12 use WPStaging\Core\WPStaging;
13 use WPStaging\Framework\Database\ExcludedTables;
14 use WPStaging\Framework\Interfaces\ShutdownableInterface;
15 use WPStaging\Framework\Traits\ResourceTrait;
16 use WPStaging\Framework\Utils\Math;
17 use WPStaging\Backend\Modules\SystemInfo;
18 use WPStaging\Framework\Security\UniqueIdentifier;
19
20 use function WPStaging\functions\debug_log;
21
22 /**
23 * Class Job
24 * @package WPStaging\Backend\Modules\Jobs
25 */
26 abstract class Job implements ShutdownableInterface
27 {
28 use ResourceTrait;
29
30 /**
31 * @var Cache
32 */
33 protected $cache;
34
35 /**
36 * @var Logger
37 */
38 protected $logger;
39
40 /**
41 * @var object
42 */
43 protected $options;
44
45 /**
46 * @var object
47 */
48 protected $settings;
49
50 /**
51 * Multisite home domain without scheme
52 * @var string
53 */
54 protected $baseUrl;
55
56 /** @var ExcludedTables */
57 protected $excludedTableService;
58
59 /** @var UniqueIdentifier */
60 protected $identifier;
61
62 /** @var Math */
63 protected $utilsMath;
64
65 /** @var SystemInfo */
66 protected $systemInfo;
67
68 /**
69 * Job constructor.
70 * @throws Exception
71 */
72 public function __construct()
73 {
74 $this->utilsMath = new Math();
75
76 $this->excludedTableService = new ExcludedTables();
77
78 // Services
79 $this->cache = new Cache(-1, WPStaging::getContentDir());
80 //$this->logger = WPStaging::make(Logger::class);
81 $this->logger = WPStaging::getInstance()->get("logger");
82 $this->systemInfo = WPStaging::make(SystemInfo::class);
83 $this->identifier = WPStaging::make(UniqueIdentifier::class);
84
85 // Settings and Options
86 $this->options = $this->cache->get("clone_options");
87 $this->settings = (object)((new Settings())->setDefault());
88
89 if (!$this->options) {
90 $this->options = new stdClass();
91 }
92
93 if (isset($this->options->existingClones) && is_object($this->options->existingClones)) {
94 $this->options->existingClones = json_decode(json_encode($this->options->existingClones), true);
95 }
96
97 if (method_exists($this, "initialize")) {
98 $this->initialize();
99 }
100 }
101
102 public function onWpShutdown()
103 {
104 // Commit logs
105 /*if ($this->logger instanceof Logger) {
106 if (isset($this->options->mainJob)) {
107 $this->logger->setFileName($this->getLogFilename());
108 }
109 $this->logger->commit();
110 } else {
111 debug_log('Tried to commit log, but logger is no instance of Logger.');
112 }*/
113 }
114
115 /**
116 * @param null|array|object $options
117 * @return bool
118 * @throws Exception
119 */
120 public function saveOptions($options = null)
121 {
122 // Get default options
123 if ($options === null) {
124 $options = $this->options;
125 }
126
127 $now = new DateTime();
128 $options->expiresAt = $now->add(new DateInterval('P1D'))->format('Y-m-d H:i:s');
129
130 if (!property_exists($options, 'jobIdentifier')) {
131 $options->jobIdentifier = rand(0, 2147483647); // 32 bits int max
132 }
133
134 // Ensure that it is an object
135 $options = json_decode(json_encode($options));
136
137 return $this->cache->save('clone_options', $options);
138 }
139
140 /**
141 * @return object
142 */
143 public function getOptions()
144 {
145 return $this->options;
146 }
147
148 /**
149 * Get current time in seconds
150 * @return float
151 */
152 protected function time()
153 {
154 $time = microtime();
155 $time = explode(' ', $time);
156 $time = (float)$time[1] + (float)$time[0];
157 return $time;
158 }
159
160 /**
161 * @return bool
162 */
163 public function isOverThreshold()
164 {
165 // Check if the memory is over threshold
166 $usedMemory = $this->getMemoryPeakUsage();
167 $maxMemoryLimit = $this->getMaxMemoryLimit();
168 $scriptMemoryLimit = $this->getScriptMemoryLimit();
169
170 $this->debugLog(
171 sprintf(
172 "Used Memory: %s Max Memory Limit: %s Max Script Memory Limit: %s",
173 size_format($usedMemory),
174 size_format($maxMemoryLimit),
175 size_format($scriptMemoryLimit)
176 ),
177 Logger::TYPE_DEBUG
178 );
179
180 if ($this->isMemoryLimit()) {
181 $this->log(
182 sprintf(
183 "Used Memory: %s Memory Limit: %s Max Script memory limit: %s",
184 size_format($usedMemory),
185 size_format($maxMemoryLimit),
186 size_format($scriptMemoryLimit)
187 ),
188 Logger::TYPE_ERROR
189 );
190
191 return true;
192 }
193
194 // Check if execution time is over threshold
195 if ($this->isTimeLimit()) {
196 $this->debugLog(
197 sprintf(
198 "RESET TIME: current time: %s, Start Time: %d, exec time limit: %s",
199 $this->getRunningTime(),
200 WPStaging::$startTime,
201 $this->findExecutionTimeLimit()
202 )
203 );
204 return true;
205 }
206
207 return false;
208 }
209
210 /**
211 * @param string $msg
212 * @param string $type
213 */
214 public function log($msg, $type = Logger::TYPE_INFO)
215 {
216 if ($this->logger === null) {
217 return;
218 }
219
220 $this->logger->setFileName($this->getLogFilename());
221
222 $this->logger->add($msg, $type);
223 }
224
225 /**
226 * @return string
227 */
228 private function getLogFilename()
229 {
230 $uniqueId = $this->identifier->getIdentifier();
231 // If job is not cloning i.e. updating, resetting, pushing
232 if (!empty($this->options->mainJob) && $this->options->mainJob !== 'cloning') {
233 return $this->options->mainJob . '_' . $uniqueId . '_' . date('Y-m-d', time());
234 }
235
236 // If job is cloning
237 if (!empty($this->options->clone) && !empty($this->options->mainJob)) {
238 return $this->options->mainJob . '_' . $uniqueId . '_' . $this->options->clone . '_' . date('Y-m-d', time());
239 }
240
241 if (empty($this->options->clone) && !empty($this->options->mainJob)) {
242 return $this->options->mainJob . '_' . $uniqueId . '_unknown_clone_' . date('Y-m-d', time());
243 }
244
245 if (!empty($this->options->clone) && empty($this->options->mainJob)) {
246 return 'unknown_job_' . $uniqueId . '_' . $this->options->clone . '_' . date('Y-m-d', time());
247 }
248
249 return 'unknown_job_' . $uniqueId . '_' . date('Y-m-d', time());
250 }
251
252 /**
253 * @param string $msg
254 * @param string $type
255 */
256 public function debugLog($msg, $type = Logger::TYPE_INFO)
257 {
258 $this->logger->setFileName($this->getLogFilename());
259
260 if (isset($this->settings->debugMode)) {
261 $this->logger->add($msg, $type);
262 }
263 }
264
265 /**
266 * Throw an error message via json and stop further execution
267 * @param string $message
268 */
269 public function returnException($message = '')
270 {
271 wp_die(
272 json_encode(
273 [
274 'job' => isset($this->options->currentJob) ? $this->options->currentJob : '',
275 'status' => false,
276 'message' => $message,
277 'error' => true
278 ]
279 )
280 );
281 }
282
283 /**
284 * Is job running
285 * @return bool
286 */
287 protected function isRunning()
288 {
289 if (!isset($this->options) || !isset($this->options->isRunning) || !isset($this->options->expiresAt)) {
290 return false;
291 }
292
293 try {
294 $now = new DateTime();
295 $expiresAt = new DateTime($this->options->expiresAt);
296 return $this->options->isRunning === true && $now < $expiresAt;
297 } catch (Exception $e) {
298 }
299
300 return false;
301 }
302
303 protected function isPro()
304 {
305 return defined('WPSTGPRO_VERSION');
306 }
307
308 /**
309 * @return bool
310 */
311 protected function isMultisiteAndPro()
312 {
313 return $this->isPro() && is_multisite();
314 }
315
316 /**
317 * @return bool
318 */
319 public function isNetworkClone()
320 {
321 if (!isset($this->options->networkClone)) {
322 return false;
323 }
324
325 return $this->isMultisiteAndPro() && $this->options->networkClone;
326 }
327
328 /**
329 * Should exclude wp-config file during clone update
330 *
331 * @return bool
332 */
333 public function excludeWpConfigDuringUpdate()
334 {
335 return $this->options->mainJob === Updating::NORMAL_UPDATE;
336 }
337
338 /**
339 * Check if external database is used
340 * @return bool
341 */
342 protected function isExternalDatabase()
343 {
344 return !(empty($this->options->databaseUser) && empty($this->options->databasePassword));
345 }
346 }
347