119 lines
3.9 KiB
PHP
119 lines
3.9 KiB
PHP
<?php
|
|
|
|
if (!defined('ABSPATH')) exit;
|
|
|
|
if (class_exists('Pausera')) return;
|
|
|
|
class Pausera {
|
|
private $options;
|
|
|
|
public function __construct() {
|
|
$defaults = [
|
|
'enable_maintenance' => false,
|
|
'excluded_slugs' => [],
|
|
'excluded_ips' => [],
|
|
'excluded_roles' => [],
|
|
'allowed_users' => [],
|
|
'redirect_url' => '/maintenance',
|
|
];
|
|
|
|
$saved_options = get_option('pausera_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('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 register_settings() {
|
|
register_setting('pausera_group', 'pausera_settings', [
|
|
'sanitize_callback' => [$this, 'sanitize_settings']
|
|
]);
|
|
}
|
|
|
|
public function sanitize_settings($input) {
|
|
add_option('pausera_settings_saved', 1);
|
|
return [
|
|
'enable_maintenance' => !empty($input['enable_maintenance']),
|
|
'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'),
|
|
'allowed_users' => isset($input['allowed_users']) ? array_filter(array_map('sanitize_user', (array) $input['allowed_users'])) : [],
|
|
|
|
];
|
|
}
|
|
|
|
public function admin_notice() {
|
|
if (get_option('pausera_settings_saved')) {
|
|
delete_option('pausera_settings_saved');
|
|
echo '<div class="notice notice-success is-dismissible"><p>Settings saved successfully.</p></div>';
|
|
}
|
|
}
|
|
|
|
public function admin_menu() {
|
|
add_menu_page(
|
|
'Pausera',
|
|
'Pausera',
|
|
'manage_options',
|
|
'pausera',
|
|
[$this, 'settings_page'],
|
|
'dashicons-shield-alt',
|
|
80
|
|
);
|
|
}
|
|
|
|
public function handle_redirect() {
|
|
if (empty($this->options['enable_maintenance'])) return;
|
|
|
|
if (is_user_logged_in()) {
|
|
$user = wp_get_current_user();
|
|
if (in_array($user->user_login, $this->options['allowed_users'] ?? [])) return;
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
public function settings_page() {
|
|
do_action('pausera_render_ui');
|
|
}
|
|
|
|
public function add_admin_bar_status($wp_admin_bar) {
|
|
if (!current_user_can('manage_options')) return;
|
|
|
|
$enabled = !empty($this->options['enable_maintenance']);
|
|
$label = 'Pausera: ' . ($enabled ? 'ON' : 'OFF');
|
|
$color = $enabled ? 'orange' : '#999';
|
|
|
|
$wp_admin_bar->add_node([
|
|
'id' => 'pausera_status',
|
|
'title' => '<span style="color:' . esc_attr($color) . '; font-weight: bold;">' . esc_html($label) . '</span>',
|
|
'href' => admin_url('admin.php?page=pausera'),
|
|
'meta' => ['title' => 'Pausera Status']
|
|
]);
|
|
}
|
|
}
|