File: /www/wwwroot/www.213n.cn/wp-content/themes/rk-blogger/wbc/settings/options.inc.php
<?php
/**
* Author: wbolt team
* Author URI: https://www.wbolt.com
*/
class WBOptions
{
public static $option_name = 'wb_theme_cnf';
public static $cnf_fields = array();
public static $more_menu_items = array(
array(
'name' => '广告管理',
'slug' => 'bn'
),
array(
'name' => '友链管理',
'slug' => 'links'
),
array(
'name' => '环境参数',
'slug' => 'env'
),
array(
'name' => '推荐资源',
'slug' => 'more'
)
);
public function __construct()
{
}
public static function init()
{
if (is_admin()) {
add_filter('script_loader_tag', [__CLASS__, 'script_tag_handler'], 10, 3);
add_action('admin_menu', array(__CLASS__, 'options_page_menu'));
add_action('admin_init', array(__CLASS__, 'admin_init'), 1);
add_action('wp_ajax_wb_setting_ajax', array(__CLASS__, 'ajax_handler'));
}
add_filter('wb_social_icons', array(__CLASS__, 'wb_social_icons_handler'));
}
public static function admin_init()
{
add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 1);
self::load();
}
public static function enqueue_scripts($hook)
{
global $wb_settings_page_hook_theme;
if ($wb_settings_page_hook_theme != $hook) {
return;
}
wp_register_script('wbs-inline-js', false, null, false);
wp_enqueue_script('wbs-inline-js');
$_ajax_nonce = wp_create_nonce('wp_ajax_wb_admin');
$inline_script = 'var _admin_url ="' . admin_url() . '",
_wp_cf = {ajax_url: "' . admin_url('/admin-ajax.php') . '"},
wb_vue_path = "' . WB_THEME_URI . '/settings/";' . "\n";
$js_cnf = [
'admin_options_reading' => admin_url('options-reading.php'),
'admin_nav_menus' => admin_url('nav-menus.php'),
'dir_url' => WB_THEME_URI,
'admin_home' => admin_url('/'),
'ajax_url' => admin_url('/admin-ajax.php'),
'home_url' => home_url(),
'_wb_ajax_nonce' => $_ajax_nonce,
'_pd_name' => WB_THEMES_NAME,
'_pd_code' => WB_THEMES_CODE,
'_version' => WB_THEMES_VER,
'action' => [
'act' => 'wb_setting_ajax',
'fetch' => 'get_theme_setting',
'push' => 'set_theme_setting'
]
];
$inline_script .= " var wb_js_cnf = " . json_encode($js_cnf) . ";\n";
wp_add_inline_script('wbs-inline-js', $inline_script, 'before');
echo vite('src/main.js');
}
public static function options_page_menu()
{
global $wb_settings_page_hook_theme, $submenu;
$wb_settings_page_hook_theme = add_menu_page(
'主题设置',
'主题设置',
'manage_options',
'wbs',
array(__CLASS__, 'create_options_page'),
WB_THEME_URI . '/wbc/settings/assets/wbolt_ico.svg'
);
add_submenu_page(
'wbs',
'基本设置',
'基本设置',
'manage_options',
'wbs#/base',
array(__CLASS__, 'create_options_page')
);
foreach (self::$more_menu_items as $item) {
add_submenu_page(
'wbs',
$item['name'],
$item['name'],
'manage_options',
'wbs#/' . $item['slug'],
array(__CLASS__, 'sub_options_page')
);
}
unset($submenu['wbs'][0]);
}
public static function create_options_page()
{
if (!current_user_can('manage_options')) {
wp_die('无该页面访问权限。');
}
wp_enqueue_media();
echo '<div id="app"></div>';
}
public static function sub_options_page()
{
return;
}
public static function def_opt()
{
$cnf = self::get_cnf();
$fields = [];
foreach ($cnf as $key => $data) {
if (!empty($data['default'])) {
$fields = array_merge($fields, $data['default']);
}
}
/**
* 主题设置初始数据结构输出
*/
return apply_filters('wb_setting_initial_data', $fields);
}
/**
* 扩展属性
* @param $cnf array 当前属性
* @param $conf array 扩展属性
*/
public static function extend_conf(&$cnf, $conf)
{
if (is_array($conf)) foreach ($conf as $k => $v) {
if (is_int($k)) {
continue;
}
if (!isset($cnf[$k])) {
$cnf[$k] = $v;
} else if (is_array($v)) {
if (!is_array($cnf[$k])) {
$cnf[$k] = array();
}
self::extend_conf($cnf[$k], $v);
}
}
}
// 配置值
public static function opt($name = '', $default = false)
{
static $options = null;
if (null == $options) {
$options = get_option(self::$option_name, array());
$options = apply_filters('wb_theme_base_options', $options);
if (empty($options)) {
$options = self::def_opt();
} else {
self::extend_conf($options, self::def_opt());
}
}
$return = null;
do {
if (!$name) {
$return = $options;
break;
}
$ret = $options;
$ak = explode('.', $name);
foreach ($ak as $sk) {
if (isset($ret[$sk])) {
$ret = $ret[$sk];
} else {
$ret = $default;
break;
}
}
$return = $ret;
} while (0);
return apply_filters('wb_theme_get_conf', $return, $name, $default);
}
public static function ajax_handler()
{
if (!is_user_logged_in()) {
echo 'fail';
exit();
}
if (!current_user_can('manage_options')) {
exit();
}
switch ($_POST['do']) {
case 'chk_ver':
$http = wp_remote_get('https://www.wbolt.com/wb-api/v1/themes/checkver?code=' . WB_THEMES_CODE . '&ver=' . WB_THEMES_VER . '&chk=1', array('sslverify' => false, 'headers' => array('referer' => home_url()),));
if (wp_remote_retrieve_response_code($http) == 200) {
echo wp_remote_retrieve_body($http);
exit();
}
break;
case 'get_theme_setting':
$ret = array('code' => '0', 'desc' => 'success');
$key = $_POST['key'];
$ret['data'] = self::get_theme_setting($key);
header('content-type:text/json;charset=utf-8');
echo json_encode($ret);
break;
case 'set_theme_setting':
$ret = array('code' => '0', 'desc' => 'success');
$opt_data = $_POST['opt'];
$key = $_POST['key'];
$ret['data'] = self::set_theme_setting($key, $opt_data);
header('content-type:text/json;charset=utf-8');
echo json_encode($ret);
break;
case 'get_cate_list':
$ret = array('code' => '0', 'desc' => 'success');
$cat_data = wb_get_root_category();
$cat_list = array();
foreach ($cat_data as $k => $cat) {
$cat_list[$cat['term_id']] = $cat;
}
$ret['data']['cate_list'] = $cat_list;
$ret['data']['root_cat_ids'] = wb_get_root_category_id();
header('content-type:text/json;charset=utf-8');
echo json_encode($ret);
break;
case 'get_env_info':
$ret = array('code' => '0', 'desc' => 'success');
$ret['data'] = self::get_env_info();
header('content-type:text/json;charset=utf-8');
echo json_encode($ret);
break;
}
exit();
}
public static function set_theme_setting($key = '', $data = [])
{
$opt = self::opt();
$old = $opt;
switch ($key) {
case 'footer':
if ($data['social']) {
$opt['social'] = self::stripslashes_deep($data['social']);
unset($data['social']);
}
$opt['footer'] = self::stripslashes_deep($data);
break;
case 'ads':
$opt['ads'] = self::stripslashes_deep($data);
break;
case 'links':
$opt['links'] = self::stripslashes_deep($data);
break;
default:
foreach ($data as $slug => $value) {
$opt[$slug] = self::stripslashes_deep($value);
}
break;
}
update_option(self::$option_name, $opt);
do_action('wb_theme_option_update', $opt, $old);
return $opt;
}
public static function stripslashes_deep($value)
{
if (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = self::stripslashes_deep($v);
}
} else {
$value = stripslashes($value);
}
return $value;
}
public static function get_theme_setting($key = '')
{
$ret = array();
if (empty($key)) {
return self::opt();
}
switch ($key) {
case 'general':
$ret['opt'] = self::opt();
$ret['cnf'] = self::get_cnf('general');
break;
case 'ads':
$ret['opt'] = self::opt('ads');
$ret['cnf'] = self::get_cnf('ads');
break;
case 'links':
$ret['opt'] = self::opt('links');
$ret['cnf'] = self::get_cnf('links');
break;
case 'code':
$ret['opt'] = self::opt();
$ret['cnf'] = self::get_cnf('code');
break;
case 'footer':
$ret['opt'] = self::opt('footer');
$ret['opt']['social'] = self::opt('social');
$ret['cnf'] = self::get_cnf('footer');
break;
default:
$keys = explode(',', $key);
if (count($keys) > 1) {
foreach ($keys as $k) {
$ret['opt'][$k] = self::opt($k);
$ret['cnf'][$k] = self::get_cnf($k);
}
} else {
$ret['cnf'] = self::get_cnf();
$ret['opt'] = self::opt($key) ?: self::opt();
}
break;
}
return $ret;
}
/**
* 获取配置
*
* @param string $key
* @return [string] | [array]
*/
public static function get_cnf($key = '')
{
$cnf = self::$cnf_fields;
if (empty($cnf)) {
$cnf_fields_file = get_theme_file_path('wbc/settings/json/fields.json');
$cnf_fields_file = file_get_contents($cnf_fields_file) ?? [];
$cnf = json_decode($cnf_fields_file, true);
$cnf = apply_filters('wb_setting_cnf_fields', $cnf);
self::$cnf_fields = $cnf;
}
if ($key) {
return $cnf[$key] ?? '';
}
return $cnf;
}
/**
* 环境参数
*/
public static function get_env_info()
{
$cache = get_option('wb_ent_info');
if ($cache && $cache['time'] > current_time('U')) {
return $cache['info'];
}
$ent_info = array(
'pd_name' => WB_THEMES_CODE,
'current_ver' => WB_THEMES_VER,
'home_url' => WB_THEMES_VER,
'wp_content_dir' => WP_CONTENT_DIR,
'abspath' => ABSPATH,
'wp_debug' => defined('WP_DEBUG') && WP_DEBUG ? '开启' : '关闭',
'wp_locale' => esc_attr(get_locale()),
);
global $wp_version;
$ent_info['wp_version'] = $wp_version;
$memory = ini_get('memory_limit');
// If we can't get it, fallback to WP_MEMORY_LIMIT.
if (!$memory || -1 === $memory) {
$memory = wp_convert_hr_to_bytes(WP_MEMORY_LIMIT);
}
// Make sure the value is properly formatted in bytes.
if (!is_numeric($memory)) {
$memory = wp_convert_hr_to_bytes($memory);
}
$ent_info['memory'] = esc_attr(size_format($memory));
/**
* 服务器环境
*/
$ent_info['server_software'] = isset($_SERVER['SERVER_SOFTWARE']) ? esc_attr(sanitize_text_field(wp_unslash($_SERVER['SERVER_SOFTWARE']))) : 'Unknown';
$php_version = null;
if (defined('PHP_VERSION')) {
$php_version = PHP_VERSION;
} elseif (function_exists('phpversion')) {
$php_version = phpversion();
}
$ent_info['php_version'] = $php_version ? $php_version : 'Unknown';
$ent_info['post_max_size'] = esc_attr(size_format(wp_convert_hr_to_bytes(ini_get('post_max_size'))));
$ent_info['max_execution_time'] = esc_attr(ini_get('max_execution_time')) . 's';
$ent_info['max_input_vars'] = esc_attr(ini_get('max_input_vars'));
$ent_info['zip_archive'] = class_exists('ZipArchive') ? '启用' : '未启用';
global $wpdb;
$ent_info['db_version'] = esc_attr($wpdb->db_version());
$ent_info['wp_max_upload_size'] = esc_attr(size_format(wp_max_upload_size()));
$ent_info['dom_document'] = class_exists('DOMDocument') ? '启用' : '未启用';
$response = wp_remote_get(
'https://www.wbolt.com',
array(
'timeout' => 3,
'sslverify' => false,
'user-agent' => 'wbolt-remote-get-test',
)
);
$ent_info['wp_remote_get'] = (wp_remote_retrieve_response_code($response) == 200) ? '成功' : '失败';
$response = wp_remote_post(
'https://www.wbolt.com',
array(
'timeout' => 3,
'sslverify' => false,
'user-agent' => 'wbolt-remote-post-test',
)
);
$ent_info['wp_remote_post'] = (wp_remote_retrieve_response_code($response) == 200) ? '成功' : '失败';
$info = '未安装';
if (extension_loaded('gd') && function_exists('gd_info')) {
$info = '已安装';
$gd_info = gd_info();
if (isset($gd_info['GD Version'])) {
$info = $gd_info['GD Version'];
}
}
$ent_info['gd_info'] = esc_attr($info);
/**
* 激活插件
*/
$active_plugins = (array) get_option('active_plugins', array());
$plugins = get_plugins();
foreach ($active_plugins as $plug) {
$row = $plugins[$plug];
$ent_info['active_plugins'][] = array(
'name' => $row['Name'],
'author' => $row['Author']
);
}
$cache = array('info' => $ent_info, 'time' => current_time('U') + 300);
update_option('wb_ent_info', $cache, false);
return $ent_info;
}
public static function load()
{
add_action('load-themes.php', array(__CLASS__, 'check_version'), 100);
add_action('load-update.php', array(__CLASS__, 'check_version'), 100);
add_action('load-update-core.php', array(__CLASS__, 'check_version'), 100);
add_action('wp_update_themes', array(__CLASS__, 'check_version'), 100);
}
public static function check_version()
{
global $wp_version;
$update_themes = get_site_transient('update_themes');
if (!is_object($update_themes)) {
return;
}
$theme = wp_get_theme();
$tpl = $theme->get_template();
if (isset($update_themes->response[$tpl])) {
return;
}
$doing_cron = wp_doing_cron();
// Check for update on a different schedule, depending on the page.
switch (current_filter()) {
case 'upgrader_process_complete':
$timeout = 0;
break;
case 'load-update-core.php':
$timeout = MINUTE_IN_SECONDS;
break;
case 'load-themes.php':
case 'load-update.php':
$timeout = HOUR_IN_SECONDS;
break;
default:
if ($doing_cron) {
$timeout = 2 * HOUR_IN_SECONDS;
} else {
$timeout = 12 * HOUR_IN_SECONDS;
}
}
$time_not_changed = isset($update_themes->wb_checked) && $timeout > (time() - $update_themes->wb_checked);
if ($time_not_changed) {
return;
}
$update_themes->wb_checked = time();
set_site_transient('update_themes', $update_themes);
if ($doing_cron) {
$timeout = 30;
} else {
$timeout = 3;
}
$options = array(
'sslverify' => false,
'timeout' => $timeout,
'user-agent' => 'Wbolt WordPress/' . $wp_version . '; ' . home_url('/'),
'headers' => array('referer' => home_url()),
);
$url = 'https://www.wbolt.com/wb-api/v1/themes/checkver?chk=1&code=' . WB_THEMES_CODE . '&ver=' . WB_THEMES_VER;
$raw_response = wp_remote_get($url, $options);
if (is_wp_error($raw_response) || 200 != wp_remote_retrieve_response_code($raw_response)) {
return;
}
$new_version = trim(wp_remote_retrieve_body($raw_response));
if (!$new_version || !preg_match('#^\d[0-9\.]+#', $new_version)) {
return;
}
$update_themes->response[$tpl] = array(
'theme' => $tpl,
'new_version' => $new_version,
'url' => 'https://www.wbolt.com/themes/' . WB_THEMES_CODE . '/',
'package' => 'https://www.wbolt.com/wb-api/v1/themes/upgrade?p=' . WB_THEMES_CODE . '&v=free',
'requires' => '4.9.6',
'requires_php' => '5.3.5',
);
set_site_transient('update_themes', $update_themes);
}
/**
* 插入广告位
*/
static function inset_adblock($ad_name = '', $box_class = 'adbanner-block')
{
if (!$ad_name) return;
$ads = self::opt('ads.' . $ad_name);
if (empty($ads)) {
return;
}
if ($ads['type'] == 0) return;
$ad_html = '<div class="' . $box_class . '">';
if ($ads['type'] == 1) {
$ad_html .= $ads['code'];
} elseif ($ads['type'] == 2) {
$ad_html .= '<a href="' . $ads['url'] . '" target="_blank" rel="nofollow"><img class="adbn-img" src="' . $ads['img'] . '"></a>';
} else {
return;
}
$ad_html .= '</div>';
return $ad_html;
}
/**
* 友链输出
*/
static function wb_get_links()
{
$links_items = self::opt('links');
if (empty($links_items)) return '';
$tpl = '';
foreach ($links_items as $item) :
$target = isset($item['target']) && $item['target'] ? ' target="_blank"' : '';
$nofollow = isset($item['nofollow']) && $item['nofollow'] ? ' rel=" nofollow"' : '';
$tpl .= '<a href="' . $item['url'] . '"' . $target . $nofollow . ' >';
if (isset($item['img']) && $item['img']) {
$tpl .= '<img src="' . $item['img'] . '" alt="">';
}
$tpl .= '<span>' . $item['name'] . '</span>';
$tpl .= '</a>';
endforeach;
return $tpl;
}
/**
* 页脚图标
* @param $attr
*
* @return bool
*/
public static function wb_social_icons_handler($attr)
{
$social_items = wb_opt('social');
if (!$social_items) return false;
$wp_class = isset($attr['class']) ? $attr['class'] : 'wb-social-items';
$tpl = '<div class="' . $wp_class . '">';
foreach ($social_items as $k => $item) {
if (!$item['status']) continue;
if (!$item['url'] && !$item['img']) continue;
$class = $item['type'] == 1 ? ' with-popover' : '';
$url_tpl = '';
if (!$item['type'] && $item['url']) {
$url_tpl = ' href="' . $item['url'] . '" target="_blank"';
}
$tpl .= '<a class="social-item' . $class . '"' . $url_tpl . '>';
$tpl .= wbolt_svg_icon('wbsico-' . $item['code']);
if ($item['type'] == 1) {
$tpl .= '<div class="popover-img">
<div class="ppi-inner"><img src="' . $item['img'] . '"></div>
</div>';
}
$tpl .= '</a>';
}
$tpl .= '</div>';
echo $tpl;
}
/**
* js输出加type="module"
* 适用vite生成module js
*
* @param [type] $tag
* @param [type] $handle
* @param [type] $src
* @return string
*/
public static function script_tag_handler($tag, $handle, $src)
{
if (preg_match("/wbs-/i", $handle)) {
return '<script type="module" src="' . esc_url($src) . '" defer></script>' . "\n";
} else {
return $tag;
}
}
}
WBOptions::init();