File: /www/wwwroot/nasuhang.com/wp-content/themes/twentytwentyfive/inc/guide-settings.php
<?php
/**
* Twenty Twenty-Five Custom Settings - 导游配置
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class HZBY_Guide_Settings {
private static $option_key = 'hzby_guide_settings';
public static function init() {
add_action( 'admin_menu', array( __CLASS__, 'add_menu' ) );
add_action( 'admin_init', array( __CLASS__, 'register_settings' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_media' ) );
}
public static function add_menu() {
add_menu_page(
'导游配置',
'导游配置',
'manage_options',
'hzby-guide-settings',
array( __CLASS__, 'render_page' ),
'dashicons-id-alt',
30
);
}
public static function enqueue_media( $hook ) {
if ( $hook !== 'toplevel_page_hzby-guide-settings' ) {
return;
}
wp_enqueue_media();
wp_enqueue_script(
'hzby-guide-media',
get_template_directory_uri() . '/inc/guide-media.js',
array(),
'1.0.0',
true
);
}
public static function register_settings() {
register_setting( 'hzby_guide_group', self::$option_key );
}
public static function get_settings() {
$defaults = array(
'name' => '',
'weChat' => '',
'tel' => '',
'address' => '',
'customerServiceId' => '',
'office' => '',
'avatar' => '',
'qrcode' => '',
'lineId' => '',
'lineQrcode' => '',
'whatsappId' => '',
'whatsappQrcode' => '',
);
$saved = get_option( self::$option_key, array() );
return wp_parse_args( $saved, $defaults );
}
public static function render_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$settings = self::get_settings();
?>
<div class="wrap">
<h1>导游配置</h1>
<form method="post" action="options.php">
<?php settings_fields( 'hzby_guide_group' ); ?>
<table class="form-table">
<tr>
<th><label for="hzby_name">导游名称</label></th>
<td><input type="text" id="hzby_name" name="<?php echo esc_attr( self::$option_key ); ?>[name]" value="<?php echo esc_attr( $settings['name'] ); ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label for="hzby_weChat">微信号</label></th>
<td><input type="text" id="hzby_weChat" name="<?php echo esc_attr( self::$option_key ); ?>[weChat]" value="<?php echo esc_attr( $settings['weChat'] ); ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label for="hzby_tel">电话</label></th>
<td><input type="text" id="hzby_tel" name="<?php echo esc_attr( self::$option_key ); ?>[tel]" value="<?php echo esc_attr( $settings['tel'] ); ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label for="hzby_address">地址 (address)</label></th>
<td><input type="text" id="hzby_address" name="<?php echo esc_attr( self::$option_key ); ?>[address]" value="<?php echo esc_attr( $settings['address'] ); ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label for="hzby_customerServiceId">客服 ID</label></th>
<td><input type="text" id="hzby_customerServiceId" name="<?php echo esc_attr( self::$option_key ); ?>[customerServiceId]" value="<?php echo esc_attr( $settings['customerServiceId'] ); ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label for="hzby_office">办公室 ID</label></th>
<td><input type="text" id="hzby_office" name="<?php echo esc_attr( self::$option_key ); ?>[office]" value="<?php echo esc_attr( $settings['office'] ); ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label>导游头像</label></th>
<td>
<input type="hidden" id="hzby_avatar" name="<?php echo esc_attr( self::$option_key ); ?>[avatar]" value="<?php echo esc_attr( $settings['avatar'] ); ?>" />
<img id="hzby_avatar_preview" src="<?php echo esc_url( $settings['avatar'] ); ?>" style="max-width:120px;max-height:120px;display:<?php echo $settings['avatar'] ? 'block' : 'none'; ?>;border-radius:8px;margin-bottom:8px;" />
<br>
<button type="button" class="button hzby-media-upload" data-target="#hzby_avatar" data-preview="#hzby_avatar_preview">选择图片</button>
<button type="button" class="button hzby-media-remove" data-target="#hzby_avatar" data-preview="#hzby_avatar_preview">移除</button>
</td>
</tr>
<tr>
<th><label>微信二维码</label></th>
<td>
<input type="hidden" id="hzby_qrcode" name="<?php echo esc_attr( self::$option_key ); ?>[qrcode]" value="<?php echo esc_attr( $settings['qrcode'] ); ?>" />
<img id="hzby_qrcode_preview" src="<?php echo esc_url( $settings['qrcode'] ); ?>" style="max-width:120px;max-height:120px;display:<?php echo $settings['qrcode'] ? 'block' : 'none'; ?>;border-radius:8px;margin-bottom:8px;" />
<br>
<button type="button" class="button hzby-media-upload" data-target="#hzby_qrcode" data-preview="#hzby_qrcode_preview">选择图片</button>
<button type="button" class="button hzby-media-remove" data-target="#hzby_qrcode" data-preview="#hzby_qrcode_preview">移除</button>
</td>
</tr>
<tr>
<th><label>Line 账号</label></th>
<td><input type="text" id="hzby_lineId" name="<?php echo esc_attr( self::$option_key ); ?>[lineId]" value="<?php echo esc_attr( $settings['lineId'] ); ?>" class="regular-text" placeholder="Line ID" /></td>
</tr>
<tr>
<th><label>Line 二维码</label></th>
<td>
<input type="hidden" id="hzby_lineQrcode" name="<?php echo esc_attr( self::$option_key ); ?>[lineQrcode]" value="<?php echo esc_attr( $settings['lineQrcode'] ); ?>" />
<img id="hzby_lineQrcode_preview" src="<?php echo esc_url( $settings['lineQrcode'] ); ?>" style="max-width:120px;max-height:120px;display:<?php echo $settings['lineQrcode'] ? 'block' : 'none'; ?>;border-radius:8px;margin-bottom:8px;" />
<br>
<button type="button" class="button hzby-media-upload" data-target="#hzby_lineQrcode" data-preview="#hzby_lineQrcode_preview">选择图片</button>
<button type="button" class="button hzby-media-remove" data-target="#hzby_lineQrcode" data-preview="#hzby_lineQrcode_preview">移除</button>
</td>
</tr>
<tr>
<th><label>WhatsApp 号码</label></th>
<td><input type="text" id="hzby_whatsappId" name="<?php echo esc_attr( self::$option_key ); ?>[whatsappId]" value="<?php echo esc_attr( $settings['whatsappId'] ); ?>" class="regular-text" placeholder="852XXXXXXXX" /></td>
</tr>
<tr>
<th><label>WhatsApp 二维码</label></th>
<td>
<input type="hidden" id="hzby_whatsappQrcode" name="<?php echo esc_attr( self::$option_key ); ?>[whatsappQrcode]" value="<?php echo esc_attr( $settings['whatsappQrcode'] ); ?>" />
<img id="hzby_whatsappQrcode_preview" src="<?php echo esc_url( $settings['whatsappQrcode'] ); ?>" style="max-width:120px;max-height:120px;display:<?php echo $settings['whatsappQrcode'] ? 'block' : 'none'; ?>;border-radius:8px;margin-bottom:8px;" />
<br>
<button type="button" class="button hzby-media-upload" data-target="#hzby_whatsappQrcode" data-preview="#hzby_whatsappQrcode_preview">选择图片</button>
<button type="button" class="button hzby-media-remove" data-target="#hzby_whatsappQrcode" data-preview="#hzby_whatsappQrcode_preview">移除</button>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
}
HZBY_Guide_Settings::init();