| @@ -1,5 +1,5 @@ | ||
| 1 | -<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 1 | +<?php | |
| 2 | 2 | /** |
| 3 | 3 | * Module Name: Monitor |
| 4 | 4 | * Module Description: Jetpack’s downtime monitoring will continuously watch your site and alert you the moment that downtime is detected. |
| 5 | 5 | * Sort Order: 28 |
| @@ -5,15 +5,12 @@ | ||
| 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 | 11 | * Feature: Security |
| 13 | 12 | * Additional Search Queries: monitor, uptime, downtime, monitoring, maintenance, maintenance mode, offline, site is down, site down, down, repair, error |
| 14 | - * | |
| 15 | - * @package automattic/jetpack | |
| 16 | 13 | */ |
| 17 | 14 | |
| 18 | 15 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 19 | 16 | |
| @@ -21,28 +18,15 @@ | ||
| 21 | 18 | * Class Jetpack_Monitor |
| 22 | 19 | */ |
| 23 | 20 | class Jetpack_Monitor { |
| 24 | 21 | |
| 25 | - /** | |
| 26 | - * Name of the module. | |
| 27 | - * | |
| 28 | - * @var string Name of module. | |
| 29 | - */ | |
| 30 | 22 | public $module = 'monitor'; |
| 31 | 23 | |
| 32 | - /** | |
| 33 | - * Constructor. | |
| 34 | - */ | |
| 35 | - public function __construct() { | |
| 24 | + function __construct() { | |
| 36 | 25 | add_action( 'jetpack_modules_loaded', array( $this, 'jetpack_modules_loaded' ) ); |
| 37 | 26 | add_action( 'jetpack_activate_module_monitor', array( $this, 'activate_module' ) ); |
| 38 | 27 | } |
| 39 | 28 | |
| 40 | - /** | |
| 41 | - * Runs upon module activation. | |
| 42 | - * | |
| 43 | - * @return void | |
| 44 | - */ | |
| 45 | 29 | public function activate_module() { |
| 46 | 30 | if ( ( new Connection_Manager( 'jetpack' ) )->is_user_connected() ) { |
| 47 | 31 | self::update_option_receive_jetpack_monitor_notification( true ); |
| 48 | 32 | } |
| @@ -47,15 +31,30 @@ | ||
| 47 | 31 | self::update_option_receive_jetpack_monitor_notification( true ); |
| 48 | 32 | } |
| 49 | 33 | } |
| 50 | 34 | |
| 35 | + public function jetpack_modules_loaded() { | |
| 36 | + Jetpack::enable_module_configurable( $this->module ); | |
| 37 | + } | |
| 38 | + | |
| 51 | 39 | /** |
| 52 | - * Runs on the jetpack_modules_loaded hook to enable configuation. | |
| 40 | + * Send an API request to check if the monitor is active. | |
| 53 | 41 | * |
| 54 | - * @return void | |
| 42 | + * @return mixed | |
| 43 | + * | |
| 44 | + * @deprecated 8.9.0 The method is not being used since Jetpack 4.2.0, to be removed. | |
| 55 | 45 | */ |
| 56 | - public function jetpack_modules_loaded() { | |
| 57 | - Jetpack::enable_module_configurable( $this->module ); | |
| 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(); | |
| 58 | 57 | } |
| 59 | 58 | |
| 60 | 59 | /** |
| 61 | 60 | * Whether to receive the notifications. |
| @@ -64,17 +63,15 @@ | ||
| 64 | 63 | * |
| 65 | 64 | * @return bool |
| 66 | 65 | */ |
| 67 | 66 | 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 | - ); | |
| 67 | + $xml = new Jetpack_IXR_Client( array( | |
| 68 | + 'user_id' => get_current_user_id() | |
| 69 | + ) ); | |
| 73 | 70 | $xml->query( 'jetpack.monitor.setNotifications', (bool) $value ); |
| 74 | 71 | |
| 75 | 72 | if ( $xml->isError() ) { |
| 76 | - wp_die( sprintf( '%s: %s', esc_html( $xml->getErrorCode() ), esc_html( $xml->getErrorMessage() ) ) ); | |
| 73 | + wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); | |
| 77 | 74 | } |
| 78 | 75 | |
| 79 | 76 | // To be used only in Jetpack_Core_Json_Api_Endpoints::get_remote_value. |
| 80 | 77 | update_option( 'monitor_receive_notifications', (bool) $value ); |
| @@ -91,19 +88,17 @@ | ||
| 91 | 88 | * @param bool $die_on_error Whether to issue a wp_die when an error occurs or return a WP_Error object. |
| 92 | 89 | * |
| 93 | 90 | * @return boolean|WP_Error |
| 94 | 91 | */ |
| 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 | - ); | |
| 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 | + ) ); | |
| 101 | 96 | $xml->query( 'jetpack.monitor.isUserInNotifications' ); |
| 102 | 97 | |
| 103 | 98 | if ( $xml->isError() ) { |
| 104 | 99 | if ( $die_on_error ) { |
| 105 | - wp_die( sprintf( '%s: %s', esc_html( $xml->getErrorCode() ), esc_html( $xml->getErrorMessage() ) ), 400 ); | |
| 100 | + wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); | |
| 106 | 101 | } else { |
| 107 | 102 | return new WP_Error( $xml->getErrorCode(), $xml->getErrorMessage(), array( 'status' => 400 ) ); |
| 108 | 103 | } |
| 109 | 104 | } |
| @@ -110,12 +105,56 @@ | ||
| 110 | 105 | return $xml->getResponse(); |
| 111 | 106 | } |
| 112 | 107 | |
| 113 | 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 | + /* | |
| 114 | 153 | * Returns date of the last downtime. |
| 115 | 154 | * |
| 116 | 155 | * @since 4.0.0 |
| 117 | - * @return string date in YYYY-MM-DD HH:mm:ss format | |
| 156 | + * @return date in YYYY-MM-DD HH:mm:ss format | |
| 118 | 157 | */ |
| 119 | 158 | public function monitor_get_last_downtime() { |
| 120 | 159 | $xml = new Jetpack_IXR_Client(); |
| 121 | 160 | |
| @@ -128,7 +167,8 @@ | ||
| 128 | 167 | set_transient( 'monitor_last_downtime', $xml->getResponse(), 10 * MINUTE_IN_SECONDS ); |
| 129 | 168 | |
| 130 | 169 | return $xml->getResponse(); |
| 131 | 170 | } |
| 171 | + | |
| 132 | 172 | } |
| 133 | 173 | |
| 134 | -new Jetpack_Monitor(); | |
| 174 | +new Jetpack_Monitor; | |