Completed
Push — add/monitor-wp-notification ( 4826d7 )
by
unknown
12:13
created

Jetpack_Monitor::user_receives_notifications()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Module Name: Monitor
4
 * Module Description: Receive immediate notifications if your site goes down, 24/7.
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: Security
12
 * Additional Search Queries: monitor, uptime, downtime, monitoring
13
 */
14
15
class Jetpack_Monitor {
16
17
	public $module = 'monitor';
18
19
	function __construct() {
20
		add_action( 'jetpack_modules_loaded', array( $this, 'jetpack_modules_loaded' ) );
21
		add_action( 'jetpack_activate_module_monitor', array( $this, 'activate_module' ) );
22
	}
23
24
	public function activate_module() {
25
		if ( Jetpack::is_user_connected() ) {
26
			self::update_option_receive_jetpack_monitor_notification( array( 'email' ) );
27
		}
28
	}
29
30
	public function jetpack_modules_loaded() {
31
		Jetpack::enable_module_configurable( $this->module );
32
		Jetpack::module_configuration_load( $this->module, array( $this, 'jetpack_configuration_load' ) );
33
		Jetpack::module_configuration_screen( $this->module, array( $this, 'jetpack_configuration_screen' ) );
34
	}
35
36
	public function jetpack_configuration_load() {
37
		if ( Jetpack::is_user_connected() && ! self::is_active() ) {
38
			Jetpack::deactivate_module( $this->module );
39
			Jetpack::state( 'message', 'module_deactivated' );
40
			wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
41
			die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method jetpack_configuration_load() contains an exit expression.

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.

Loading history...
42
		}
43
		if ( ! empty( $_POST['action'] ) && $_POST['action'] == 'monitor-save' ) {
44
			check_admin_referer( 'monitor-settings' );
45
			$enable_fields = array_intersect( array_keys( $_POST ), array( 'email', 'wp_note' ) );
46
			$this->update_option_receive_jetpack_monitor_notification( $enable_fields );
47
			Jetpack::state( 'message', 'module_configured' );
48
			wp_safe_redirect( Jetpack::module_configuration_url( $this->module ) );
49
		}
50
	}
51
52
	public function jetpack_configuration_screen() {
53
		$user_data = Jetpack::get_connected_user_data();
54
		$methods = $this->get_notification_methods();
55
		$show_methods = array(
56
			'email' => esc_html__( 'Receive Monitor Email Notifications.' , 'jetpack'),
57
			'wp_note' => esc_html__( 'Receive Monitor WordPress Notifications.' , 'jetpack'),
58
		);
59
		?>
60
		<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' ); ?>
61
		<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>
62
		<div class="narrow">
63
		<?php if ( Jetpack::is_user_connected() && current_user_can( 'manage_options' ) ) : ?>
64
			<form method="post" id="monitor-settings">
65
				<input type="hidden" name="action" value="monitor-save" />
66
				<?php wp_nonce_field( 'monitor-settings' ); ?>
67
68
				<table id="menu" class="form-table">
69
						<tr>
70
						<th scope="row">
71
							<?php _e( 'Notifications', 'jetpack' ); ?>
72
						</th>
73
						<td>
74
							<?php foreach ( $show_methods as $slug => $label ) { ?>
75
								<?php $field_id = 'receive_jetpack_monitor_notification_' . esc_attr( $slug ); ?>
76
								<label for="<?php echo $field_id; ?>">
77
										<input type="checkbox" name="<?php echo esc_attr( $slug ); ?>"
78
											id="<?php echo $field_id; ?>"
79
											value="active"<?php checked( in_array( $slug, $methods ) ); ?> />
80
									<span><?php echo( $label ) ?></span>
81
								</label>
82
								<p class="description">
83
								<?php
84
								if ( 'email' === $slug ) {
85
									printf(
86
										__('Emails will be sent to %s (<a href="%s">Edit</a>)', 'jetpack' ),
87
										esc_html( $user_data['email'] ),
88
										'https://wordpress.com/settings/account/'
89
									);
90
								}
91
								?></p>
92
								<p>&nbsp;</p>
93
							<?php } ?>
94
						</td>
95
					</tr>
96
				</table>
97
				<?php submit_button(); ?>
98
			</form>
99
		<?php else : ?>
100
			<p><?php _e( 'This profile is not currently linked to a WordPress.com Profile.', 'jetpack' ); ?></p>
101
		<?php endif; ?>
102
		</div>
103
		<?php
104
	}
105
106 View Code Duplication
	public function is_active() {
107
		Jetpack::load_xml_rpc_client();
108
		$xml = new Jetpack_IXR_Client( array(
109
			'user_id' => get_current_user_id()
110
		) );
111
		$xml->query( 'jetpack.monitor.isActive' );
112
		if ( $xml->isError() ) {
113
			wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
114
		}
115
		return $xml->getResponse();
116
	}
117
118
	/**
119
	 * Tells jetpack.wordpress.com how current user wants to be notified by
120
	 * Monitor.
121
	 *
122
	 * @param array $methods like [ "email", "wp-note" ].
123
	 * @return bool true on success
124
	 */
125
	public function update_option_receive_jetpack_monitor_notification( $methods ) {
126
		Jetpack::load_xml_rpc_client();
127
		$user_id = get_current_user_id();
128
		$methods = array_unique( $methods );
129
		$xml = new Jetpack_IXR_Client( array(
130
			'user_id' => $user_id
131
		) );
132
		$xml->query( 'jetpack.monitor.setNotificationMethods', $methods );
133
134
		if ( $xml->isError() ) {
135
			wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
136
		}
137
138
		// To be used only in Jetpack_Core_Json_Api_Endpoints::get_remote_value.
139
		$options = get_option( 'monitor_notification_methods' );
140
		if ( ! is_array( $options ) ) {
141
			$options = array();
142
		}
143
		$options[ $user_id ] = $methods;
144
		update_option( 'monitor_notification_methods', $options );
145
146
		return true;
147
	}
148
149
	/**
150
	 * Reach out to jetpack.wordpress.com to get list of which notifictation
151
	 * methods are turned on for the current user.  Returned object looks like:
152
	 *		[ 'email', 'wp_note' ]
153
	 *
154
	 * @param bool $die_on_error Whether to issue a wp_die when an error occurs or return a WP_Error object.
155
	 *
156
	 * @return array|WP_Error
157
	 */
158
	public function get_notification_methods( $die_on_error = true ) {
159
		Jetpack::load_xml_rpc_client();
160
		$xml = new Jetpack_IXR_Client( array(
161
			'user_id' => get_current_user_id()
162
		) );
163
		$xml->query( 'jetpack.monitor.getNotificationMethods' );
164
165
		if ( $xml->isError() ) {
166
			if ( $die_on_error ) {
167
				wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
168
			} else {
169
				return new WP_Error( $xml->getErrorCode(), $xml->getErrorMessage(), array( 'status' => 400 ) );
170
			}
171
		}
172
		return $xml->getResponse();
173
	}
174
175 View Code Duplication
	public function activate_monitor() {
176
		Jetpack::load_xml_rpc_client();
177
		$xml = new Jetpack_IXR_Client( array(
178
			'user_id' => get_current_user_id()
179
		) );
180
181
		$xml->query( 'jetpack.monitor.activate' );
182
183
		if ( $xml->isError() ) {
184
			wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
185
		}
186
		return true;
187
	}
188
189 View Code Duplication
	public function deactivate_monitor() {
190
		Jetpack::load_xml_rpc_client();
191
		$xml = new Jetpack_IXR_Client( array(
192
			'user_id' => get_current_user_id()
193
		) );
194
195
		$xml->query( 'jetpack.monitor.deactivate' );
196
197
		if ( $xml->isError() ) {
198
			wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
199
		}
200
		return true;
201
	}
202
203
	/*
204
	 * Returns date of the last downtime.
205
	 *
206
	 * @since 4.0.0
207
	 * @return date in YYYY-MM-DD HH:mm:ss format
208
	 */
209
	public function monitor_get_last_downtime() {
210
//		if ( $last_down = get_transient( 'monitor_last_downtime' ) ) {
211
//			return $last_down;
212
//		}
213
214
		Jetpack::load_xml_rpc_client();
215
		$xml = new Jetpack_IXR_Client( array(
216
			'user_id' => get_current_user_id()
217
		) );
218
219
		$xml->query( 'jetpack.monitor.getLastDowntime' );
220
221
		if ( $xml->isError() ) {
222
			return new WP_Error( 'monitor-downtime', $xml->getErrorMessage() );
223
		}
224
225
		set_transient( 'monitor_last_downtime', $xml->getResponse(), 10 * MINUTE_IN_SECONDS );
226
227
		return $xml->getResponse();
228
	}
229
230
}
231
232
new Jetpack_Monitor;
233