|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Module Name: Monitor |
|
4
|
|
|
* Module Description: Jetpack’s downtime monitoring will continuously watch your site, and alert you the moment that downtime is detected. |
|
5
|
|
|
* Jumpstart Description: Receive immediate notifications if your site goes down, 24/7. |
|
6
|
|
|
* Sort Order: 28 |
|
7
|
|
|
* Recommendation Order: 10 |
|
8
|
|
|
* First Introduced: 2.6 |
|
9
|
|
|
* Requires Connection: Yes |
|
10
|
|
|
* Auto Activate: No |
|
11
|
|
|
* Module Tags: Recommended |
|
12
|
|
|
* Feature: Security, Jumpstart |
|
13
|
|
|
* Additional Search Queries: monitor, uptime, downtime, monitoring, maintenance, maintenance mode, offline, site is down, site down, down, repair, error |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
class Jetpack_Monitor { |
|
17
|
|
|
|
|
18
|
|
|
public $module = 'monitor'; |
|
19
|
|
|
|
|
20
|
|
|
function __construct() { |
|
21
|
|
|
add_action( 'jetpack_modules_loaded', array( $this, 'jetpack_modules_loaded' ) ); |
|
22
|
|
|
add_action( 'jetpack_activate_module_monitor', array( $this, 'activate_module' ) ); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function activate_module() { |
|
26
|
|
|
if ( Jetpack::is_user_connected() ) { |
|
27
|
|
|
self::update_option_receive_jetpack_monitor_notification( true ); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function jetpack_modules_loaded() { |
|
32
|
|
|
Jetpack::enable_module_configurable( $this->module ); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
View Code Duplication |
public function is_active() { |
|
36
|
|
|
Jetpack::load_xml_rpc_client(); |
|
37
|
|
|
$xml = new Jetpack_IXR_Client( array( |
|
38
|
|
|
'user_id' => get_current_user_id() |
|
39
|
|
|
) ); |
|
40
|
|
|
$xml->query( 'jetpack.monitor.isActive' ); |
|
41
|
|
|
if ( $xml->isError() ) { |
|
42
|
|
|
wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); |
|
43
|
|
|
} |
|
44
|
|
|
return $xml->getResponse(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function update_option_receive_jetpack_monitor_notification( $value ) { |
|
48
|
|
|
Jetpack::load_xml_rpc_client(); |
|
49
|
|
|
$xml = new Jetpack_IXR_Client( array( |
|
50
|
|
|
'user_id' => get_current_user_id() |
|
51
|
|
|
) ); |
|
52
|
|
|
$xml->query( 'jetpack.monitor.setNotifications', (bool) $value ); |
|
53
|
|
|
|
|
54
|
|
|
if ( $xml->isError() ) { |
|
55
|
|
|
wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// To be used only in Jetpack_Core_Json_Api_Endpoints::get_remote_value. |
|
59
|
|
|
update_option( 'monitor_receive_notifications', (bool) $value ); |
|
60
|
|
|
|
|
61
|
|
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Checks the status of notifications for current Jetpack site user. |
|
66
|
|
|
* |
|
67
|
|
|
* @since 2.8 |
|
68
|
|
|
* @since 4.1.0 New parameter $die_on_error. |
|
69
|
|
|
* |
|
70
|
|
|
* @param bool $die_on_error Whether to issue a wp_die when an error occurs or return a WP_Error object. |
|
71
|
|
|
* |
|
72
|
|
|
* @return boolean|WP_Error |
|
73
|
|
|
*/ |
|
74
|
|
|
static function user_receives_notifications( $die_on_error = true ) { |
|
75
|
|
|
Jetpack::load_xml_rpc_client(); |
|
76
|
|
|
$xml = new Jetpack_IXR_Client( array( |
|
77
|
|
|
'user_id' => get_current_user_id() |
|
78
|
|
|
) ); |
|
79
|
|
|
$xml->query( 'jetpack.monitor.isUserInNotifications' ); |
|
80
|
|
|
|
|
81
|
|
|
if ( $xml->isError() ) { |
|
82
|
|
|
if ( $die_on_error ) { |
|
83
|
|
|
wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); |
|
84
|
|
|
} else { |
|
85
|
|
|
return new WP_Error( $xml->getErrorCode(), $xml->getErrorMessage(), array( 'status' => 400 ) ); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
return $xml->getResponse(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
View Code Duplication |
public function activate_monitor() { |
|
92
|
|
|
Jetpack::load_xml_rpc_client(); |
|
93
|
|
|
$xml = new Jetpack_IXR_Client( array( |
|
94
|
|
|
'user_id' => get_current_user_id() |
|
95
|
|
|
) ); |
|
96
|
|
|
|
|
97
|
|
|
$xml->query( 'jetpack.monitor.activate' ); |
|
98
|
|
|
|
|
99
|
|
|
if ( $xml->isError() ) { |
|
100
|
|
|
wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); |
|
101
|
|
|
} |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
View Code Duplication |
public function deactivate_monitor() { |
|
106
|
|
|
Jetpack::load_xml_rpc_client(); |
|
107
|
|
|
$xml = new Jetpack_IXR_Client( array( |
|
108
|
|
|
'user_id' => get_current_user_id() |
|
109
|
|
|
) ); |
|
110
|
|
|
|
|
111
|
|
|
$xml->query( 'jetpack.monitor.deactivate' ); |
|
112
|
|
|
|
|
113
|
|
|
if ( $xml->isError() ) { |
|
114
|
|
|
wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); |
|
115
|
|
|
} |
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/* |
|
120
|
|
|
* Returns date of the last downtime. |
|
121
|
|
|
* |
|
122
|
|
|
* @since 4.0.0 |
|
123
|
|
|
* @return date in YYYY-MM-DD HH:mm:ss format |
|
124
|
|
|
*/ |
|
125
|
|
|
public function monitor_get_last_downtime() { |
|
126
|
|
|
// if ( $last_down = get_transient( 'monitor_last_downtime' ) ) { |
|
127
|
|
|
// return $last_down; |
|
128
|
|
|
// } |
|
129
|
|
|
|
|
130
|
|
|
Jetpack::load_xml_rpc_client(); |
|
131
|
|
|
$xml = new Jetpack_IXR_Client( array( |
|
132
|
|
|
'user_id' => get_current_user_id() |
|
133
|
|
|
) ); |
|
134
|
|
|
|
|
135
|
|
|
$xml->query( 'jetpack.monitor.getLastDowntime' ); |
|
136
|
|
|
|
|
137
|
|
|
if ( $xml->isError() ) { |
|
138
|
|
|
return new WP_Error( 'monitor-downtime', $xml->getErrorMessage() ); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
set_transient( 'monitor_last_downtime', $xml->getResponse(), 10 * MINUTE_IN_SECONDS ); |
|
142
|
|
|
|
|
143
|
|
|
return $xml->getResponse(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
new Jetpack_Monitor; |
|
149
|
|
|
|