PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 8.9.4
Jetpack – WP Security, Backup, Speed, & Growth v8.9.4
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 14.4.2 All 500 releases
jetpack / modules / monitor.php
monitor.php
170 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * 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, maintenance, maintenance mode, offline, site is down, site down, down, repair, error
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( true );
27 }
28 }
29
30 public function jetpack_modules_loaded() {
31 Jetpack::enable_module_configurable( $this->module );
32 }
33
34 /**
35 * Send an API request to check if the monitor is active.
36 *
37 * @return mixed
38 *
39 * @deprecated 8.9.0 The method is not being used since Jetpack 4.2.0, to be removed.
40 */
41 public function is_active() {
42 _deprecated_function( __METHOD__, 'jetpack-8.9.0' );
43
44 $xml = new Jetpack_IXR_Client( array(
45 'user_id' => get_current_user_id()
46 ) );
47 $xml->query( 'jetpack.monitor.isActive' );
48 if ( $xml->isError() ) {
49 wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
50 }
51 return $xml->getResponse();
52 }
53
54 /**
55 * Whether to receive the notifications.
56 *
57 * @param bool $value `true` to enable notifications, `false` to disable them.
58 *
59 * @return bool
60 */
61 public function update_option_receive_jetpack_monitor_notification( $value ) {
62 $xml = new Jetpack_IXR_Client( array(
63 'user_id' => get_current_user_id()
64 ) );
65 $xml->query( 'jetpack.monitor.setNotifications', (bool) $value );
66
67 if ( $xml->isError() ) {
68 wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
69 }
70
71 // To be used only in Jetpack_Core_Json_Api_Endpoints::get_remote_value.
72 update_option( 'monitor_receive_notifications', (bool) $value );
73
74 return true;
75 }
76
77 /**
78 * Checks the status of notifications for current Jetpack site user.
79 *
80 * @since 2.8
81 * @since 4.1.0 New parameter $die_on_error.
82 *
83 * @param bool $die_on_error Whether to issue a wp_die when an error occurs or return a WP_Error object.
84 *
85 * @return boolean|WP_Error
86 */
87 static function user_receives_notifications( $die_on_error = true ) {
88 $xml = new Jetpack_IXR_Client( array(
89 'user_id' => get_current_user_id()
90 ) );
91 $xml->query( 'jetpack.monitor.isUserInNotifications' );
92
93 if ( $xml->isError() ) {
94 if ( $die_on_error ) {
95 wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
96 } else {
97 return new WP_Error( $xml->getErrorCode(), $xml->getErrorMessage(), array( 'status' => 400 ) );
98 }
99 }
100 return $xml->getResponse();
101 }
102
103 /**
104 * Send an API request to activate the monitor.
105 *
106 * @return bool
107 *
108 * @deprecated 8.9.0 The method is not being used since Jetpack 4.2.0, to be removed.
109 */
110 public function activate_monitor() {
111 _deprecated_function( __METHOD__, 'jetpack-8.9.0' );
112
113 $xml = new Jetpack_IXR_Client( array(
114 'user_id' => get_current_user_id()
115 ) );
116
117 $xml->query( 'jetpack.monitor.activate' );
118
119 if ( $xml->isError() ) {
120 wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
121 }
122 return true;
123 }
124
125 /**
126 * Send an API request to deactivate the monitor.
127 *
128 * @return bool
129 *
130 * @deprecated 8.9.0 The method is not being used since Jetpack 4.2.0, to be removed.
131 */
132 public function deactivate_monitor() {
133 _deprecated_function( __METHOD__, 'jetpack-8.9.0' );
134
135 $xml = new Jetpack_IXR_Client( array(
136 'user_id' => get_current_user_id()
137 ) );
138
139 $xml->query( 'jetpack.monitor.deactivate' );
140
141 if ( $xml->isError() ) {
142 wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
143 }
144 return true;
145 }
146
147 /*
148 * Returns date of the last downtime.
149 *
150 * @since 4.0.0
151 * @return date in YYYY-MM-DD HH:mm:ss format
152 */
153 public function monitor_get_last_downtime() {
154 $xml = new Jetpack_IXR_Client();
155
156 $xml->query( 'jetpack.monitor.getLastDowntime' );
157
158 if ( $xml->isError() ) {
159 return new WP_Error( 'monitor-downtime', $xml->getErrorMessage() );
160 }
161
162 set_transient( 'monitor_last_downtime', $xml->getResponse(), 10 * MINUTE_IN_SECONDS );
163
164 return $xml->getResponse();
165 }
166
167 }
168
169 new Jetpack_Monitor;
170