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