PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.3
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.3
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.3, at class/class-mainwp-db-backup.php

580 lines 20.1 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 { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
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 === static::$instance ) {
38 static::$instance = new self();
39 }
40 return static::$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 && '' !== $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 ) { // phpcs:ignore -- NOSONAR - complex.
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 && ( 'full' === $task->type ) && ! $task->paused ) {
156 return true;
157 }
158 }
159 }
160 }
161 return false;
162 }
163
164 /**
165 * Method remove_backup_task()
166 *
167 * Remove backup task.
168 *
169 * @param mixed $id Task ID.
170 *
171 * @return void
172 */
173 public function remove_backup_task( $id ) {
174 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_backup' ) . ' WHERE id = %d', $id ) );
175 }
176
177 /**
178 * Method get_backup_task_by_id()
179 *
180 * Get backup task by id.
181 *
182 * @param mixed $id Task ID.
183 *
184 * @return (object|null) Database query result for Backup Task or null on failure
185 */
186 public function get_backup_task_by_id( $id ) {
187 return $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'wp_backup' ) . ' WHERE id= %d ', $id ) );
188 }
189
190 /**
191 * Method get_backup_tasks_for_user()
192 *
193 * Get backup tasks for current user.
194 *
195 * @param string $orderBy Task order.
196 *
197 * @return object|null Database query result for backup tasks for current user or null on failer.
198 *
199 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
200 */
201 public function get_backup_tasks_for_user( $orderBy = 'name' ) {
202 if ( MainWP_System::instance()->is_single_user() ) {
203 return $this->get_backup_tasks( null, $orderBy );
204 }
205
206 /**
207 * Current user global.
208 *
209 * @global string
210 */
211 global $current_user;
212
213 return $this->get_backup_tasks( $current_user->ID, $orderBy );
214 }
215
216 /**
217 * Method get_backup_tasks()
218 *
219 * Get backup tasks for current user.
220 *
221 * @param null $userid Current user ID.
222 * @param string $orderBy Task order.
223 *
224 * @return (object|null) Database query result for backup tasks for current user or null on failer.
225 */
226 public function get_backup_tasks( $userid = null, $orderBy = null ) {
227 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 );
228 }
229
230 /**
231 * Method add_backup_task()
232 *
233 * Add backup task.
234 *
235 * @param mixed $userid Current user ID.
236 * @param mixed $name Name of backup.
237 * @param mixed $schedule Backup schedual.
238 * @param mixed $type Type of backup, full|db.
239 * @param mixed $exclude Files or directories to exclude.
240 * @param mixed $sites Child Sites to backup.
241 * @param mixed $groups Groups to backup.
242 * @param mixed $subfolder Folder the backups are going into.
243 * @param mixed $filename Filename of the backups.
244 * @param mixed $template Backup template.
245 * @param mixed $excludebackup Backup files to exclude.
246 * @param mixed $excludecache Cache files to exclude.
247 * @param mixed $excludenonwp Non-wp files to exclude.
248 * @param mixed $excludezip Archives to exclude.
249 * @param mixed $archiveFormat Format to store backups in.
250 * @param mixed $maximumFileDescriptorsOverride Overide maximum file descriptors.
251 * @param mixed $maximumFileDescriptorsAuto Auto maximum file discriptors.
252 * @param mixed $maximumFileDescriptors Maximum file descriptors.
253 * @param mixed $loadFilesBeforeZip Load files before Zip.
254 *
255 * @return int|false The number of rows added, or false on error.
256 *
257 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
258 * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
259 */
260 public function add_backup_task(
261 $userid,
262 $name,
263 $schedule,
264 $type,
265 $exclude,
266 $sites,
267 $groups,
268 $subfolder,
269 $filename,
270 $template,
271 $excludebackup,
272 $excludecache,
273 $excludenonwp,
274 $excludezip,
275 $archiveFormat,
276 $maximumFileDescriptorsOverride,
277 $maximumFileDescriptorsAuto,
278 $maximumFileDescriptors,
279 $loadFilesBeforeZip
280 ) {
281
282 // to fix null value.
283 if ( empty( $exclude ) ) {
284 $exclude = '';
285 }
286
287 if ( MainWP_Utility::ctype_digit( $userid ) ) {
288 $values = array(
289 'userid' => $userid,
290 'name' => $name,
291 'schedule' => $schedule,
292 'type' => $type,
293 'exclude' => $exclude,
294 'sites' => $sites,
295 'groups' => $groups,
296 'last' => 0,
297 'last_run' => 0,
298 'last_run_manually' => 0,
299 'completed_sites' => '',
300 'completed' => 0,
301 'backup_errors' => '',
302 'subfolder' => MainWP_Utility::remove_preslash_spaces( $subfolder ),
303 'filename' => $filename,
304 'paused' => 0,
305 'template' => $template,
306 'excludebackup' => $excludebackup,
307 'excludecache' => $excludecache,
308 'excludenonwp' => $excludenonwp,
309 'excludezip' => $excludezip,
310 'archiveFormat' => $archiveFormat,
311 'loadFilesBeforeZip' => $loadFilesBeforeZip,
312 'maximumFileDescriptorsOverride' => $maximumFileDescriptorsOverride,
313 'maximumFileDescriptorsAuto' => $maximumFileDescriptorsAuto,
314 'maximumFileDescriptors' => $maximumFileDescriptors,
315 );
316
317 if ( $this->wpdb->insert( $this->table_name( 'wp_backup' ), $values ) ) {
318 return $this->get_backup_task_by_id( $this->wpdb->insert_id );
319 }
320 }
321
322 return false;
323 }
324
325 /**
326 * Method update_backup_task()
327 *
328 * Update backup task.
329 *
330 * @param mixed $id task id.
331 * @param mixed $userid Current user ID.
332 * @param mixed $name Name of backup.
333 * @param mixed $schedule Backup schedual.
334 * @param mixed $type Type of backup, full|db.
335 * @param mixed $exclude Files or directories to exclude.
336 * @param mixed $sites Child Sites to backup.
337 * @param mixed $groups Groups to backup.
338 * @param mixed $subfolder Folder the backups are going into.
339 * @param mixed $filename Filename of the backups.
340 * @param mixed $excludebackup Backup files to exclude.
341 * @param mixed $excludecache Cache files to exclude.
342 * @param mixed $excludenonwp Non-wp files to exclude.
343 * @param mixed $excludezip Archives to exclude.
344 * @param mixed $archiveFormat Format to store backups in.
345 * @param mixed $maximumFileDescriptorsOverride Overide maximum file descriptors.
346 * @param mixed $maximumFileDescriptorsAuto Auto maximum file discriptors.
347 * @param mixed $maximumFileDescriptors Maximum file descriptors.
348 * @param mixed $loadFilesBeforeZip Load files before Zip.
349 *
350 * @return int|false The number of rows updated, or false on error.
351 *
352 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
353 * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
354 */
355 public function update_backup_task(
356 $id,
357 $userid,
358 $name,
359 $schedule,
360 $type,
361 $exclude,
362 $sites,
363 $groups,
364 $subfolder,
365 $filename,
366 $excludebackup,
367 $excludecache,
368 $excludenonwp,
369 $excludezip,
370 $archiveFormat,
371 $maximumFileDescriptorsOverride,
372 $maximumFileDescriptorsAuto,
373 $maximumFileDescriptors,
374 $loadFilesBeforeZip
375 ) {
376
377 if ( MainWP_Utility::ctype_digit( $userid ) && MainWP_Utility::ctype_digit( $id ) ) {
378 return $this->wpdb->update(
379 $this->table_name( 'wp_backup' ),
380 array(
381 'userid' => $userid,
382 'name' => $name,
383 'schedule' => $schedule,
384 'type' => $type,
385 'exclude' => $exclude,
386 'sites' => $sites,
387 'groups' => $groups,
388 'subfolder' => MainWP_Utility::remove_preslash_spaces( $subfolder ),
389 'filename' => $filename,
390 'excludebackup' => $excludebackup,
391 'excludecache' => $excludecache,
392 'excludenonwp' => $excludenonwp,
393 'excludezip' => $excludezip,
394 'archiveFormat' => $archiveFormat,
395 'loadFilesBeforeZip' => $loadFilesBeforeZip,
396 'maximumFileDescriptorsOverride' => $maximumFileDescriptorsOverride,
397 'maximumFileDescriptorsAuto' => $maximumFileDescriptorsAuto,
398 'maximumFileDescriptors' => $maximumFileDescriptors,
399 ),
400 array( 'id' => $id )
401 );
402 }
403
404 return false;
405 }
406
407 /**
408 * Method update_backup_task_with_values()
409 *
410 * Update backup task with values.
411 *
412 * @param mixed $id Task ID.
413 * @param mixed $values values to update.
414 *
415 * @return (int|false) The number of rows updated, or false on error.
416 */
417 public function update_backup_task_with_values( $id, $values ) {
418 if ( ! is_array( $values ) ) {
419 return false;
420 }
421
422 return $this->wpdb->update( $this->table_name( 'wp_backup' ), $values, array( 'id' => $id ) );
423 }
424
425 /**
426 * Method update_backup_run()
427 *
428 * Update backup run.
429 *
430 * @param mixed $id Task ID.
431 *
432 * @return int|false The number of rows updated, or false on error.
433 *
434 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
435 */
436 public function update_backup_run( $id ) {
437 if ( MainWP_Utility::ctype_digit( $id ) ) {
438 return $this->wpdb->update(
439 $this->table_name( 'wp_backup' ),
440 array(
441 'last_run' => time(),
442 'last' => time(),
443 'completed_sites' => wp_json_encode( array() ),
444 ),
445 array( 'id' => $id )
446 );
447 }
448
449 return false;
450 }
451
452 /**
453 * Method update_backup_run_manually()
454 *
455 * Update backup run manually.
456 *
457 * @param mixed $id Task ID.
458 *
459 * @return int|false The number of rows updated, or false on error.
460 *
461 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
462 */
463 public function update_backup_run_manually( $id ) {
464 if ( MainWP_Utility::ctype_digit( $id ) ) {
465 return $this->wpdb->update( $this->table_name( 'wp_backup' ), array( 'last_run_manually' => time() ), array( 'id' => $id ) );
466 }
467
468 return false;
469 }
470
471 /**
472 * Method update_backup_completed()
473 *
474 * Update backup completed()
475 *
476 * @param mixed $id Task ID.
477 *
478 * @return int|false The number of rows updated, or false on error.
479 *
480 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
481 */
482 public function update_backup_completed( $id ) {
483 if ( MainWP_Utility::ctype_digit( $id ) ) {
484 return $this->wpdb->update( $this->table_name( 'wp_backup' ), array( 'completed' => time() ), array( 'id' => $id ) );
485 }
486
487 return false;
488 }
489
490 /**
491 * Method update_backup_errors()
492 *
493 * Update backup errors.
494 *
495 * @param mixed $id Task ID.
496 * @param mixed $errors Backup errors.
497 *
498 * @return int|false The number of rows updated, or false on error.
499 *
500 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
501 */
502 public function update_backup_errors( $id, $errors ) {
503 if ( MainWP_Utility::ctype_digit( $id ) ) {
504 if ( '' === $errors ) {
505 return $this->wpdb->update( $this->table_name( 'wp_backup' ), array( 'backup_errors' => '' ), array( 'id' => $id ) );
506 } else {
507 $task = $this->get_backup_task_by_id( $id );
508
509 return $this->wpdb->update( $this->table_name( 'wp_backup' ), array( 'backup_errors' => $task->backup_errors . $errors ), array( 'id' => $id ) );
510 }
511 }
512
513 return false;
514 }
515
516 /**
517 * Method update_completed_sites()
518 *
519 * Update completed sites.
520 *
521 * @param mixed $id Task ID.
522 * @param mixed $completedSites Completed sites.
523 *
524 * @return int|false The number of rows updated, or false on error.
525 *
526 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
527 */
528 public function update_completed_sites( $id, $completedSites ) {
529 if ( MainWP_Utility::ctype_digit( $id ) ) {
530 return $this->wpdb->update( $this->table_name( 'wp_backup' ), array( 'completed_sites' => wp_json_encode( $completedSites ) ), array( 'id' => $id ) );
531 }
532
533 return false;
534 }
535
536 /**
537 * Method get_backup_tasks_to_complete()
538 *
539 * Get backup tasks to complete.
540 *
541 * @return object Backup tasks.
542 */
543 public function get_backup_tasks_to_complete() {
544 return $this->wpdb->get_results( 'SELECT * FROM ' . $this->table_name( 'wp_backup' ) . ' WHERE paused = 0 AND completed < last_run', OBJECT );
545 }
546
547 /**
548 * Method get backup tasks daily.
549 *
550 * Get daily backup tasks.
551 *
552 * @return object Backup tasks.
553 */
554 public function get_backup_tasks_todo_daily() {
555 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 );
556 }
557
558 /**
559 * Method get backup tasks weekly.
560 *
561 * Get weekly backup tasks.
562 *
563 * @return object Backup tasks.
564 */
565 public function get_backup_tasks_todo_weekly() {
566 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 );
567 }
568
569 /**
570 * Method get backup tasks monthly.
571 *
572 * Get monthly backup tasks.
573 *
574 * @return object Backup tasks.
575 */
576 public function get_backup_tasks_todo_monthly() {
577 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 );
578 }
579 }
580