|
|
<?php
|
|
|
/**
|
|
|
* 维护模式功能
|
|
|
*
|
|
|
* 提供网站维护模式的开启/关闭及自定义设置
|
|
|
*/
|
|
|
|
|
|
// 防止直接访问
|
|
|
if (!defined('ABSPATH')) {
|
|
|
exit;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 注册维护模式设置菜单
|
|
|
*/
|
|
|
function nenghui_maintenance_menu() {
|
|
|
add_options_page(
|
|
|
'维护模式设置', // 页面标题
|
|
|
'维护模式', // 菜单标题
|
|
|
'manage_options', // 权限要求
|
|
|
'nenghui-maintenance', // 菜单slug
|
|
|
'nenghui_maintenance_page_html' // 回调函数
|
|
|
);
|
|
|
}
|
|
|
add_action('admin_menu', 'nenghui_maintenance_menu');
|
|
|
|
|
|
/**
|
|
|
* 注册设置项
|
|
|
*/
|
|
|
function nenghui_register_maintenance_settings() {
|
|
|
register_setting('nenghui_maintenance_options', 'nenghui_maintenance_mode_enabled');
|
|
|
register_setting('nenghui_maintenance_options', 'nenghui_maintenance_mode_title');
|
|
|
register_setting('nenghui_maintenance_options', 'nenghui_maintenance_mode_content');
|
|
|
}
|
|
|
add_action('admin_init', 'nenghui_register_maintenance_settings');
|
|
|
|
|
|
/**
|
|
|
* 设置页面 HTML
|
|
|
*/
|
|
|
function nenghui_maintenance_page_html() {
|
|
|
if (!current_user_can('manage_options')) {
|
|
|
return;
|
|
|
}
|
|
|
?>
|
|
|
<div class="wrap">
|
|
|
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
|
|
|
<form action="options.php" method="post">
|
|
|
<?php
|
|
|
settings_fields('nenghui_maintenance_options');
|
|
|
do_settings_sections('nenghui_maintenance_options');
|
|
|
|
|
|
$enabled = get_option('nenghui_maintenance_mode_enabled');
|
|
|
$title = get_option('nenghui_maintenance_mode_title', '网站维护中');
|
|
|
$content = get_option('nenghui_maintenance_mode_content', '我们正在对网站进行升级维护,请稍后再访问。');
|
|
|
?>
|
|
|
<table class="form-table">
|
|
|
<tr valign="top">
|
|
|
<th scope="row">启用维护模式</th>
|
|
|
<td>
|
|
|
<label>
|
|
|
<input type="checkbox" name="nenghui_maintenance_mode_enabled" value="1" <?php checked(1, $enabled, true); ?> />
|
|
|
开启维护模式
|
|
|
</label>
|
|
|
<p class="description">开启后,只有管理员可以访问前台,普通访客将看到维护页面。</p>
|
|
|
</td>
|
|
|
</tr>
|
|
|
<tr valign="top">
|
|
|
<th scope="row">维护标题</th>
|
|
|
<td>
|
|
|
<input type="text" name="nenghui_maintenance_mode_title" value="<?php echo esc_attr($title); ?>" class="regular-text" />
|
|
|
</td>
|
|
|
</tr>
|
|
|
<tr valign="top">
|
|
|
<th scope="row">维护内容</th>
|
|
|
<td>
|
|
|
<?php
|
|
|
wp_editor($content, 'nenghui_maintenance_mode_content', array(
|
|
|
'textarea_name' => 'nenghui_maintenance_mode_content',
|
|
|
'media_buttons' => false,
|
|
|
'textarea_rows' => 10,
|
|
|
'teeny' => true
|
|
|
));
|
|
|
?>
|
|
|
</td>
|
|
|
</tr>
|
|
|
</table>
|
|
|
<?php submit_button(); ?>
|
|
|
</form>
|
|
|
</div>
|
|
|
<?php
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行维护模式拦截
|
|
|
*/
|
|
|
function nenghui_maintenance_mode_handler() {
|
|
|
// 如果维护模式未开启,直接返回
|
|
|
if (!get_option('nenghui_maintenance_mode_enabled')) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 如果是管理员或已登录用户,允许访问
|
|
|
// 这里使用 edit_themes 权限作为判断标准,通常管理员和编辑都有此权限
|
|
|
// 或者简单地使用 is_user_logged_in() 允许所有登录用户
|
|
|
if (current_user_can('edit_themes') || is_user_logged_in()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 排除登录页面和后台
|
|
|
if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) {
|
|
|
return;
|
|
|
}
|
|
|
if (is_admin()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 获取设置的标题和内容
|
|
|
$title = get_option('nenghui_maintenance_mode_title', '网站维护中');
|
|
|
$content = get_option('nenghui_maintenance_mode_content', '我们正在对网站进行升级维护,请稍后再访问。');
|
|
|
// 处理内容中的换行(如果是纯文本框输入的话,但这里用了wp_editor,通常不需要nl2br,不过为了保险可以加上wpautop)
|
|
|
$content_html = wpautop($content);
|
|
|
|
|
|
// 获取站点 Logo
|
|
|
$logo_url = get_theme_mod('site_logo');
|
|
|
if (!$logo_url) {
|
|
|
$logo_url = 'https://nenghui.com/wp-content/uploads/2025/11/logo-2.png';
|
|
|
}
|
|
|
|
|
|
// 发送 503 状态码
|
|
|
header('HTTP/1.1 503 Service Unavailable');
|
|
|
header('Retry-After: 3600');
|
|
|
|
|
|
// 输出维护页面
|
|
|
?>
|
|
|
<!DOCTYPE html>
|
|
|
<html lang="<?php echo get_bloginfo('language'); ?>">
|
|
|
<head>
|
|
|
<meta charset="<?php echo get_bloginfo('charset'); ?>">
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
<title><?php echo esc_html($title); ?> - <?php echo get_bloginfo('name'); ?></title>
|
|
|
<style>
|
|
|
:root {
|
|
|
--primary-color: #007cba;
|
|
|
--text-color: #333;
|
|
|
--bg-color: #f7f9fc;
|
|
|
}
|
|
|
body {
|
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
|
|
|
background: var(--bg-color);
|
|
|
color: var(--text-color);
|
|
|
height: 100vh;
|
|
|
margin: 0;
|
|
|
display: flex;
|
|
|
align-items: center;
|
|
|
justify-content: center;
|
|
|
text-align: center;
|
|
|
}
|
|
|
.maintenance-container {
|
|
|
background: #fff;
|
|
|
padding: 60px 40px;
|
|
|
border-radius: 12px;
|
|
|
box-shadow: 0 10px 30px rgba(0,0,0,0.08);
|
|
|
max-width: 500px;
|
|
|
width: 90%;
|
|
|
margin: 20px;
|
|
|
position: relative;
|
|
|
overflow: hidden;
|
|
|
}
|
|
|
.maintenance-container::before {
|
|
|
content: '';
|
|
|
position: absolute;
|
|
|
top: 0;
|
|
|
left: 0;
|
|
|
width: 100%;
|
|
|
height: 6px;
|
|
|
background: linear-gradient(90deg, #007cba, #00a0d2);
|
|
|
}
|
|
|
.logo {
|
|
|
margin-bottom: 30px;
|
|
|
}
|
|
|
.logo img {
|
|
|
max-height: 60px;
|
|
|
width: auto;
|
|
|
}
|
|
|
h1 {
|
|
|
color: #222;
|
|
|
margin: 0 0 20px;
|
|
|
font-size: 24px;
|
|
|
font-weight: 600;
|
|
|
}
|
|
|
.content {
|
|
|
font-size: 16px;
|
|
|
line-height: 1.6;
|
|
|
color: #666;
|
|
|
margin-bottom: 30px;
|
|
|
}
|
|
|
.footer {
|
|
|
margin-top: 30px;
|
|
|
font-size: 13px;
|
|
|
color: #999;
|
|
|
border-top: 1px solid #f0f0f0;
|
|
|
padding-top: 20px;
|
|
|
}
|
|
|
.maintenance-icon {
|
|
|
font-size: 48px;
|
|
|
margin-bottom: 20px;
|
|
|
color: var(--primary-color);
|
|
|
}
|
|
|
</style>
|
|
|
</head>
|
|
|
<body>
|
|
|
<div class="maintenance-container">
|
|
|
<?php if ($logo_url): ?>
|
|
|
<div class="logo">
|
|
|
<img src="<?php echo esc_url($logo_url); ?>" alt="<?php echo esc_attr(get_bloginfo('name')); ?>">
|
|
|
</div>
|
|
|
<?php else: ?>
|
|
|
<div class="maintenance-icon">⚙️</div>
|
|
|
<?php endif; ?>
|
|
|
|
|
|
<h1><?php echo esc_html($title); ?></h1>
|
|
|
|
|
|
<div class="content">
|
|
|
<?php echo $content_html; // 允许HTML内容 ?>
|
|
|
</div>
|
|
|
|
|
|
<div class="footer">
|
|
|
© <?php echo date('Y'); ?> Nenghui Energy Technology Co.,Ltd. All rights reserved.
|
|
|
</div>
|
|
|
</div>
|
|
|
</body>
|
|
|
</html>
|
|
|
<?php
|
|
|
exit();
|
|
|
}
|
|
|
// 挂载到 template_redirect 或 get_header
|
|
|
add_action('template_redirect', 'nenghui_maintenance_mode_handler');
|