PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 9.5
Jetpack – WP Security, Backup, Speed, & Growth v9.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
jetpack / modules / monitor.php

monitor.php in Jetpack – WP Security, Backup, Speed, & Growth 9.5, at modules/monitor.php

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