-
Notifications
You must be signed in to change notification settings - Fork 1
/
sitesentry-maintenance.php
86 lines (78 loc) · 3.73 KB
/
sitesentry-maintenance.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
<?php
/*
Plugin Name: SiteSentry Maintenance
Description: A sophisticated maintenance mode plugin for WordPress that allows administrators to schedule maintenance windows, customize messages using a WYSIWYG editor, and exclude administrators from the maintenance restrictions.
Version: 1.0.1
Author: Yeasin Arafat
*/
// Hook for adding admin menus
add_action('admin_menu', 'sitesentry_maintenance_menu');
// Action function for above hook
function sitesentry_maintenance_menu() {
add_menu_page('SiteSentry Maintenance', 'SiteSentry Maintenance', 'administrator', 'sitesentry_maintenance', 'sitesentry_maintenance_settings_page');
}
// Function to display the settings page
function sitesentry_maintenance_settings_page() {
?>
<div class="wrap">
<h2>SiteSentry Maintenance Settings</h2>
<form method="post" action="options.php">
<?php settings_fields('sitesentry_maintenance_options_group'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Maintenance Mode Enabled:</th>
<td><input type="checkbox" name="maintenance_mode_enabled" value="1" <?php checked(1, get_option('maintenance_mode_enabled'), true); ?> /></td>
</tr>
<tr valign="top">
<th scope="row">Maintenance Message:</th>
<td>
<?php
$content = get_option('maintenance_message');
$editor_id = 'maintenance_message';
$settings = array(
'textarea_name' => 'maintenance_message'
);
wp_editor($content, $editor_id, $settings);
?>
</td>
</tr>
<tr valign="top">
<th scope="row">Start Time:</th>
<td><input type="datetime-local" name="maintenance_start_time" value="<?php echo get_option('maintenance_start_time'); ?>"></td>
</tr>
<tr valign="top">
<th scope="row">End Time:</th>
<td><input type="datetime-local" name="maintenance_end_time" value="<?php echo get_option('maintenance_end_time'); ?>"></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Register and define the settings
add_action('admin_init', 'sitesentry_maintenance_admin_init');
function sitesentry_maintenance_admin_init(){
register_setting('sitesentry_maintenance_options_group', 'maintenance_mode_enabled');
register_setting('sitesentry_maintenance_options_group', 'maintenance_message', 'sitesentry_maintenance_message_sanitize');
register_setting('sitesentry_maintenance_options_group', 'maintenance_start_time');
register_setting('sitesentry_maintenance_options_group', 'maintenance_end_time');
}
function sitesentry_maintenance_message_sanitize($input) {
return wp_kses_post($input);
}
// Hook into the WordPress 'init' action to check for maintenance mode
add_action('init', 'sitesentry_check_for_maintenance_mode');
function sitesentry_check_for_maintenance_mode() {
if (current_user_can('administrator')) {
return; // Skip maintenance mode for administrators
}
$isEnabled = get_option('maintenance_mode_enabled');
$startTime = strtotime(get_option('maintenance_start_time'));
$endTime = strtotime(get_option('maintenance_end_time'));
$currentTime = time();
if ($isEnabled && $currentTime > $startTime && $currentTime < $endTime) {
wp_die(wp_kses_post(get_option('maintenance_message')));
}
}
?>