-
Notifications
You must be signed in to change notification settings - Fork 15
/
maintenance-mode.php
220 lines (195 loc) · 6.78 KB
/
maintenance-mode.php
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* Maintenance Mode
*
* @package Automattic/MaintenanceMode
* @author Automattic
* @copyright 2008-onwards Automattic, and contributors.
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: Maintenance Mode
* Plugin URI: https://github.com/Automattic/maintenance-mode-wp
* Description: Shut down your site for a little while and do some maintenance on it!
* Version: 0.3.1
* Requires at least: 5.9
* Requires PHP: 7.4
* Author: WordPress VIP, Automattic
* Author URI: https://wpvip.com
* Text Domain: maintenance-mode
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
// Stops the execution early if the VIP_MAINTENANCE_MODE constant is not set to `true`.
if ( ! defined( 'VIP_MAINTENANCE_MODE' ) || true !== VIP_MAINTENANCE_MODE ) {
add_action( 'admin_notices', 'vip_maintenance_mode_admin_notice__constant_not_set' );
return;
}
/**
* Displays a warning in the admin to inform users about the VIP_MAINTENANCE_MODE constant
*
* Only users who can activate plugins will see the warning.
*
* @since 0.1.1
*/
function vip_maintenance_mode_admin_notice__constant_not_set(): void {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$class = 'notice notice-warning';
$message = __( "Maintenance Mode won't work until you set the VIP_MAINTENANCE_MODE constant to <code>true</code>.", 'maintenance-mode' );
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), wp_kses( $message, array( 'code' => array() ) ) );
}
/**
* Checks to see if the current user can bypass maintenance mode
*/
function vip_maintenance_mode_current_user_can_bypass() {
/**
* Filters the required capability to avoid the redirect to the maintenance page.
*
* @since 0.1.0
*/
$required_capability = apply_filters( 'vip_maintenance_mode_required_cap', 'edit_posts' );
// phpcs:ignore WordPress.WP.Capabilities.Undetermined
return current_user_can( $required_capability );
}
add_action( 'template_redirect', 'vip_maintenance_mode_template_redirect' );
/**
* Redirects visitors and users without edit_posts capability to the maintenance page
*
* Uses the plugin template when there's no template called `template-maintenance-mode.php` in the theme root folder.
*
* @since 0.1.1
*/
function vip_maintenance_mode_template_redirect(): void {
if ( vip_maintenance_mode_current_user_can_bypass() ) {
return;
}
/**
* Filters whether to respond with a 503 status code.
*
* The 503 status code prevents search engines to index the content of the maintenance page.
*
* @since 0.1.1
*
* @param bool $bool Whether to respond with a 503 status code. Default true.
*/
$respond_503 = apply_filters( 'vip_maintenance_mode_respond_503', true );
if ( true === $respond_503 ) {
status_header( 503 );
/**
* Filters the Retry-After value used to indicate how long the service is expected to be unavailable.
*
* @since 0.1.1
*
* @param int|string The delay in seconds or an http-date. Default to one hour.
*/
$retry_after = apply_filters( 'vip_maintenance_mode_retry_after', 3600 );
if ( is_int( $retry_after ) ) {
header( 'Retry-After: ' . absint( $retry_after ) );
} elseif ( strtotime( $retry_after ) > time() ) {
header( 'Retry-After: ' . gmdate( 'D, d M Y H:i:s T', strtotime( $retry_after ) ) );
}
}
header( 'X-Maintenance-Mode-WP: true' );
// Set default arguments for get_template_part().
$defaults = array(
'slug' => 'template-maintenance-mode',
'name' => '',
'args' => array(),
);
/**
* Filters the default arguments for the maintenance mode template.
*
* @since 0.3.0
*
* @param array $defaults The default arguments for the template part.
* @return array Modified arguments for the template part.
*/
$defaults = apply_filters( 'vip_maintenance_mode_template_args', $defaults );
if ( ! get_template_part( $defaults['slug'], $defaults['name'], $defaults['args'] ) ) {
include __DIR__ . '/template-maintenance-mode.php';
}
exit;
}
add_action( 'rest_authentication_errors', 'vip_maintenance_mode_restrict_rest_api' );
/**
* Restricts access to the REST API during maintenance mode.
*
* This function checks if the REST API should be restricted during maintenance mode and returns an error if the user is not logged in or does not have the capability to bypass maintenance mode.
*
* @param WP_Error|mixed $result The result of the authentication check.
* @return WP_Error|mixed The error object if the REST API is restricted, otherwise the original result.
*/
function vip_maintenance_mode_restrict_rest_api( $result ) {
if ( ! empty( $result ) ) {
return $result;
}
$should_restrict_api = apply_filters( 'vip_maintenance_mode_restrict_rest_api', true );
if ( ! $should_restrict_api ) {
return $result;
}
$error_message = apply_filters( 'vip_maintenance_mode_rest_api_error_message', __( 'REST API access is currently restricted while this site is undergoing maintenance.', 'maintenance-mode' ) );
$maintenance_rest_error = new WP_Error(
'vip_maintenance_mode_rest_error',
$error_message,
array(
'status' => 503,
)
);
if ( ! is_user_logged_in() ) {
return $maintenance_rest_error;
}
if ( ! vip_maintenance_mode_current_user_can_bypass() ) {
return $maintenance_rest_error;
}
return $result;
}
add_action( 'admin_bar_menu', 'vip_maintenance_mode_admin_bar_menu', 8 );
/**
* Displays a notice in the admin bar to indicate that maintenance mode is enabled
*
* Only displayed to users who don't see the maintenance page.
*
* @since 0.1.1
*/
function vip_maintenance_mode_admin_bar_menu(): void {
global $wp_admin_bar;
if ( ! vip_maintenance_mode_current_user_can_bypass() ) {
return;
}
$wp_admin_bar->add_menu(
array(
'id' => 'maintenance-mode',
'parent' => 'top-secondary',
'title' => apply_filters( 'vip_maintenance_mode_admin_bar_title', __( 'Under maintenance', 'maintenance-mode' ) ),
'meta' => array( 'class' => 'mm-notice' ),
)
);
}
add_action( 'wp_enqueue_scripts', 'vip_maintenance_mode_admin_scripts' );
add_action( 'admin_enqueue_scripts', 'vip_maintenance_mode_admin_scripts' );
/**
* Styles the admin bar notice
*
* @since 0.1.1
*/
function vip_maintenance_mode_admin_scripts(): void {
$styles = '#wpadminbar .mm-notice .ab-item {
background: #ffcc00 !important;
background: linear-gradient( #ffcc00, #e6b400 ) !important;
color: #23282d !important;
font-weight:500;
}';
wp_add_inline_style( 'admin-bar', $styles );
}
add_action( 'init', 'vip_maintenance_mode_load_plugin_textdomain' );
/**
* Localize plugin
*
* @since 0.1.1
* @uses load_plugin_textdomain
*/
function vip_maintenance_mode_load_plugin_textdomain(): void {
load_plugin_textdomain( 'maintenance-mode', false, basename( __DIR__ ) . '/languages/' );
}