class-health.php
3.98 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* Health class.
*
* @package automattic/jetpack-sync
*/
namespace Automattic\Jetpack\Sync;
/**
* Health class.
*/
class Health {
/**
* Prefix of the blog lock transient.
*
* @access public
*
* @var string
*/
const STATUS_OPTION = 'sync_health_status';
/**
* Status key in option array.
*
* @access public
*
* @var string
*/
const OPTION_STATUS_KEY = 'status';
/**
* Timestamp key in option array.
*
* @access public
*
* @var string
*/
const OPTION_TIMESTAMP_KEY = 'timestamp';
/**
* Unknown status code.
*
* @access public
*
* @var string
*/
const STATUS_UNKNOWN = 'unknown';
/**
* Disabled status code.
*
* @access public
*
* @var string
*/
const STATUS_DISABLED = 'disabled';
/**
* Out of sync status code.
*
* @access public
*
* @var string
*/
const STATUS_OUT_OF_SYNC = 'out_of_sync';
/**
* In sync status code.
*
* @access public
*
* @var string
*/
const STATUS_IN_SYNC = 'in_sync';
/**
* If sync is active, Health-related hooks will be initialized after plugins are loaded.
*/
public static function init() {
add_action( 'jetpack_full_sync_end', array( __ClASS__, 'full_sync_end_update_status' ), 10, 2 );
}
/**
* Gets health status code.
*
* @return string Sync Health Status
*/
public static function get_status() {
$status = \Jetpack_Options::get_option( self::STATUS_OPTION );
if ( false === $status || ! is_array( $status ) || empty( $status[ self::OPTION_STATUS_KEY ] ) ) {
return self::STATUS_UNKNOWN;
}
switch ( $status[ self::OPTION_STATUS_KEY ] ) {
case self::STATUS_DISABLED:
case self::STATUS_OUT_OF_SYNC:
case self::STATUS_IN_SYNC:
return $status[ self::OPTION_STATUS_KEY ];
default:
return self::STATUS_UNKNOWN;
}
}
/**
* When the Jetpack plugin is upgraded, set status to disabled if sync is not enabled,
* or to unknown, if the status has never been set before.
*/
public static function on_jetpack_upgraded() {
if ( ! Settings::is_sync_enabled() ) {
self::update_status( self::STATUS_DISABLED );
return;
}
if ( false === self::is_status_defined() ) {
self::update_status( self::STATUS_UNKNOWN );
}
}
/**
* When the Jetpack plugin is activated, set status to disabled if sync is not enabled,
* or to unknown.
*/
public static function on_jetpack_activated() {
if ( ! Settings::is_sync_enabled() ) {
self::update_status( self::STATUS_DISABLED );
return;
}
self::update_status( self::STATUS_UNKNOWN );
}
/**
* Updates sync health status with either a valid status, or an unknown status.
*
* @param string $status Sync Status.
*
* @return bool True if an update occoured, or false if the status didn't change.
*/
public static function update_status( $status ) {
if ( self::get_status() === $status ) {
return false;
}
// Default Status Option.
$new_status = array(
self::OPTION_STATUS_KEY => self::STATUS_UNKNOWN,
self::OPTION_TIMESTAMP_KEY => microtime( true ),
);
switch ( $status ) {
case self::STATUS_DISABLED:
case self::STATUS_OUT_OF_SYNC:
case self::STATUS_IN_SYNC:
$new_status[ self::OPTION_STATUS_KEY ] = $status;
break;
}
\Jetpack_Options::update_option( self::STATUS_OPTION, $new_status );
return true;
}
/**
* Check if Status has been previously set.
*
* @return bool is a Status defined
*/
public static function is_status_defined() {
$status = \Jetpack_Options::get_option( self::STATUS_OPTION );
if ( false === $status || ! is_array( $status ) || empty( $status[ self::OPTION_STATUS_KEY ] ) ) {
return false;
} else {
return true;
}
}
/**
* Update Sync Status if Full Sync ended of Posts
*
* @param string $checksum The checksum that's currently being processed.
* @param array $range The ranges of object types being processed.
*/
public static function full_sync_end_update_status( $checksum, $range ) {
if ( isset( $range['posts'] ) ) {
self::update_status( self::STATUS_IN_SYNC );
}
}
}