Completed
Push — rm/deprecated-code ( 60e936...87d3e1 )
by Jeremy
38:33 queued 26:57
created

Jetpack_Monitor::deactivate_monitor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * Module Name: Monitor
4
 * Module Description: Jetpack’s downtime monitoring will continuously watch your site and alert you the moment that downtime is detected.
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
16
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
17
18
/**
19
 * Class Jetpack_Monitor
20
 */
21
class Jetpack_Monitor {
22
23
	public $module = 'monitor';
24
25
	function __construct() {
26
		add_action( 'jetpack_modules_loaded', array( $this, 'jetpack_modules_loaded' ) );
27
		add_action( 'jetpack_activate_module_monitor', array( $this, 'activate_module' ) );
28
	}
29
30
	public function activate_module() {
31
		if ( ( new Connection_Manager( 'jetpack' ) )->is_user_connected() ) {
32
			self::update_option_receive_jetpack_monitor_notification( true );
33
		}
34
	}
35
36
	public function jetpack_modules_loaded() {
37
		Jetpack::enable_module_configurable( $this->module );
38
	}
39
40
	/**
41
	 * Whether to receive the notifications.
42
	 *
43
	 * @param bool $value `true` to enable notifications, `false` to disable them.
44
	 *
45
	 * @return bool
46
	 */
47
	public function update_option_receive_jetpack_monitor_notification( $value ) {
48
		$xml = new Jetpack_IXR_Client( array(
49
			'user_id' => get_current_user_id()
50
		) );
51
		$xml->query( 'jetpack.monitor.setNotifications', (bool) $value );
52
53
		if ( $xml->isError() ) {
54
			wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
55
		}
56
57
		// To be used only in Jetpack_Core_Json_Api_Endpoints::get_remote_value.
58
		update_option( 'monitor_receive_notifications', (bool) $value );
59
60
		return true;
61
	}
62
63
	/**
64
	 * Checks the status of notifications for current Jetpack site user.
65
	 *
66
	 * @since 2.8
67
	 * @since 4.1.0 New parameter $die_on_error.
68
	 *
69
	 * @param bool $die_on_error Whether to issue a wp_die when an error occurs or return a WP_Error object.
70
	 *
71
	 * @return boolean|WP_Error
72
	 */
73
	static function user_receives_notifications( $die_on_error = true ) {
74
		$xml = new Jetpack_IXR_Client( array(
75
			'user_id' => get_current_user_id()
76
		) );
77
		$xml->query( 'jetpack.monitor.isUserInNotifications' );
78
79 View Code Duplication
		if ( $xml->isError() ) {
80
			if ( $die_on_error ) {
81
				wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
82
			} else {
83
				return new WP_Error( $xml->getErrorCode(), $xml->getErrorMessage(), array( 'status' => 400 ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with $xml->getErrorCode().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
84
			}
85
		}
86
		return $xml->getResponse();
87
	}
88
89
	/*
90
	 * Returns date of the last downtime.
91
	 *
92
	 * @since 4.0.0
93
	 * @return date in YYYY-MM-DD HH:mm:ss format
94
	 */
95
	public function monitor_get_last_downtime() {
96
		$xml = new Jetpack_IXR_Client();
97
98
		$xml->query( 'jetpack.monitor.getLastDowntime' );
99
100
		if ( $xml->isError() ) {
101
			return new WP_Error( 'monitor-downtime', $xml->getErrorMessage() );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'monitor-downtime'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
102
		}
103
104
		set_transient( 'monitor_last_downtime', $xml->getResponse(), 10 * MINUTE_IN_SECONDS );
105
106
		return $xml->getResponse();
107
	}
108
109
}
110
111
new Jetpack_Monitor;
112