class-backup-status.php
2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace HM\BackUpWordPress;
/**
* Manages status and progress of a backup
*/
class Backup_Status {
private $filename = '';
public function __construct( $id ) {
$this->id = $id;
}
public function start( $backup_filename, $status_message ) {
$this->filename = $backup_filename;
$this->set_status( $status_message );
}
public function get_backup_filename() {
if ( $this->is_started() ) {
$status = json_decode( file_get_contents( $this->get_status_filepath() ) );
if ( ! empty( $status->filename ) ) {
$this->filename = $status->filename;
}
}
return $this->filename;
}
public function is_started() {
return (bool) file_exists( $this->get_status_filepath() );
}
public function finish() {
// Delete the backup running file
if ( file_exists( $this->get_status_filepath() ) ) {
unlink( $this->get_status_filepath() );
}
}
/**
* Get the status of the running backup.
*
* @return string
*/
public function get_status() {
if ( ! file_exists( $this->get_status_filepath() ) ) {
return '';
}
$status = json_decode( file_get_contents( $this->get_status_filepath() ) );
if ( ! empty( $status->status ) ) {
return $status->status;
}
return '';
}
/**
* Set the status of the running backup
*
* @param string $message
*
* @return null
*/
public function set_status( $message ) {
// If start hasn't been called yet then we wont' have a backup filename
if ( ! $this->filename ) {
return '';
}
$status = json_encode( (object) array(
'filename' => $this->filename,
'started' => $this->get_start_time(),
'status' => $message,
) );
file_put_contents( $this->get_status_filepath(), $status );
}
/**
* Get the time that the current running backup was started
*
* @return int $timestamp
*/
public function get_start_time() {
if ( ! file_exists( $this->get_status_filepath() ) ) {
return 0;
}
$status = json_decode( file_get_contents( $this->get_status_filepath() ) );
if ( ! empty( $status->started ) && (int) (string) $status->started === $status->started ) {
return $status->started;
}
return time();
}
/**
* Get the path to the backup running file that stores the running backup status
*
* @return string
*/
public function get_status_filepath() {
return Path::get_path() . '/.backup-' . $this->id . '-running';
}
}