1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Module Name: Monitor |
4
|
|
|
* Module Description: Reports on site downtime. |
5
|
|
|
* Sort Order: 28 |
6
|
|
|
* Recommendation Order: 10 |
7
|
|
|
* First Introduced: 2.6 |
8
|
|
|
* Requires Connection: Yes |
9
|
|
|
* Auto Activate: No |
10
|
|
|
* Module Tags: Recommended |
11
|
|
|
* Feature: Recommended, Performance-Security |
12
|
|
|
* Additional Search Queries: monitor, uptime, downtime, monitoring |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
add_action( 'jetpack_activate_module_monitor', array( Jetpack::init(), 'toggle_module_on_wpcom' ) ); |
16
|
|
|
add_action( 'jetpack_deactivate_module_monitor', array( Jetpack::init(), 'toggle_module_on_wpcom' ) ); |
17
|
|
|
|
18
|
|
|
class Jetpack_Monitor { |
19
|
|
|
|
20
|
|
|
public $module = 'monitor'; |
21
|
|
|
|
22
|
|
|
function __construct() { |
23
|
|
|
add_action( 'jetpack_modules_loaded', array( $this, 'jetpack_modules_loaded' ) ); |
24
|
|
|
add_action( 'jetpack_activate_module_monitor', array( $this, 'activate_module' ) ); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function activate_module() { |
28
|
|
|
if ( Jetpack::is_user_connected() ) { |
29
|
|
|
self::update_option_receive_jetpack_monitor_notification( true ); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function jetpack_modules_loaded() { |
34
|
|
|
Jetpack::enable_module_configurable( $this->module ); |
35
|
|
|
Jetpack::module_configuration_load( $this->module, array( $this, 'jetpack_configuration_load' ) ); |
36
|
|
|
Jetpack::module_configuration_screen( $this->module, array( $this, 'jetpack_configuration_screen' ) ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function jetpack_configuration_load() { |
40
|
|
|
if ( Jetpack::is_user_connected() && ! self::is_active() ) { |
41
|
|
|
Jetpack::deactivate_module( $this->module ); |
42
|
|
|
Jetpack::state( 'message', 'module_deactivated' ); |
43
|
|
|
wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); |
44
|
|
|
die(); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
if ( ! empty( $_POST['action'] ) && $_POST['action'] == 'monitor-save' ) { |
47
|
|
|
check_admin_referer( 'monitor-settings' ); |
48
|
|
|
$this->update_option_receive_jetpack_monitor_notification( isset( $_POST['receive_jetpack_monitor_notification'] ) ); |
49
|
|
|
Jetpack::state( 'message', 'module_configured' ); |
50
|
|
|
wp_safe_redirect( Jetpack::module_configuration_url( $this->module ) ); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function jetpack_configuration_screen() { |
55
|
|
|
?> |
56
|
|
|
<p><?php esc_html_e( 'Nobody likes downtime, and that\'s why Jetpack Monitor is on the job, keeping tabs on your site by checking it every five minutes. As soon as any downtime is detected, you will receive an email notification alerting you to the issue. That way you can act quickly, to get your site back online again!', 'jetpack' ); ?> |
57
|
|
|
<p><?php esc_html_e( 'We’ll also let you know as soon as your site is up and running, so you can keep an eye on total downtime.', 'jetpack'); ?></p> |
58
|
|
|
<div class="narrow"> |
59
|
|
|
<?php if ( Jetpack::is_user_connected() && current_user_can( 'manage_options' ) ) : ?> |
60
|
|
|
<?php $user_email = Jetpack::get_connected_user_email(); ?> |
61
|
|
|
<form method="post" id="monitor-settings"> |
62
|
|
|
<input type="hidden" name="action" value="monitor-save" /> |
63
|
|
|
<?php wp_nonce_field( 'monitor-settings' ); ?> |
64
|
|
|
|
65
|
|
|
<table id="menu" class="form-table"> |
66
|
|
|
<tr> |
67
|
|
|
<th scope="row"> |
68
|
|
|
<?php _e( 'Notifications', 'jetpack' ); ?> |
69
|
|
|
</th> |
70
|
|
|
<td> |
71
|
|
|
<label for="receive_jetpack_monitor_notification"> |
72
|
|
|
<input type="checkbox" name="receive_jetpack_monitor_notification" id="receive_jetpack_monitor_notification" value="receive_jetpack_monitor_notification"<?php checked( $this->user_receives_notifications() ); ?> /> |
73
|
|
|
<span><?php _e( 'Receive Monitor Email Notifications.' , 'jetpack'); ?></span> |
74
|
|
|
</label> |
75
|
|
|
<p class="description"><?php printf( __( 'Emails will be sent to %s (<a href="%s">Edit</a>)', 'jetpack' ), $user_email, 'https://wordpress.com/settings/account/'); ?></p> |
76
|
|
|
</td> |
77
|
|
|
</tr> |
78
|
|
|
</table> |
79
|
|
|
<?php submit_button(); ?> |
80
|
|
|
</form> |
81
|
|
|
<?php else : ?> |
82
|
|
|
<p><?php _e( 'This profile is not currently linked to a WordPress.com Profile.', 'jetpack' ); ?></p> |
83
|
|
|
<?php endif; ?> |
84
|
|
|
</div> |
85
|
|
|
<?php |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function is_active() { |
89
|
|
|
Jetpack::load_xml_rpc_client(); |
90
|
|
|
$xml = new Jetpack_IXR_Client( array( |
91
|
|
|
'user_id' => get_current_user_id() |
92
|
|
|
) ); |
93
|
|
|
$xml->query( 'jetpack.monitor.isActive' ); |
94
|
|
|
if ( $xml->isError() ) { |
95
|
|
|
wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); |
96
|
|
|
} |
97
|
|
|
return $xml->getResponse(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
public function update_option_receive_jetpack_monitor_notification( $value ) { |
101
|
|
|
Jetpack::load_xml_rpc_client(); |
102
|
|
|
$xml = new Jetpack_IXR_Client( array( |
103
|
|
|
'user_id' => get_current_user_id() |
104
|
|
|
) ); |
105
|
|
|
$xml->query( 'jetpack.monitor.setNotifications', (bool) $value ); |
106
|
|
|
|
107
|
|
|
if ( $xml->isError() ) { |
108
|
|
|
wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); |
109
|
|
|
} |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
public function user_receives_notifications() { |
114
|
|
|
Jetpack::load_xml_rpc_client(); |
115
|
|
|
$xml = new Jetpack_IXR_Client( array( |
116
|
|
|
'user_id' => get_current_user_id() |
117
|
|
|
) ); |
118
|
|
|
$xml->query( 'jetpack.monitor.isUserInNotifications' ); |
119
|
|
|
|
120
|
|
|
if ( $xml->isError() ) { |
121
|
|
|
wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); |
122
|
|
|
} |
123
|
|
|
return $xml->getResponse(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function activate_monitor() { |
127
|
|
|
Jetpack::load_xml_rpc_client(); |
128
|
|
|
$xml = new Jetpack_IXR_Client( array( |
129
|
|
|
'user_id' => get_current_user_id() |
130
|
|
|
) ); |
131
|
|
|
|
132
|
|
|
$xml->query( 'jetpack.monitor.activate' ); |
133
|
|
|
|
134
|
|
|
if ( $xml->isError() ) { |
135
|
|
|
wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); |
136
|
|
|
} |
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
public function deactivate_monitor() { |
141
|
|
|
Jetpack::load_xml_rpc_client(); |
142
|
|
|
$xml = new Jetpack_IXR_Client( array( |
143
|
|
|
'user_id' => get_current_user_id() |
144
|
|
|
) ); |
145
|
|
|
|
146
|
|
|
$xml->query( 'jetpack.monitor.deactivate' ); |
147
|
|
|
|
148
|
|
|
if ( $xml->isError() ) { |
149
|
|
|
wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); |
150
|
|
|
} |
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/* |
155
|
|
|
* Returns date of the last downtime. |
156
|
|
|
* |
157
|
|
|
* @since 4.0 |
158
|
|
|
* @return date in YYYY-MM-DD HH:mm:ss format |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
public function monitor_get_last_downtime() { |
161
|
|
|
Jetpack::load_xml_rpc_client(); |
162
|
|
|
$xml = new Jetpack_IXR_Client( array( |
163
|
|
|
'user_id' => get_current_user_id() |
164
|
|
|
) ); |
165
|
|
|
|
166
|
|
|
$xml->query( 'jetpack.monitor.getLastDowntime' ); |
167
|
|
|
|
168
|
|
|
if ( $xml->isError() ) { |
169
|
|
|
return new WP_Error( 'monitor-downtime', $xml->getErrorMessage() ); |
170
|
|
|
} |
171
|
|
|
return $xml->getResponse(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
new Jetpack_Monitor; |
177
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.