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