PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-db-backup.php

class-mainwp-db-backup.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.0, at class/class-mainwp-db-backup.php

582 lines 17.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Database Controller
4 *
5 * This file handles all interactions with the DB.
6 *
7 * @package MainWP/Dashboard
8 */
9
10 namespace MainWP\Dashboard;
11
12 /**
13 * Class MainWP_DB_Backup
14 *
15 * @package MainWP\Dashboard
16 */
17 class MainWP_DB_Backup extends MainWP_DB {
18 // phpcs:disable WordPress.DB.RestrictedFunctions, WordPress.DB.PreparedSQL.NotPrepared -- unprepared SQL ok, accessing the database directly to custom database functions.
19
20 /**
21 * Instance variable class.
22 *
23 * @static
24 * @var (self|null) $instance Instance of MainWP_DB_Backup or null.
25 */
26 private static $instance = null;
27
28 /**
29 * Method instance()
30 *
31 * Create public static instance.
32 *
33 * @static
34 * @return MainWP_DB
35 */
36 public static function instance() {
37 if ( null === self::$instance ) {
38 self::$instance = new self();
39 }
40 return self::$instance;
41 }
42
43 /**
44 * Method get_website_backup_settings()
45 *
46 * Get Child Site backup settings.
47 *
48 * @param mixed $websiteid Child Site ID.
49 *
50 * @return object|null Database query result for Child Site backup settings or null on failure
51 *
52 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
53 */
54 public function get_website_backup_settings( $websiteid ) {
55 if ( ! MainWP_Utility::ctype_digit( $websiteid ) ) {
56 return null;
57 }
58
59 return $this->get_row_result( $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'wp_settings_backup' ) . ' WHERE wpid = %d ', $websiteid ) );
60 }
61
62 /**
63 * Method update_backup_task_progress()
64 *
65 * Update backup tasuk progress.
66 *
67 * @param mixed $task_id Task ID.
68 * @param mixed $wp_id Child Site ID.
69 * @param mixed $values Values to update.
70 *
71 * @return mixed get_backup_task_progres().
72 */
73 public function update_backup_task_progress( $task_id, $wp_id, $values ) {
74 $this->wpdb->update(
75 $this->table_name( 'wp_backup_progress' ),
76 $values,
77 array(
78 'task_id' => $task_id,
79 'wp_id' => $wp_id,
80 )
81 );
82
83 return $this->get_backup_task_progress( $task_id, $wp_id );
84 }
85
86 /**
87 * Method add_backup_task_progress()
88 *
89 * Add backup task progress.
90 *
91 * @param mixed $task_id Task ID.
92 * @param mixed $wp_id Child Site ID.
93 * @param mixed $information Task info to add.
94 *
95 * @return (array|null) $this->get_backup_task_progress() or null on failure
96 */
97 public function add_backup_task_progress( $task_id, $wp_id, $information ) {
98 $values = array(
99 'task_id' => $task_id,
100 'wp_id' => $wp_id,
101 'dtsFetched' => time(),
102 'fetchResult' => wp_json_encode( $information ),
103 'removedFiles' => 0,
104 'downloadedDB' => '',
105 'downloadedFULL' => '',
106 );
107
108 if ( $this->wpdb->insert( $this->table_name( 'wp_backup_progress' ), $values ) ) {
109 return $this->get_backup_task_progress( $task_id, $wp_id );
110 }
111
112 return null;
113 }
114
115 /**
116 * Method get_backup_task_progress()
117 *
118 * Get backup task progress.
119 *
120 * @param mixed $task_id Task ID.
121 * @param mixed $wp_id Child Site ID.
122 *
123 * @return (array|null) Backup Progress or null on failer.
124 */
125 public function get_backup_task_progress( $task_id, $wp_id ) {
126 $progress = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'wp_backup_progress' ) . ' WHERE task_id= %d AND wp_id = %d ', $task_id, $wp_id ) );
127
128 if ( '' !== $progress->fetchResult ) {
129 $progress->fetchResult = json_decode( $progress->fetchResult, true );
130 }
131
132 return $progress;
133 }
134
135 /**
136 * Method backup_full_task_running()
137 *
138 * Check if full backup task is running.
139 *
140 * @param mixed $wp_id Child Site ID.
141 *
142 * @return boolean true|false.
143 */
144 public function backup_full_task_running( $wp_id ) {
145
146 if ( ! get_option( 'mainwp_enableLegacyBackupFeature' ) ) {
147 return false;
148 }
149
150 $progresses = $this->wpdb->get_results( $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'wp_backup_progress' ) . ' WHERE wp_id = %d AND dtsFetched > %d ', $wp_id, time() - ( 30 * 60 ) ) );
151 if ( is_array( $progresses ) ) {
152 foreach ( $progresses as $progress ) {
153 if ( empty( $progress->downloadedDBComplete ) && empty( $progress->downloadedFULLComplete ) ) {
154 $task = $this->get_backup_task_by_id( $progress->task_id );
155 if ( $task ) {
156 if ( ( 'full' === $task->type ) && ! $task->paused ) {
157 return true;
158 }
159 }
160 }
161 }
162 }
163 return false;
164 }
165
166 /**
167 * Method remove_backup_task()
168 *
169 * Remove backup task.
170 *
171 * @param mixed $id Task ID.
172 *
173 * @return void
174 */
175 public function remove_backup_task( $id ) {
176 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_backup' ) . ' WHERE id = %d', $id ) );
177 }
178
179 /**
180 * Method get_backup_task_by_id()
181 *
182 * Get backup task by id.
183 *
184 * @param mixed $id Task ID.
185 *
186 * @return (object|null) Database query result for Backup Task or null on failure
187 */
188 public function get_backup_task_by_id( $id ) {
189 return $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'wp_backup' ) . ' WHERE id= %d ', $id ) );
190 }
191
192 /**
193 * Method get_backup_tasks_for_user()
194 *
195 * Get backup tasks for current user.
196 *
197 * @param string $orderBy Task order.
198 *
199 * @return object|null Database query result for backup tasks for current user or null on failer.
200 *
201 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
202 */
203 public function get_backup_tasks_for_user( $orderBy = 'name' ) {
204 if ( MainWP_System::instance()->is_single_user() ) {
205 return $this->get_backup_tasks( null, $orderBy );
206 }
207
208 /**
209 * Current user global.
210 *
211 * @global string
212 */
213 global $current_user;
214
215 return $this->get_backup_tasks( $current_user->ID, $orderBy );
216 }
217
218 /**
219 * Method get_backup_tasks()
220 *
221 * Get backup tasks for current user.
222 *
223 * @param null $userid Current user ID.
224 * @param string $orderBy Task order.
225 *
226 * @return (object|null) Database query result for backup tasks for current user or null on failer.
227 */
228 public function get_backup_tasks( $userid = null, $orderBy = null ) {
229 return $this->wpdb->get_results( 'SELECT * FROM ' . $this->table_name( 'wp_backup' ) . ' WHERE ' . ( null === $userid ? '' : 'userid= ' . $userid . ' AND ' ) . ' template = 0 ' . ( null !== $orderBy ? 'ORDER BY ' . $orderBy : '' ), OBJECT );
230 }
231
232 /**
233 * Method add_backup_task()
234 *
235 * Add backup task.
236 *
237 * @param mixed $userid Current user ID.
238 * @param mixed $name Name of backup.
239 * @param mixed $schedule Backup schedual.
240 * @param mixed $type Type of backup, full|db.
241 * @param mixed $exclude Files or directories to exclude.
242 * @param mixed $sites Child Sites to backup.
243 * @param mixed $groups Groups to backup.
244 * @param mixed $subfolder Folder the backups are going into.
245 * @param mixed $filename Filename of the backups.
246 * @param mixed $template Backup template.
247 * @param mixed $excludebackup Backup files to exclude.
248 * @param mixed $excludecache Cache files to exclude.
249 * @param mixed $excludenonwp Non-wp files to exclude.
250 * @param mixed $excludezip Archives to exclude.
251 * @param mixed $archiveFormat Format to store backups in.
252 * @param mixed $maximumFileDescriptorsOverride Overide maximum file descriptors.
253 * @param mixed $maximumFileDescriptorsAuto Auto maximum file discriptors.
254 * @param mixed $maximumFileDescriptors Maximum file descriptors.
255 * @param mixed $loadFilesBeforeZip Load files before Zip.
256 *
257 * @return int|false The number of rows added, or false on error.
258 *
259 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
260 * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
261 */
262 public function add_backup_task(
263 $userid,
264 $name,
265 $schedule,
266 $type,
267 $exclude,
268 $sites,
269 $groups,
270 $subfolder,
271 $filename,
272 $template,
273 $excludebackup,
274 $excludecache,
275 $excludenonwp,
276 $excludezip,
277 $archiveFormat,
278 $maximumFileDescriptorsOverride,
279 $maximumFileDescriptorsAuto,
280 $maximumFileDescriptors,
281 $loadFilesBeforeZip
282 ) {
283
284 // to fix null value.
285 if ( empty( $exclude ) ) {
286 $exclude = '';
287 }
288
289 if ( MainWP_Utility::ctype_digit( $userid ) ) {
290 $values = array(
291 'userid' => $userid,
292 'name' => $name,
293 'schedule' => $schedule,
294 'type' => $type,
295 'exclude' => $exclude,
296 'sites' => $sites,
297 'groups' => $groups,
298 'last' => 0,
299 'last_run' => 0,
300 'last_run_manually' => 0,
301 'completed_sites' => '',
302 'completed' => 0,
303 'backup_errors' => '',
304 'subfolder' => MainWP_Utility::remove_preslash_spaces( $subfolder ),
305 'filename' => $filename,
306 'paused' => 0,
307 'template' => $template,
308 'excludebackup' => $excludebackup,
309 'excludecache' => $excludecache,
310 'excludenonwp' => $excludenonwp,
311 'excludezip' => $excludezip,
312 'archiveFormat' => $archiveFormat,
313 'loadFilesBeforeZip' => $loadFilesBeforeZip,
314 'maximumFileDescriptorsOverride' => $maximumFileDescriptorsOverride,
315 'maximumFileDescriptorsAuto' => $maximumFileDescriptorsAuto,
316 'maximumFileDescriptors' => $maximumFileDescriptors,
317 );
318
319 if ( $this->wpdb->insert( $this->table_name( 'wp_backup' ), $values ) ) {
320 return $this->get_backup_task_by_id( $this->wpdb->insert_id );
321 }
322 }
323
324 return false;
325 }
326
327 /**
328 * Method update_backup_task()
329 *
330 * Update backup task.
331 *
332 * @param mixed $id task id.
333 * @param mixed $userid Current user ID.
334 * @param mixed $name Name of backup.
335 * @param mixed $schedule Backup schedual.
336 * @param mixed $type Type of backup, full|db.
337 * @param mixed $exclude Files or directories to exclude.
338 * @param mixed $sites Child Sites to backup.
339 * @param mixed $groups Groups to backup.
340 * @param mixed $subfolder Folder the backups are going into.
341 * @param mixed $filename Filename of the backups.
342 * @param mixed $excludebackup Backup files to exclude.
343 * @param mixed $excludecache Cache files to exclude.
344 * @param mixed $excludenonwp Non-wp files to exclude.
345 * @param mixed $excludezip Archives to exclude.
346 * @param mixed $archiveFormat Format to store backups in.
347 * @param mixed $maximumFileDescriptorsOverride Overide maximum file descriptors.
348 * @param mixed $maximumFileDescriptorsAuto Auto maximum file discriptors.
349 * @param mixed $maximumFileDescriptors Maximum file descriptors.
350 * @param mixed $loadFilesBeforeZip Load files before Zip.
351 *
352 * @return int|false The number of rows updated, or false on error.
353 *
354 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
355 * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
356 */
357 public function update_backup_task(
358 $id,
359 $userid,
360 $name,
361 $schedule,
362 $type,
363 $exclude,
364 $sites,
365 $groups,
366 $subfolder,
367 $filename,
368 $excludebackup,
369 $excludecache,
370 $excludenonwp,
371 $excludezip,
372 $archiveFormat,
373 $maximumFileDescriptorsOverride,
374 $maximumFileDescriptorsAuto,
375 $maximumFileDescriptors,
376 $loadFilesBeforeZip
377 ) {
378
379 if ( MainWP_Utility::ctype_digit( $userid ) && MainWP_Utility::ctype_digit( $id ) ) {
380 return $this->wpdb->update(
381 $this->table_name( 'wp_backup' ),
382 array(
383 'userid' => $userid,
384 'name' => $name,
385 'schedule' => $schedule,
386 'type' => $type,
387 'exclude' => $exclude,
388 'sites' => $sites,
389 'groups' => $groups,
390 'subfolder' => MainWP_Utility::remove_preslash_spaces( $subfolder ),
391 'filename' => $filename,
392 'excludebackup' => $excludebackup,
393 'excludecache' => $excludecache,
394 'excludenonwp' => $excludenonwp,
395 'excludezip' => $excludezip,
396 'archiveFormat' => $archiveFormat,
397 'loadFilesBeforeZip' => $loadFilesBeforeZip,
398 'maximumFileDescriptorsOverride' => $maximumFileDescriptorsOverride,
399 'maximumFileDescriptorsAuto' => $maximumFileDescriptorsAuto,
400 'maximumFileDescriptors' => $maximumFileDescriptors,
401 ),
402 array( 'id' => $id )
403 );
404 }
405
406 return false;
407 }
408
409 /**
410 * Method update_backup_task_with_values()
411 *
412 * Update backup task with values.
413 *
414 * @param mixed $id Task ID.
415 * @param mixed $values values to update.
416 *
417 * @return (int|false) The number of rows updated, or false on error.
418 */
419 public function update_backup_task_with_values( $id, $values ) {
420 if ( ! is_array( $values ) ) {
421 return false;
422 }
423
424 return $this->wpdb->update( $this->table_name( 'wp_backup' ), $values, array( 'id' => $id ) );
425 }
426
427 /**
428 * Method update_backup_run()
429 *
430 * Update backup run.
431 *
432 * @param mixed $id Task ID.
433 *
434 * @return int|false The number of rows updated, or false on error.
435 *
436 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
437 */
438 public function update_backup_run( $id ) {
439 if ( MainWP_Utility::ctype_digit( $id ) ) {
440 return $this->wpdb->update(
441 $this->table_name( 'wp_backup' ),
442 array(
443 'last_run' => time(),
444 'last' => time(),
445 'completed_sites' => wp_json_encode( array() ),
446 ),
447 array( 'id' => $id )
448 );
449 }
450
451 return false;
452 }
453
454 /**
455 * Method update_backup_run_manually()
456 *
457 * Update backup run manually.
458 *
459 * @param mixed $id Task ID.
460 *
461 * @return int|false The number of rows updated, or false on error.
462 *
463 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
464 */
465 public function update_backup_run_manually( $id ) {
466 if ( MainWP_Utility::ctype_digit( $id ) ) {
467 return $this->wpdb->update( $this->table_name( 'wp_backup' ), array( 'last_run_manually' => time() ), array( 'id' => $id ) );
468 }
469
470 return false;
471 }
472
473 /**
474 * Method update_backup_completed()
475 *
476 * Update backup completed()
477 *
478 * @param mixed $id Task ID.
479 *
480 * @return int|false The number of rows updated, or false on error.
481 *
482 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
483 */
484 public function update_backup_completed( $id ) {
485 if ( MainWP_Utility::ctype_digit( $id ) ) {
486 return $this->wpdb->update( $this->table_name( 'wp_backup' ), array( 'completed' => time() ), array( 'id' => $id ) );
487 }
488
489 return false;
490 }
491
492 /**
493 * Method update_backup_errors()
494 *
495 * Update backup errors.
496 *
497 * @param mixed $id Task ID.
498 * @param mixed $errors Backup errors.
499 *
500 * @return int|false The number of rows updated, or false on error.
501 *
502 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
503 */
504 public function update_backup_errors( $id, $errors ) {
505 if ( MainWP_Utility::ctype_digit( $id ) ) {
506 if ( '' === $errors ) {
507 return $this->wpdb->update( $this->table_name( 'wp_backup' ), array( 'backup_errors' => '' ), array( 'id' => $id ) );
508 } else {
509 $task = $this->get_backup_task_by_id( $id );
510
511 return $this->wpdb->update( $this->table_name( 'wp_backup' ), array( 'backup_errors' => $task->backup_errors . $errors ), array( 'id' => $id ) );
512 }
513 }
514
515 return false;
516 }
517
518 /**
519 * Method update_completed_sites()
520 *
521 * Update completed sites.
522 *
523 * @param mixed $id Task ID.
524 * @param mixed $completedSites Completed sites.
525 *
526 * @return int|false The number of rows updated, or false on error.
527 *
528 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
529 */
530 public function update_completed_sites( $id, $completedSites ) {
531 if ( MainWP_Utility::ctype_digit( $id ) ) {
532 return $this->wpdb->update( $this->table_name( 'wp_backup' ), array( 'completed_sites' => wp_json_encode( $completedSites ) ), array( 'id' => $id ) );
533 }
534
535 return false;
536 }
537
538 /**
539 * Method get_backup_tasks_to_complete()
540 *
541 * Get backup tasks to complete.
542 *
543 * @return object Backup tasks.
544 */
545 public function get_backup_tasks_to_complete() {
546 return $this->wpdb->get_results( 'SELECT * FROM ' . $this->table_name( 'wp_backup' ) . ' WHERE paused = 0 AND completed < last_run', OBJECT );
547 }
548
549 /**
550 * Method get_backup_tasks_todo_daily()
551 *
552 * Get daily backup tasks.
553 *
554 * @return object Backup tasks.
555 */
556 public function get_backup_tasks_todo_daily() {
557 return $this->wpdb->get_results( 'SELECT * FROM ' . $this->table_name( 'wp_backup' ) . ' WHERE paused = 0 AND schedule="daily" AND ' . time() . ' - last_run >= ' . ( 60 * 60 * 24 ), OBJECT );
558 }
559
560 /**
561 * Method get_backup_tasks_todo_weekly()
562 *
563 * Get weekly backup tasks.
564 *
565 * @return object Backup tasks.
566 */
567 public function get_backup_tasks_todo_weekly() {
568 return $this->wpdb->get_results( 'SELECT * FROM ' . $this->table_name( 'wp_backup' ) . ' WHERE paused = 0 AND schedule="weekly" AND ' . time() . ' - last_run >= ' . ( 60 * 60 * 24 * 7 ), OBJECT );
569 }
570
571 /**
572 * Method get_backup_tasks_todo_monthly()
573 *
574 * Get monthly backup tasks.
575 *
576 * @return object Backup tasks.
577 */
578 public function get_backup_tasks_todo_monthly() {
579 return $this->wpdb->get_results( 'SELECT * FROM ' . $this->table_name( 'wp_backup' ) . ' WHERE paused = 0 AND schedule="monthly" AND ' . time() . ' - last_run >= ' . ( 60 * 60 * 24 * 30 ), OBJECT );
580 }
581 }
582