1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
<?php /** * Uptime module. * * @package Hummingbird\Core\Modules */
namespace Hummingbird\Core\Modules;
use Hummingbird\Core\Module; use Hummingbird\Core\Traits\Module as ModuleContract; use Hummingbird\Core\Utils; use WP_Error;
if ( ! defined( 'ABSPATH' ) ) { exit; }
/** * Class Uptime */ class Uptime extends Module {
use ModuleContract;
/** * Initialize module. */ public function init() { add_filter( 'wp_hummingbird_is_active_module_uptime', array( $this, 'module_status' ) ); }
/** * Implement abstract parent method for clearing cache. * * @since 1.7.1 */ public function clear_cache() { delete_site_transient( 'wphb-uptime-last-report' ); }
/** * Get last report. * * @since 1.7.1 Removed static property. * @param string $time Report period. * @param bool $force Force refresh. * * @return bool|WP_Error */ public function get_last_report( $time = 'week', $force = false ) { if ( ! Utils::is_member() ) { return new WP_Error( 'uptime-membership', __( 'You need to be a WPMU DEV Member', 'wphb' ) ); }
$current_reports = get_site_transient( 'wphb-uptime-last-report' ); if ( ! isset( $current_reports[ $time ] ) || $force ) { $current_reports = $this->refresh_report( $time ); }
if ( ! isset( $current_reports[ $time ] ) ) { return false; }
return $current_reports[ $time ]; }
/** * Get latest report from server * * @since 1.7.1 Removed static property. * @since 1.8.1 Access changed to private. Added $current_reports param. * * @access private * * @param string $time Report period. * @param bool|array $current_reports Current reports. * * @return array|mixed */ private function refresh_report( $time = 'day', $current_reports = false ) { $results = Utils::get_api()->uptime->check( $time );
if ( is_wp_error( $results ) && 412 === $results->get_error_code() ) { // Uptime has been deactivated. $this->disable_locally(); delete_site_transient( 'wphb-uptime-last-report' ); return false; }
if ( ! $current_reports ) { $current_reports = array(); }
$current_reports[ $time ] = $results; // Save for 2 minutes. set_site_transient( 'wphb-uptime-last-report', $current_reports, 2 * MINUTE_IN_SECONDS );
return $current_reports; }
/** * Check if Uptime is remotely enabled * * @return bool */ public static function is_remotely_enabled() { if ( ! Utils::is_member() ) { return false; }
$cached = get_site_transient( 'wphb-uptime-remotely-enabled' ); if ( 'yes' === $cached ) { return true; } elseif ( 'no' === $cached ) { return false; }
$api = Utils::get_api(); $result = $api->uptime->is_enabled(); // Save for 5 minutes. set_site_transient( 'wphb-uptime-remotely-enabled', $result ? 'yes' : 'no', 5 * MINUTE_IN_SECONDS );
return $result; }
/** * Enable Uptime local and remotely * * @since 1.7.1 Remove static property */ public function enable() { $this->clear_cache(); $this->enable_locally();
delete_site_transient( 'wphb-uptime-remotely-enabled' );
return Utils::get_api()->uptime->enable(); }
/** * Disable Uptime local and remotely * * @since 1.7.1 Removed static property */ public function disable() { $this->clear_cache(); $this->disable_locally();
delete_site_transient( 'wphb-uptime-remotely-enabled' );
return Utils::get_api()->uptime->disable(); }
/** * Enable locally. */ public function enable_locally() { $options = $this->get_options(); $options['enabled'] = true; $this->update_options( $options ); // Save for 3 minutes. set_site_transient( 'wphb-uptime-remotely-enabled', 'yes', 3 * MINUTE_IN_SECONDS ); }
/** * Disable locally. */ public function disable_locally() { $options = $this->get_options(); $options['enabled'] = false;
// Disable reports and notifications. $options['notifications']['enabled'] = false; $options['reports']['enabled'] = false;
// Clean all cron. wp_clear_scheduled_hook( 'wphb_uptime_report' );
$this->update_options( $options ); // Save for 3 minutes. set_site_transient( 'wphb-uptime-remotely-enabled', 'no', 3 * MINUTE_IN_SECONDS ); }
/** * Get module status. * * @param bool $current Current status. * * @return bool */ public function module_status( $current ) { $options = $this->get_options(); if ( ! $options['enabled'] ) { return false; }
return $current; }
}
|