PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 4.2.5
Jetpack – WP Security, Backup, Speed, & Growth v4.2.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
← All changes | modules/monitor.php +116 -77 14.2.24.2.5 View file →
@@ -1,124 +1,165 @@
1 -<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
1 +<?php
2 2 /**
3 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.
4 + * Module Description: Reports on site downtime.
5 5 * Sort Order: 28
6 6 * Recommendation Order: 10
7 7 * First Introduced: 2.6
8 8 * Requires Connection: Yes
9 - * Requires User Connection: Yes
10 9 * Auto Activate: No
11 10 * Module Tags: Recommended
12 - * Feature: Security
13 - * Additional Search Queries: monitor, uptime, downtime, monitoring, maintenance, maintenance mode, offline, site is down, site down, down, repair, error
14 - *
15 - * @package automattic/jetpack
11 + * Feature: Recommended, Performance-Security
12 + * Additional Search Queries: monitor, uptime, downtime, monitoring
16 13 */
17 14
18 -use Automattic\Jetpack\Connection\Manager as Connection_Manager;
19 -
20 -/**
21 - * Class Jetpack_Monitor
22 - */
23 15 class Jetpack_Monitor {
24 16
25 - /**
26 - * Name of the module.
27 - *
28 - * @var string Name of module.
29 - */
30 17 public $module = 'monitor';
31 18
32 - /**
33 - * Constructor.
34 - */
35 - public function __construct() {
19 + function __construct() {
36 20 add_action( 'jetpack_modules_loaded', array( $this, 'jetpack_modules_loaded' ) );
37 21 add_action( 'jetpack_activate_module_monitor', array( $this, 'activate_module' ) );
38 22 }
39 23
40 - /**
41 - * Runs upon module activation.
42 - *
43 - * @return void
44 - */
45 24 public function activate_module() {
46 - if ( ( new Connection_Manager( 'jetpack' ) )->is_user_connected() ) {
25 + if ( Jetpack::is_user_connected() ) {
47 26 self::update_option_receive_jetpack_monitor_notification( true );
48 27 }
49 28 }
50 29
51 - /**
52 - * Runs on the jetpack_modules_loaded hook to enable configuation.
53 - *
54 - * @return void
55 - */
56 30 public function jetpack_modules_loaded() {
57 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' ) );
58 34 }
59 35
60 - /**
61 - * Whether to receive the notifications.
62 - *
63 - * @param bool $value `true` to enable notifications, `false` to disable them.
64 - *
65 - * @return bool
66 - */
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();
42 + }
43 + if ( ! empty( $_POST['action'] ) && $_POST['action'] == 'monitor-save' ) {
44 + check_admin_referer( 'monitor-settings' );
45 + $this->update_option_receive_jetpack_monitor_notification( isset( $_POST['receive_jetpack_monitor_notification'] ) );
46 + Jetpack::state( 'message', 'module_configured' );
47 + wp_safe_redirect( Jetpack::module_configuration_url( $this->module ) );
48 + }
49 + }
50 +
51 + public function jetpack_configuration_screen() {
52 + ?>
53 + <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' ); ?>
54 + <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>
55 + <div class="narrow">
56 + <?php if ( Jetpack::is_user_connected() && current_user_can( 'manage_options' ) ) : ?>
57 + <?php $user_email = Jetpack::get_connected_user_email(); ?>
58 + <form method="post" id="monitor-settings">
59 + <input type="hidden" name="action" value="monitor-save" />
60 + <?php wp_nonce_field( 'monitor-settings' ); ?>
61 +
62 + <table id="menu" class="form-table">
63 + <tr>
64 + <th scope="row">
65 + <?php _e( 'Notifications', 'jetpack' ); ?>
66 + </th>
67 + <td>
68 + <label for="receive_jetpack_monitor_notification">
69 + <input type="checkbox" name="receive_jetpack_monitor_notification" id="receive_jetpack_monitor_notification" value="receive_jetpack_monitor_notification"<?php checked( $this->user_receives_notifications() ); ?> />
70 + <span><?php _e( 'Receive Monitor Email Notifications.' , 'jetpack'); ?></span>
71 + </label>
72 + <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>
73 + </td>
74 + </tr>
75 + </table>
76 + <?php submit_button(); ?>
77 + </form>
78 + <?php else : ?>
79 + <p><?php _e( 'This profile is not currently linked to a WordPress.com Profile.', 'jetpack' ); ?></p>
80 + <?php endif; ?>
81 + </div>
82 + <?php
83 + }
84 +
85 + public function is_active() {
86 + Jetpack::load_xml_rpc_client();
87 + $xml = new Jetpack_IXR_Client( array(
88 + 'user_id' => get_current_user_id()
89 + ) );
90 + $xml->query( 'jetpack.monitor.isActive' );
91 + if ( $xml->isError() ) {
92 + wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
93 + }
94 + return $xml->getResponse();
95 + }
96 +
67 97 public function update_option_receive_jetpack_monitor_notification( $value ) {
68 - $xml = new Jetpack_IXR_Client(
69 - array(
70 - 'user_id' => get_current_user_id(),
71 - )
72 - );
98 + Jetpack::load_xml_rpc_client();
99 + $xml = new Jetpack_IXR_Client( array(
100 + 'user_id' => get_current_user_id()
101 + ) );
73 102 $xml->query( 'jetpack.monitor.setNotifications', (bool) $value );
74 103
75 104 if ( $xml->isError() ) {
76 - wp_die( sprintf( '%s: %s', esc_html( $xml->getErrorCode() ), esc_html( $xml->getErrorMessage() ) ) );
105 + wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
77 106 }
107 + return true;
108 + }
78 109
79 - // To be used only in Jetpack_Core_Json_Api_Endpoints::get_remote_value.
80 - update_option( 'monitor_receive_notifications', (bool) $value );
110 + public function user_receives_notifications() {
111 + Jetpack::load_xml_rpc_client();
112 + $xml = new Jetpack_IXR_Client( array(
113 + 'user_id' => get_current_user_id()
114 + ) );
115 + $xml->query( 'jetpack.monitor.isUserInNotifications' );
81 116
117 + if ( $xml->isError() ) {
118 + wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
119 + }
120 + return $xml->getResponse();
121 + }
122 +
123 + public function activate_monitor() {
124 + Jetpack::load_xml_rpc_client();
125 + $xml = new Jetpack_IXR_Client( array(
126 + 'user_id' => get_current_user_id()
127 + ) );
128 +
129 + $xml->query( 'jetpack.monitor.activate' );
130 +
131 + if ( $xml->isError() ) {
132 + wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
133 + }
82 134 return true;
83 135 }
84 136
85 - /**
86 - * Checks the status of notifications for current Jetpack site user.
87 - *
88 - * @since 2.8
89 - * @since 4.1.0 New parameter $die_on_error.
90 - *
91 - * @param bool $die_on_error Whether to issue a wp_die when an error occurs or return a WP_Error object.
92 - *
93 - * @return boolean|WP_Error
94 - */
95 - public static function user_receives_notifications( $die_on_error = true ) {
96 - $xml = new Jetpack_IXR_Client(
97 - array(
98 - 'user_id' => get_current_user_id(),
99 - )
100 - );
101 - $xml->query( 'jetpack.monitor.isUserInNotifications' );
137 + public function deactivate_monitor() {
138 + Jetpack::load_xml_rpc_client();
139 + $xml = new Jetpack_IXR_Client( array(
140 + 'user_id' => get_current_user_id()
141 + ) );
102 142
143 + $xml->query( 'jetpack.monitor.deactivate' );
144 +
103 145 if ( $xml->isError() ) {
104 - if ( $die_on_error ) {
105 - wp_die( sprintf( '%s: %s', esc_html( $xml->getErrorCode() ), esc_html( $xml->getErrorMessage() ) ), 400 );
106 - } else {
107 - return new WP_Error( $xml->getErrorCode(), $xml->getErrorMessage(), array( 'status' => 400 ) );
108 - }
146 + wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
109 147 }
110 - return $xml->getResponse();
148 + return true;
111 149 }
112 150
113 - /**
151 + /*
114 152 * Returns date of the last downtime.
115 153 *
116 154 * @since 4.0.0
117 - * @return string date in YYYY-MM-DD HH:mm:ss format
155 + * @return date in YYYY-MM-DD HH:mm:ss format
118 156 */
119 157 public function monitor_get_last_downtime() {
120 - $xml = new Jetpack_IXR_Client();
158 + Jetpack::load_xml_rpc_client();
159 + $xml = new Jetpack_IXR_Client( array(
160 + 'user_id' => get_current_user_id()
161 + ) );
121 162
122 163 $xml->query( 'jetpack.monitor.getLastDowntime' );
123 164
124 165 if ( $xml->isError() ) {
@@ -123,12 +164,10 @@
123 164
124 165 if ( $xml->isError() ) {
125 166 return new WP_Error( 'monitor-downtime', $xml->getErrorMessage() );
126 167 }
127 -
128 - set_transient( 'monitor_last_downtime', $xml->getResponse(), 10 * MINUTE_IN_SECONDS );
129 -
130 168 return $xml->getResponse();
131 169 }
170 +
132 171 }
133 172
134 -new Jetpack_Monitor();
173 +new Jetpack_Monitor;