Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
38.10% covered (danger)
38.10%
8 / 21
10.00% covered (danger)
10.00%
1 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Monitor
29.41% covered (danger)
29.41%
5 / 17
10.00% covered (danger)
10.00%
1 / 10
72.44
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 delete_all_notifications_copies
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 delete_notifications_copy
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 notifications_copy_option_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 save_notifications_copy
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 activate_module
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 jetpack_modules_loaded
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 update_option_receive_jetpack_monitor_notification
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 user_receives_notifications
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 monitor_get_last_downtime
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * Module Name: Downtime Monitor
4 * Module Description: Get instant alerts if your site goes down and know when it’s back online.
5 * Sort Order: 28
6 * Recommendation Order: 10
7 * First Introduced: 2.6
8 * Requires Connection: Yes
9 * Requires User Connection: Yes
10 * Auto Activate: No
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
16 */
17
18use Automattic\Jetpack\Monitor\Monitor;
19
20if ( ! defined( 'ABSPATH' ) ) {
21    exit( 0 );
22}
23
24/**
25 * Class Jetpack_Monitor
26 *
27 * Kept for backward compatibility; the logic lives in the automattic/jetpack-monitor package.
28 *
29 * @phan-constructor-used-for-side-effects
30 */
31class Jetpack_Monitor {
32
33    /**
34     * Name of the module.
35     *
36     * @var string Name of module.
37     */
38    public $module = 'monitor';
39
40    /**
41     * Constructor.
42     */
43    public function __construct() {
44        Monitor::init();
45        add_action( 'jetpack_modules_loaded', array( $this, 'jetpack_modules_loaded' ) );
46    }
47
48    /**
49     * Forgets every linked user's saved notification choice, while a site disconnect can still say who was linked.
50     *
51     * @since $$next-version$$
52     *
53     * @return void
54     */
55    public function delete_all_notifications_copies() {
56        Monitor::delete_all_notifications_copies();
57    }
58
59    /**
60     * Forgets a user's saved notification choice, which belongs to the WordPress.com account they had linked.
61     *
62     * @since $$next-version$$
63     *
64     * @param int $user_id User ID.
65     *
66     * @return void
67     */
68    public function delete_notifications_copy( $user_id ) {
69        Monitor::delete_notifications_copy( $user_id );
70    }
71
72    /**
73     * Name of the option holding a user's local copy of their notification choice.
74     *
75     * @since $$next-version$$
76     *
77     * @param int $user_id User ID.
78     *
79     * @return string
80     */
81    public static function notifications_copy_option_name( $user_id ) {
82        return Monitor::notifications_copy_option_name( $user_id );
83    }
84
85    /**
86     * Saves the current user's notification choice where Jetpack_Core_Json_Api_Endpoints::get_remote_value reads it.
87     *
88     * @since $$next-version$$
89     *
90     * @param bool $enabled Whether the current user receives notifications.
91     *
92     * @return void
93     */
94    public static function save_notifications_copy( $enabled ) {
95        Monitor::save_notifications_copy( $enabled );
96    }
97
98    /**
99     * Runs upon module activation.
100     *
101     * @return void
102     */
103    public function activate_module() {
104        Monitor::activate_module();
105    }
106
107    /**
108     * Runs on the jetpack_modules_loaded hook to enable configuation.
109     *
110     * @return void
111     */
112    public function jetpack_modules_loaded() {
113        Jetpack::enable_module_configurable( $this->module );
114    }
115
116    /**
117     * Whether to receive the notifications.
118     *
119     * @param bool $value `true` to enable notifications, `false` to disable them.
120     *
121     * @return bool
122     */
123    public function update_option_receive_jetpack_monitor_notification( $value ) {
124        $result = Monitor::set_notifications( $value );
125
126        if ( is_wp_error( $result ) ) {
127            wp_die( sprintf( '%s: %s', esc_html( $result->get_error_code() ), esc_html( $result->get_error_message() ) ) );
128        }
129
130        return true;
131    }
132
133    /**
134     * Checks the status of notifications for current Jetpack site user.
135     *
136     * @since 2.8
137     * @since 4.1.0 New parameter $die_on_error.
138     *
139     * @param bool $die_on_error Whether to issue a wp_die when an error occurs or return a WP_Error object.
140     *
141     * @return boolean|WP_Error
142     */
143    public static function user_receives_notifications( $die_on_error = true ) {
144        $result = Monitor::user_receives_notifications();
145
146        if ( is_wp_error( $result ) && $die_on_error ) {
147            wp_die( sprintf( '%s: %s', esc_html( $result->get_error_code() ), esc_html( $result->get_error_message() ) ), 400 );
148        }
149        return $result;
150    }
151
152    /**
153     * Returns date of the last downtime.
154     *
155     * @since 4.0.0
156     * @return string date in YYYY-MM-DD HH:mm:ss format
157     */
158    public function monitor_get_last_downtime() {
159        return Monitor::get_last_downtime();
160    }
161}
162
163new Jetpack_Monitor();
164
165// Register Jetpack Monitor abilities (WordPress Abilities API, WP 6.9+).
166\Automattic\Jetpack\Plugin\Abilities\Monitor_Abilities::init();