2 Star 25 Fork 3

VTHEME / Mirage 主题

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
functions.php 9.46 KB
一键复制 编辑 原始数据 按行查看 历史
像素鱼丸 提交于 2026-07-22 19:31 +08:00 . 3.10.0
<?php
if (!defined('ABSPATH')) exit;
/**
* functions and definitions
*
* @author: vtheme <mail@vtheme.cn>
* @link https://vtheme.cn
*/
define('THEME_OPTION_NAME', 'mirage');
define('THEME_DIR', get_template_directory());
define('THEME_URL', get_template_directory_uri());
if (!function_exists('p')) :
function p($arg)
{
echo "<pre style='padding-left:190px;font-size:12px;'>\n";
print_r($arg);
echo "</pre>\n\n";
}
endif;
function vt_get_config($key = null, $default = null)
{
static $config = null;
if ($config === null) {
$raw = get_option(THEME_OPTION_NAME);
$config = is_array($raw) ? $raw : [];
}
if ($key === null) {
return $config;
}
return array_key_exists($key, $config) ? $config[$key] : $default;
}
require_once THEME_DIR . '/inc/helper.php';
require_once THEME_DIR . '/inc/i18n.php';
require_once THEME_DIR . '/inc/config.php';
require_once THEME_DIR . '/inc/menu.php';
require_once THEME_DIR . '/inc/meta.php';
require_once THEME_DIR . '/inc/widget.php';
require_once THEME_DIR . '/inc/setting.php';
require_once THEME_DIR . '/inc/shortcode.php';
require_once THEME_DIR . '/inc/switch.php';
require_once THEME_DIR . '/inc/category.php';
require_once THEME_DIR . '/inc/user.php';
require_once THEME_DIR . '/inc/ajax.php';
require_once THEME_DIR . '/inc/rewrite.php';
require_once THEME_DIR . '/inc/common/user-stat.php';
require_once THEME_DIR . '/api/_loader.php';
/**
* 主题菜单设置
*/
if (function_exists('register_nav_menus')) {
register_nav_menus(
array(
'header_main' => __('Mirage Header Menu'),
'footer_nav' => __('Mirage Footer Menu'),
'side_menu' => __('Mirage Sider Menu')
)
);
}
/**
* 主题激活后检查和更新模块配置
*/
add_action('after_switch_theme', 'vt_init_theme_modules');
function vt_init_theme_modules() {
// 只有超级管理员才能执行
if (!current_user_can('manage_options')) {
return;
}
$config = get_option(THEME_OPTION_NAME);
// 定义默认模块(与 config.php 中的 default 保持一致)
$default_modules = array(
'slider' => __('Sliders', 'vt'),
'notices' => __('Notices', 'vt'),
'hot' => __('Hot topics', 'vt'),
'last' => __('Latest', 'vt'),
'links' => __('Links', 'vt'),
);
// 获取当前配置的 enabled 和 disabled
$enabled = isset($config['home_layout']['enabled']) ? $config['home_layout']['enabled'] : array();
$disabled = isset($config['home_layout']['disabled']) ? $config['home_layout']['disabled'] : array();
// 合并为 current
$current = array_merge($enabled, $disabled);
// 找出缺失的模块(在 default 中但不在 current 中)
$missing = array_diff_key($default_modules, $current);
// 如果有缺失的模块,添加到 disabled 中
if (!empty($missing)) {
foreach ($missing as $key => $name) {
$disabled[$key] = $name;
}
// 更新配置
$config['home_layout']['enabled'] = $enabled;
$config['home_layout']['disabled'] = $disabled;
update_option(THEME_OPTION_NAME, $config);
}
}
function vt_get_post_category_name($post_ID){
global $wpdb;
$sql = $wpdb->prepare("SELECT `term_taxonomy_id` FROM $wpdb->term_relationships WHERE `object_id` = %d", $post_ID);
$cat_id_arr = $wpdb->get_results($sql,ARRAY_A);
if(empty($cat_id_arr)) return '';
$cat_id = $cat_id_arr[0]['term_taxonomy_id'];
$term = get_term( $cat_id );
return !empty($term->name) ? $term->name : '';
}
/**
* 设置文章浏览次数
* @param [type] $postID
* @return void
*/
function set_post_views($postID)
{
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if ($count == '') {
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '1');
} else {
$count++;
update_post_meta($postID, $count_key, $count);
}
}
/**
* 获取文章浏览次数
* @param [type] $postID
* @return void
*/
function get_post_views($postID)
{
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if ($count == '') {
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return 0;
}
return $count;
}
function vt_get_time($time){
return formatHumanTime($time);
}
/*
* 获取自定义头像
*/
if (!function_exists('vt_get_custom_avatar_url')) {
function vt_get_custom_avatar_url($user_id)
{
// 修复:统一使用正确的meta key
$attachment_id = get_user_meta($user_id, "user_avatar_attachment_id", true);
if ($attachment_id) {
$avatar = wp_get_attachment_image_src($attachment_id, 'medium')[0];
} else {
$avatar = THEME_URL . '/assets/images/avatar.jpg';
}
return $avatar;
}
}
/**
* 获取特色图片 url
* @param {int} $post_id 文章ID
*/
if (!function_exists('vt_get_thumbnail_url')) {
function vt_get_thumbnail_url($post_id, $size='thumbnail')
{
$url = get_the_post_thumbnail_url($post_id, $size);
if (!$url) {
$url = vt_get_config('default_image', '');
$url = $url ? $url : get_template_directory_uri() . '/assets/images/default.jpg';
}
return $url;
}
}
/**
* 显示广告
* 头部广告 ad_header
* 底部广告 ad_footer
* 侧边栏广告 ad_sidebar
* 文章头部广告 ad_single_top
* 文章底部广告 ad_single_bottom
*/
function vt_theme_ad($position) {
$enable = vt_get_config($position . '_enable', true);
$code = vt_get_config($position . '_code', '');
$code_pc = vt_get_config($position . '_code_pc', '');
if (empty($enable) || empty($code)) {
return '';
}
$html = '<div class="vt-theme-ad ad-' . esc_attr($position) . '">';
// 白名单过滤广告标签,杜绝XSS
$allow_tags = array(
'a' => array('href' => true, 'target' => true),
'img' => array('src' => true, 'alt' => true),
'div' => array('style' => true),
'span' => array('style' => true),
'p' => array()
);
$html .= '<div class="ad-mobile">' . wp_kses($code, $allow_tags) . '</div>';
if (!empty($code_pc)) {
$html .= '<div class="ad-pc">' . wp_kses($code_pc, $allow_tags) . '</div>';
}
$html .= '</div>';
return $html;
}
function vt_truncate_comment_content($content, $length = 100) {
$safe = wp_kses_post($content);
if (mb_strlen($safe, 'UTF-8') <= $length) {
return $safe;
}
return mb_substr($safe, 0, $length, 'UTF-8') . '…';
}
function vt_friendly_time( $datetime ) {
$timestamp = is_numeric( $datetime ) ? $datetime : strtotime( $datetime );
if ( ! $timestamp ) return '';
return sprintf( __( '%s ago', 'v' ), human_time_diff( $timestamp, current_time( 'timestamp' ) ) );
}
function v_active($current, $action){
$current = sanitize_key($current);
$action = sanitize_key($action);
return $action === $current ? 'active' : '';
}
/**
* 在前端页面中生成美化URL的辅助函数
*/
function v_get_user_url($user_id, $action = 'profile') {
if ($action === 'profile') {
return home_url("/user/{$user_id}");
}
return home_url("/user/{$user_id}/{$action}");
}
/**
* 权限校验:已登录 + Nonce 双重校验(需登录接口)
*/
function vt_permission_check( $request ) {
if ( ! is_user_logged_in() ) {
return new \WP_Error('unauthorized', __('Please login first', 'vt'), ['status' => 401]);
}
$nonce = $request->get_header( 'X-WP-Nonce' );
if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
return new \WP_Error('invalid_nonce', __('Illegal request token', 'vt'), ['status' => 403]);
}
return true;
}
/**
* 开放接口 Nonce 校验(无需登录、仅防CSRF)
*/
function vt_nonce_check( $request ) {
$nonce = $request->get_header( 'X-WP-Nonce' );
if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
return new \WP_Error('invalid_nonce', __('Illegal request token', 'vt'), ['status' => 403]);
}
return true;
}
/**
* WP REST 接口通用限流工具
* @param string $key 限流唯一标识
* @param int $max 最大允许次数
* @param int $expire 过期秒数
* @return true|\WP_Error true=放行,WP_Error=超限
*/
function vt_rate_limit_check(string $key, int $max, int $expire)
{
$count = get_transient($key) ?: 0;
if ($count >= $max) {
return new \WP_Error(
'request_limit',
__('Too many requests, please try later', 'vt'),
['status' => 429]
);
}
set_transient($key, $count + 1, $expire);
return true;
}
function vt_print_ajax_var() {
// 仅文章/页面详情页输出,首页列表不加载节省资源
if ( ! is_singular() ) {
echo '<script type="text/javascript">var ajax_object = {};</script>';
}
$ajax_data = array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('ajax_nonce'),
'submit_nonce' => wp_create_nonce('ajax_submit_comment'),
'wp_rest_nonce' => wp_create_nonce('wp_rest'),
'current_user_id' => get_current_user_id()
);
echo '<script type="text/javascript">var ajax_object = ' . json_encode($ajax_data) . ';</script>';
}
add_action('wp_footer', 'vt_print_ajax_var');
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/vthemecn/mirage.git
git@gitee.com:vthemecn/mirage.git
vthemecn
mirage
Mirage 主题
main

搜索帮助