PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 9.7.2
Jetpack – WP Security, Backup, Speed, & Growth v9.7.2
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
176 lines 4.6 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 * Requires User Connection: Yes
10 * Auto Activate: No
11 * 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
16 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
17
18 /**
19 * Class Jetpack_Monitor
20 */
21 class Jetpack_Monitor {
22
23 public $module = 'monitor';
24
25 function __construct() {
26 add_action( 'jetpack_modules_loaded', array( $this, 'jetpack_modules_loaded' ) );
27 add_action( 'jetpack_activate_module_monitor', array( $this, 'activate_module' ) );
28 }
29
30 public function activate_module() {
31 if ( ( new Connection_Manager( 'jetpack' ) )->is_user_connected() ) {
32 self::update_option_receive_jetpack_monitor_notification( true );
33 }
34 }
35
36 public function jetpack_modules_loaded() {
37 Jetpack::enable_module_configurable( $this->module );
38 }
39
40 /**
41 * Send an API request to check if the monitor is active.
42 *
43 * @return mixed
44 *
45 * @deprecated 8.9.0 The method is not being used since Jetpack 4.2.0, to be removed.
46 */
47 public function is_active() {
48 _deprecated_function( __METHOD__, 'jetpack-8.9.0' );
49
50 $xml = new Jetpack_IXR_Client( array(
51 'user_id' => get_current_user_id()
52 ) );
53 $xml->query( 'jetpack.monitor.isActive' );
54 if ( $xml->isError() ) {
55 wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
56 }
57 return $xml->getResponse();
58 }
59
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 */
67 public function update_option_receive_jetpack_monitor_notification( $value ) {
68 $xml = new Jetpack_IXR_Client( array(
69 'user_id' => get_current_user_id()
70 ) );
71 $xml->query( 'jetpack.monitor.setNotifications', (bool) $value );
72
73 if ( $xml->isError() ) {
74 wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
75 }
76
77 // To be used only in Jetpack_Core_Json_Api_Endpoints::get_remote_value.
78 update_option( 'monitor_receive_notifications', (bool) $value );
79
80 return true;
81 }
82
83 /**
84 * Checks the status of notifications for current Jetpack site user.
85 *
86 * @since 2.8
87 * @since 4.1.0 New parameter $die_on_error.
88 *
89 * @param bool $die_on_error Whether to issue a wp_die when an error occurs or return a WP_Error object.
90 *
91 * @return boolean|WP_Error
92 */
93 static function user_receives_notifications( $die_on_error = true ) {
94 $xml = new Jetpack_IXR_Client( array(
95 'user_id' => get_current_user_id()
96 ) );
97 $xml->query( 'jetpack.monitor.isUserInNotifications' );
98
99 if ( $xml->isError() ) {
100 if ( $die_on_error ) {
101 wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
102 } else {
103 return new WP_Error( $xml->getErrorCode(), $xml->getErrorMessage(), array( 'status' => 400 ) );
104 }
105 }
106 return $xml->getResponse();
107 }
108
109 /**
110 * Send an API request to activate the monitor.
111 *
112 * @return bool
113 *
114 * @deprecated 8.9.0 The method is not being used since Jetpack 4.2.0, to be removed.
115 */
116 public function activate_monitor() {
117 _deprecated_function( __METHOD__, 'jetpack-8.9.0' );
118
119 $xml = new Jetpack_IXR_Client( array(
120 'user_id' => get_current_user_id()
121 ) );
122
123 $xml->query( 'jetpack.monitor.activate' );
124
125 if ( $xml->isError() ) {
126 wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
127 }
128 return true;
129 }
130
131 /**
132 * Send an API request to deactivate the monitor.
133 *
134 * @return bool
135 *
136 * @deprecated 8.9.0 The method is not being used since Jetpack 4.2.0, to be removed.
137 */
138 public function deactivate_monitor() {
139 _deprecated_function( __METHOD__, 'jetpack-8.9.0' );
140
141 $xml = new Jetpack_IXR_Client( array(
142 'user_id' => get_current_user_id()
143 ) );
144
145 $xml->query( 'jetpack.monitor.deactivate' );
146
147 if ( $xml->isError() ) {
148 wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
149 }
150 return true;
151 }
152
153 /*
154 * Returns date of the last downtime.
155 *
156 * @since 4.0.0
157 * @return date in YYYY-MM-DD HH:mm:ss format
158 */
159 public function monitor_get_last_downtime() {
160 $xml = new Jetpack_IXR_Client();
161
162 $xml->query( 'jetpack.monitor.getLastDowntime' );
163
164 if ( $xml->isError() ) {
165 return new WP_Error( 'monitor-downtime', $xml->getErrorMessage() );
166 }
167
168 set_transient( 'monitor_last_downtime', $xml->getResponse(), 10 * MINUTE_IN_SECONDS );
169
170 return $xml->getResponse();
171 }
172
173 }
174
175 new Jetpack_Monitor;
176