maintenance-von-moh/maintenance-von-moh.php
2025-04-26 00:58:26 +02:00

213 lines
9.4 KiB
PHP

<?php
/**
* Plugin Name: Maintenance von Moh
* Description: Custom maintenance mode with page exclusion, roles control, IP whitelist, and more.
* Version: 2.1
* Author: Moh Farawati
*/
if (!defined('ABSPATH')) exit;
class Moh_Maintenance_Mode {
private $options;
public function __construct() {
$defaults = [
'enable_maintenance' => false,
'excluded_slugs' => [],
'excluded_ips' => [],
'excluded_roles' => [],
'redirect_url' => '/maintenance'
];
$saved_options = get_option('ab_maintenance_settings', []);
$this->options = wp_parse_args($saved_options, $defaults);
add_action('admin_menu', [$this, 'admin_menu']);
add_action('admin_init', [$this, 'register_settings']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']);
add_action('template_redirect', [$this, 'handle_redirect']);
add_action('admin_notices', [$this, 'admin_notice']);
add_action('admin_bar_menu', [$this, 'add_admin_bar_status'], 100);
}
public function admin_menu() {
add_menu_page(
'Maintenance von Moh',
'Maintenance Mode',
'manage_options',
'ab_maintenance',
[$this, 'settings_page'],
'dashicons-shield',
80
);
}
public function enqueue_admin_assets() {
if (isset($_GET['page']) && $_GET['page'] === 'ab_maintenance') {
wp_enqueue_style('maintenance-admin-style', plugin_dir_url(__FILE__) . 'maintenance-admin-style.css', [], filemtime(plugin_dir_path(__FILE__) . 'maintenance-admin-style.css'));
wp_enqueue_script('select2', plugin_dir_url(__FILE__) . 'assets/select2/select2.min.js', ['jquery'], '4.1.0', true);
wp_enqueue_style('select2-style', plugin_dir_url(__FILE__) . 'assets/select2/select2.min.css', [], '4.1.0');
add_action('admin_footer', function () {
echo "<script>jQuery(document).ready(function($){ $('.acf-style-select').select2(); $('.nav-tab').on('mousedown', function(e){ e.preventDefault(); }); });</script>";
});
}
}
public function register_settings() {
register_setting('ab_maintenance_group', 'ab_maintenance_settings', [
'sanitize_callback' => [$this, 'sanitize_settings']
]);
}
public function sanitize_settings($input) {
add_option('ab_maintenance_settings_saved', 1);
return [
'enable_maintenance' => isset($input['enable_maintenance']) ? true : false,
'excluded_slugs' => isset($input['excluded_slugs']) ? array_filter(array_map('sanitize_title', (array) $input['excluded_slugs'])) : [],
'excluded_ips' => array_filter(array_map('trim', explode(',', sanitize_text_field($input['excluded_ips'] ?? '')))),
'excluded_roles' => isset($input['excluded_roles']) ? array_filter(array_map('sanitize_text_field', (array) $input['excluded_roles'])) : [],
'redirect_url' => esc_url_raw($input['redirect_url'] ?? '/maintenance')
];
}
public function admin_notice() {
if (get_option('ab_maintenance_settings_saved')) {
delete_option('ab_maintenance_settings_saved');
echo '<div class="notice notice-success is-dismissible"><p>Settings saved successfully.</p></div>';
}
}
public function add_admin_bar_status($wp_admin_bar) {
if (!current_user_can('manage_options')) return;
$enabled = !empty($this->options['enable_maintenance']);
$label = $enabled ? 'Maintenance: ON' : 'Maintenance: OFF';
$class = $enabled ? 'ab-item maintenance-on' : 'ab-item maintenance-off';
$wp_admin_bar->add_node([
'id' => 'maintenance_mode_status',
'title' => '<span class="' . esc_attr($class) . '">' . esc_html($label) . '</span>',
'href' => admin_url('admin.php?page=ab_maintenance')
]);
}
public function settings_page() {
$active_tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'general';
$settings = wp_parse_args(get_option('ab_maintenance_settings', []), $this->options);
$settings['excluded_slugs'] = is_array($settings['excluded_slugs']) ? $settings['excluded_slugs'] : [];
$settings['excluded_ips'] = is_array($settings['excluded_ips']) ? $settings['excluded_ips'] : [];
$settings['excluded_roles'] = is_array($settings['excluded_roles']) ? $settings['excluded_roles'] : [];
?>
<div class="wrap maintenance-wrap">
<h1>🛡️ Maintenance Mode</h1>
<div class="acf-tabs">
<a href="?page=ab_maintenance&tab=general" class="acf-tab <?php echo $active_tab == 'general' ? 'acf-tab-active' : ''; ?>">General</a>
<a href="?page=ab_maintenance&tab=advanced" class="acf-tab <?php echo $active_tab == 'advanced' ? 'acf-tab-active' : ''; ?>">Advanced</a>
<a href="?page=ab_maintenance&tab=about" class="acf-tab <?php echo $active_tab == 'about' ? 'acf-tab-active' : ''; ?>">About</a>
</div>
<?php if ($active_tab == 'general') : ?>
<form method="post" action="options.php">
<?php settings_fields('ab_maintenance_group'); ?>
<div class="maintenance-card">
<div class="maintenance-field flex-row-between">
<label><strong>Enable Maintenance Mode</strong></label>
<label class="switch">
<input type="checkbox" name="ab_maintenance_settings[enable_maintenance]" value="1" <?php checked($settings['enable_maintenance'], true); ?> />
<span class="slider round"></span>
</label>
</div>
</div>
<div class="maintenance-card">
<div class="maintenance-field">
<label><strong>Excluded Pages</strong></label>
<select name="ab_maintenance_settings[excluded_slugs][]" multiple class="acf-style-select" style="width:100%">
<?php foreach (get_pages() as $page) : $slug = basename(get_permalink($page->ID)); ?>
<option value="<?php echo $slug; ?>" <?php selected(in_array($slug, $settings['excluded_slugs'])); ?>><?php echo esc_html($page->post_title); ?></option>
<?php endforeach; ?>
</select>
<p class="description">Select pages to exclude from maintenance. You can select multiple.</p>
</div>
</div>
<div class="maintenance-card">
<div class="maintenance-field">
<label><strong>Excluded IPs</strong></label>
<input type="text" name="ab_maintenance_settings[excluded_ips]" value="<?php echo esc_attr(implode(',', $settings['excluded_ips'])); ?>" class="regular-text" />
<p class="description">Example: <code>127.0.0.1,88.21.33.77</code></p>
</div>
</div>
<div class="maintenance-card">
<div class="maintenance-field">
<label><strong>Excluded Roles</strong></label>
<select name="ab_maintenance_settings[excluded_roles][]" multiple class="acf-style-select" style="width:100%">
<?php global $wp_roles; foreach ($wp_roles->roles as $key => $role) : ?>
<option value="<?php echo esc_attr($key); ?>" <?php selected(in_array($key, $settings['excluded_roles'])); ?>><?php echo esc_html($role['name']); ?></option>
<?php endforeach; ?>
</select>
<p class="description">Select user roles that should bypass maintenance mode.</p>
</div>
</div>
<div class="maintenance-card">
<div class="maintenance-field">
<label><strong>Redirect Page</strong></label>
<select name="ab_maintenance_settings[redirect_url]" class="acf-style-select" style="width:100%">
<?php foreach (get_pages() as $page) : $slug = '/' . basename(get_permalink($page->ID)); ?>
<option value="<?php echo esc_attr($slug); ?>" <?php selected($settings['redirect_url'], $slug); ?>><?php echo esc_html($page->post_title); ?></option>
<?php endforeach; ?>
</select>
<p class="description">Choose the page visitors will be redirected to.</p>
</div>
</div>
<div class="maintenance-card">
<button type="submit" class="button button-primary" style="border-radius:6px;padding:8px 22px;font-size:14px;">Save Settings</button>
</div>
</form>
<?php elseif ($active_tab == 'advanced') : ?>
<p>🚧 Coming soon: advanced tools and features.</p>
<?php elseif ($active_tab == 'about') : ?>
<p><strong>Maintenance</strong> by Moh Farawati.<br>This plugin was crafted with ❤️ to help developers manage maintenance with control and elegance.</p>
<?php endif; ?>
</div>
<?php
}
public function handle_redirect() {
if (empty($this->options['enable_maintenance'])) return;
if (is_user_logged_in()) {
$user = wp_get_current_user();
foreach ($user->roles as $role) {
if (in_array($role, $this->options['excluded_roles'])) return;
}
}
if (in_array($_SERVER['REMOTE_ADDR'], $this->options['excluded_ips'])) return;
global $wp_query;
if (!$this->options['enable_maintenance'] && $wp_query->is_404()) return;
$current_path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
$redirect_path = trim($this->options['redirect_url'], '/');
if ($current_path === $redirect_path) return;
foreach ($this->options['excluded_slugs'] as $slug) {
if (stripos($current_path, $slug) !== false) return;
}
if (!is_admin()) {
wp_redirect(home_url($this->options['redirect_url']));
exit;
}
}
}
new Moh_Maintenance_Mode();