| @@ -1,86 +1,111 @@ | ||
| 1 | -<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 1 | +<?php | |
| 2 | 2 | /** |
| 3 | - * Module Name: Downtime Monitor | |
| 4 | - * Module Description: Get instant alerts if your site goes down and know when it’s back online. | |
| 3 | + * Module Name: Monitor | |
| 4 | + * Module Description: Receive immediate notifications if your site goes down, 24/7. | |
| 5 | + * Jumpstart Description: Receive immediate notifications if your site goes down, 24/7. | |
| 5 | 6 | * Sort Order: 28 |
| 6 | 7 | * Recommendation Order: 10 |
| 7 | 8 | * First Introduced: 2.6 |
| 8 | 9 | * Requires Connection: Yes |
| 9 | - * Requires User Connection: Yes | |
| 10 | 10 | * Auto Activate: No |
| 11 | 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 | - * @package automattic/jetpack | |
| 12 | + * Feature: Security, Jumpstart | |
| 13 | + * Additional Search Queries: monitor, uptime, downtime, monitoring | |
| 16 | 14 | */ |
| 17 | 15 | |
| 18 | -use Automattic\Jetpack\Connection\Manager as Connection_Manager; | |
| 19 | - | |
| 20 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 21 | - exit( 0 ); | |
| 22 | -} | |
| 23 | - | |
| 24 | -/** | |
| 25 | - * Class Jetpack_Monitor | |
| 26 | - * | |
| 27 | - * @phan-constructor-used-for-side-effects | |
| 28 | - */ | |
| 29 | 16 | class Jetpack_Monitor { |
| 30 | 17 | |
| 31 | - /** | |
| 32 | - * Name of the module. | |
| 33 | - * | |
| 34 | - * @var string Name of module. | |
| 35 | - */ | |
| 36 | 18 | public $module = 'monitor'; |
| 37 | 19 | |
| 38 | - /** | |
| 39 | - * Constructor. | |
| 40 | - */ | |
| 41 | - public function __construct() { | |
| 20 | + function __construct() { | |
| 42 | 21 | add_action( 'jetpack_modules_loaded', array( $this, 'jetpack_modules_loaded' ) ); |
| 43 | 22 | add_action( 'jetpack_activate_module_monitor', array( $this, 'activate_module' ) ); |
| 44 | 23 | } |
| 45 | 24 | |
| 46 | - /** | |
| 47 | - * Runs upon module activation. | |
| 48 | - * | |
| 49 | - * @return void | |
| 50 | - */ | |
| 51 | 25 | public function activate_module() { |
| 52 | - if ( ( new Connection_Manager( 'jetpack' ) )->is_user_connected() ) { | |
| 26 | + if ( Jetpack::is_user_connected() ) { | |
| 53 | 27 | self::update_option_receive_jetpack_monitor_notification( true ); |
| 54 | 28 | } |
| 55 | 29 | } |
| 56 | 30 | |
| 57 | - /** | |
| 58 | - * Runs on the jetpack_modules_loaded hook to enable configuation. | |
| 59 | - * | |
| 60 | - * @return void | |
| 61 | - */ | |
| 62 | 31 | public function jetpack_modules_loaded() { |
| 63 | 32 | Jetpack::enable_module_configurable( $this->module ); |
| 33 | + Jetpack::module_configuration_load( $this->module, array( $this, 'jetpack_configuration_load' ) ); | |
| 34 | + Jetpack::module_configuration_screen( $this->module, array( $this, 'jetpack_configuration_screen' ) ); | |
| 64 | 35 | } |
| 65 | 36 | |
| 66 | - /** | |
| 67 | - * Whether to receive the notifications. | |
| 68 | - * | |
| 69 | - * @param bool $value `true` to enable notifications, `false` to disable them. | |
| 70 | - * | |
| 71 | - * @return bool | |
| 72 | - */ | |
| 37 | + public function jetpack_configuration_load() { | |
| 38 | + if ( Jetpack::is_user_connected() && ! self::is_active() ) { | |
| 39 | + Jetpack::deactivate_module( $this->module ); | |
| 40 | + Jetpack::state( 'message', 'module_deactivated' ); | |
| 41 | + wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); | |
| 42 | + die(); | |
| 43 | + } | |
| 44 | + if ( ! empty( $_POST['action'] ) && $_POST['action'] == 'monitor-save' ) { | |
| 45 | + check_admin_referer( 'monitor-settings' ); | |
| 46 | + $this->update_option_receive_jetpack_monitor_notification( isset( $_POST['receive_jetpack_monitor_notification'] ) ); | |
| 47 | + Jetpack::state( 'message', 'module_configured' ); | |
| 48 | + wp_safe_redirect( Jetpack::module_configuration_url( $this->module ) ); | |
| 49 | + exit; | |
| 50 | + } | |
| 51 | + } | |
| 52 | + | |
| 53 | + public function jetpack_configuration_screen() { | |
| 54 | + ?> | |
| 55 | + <p><?php esc_html_e( 'Nobody likes downtime, and that\'s why Downtime Monitor is on the job, keeping tabs on your site by checking it every five minutes. As soon as any downtime is detected, you will receive an email notification alerting you to the issue. That way you can act quickly, to get your site back online again!', 'jetpack' ); ?> | |
| 56 | + <p><?php esc_html_e( 'We’ll also let you know as soon as your site is up and running, so you can keep an eye on total downtime.', 'jetpack'); ?></p> | |
| 57 | + <div class="narrow"> | |
| 58 | + <?php if ( Jetpack::is_user_connected() && current_user_can( 'manage_options' ) ) : ?> | |
| 59 | + <?php $user_email = Jetpack::get_connected_user_email(); ?> | |
| 60 | + <form method="post" id="monitor-settings"> | |
| 61 | + <input type="hidden" name="action" value="monitor-save" /> | |
| 62 | + <?php wp_nonce_field( 'monitor-settings' ); ?> | |
| 63 | + | |
| 64 | + <table id="menu" class="form-table"> | |
| 65 | + <tr> | |
| 66 | + <th scope="row"> | |
| 67 | + <?php _e( 'Notifications', 'jetpack' ); ?> | |
| 68 | + </th> | |
| 69 | + <td> | |
| 70 | + <label for="receive_jetpack_monitor_notification"> | |
| 71 | + <input type="checkbox" name="receive_jetpack_monitor_notification" id="receive_jetpack_monitor_notification" value="receive_jetpack_monitor_notification"<?php checked( self::user_receives_notifications() ); ?> /> | |
| 72 | + <span><?php _e( 'Receive Monitor Email Notifications.' , 'jetpack'); ?></span> | |
| 73 | + </label> | |
| 74 | + <p class="description"><?php printf( __( 'Emails will be sent to %s (<a href="%s">Edit</a>)', 'jetpack' ), $user_email, 'https://wordpress.com/settings/account/'); ?></p> | |
| 75 | + </td> | |
| 76 | + </tr> | |
| 77 | + </table> | |
| 78 | + <?php submit_button(); ?> | |
| 79 | + </form> | |
| 80 | + <?php else : ?> | |
| 81 | + <p><?php _e( 'This profile is not currently linked to a WordPress.com Profile.', 'jetpack' ); ?></p> | |
| 82 | + <?php endif; ?> | |
| 83 | + </div> | |
| 84 | + <?php | |
| 85 | + } | |
| 86 | + | |
| 87 | + public function is_active() { | |
| 88 | + Jetpack::load_xml_rpc_client(); | |
| 89 | + $xml = new Jetpack_IXR_Client( array( | |
| 90 | + 'user_id' => get_current_user_id() | |
| 91 | + ) ); | |
| 92 | + $xml->query( 'jetpack.monitor.isActive' ); | |
| 93 | + if ( $xml->isError() ) { | |
| 94 | + wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); | |
| 95 | + } | |
| 96 | + return $xml->getResponse(); | |
| 97 | + } | |
| 98 | + | |
| 73 | 99 | public function update_option_receive_jetpack_monitor_notification( $value ) { |
| 74 | - $xml = new Jetpack_IXR_Client( | |
| 75 | - array( | |
| 76 | - 'user_id' => get_current_user_id(), | |
| 77 | - ) | |
| 78 | - ); | |
| 100 | + Jetpack::load_xml_rpc_client(); | |
| 101 | + $xml = new Jetpack_IXR_Client( array( | |
| 102 | + 'user_id' => get_current_user_id() | |
| 103 | + ) ); | |
| 79 | 104 | $xml->query( 'jetpack.monitor.setNotifications', (bool) $value ); |
| 80 | 105 | |
| 81 | 106 | if ( $xml->isError() ) { |
| 82 | - wp_die( sprintf( '%s: %s', esc_html( $xml->getErrorCode() ), esc_html( $xml->getErrorMessage() ) ) ); | |
| 107 | + wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); | |
| 83 | 108 | } |
| 84 | 109 | |
| 85 | 110 | // To be used only in Jetpack_Core_Json_Api_Endpoints::get_remote_value. |
| 86 | 111 | update_option( 'monitor_receive_notifications', (bool) $value ); |
| @@ -97,19 +122,18 @@ | ||
| 97 | 122 | * @param bool $die_on_error Whether to issue a wp_die when an error occurs or return a WP_Error object. |
| 98 | 123 | * |
| 99 | 124 | * @return boolean|WP_Error |
| 100 | 125 | */ |
| 101 | - public static function user_receives_notifications( $die_on_error = true ) { | |
| 102 | - $xml = new Jetpack_IXR_Client( | |
| 103 | - array( | |
| 104 | - 'user_id' => get_current_user_id(), | |
| 105 | - ) | |
| 106 | - ); | |
| 126 | + static function user_receives_notifications( $die_on_error = true ) { | |
| 127 | + Jetpack::load_xml_rpc_client(); | |
| 128 | + $xml = new Jetpack_IXR_Client( array( | |
| 129 | + 'user_id' => get_current_user_id() | |
| 130 | + ) ); | |
| 107 | 131 | $xml->query( 'jetpack.monitor.isUserInNotifications' ); |
| 108 | 132 | |
| 109 | 133 | if ( $xml->isError() ) { |
| 110 | 134 | if ( $die_on_error ) { |
| 111 | - wp_die( sprintf( '%s: %s', esc_html( $xml->getErrorCode() ), esc_html( $xml->getErrorMessage() ) ), 400 ); | |
| 135 | + wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); | |
| 112 | 136 | } else { |
| 113 | 137 | return new WP_Error( $xml->getErrorCode(), $xml->getErrorMessage(), array( 'status' => 400 ) ); |
| 114 | 138 | } |
| 115 | 139 | } |
| @@ -115,17 +139,52 @@ | ||
| 115 | 139 | } |
| 116 | 140 | return $xml->getResponse(); |
| 117 | 141 | } |
| 118 | 142 | |
| 119 | - /** | |
| 143 | + public function activate_monitor() { | |
| 144 | + Jetpack::load_xml_rpc_client(); | |
| 145 | + $xml = new Jetpack_IXR_Client( array( | |
| 146 | + 'user_id' => get_current_user_id() | |
| 147 | + ) ); | |
| 148 | + | |
| 149 | + $xml->query( 'jetpack.monitor.activate' ); | |
| 150 | + | |
| 151 | + if ( $xml->isError() ) { | |
| 152 | + wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); | |
| 153 | + } | |
| 154 | + return true; | |
| 155 | + } | |
| 156 | + | |
| 157 | + public function deactivate_monitor() { | |
| 158 | + Jetpack::load_xml_rpc_client(); | |
| 159 | + $xml = new Jetpack_IXR_Client( array( | |
| 160 | + 'user_id' => get_current_user_id() | |
| 161 | + ) ); | |
| 162 | + | |
| 163 | + $xml->query( 'jetpack.monitor.deactivate' ); | |
| 164 | + | |
| 165 | + if ( $xml->isError() ) { | |
| 166 | + wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); | |
| 167 | + } | |
| 168 | + return true; | |
| 169 | + } | |
| 170 | + | |
| 171 | + /* | |
| 120 | 172 | * Returns date of the last downtime. |
| 121 | 173 | * |
| 122 | 174 | * @since 4.0.0 |
| 123 | - * @return string date in YYYY-MM-DD HH:mm:ss format | |
| 175 | + * @return date in YYYY-MM-DD HH:mm:ss format | |
| 124 | 176 | */ |
| 125 | 177 | public function monitor_get_last_downtime() { |
| 126 | - $xml = new Jetpack_IXR_Client(); | |
| 178 | +// if ( $last_down = get_transient( 'monitor_last_downtime' ) ) { | |
| 179 | +// return $last_down; | |
| 180 | +// } | |
| 127 | 181 | |
| 182 | + Jetpack::load_xml_rpc_client(); | |
| 183 | + $xml = new Jetpack_IXR_Client( array( | |
| 184 | + 'user_id' => get_current_user_id() | |
| 185 | + ) ); | |
| 186 | + | |
| 128 | 187 | $xml->query( 'jetpack.monitor.getLastDowntime' ); |
| 129 | 188 | |
| 130 | 189 | if ( $xml->isError() ) { |
| 131 | 190 | return new WP_Error( 'monitor-downtime', $xml->getErrorMessage() ); |
| @@ -134,10 +193,8 @@ | ||
| 134 | 193 | set_transient( 'monitor_last_downtime', $xml->getResponse(), 10 * MINUTE_IN_SECONDS ); |
| 135 | 194 | |
| 136 | 195 | return $xml->getResponse(); |
| 137 | 196 | } |
| 197 | + | |
| 138 | 198 | } |
| 139 | 199 | |
| 140 | -new Jetpack_Monitor(); | |
| 141 | - | |
| 142 | -// Register Jetpack Monitor abilities (WordPress Abilities API, WP 6.9+). | |
| 143 | -\Automattic\Jetpack\Plugin\Abilities\Monitor_Abilities::init(); | |
| 200 | +new Jetpack_Monitor; | |