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