* 热门排行榜小工具(悬停大图)- 深度优化版 * 文件路径: /wp-content/themes/zibll/inc/functions/bbs/widgets/ranking-widget.php * * 优化内容: * 1. 缓存机制 - 减少数据库查询 * 2. 初始渲染优化 - 避免空白等待 * 3. 代码结构重构 - 提高可维护性 * 4. 性能优化 - 减少重复加载 */ // 防止直接访问 if (!defined('ABSPATH')) { exit; } global $zib_bbs; Zib_CFSwidget::create('zib_bbs_widget_ui_tab_posts_ranking', array( 'title' => '[' . $zib_bbs->forum_name . ']热门排行榜(悬停大图)', 'zib_title' => false, 'zib_affix' => true, 'zib_show' => true, 'description' => '多个TAB栏目切换显示' . $zib_bbs->posts_name . '热门排行榜,支持日期筛选,鼠标悬停显示大图(已优化缓存机制)', 'fields' => array( array( 'title' => '显示数量', 'id' => 'paged_size', 'class' => '', 'default' => 12, 'max' => 20, 'min' => 4, 'step' => 1, 'unit' => '篇', 'type' => 'spinner', ), array( 'id' => 'paginate', 'title' => '翻页按钮', 'default' => 'none', 'type' => "radio", 'inline' => true, 'options' => array( 'none' => __('不允许翻页', 'zib_language'), 'ajax_lists' => __('AJAX追加列表翻页', 'zib_language'), 'default' => __('数字翻页按钮', 'zib_language'), ), ), // 新增缓存配置 array( 'title' => '缓存设置', 'id' => 'cache_time', 'default' => 300, 'min' => 60, 'max' => 3600, 'step' => 60, 'unit' => '秒', 'desc' => '设置缓存时间,减少数据库查询,提高页面加载速度', 'type' => 'spinner', ), array( 'id' => 'show_cache_debug', 'title' => '显示缓存调试信息', 'desc' => '仅管理员可见,用于监控缓存状态和性能', 'type' => 'switcher', 'default' => false, ), array( 'id' => 'tabs', 'type' => 'group', 'accordion_title_number' => true, 'button_title' => '添加栏目', 'sanitize' => false, 'title' => '栏目', 'default' => array( array( 'title' => '周榜', 'date_range' => '7' ), array( 'title' => '月榜', 'date_range' => '30' ), array( 'title' => '年榜', 'date_range' => '365' ), ), 'fields' => array( array( 'id' => 'title', 'title' => '标题(必填)', 'desc' => '栏目显示的标题,支持HTML代码,注意代码规范', 'attributes' => array( 'rows' => 1, ), 'sanitize' => false, 'type' => 'textarea', ), array( 'title' => '日期范围', 'id' => 'date_range', 'default' => '', 'type' => "select", 'options' => array( '' => '不限时间', '1' => '最近1天', '7' => '最近1周', '30' => '最近1月', '365' => '最近1年', ), ), array( 'label' => '仅显示当前' . $zib_bbs->plate_name . '的' . $zib_bbs->posts_name, 'id' => 'current_plate', 'desc' => '当此模块放置在' . $zib_bbs->plate_name . '页面的时候,开启此功能后,则按照当前' . $zib_bbs->plate_name . '进行筛选。可实现本版热门、本版精华等效果
开启此功能后,该栏目只会在' . $zib_bbs->plate_name . '和' . $zib_bbs->posts_name . '页面显示
', 'type' => 'switcher', 'default' => false, ), array( 'dependency' => array('current_plate', '==', '', '', 'visible'), 'id' => 'include_plate', 'title' => __('包含' . $zib_bbs->plate_name, 'zib_language'), 'desc' => '仅显示所选' . $zib_bbs->plate_name . '的帖子,支持单选、多选。输入版块关键词搜索选择', 'default' => '', 'options' => 'post', 'query_args' => array( 'post_type' => 'plate', ), 'ajax' => true, 'settings' => array( 'min_length' => 2, ), 'placeholder' => '输入关键词以搜索' . $zib_bbs->plate_name, 'chosen' => true, 'multiple' => true, 'type' => 'select', ), array( 'dependency' => array('include_plate|current_plate', '==|==', '|', '', 'visible'), 'id' => 'exclude_plate', 'title' => __('排除版块', 'zib_language'), 'desc' => '排除所选版块的帖子,支持单选、多选。输入版块关键词搜索选择', 'default' => '', 'options' => 'post', 'query_args' => array( 'post_type' => 'plate', ), 'ajax' => true, 'settings' => array( 'min_length' => 2, ), 'placeholder' => '输入关键词以搜索版块分类', 'chosen' => true, 'multiple' => true, 'type' => 'select', ), array( 'id' => 'include_topic', 'title' => __('包含' . $zib_bbs->topic_name, 'zib_language'), 'desc' => '仅显示所选' . $zib_bbs->topic_name . '的' . $zib_bbs->posts_name . ',支持单选、多选。输入关键词搜索选择', 'default' => '', 'options' => 'categories', 'query_args' => array( 'taxonomy' => 'forum_topic', ), 'placeholder' => '输入关键词以搜索' . $zib_bbs->topic_name, 'chosen' => true, 'multiple' => true, 'ajax' => true, 'settings' => array( 'min_length' => 2, ), 'type' => 'select', ), array( 'id' => 'include_tag', 'title' => __('包含' . $zib_bbs->tag_name, 'zib_language'), 'desc' => '仅显示所选' . $zib_bbs->tag_name . '的' . $zib_bbs->posts_name . ',支持单选、多选。输入关键词搜索选择', 'default' => '', 'options' => 'categories', 'query_args' => array( 'taxonomy' => 'forum_tag', ), 'placeholder' => '输入关键词以搜索' . $zib_bbs->tag_name, 'chosen' => true, 'ajax' => true, 'settings' => array( 'min_length' => 2, ), 'multiple' => true, 'type' => 'select', ), array( 'title' => '类型筛选', 'id' => 'bbs_type', 'default' => '', 'type' => "checkbox", 'placeholder' => '限制帖子类型,支持单选、多选', 'inline' => true, 'options' => 'zib_bbs_get_posts_type_options', ), array( 'title' => '阅读权限筛选', 'id' => 'allow_view', 'default' => [], 'inline' => true, 'type' => "checkbox", 'placeholder' => '不做其它筛选', 'options' => array( 'signin' => '登录后查看', 'comment' => '评论后查看', 'pay' => '付费查看', 'points' => '支付积分查看', 'roles' => '部分用户可查看', ), ), array( 'title' => '其它筛选', 'id' => 'filter', 'default' => [], 'inline' => true, 'type' => "checkbox", 'options' => array( 'topping' => '置顶帖子', 'vote' => '投票帖子', 'essence' => '精华帖子', 'question_status' => '提问已解决', 'is_hot' => '热门帖子', ), ), array( 'title' => '排序方式', 'id' => 'orderby', 'default' => 'date', 'type' => "select", 'options' => zib_bbs_get_posts_order_options(), ), ), ), ), )); /** * 主要Widget渲染函数 */ function zib_bbs_widget_ui_tab_posts_ranking($args, $instance) { $show_class = Zib_CFSwidget::show_class($instance); if (!$show_class || empty($instance['tabs'])) { return; } $widget_id = $args['widget_id']; $id_base = 'zib_bbs_widget_ui_tab_posts_ranking'; $index = str_replace($id_base . '-', '', $widget_id); // 渲染完整的Widget HTML $main_html = zib_bbs_get_ranking_widget_html($widget_id, $instance, $index); Zib_CFSwidget::echo_before($instance, 'widget-tab-bbs-ranking'); echo $main_html; Zib_CFSwidget::echo_after($instance); } /** * 生成Widget完整HTML结构 */ function zib_bbs_get_ranking_widget_html($widget_id, $instance, $index) { $id_base = 'zib_bbs_widget_ui_tab_posts_ranking'; $ajax_href = add_query_arg(array( 'action' => 'ajax_widget_ui', 'id' => $id_base, 'index' => $index, ), admin_url('/admin-ajax.php')); $tabs_con = ''; $tabs_nav = ''; $tabs_i = 1; $cache_debug_info = ''; foreach ($instance['tabs'] as $tabs_key => $tabs) { if (empty($tabs['title'])) { continue; } $tab_id = $widget_id . '-' . $tabs_i; $nav_class = $tabs_i == 1 ? 'active' : ''; $con_class = $tabs_i == 1 ? ' active in' : ''; // 检查是否需要限制当前版块 if (!empty($tabs['current_plate'])) { $current_plate = zib_bbs_get_the_plate_id(); if(!$current_plate){ continue; } $ajax_href = add_query_arg('current_plate', $current_plate, $ajax_href); } // 第一个Tab直接渲染内容,避免空白等待 if ($tabs_i == 1) { // 生成第一个Tab的内容 $first_tab_content = zib_bbs_get_ranking_tab_content($tabs_key, $tabs, $instance); $con_html = $first_tab_content['content']; $cache_debug_info = $first_tab_content['debug_info']; // IAS分页器 $ias_args = array( 'type' => 'ias', 'loader' => '', 'url' => $ajax_href, ); $con_html .= zib_get_ias_ajaxpager($ias_args); } else { // 其他Tab保持延迟加载 $con_html = ''; $con_html .= ''; $con_html .= ''; } $tabs_nav .= '
  • ' . $tabs['title'] . '
  • '; $tabs_con .= '
    ' . $con_html . '
    '; $tabs_i++; } if (!$tabs_nav) { return ''; } // 调试信息(仅管理员可见) $debug_html = ''; if (!empty($instance['show_cache_debug']) && current_user_can('manage_options') && $cache_debug_info) { $debug_html = '
    ' . $cache_debug_info . '
    '; } $main_html = '
    热门排行榜
      ' . $tabs_nav . '
    ' . $tabs_con . '
    ' . $debug_html . '
    '; // 添加CSS和JS $main_html .= zib_bbs_get_ranking_css_js(); return $main_html; } /** * 获取Tab内容(带缓存机制) */ function zib_bbs_get_ranking_tab_content($tab_key, $tab_args, $instance) { // 生成缓存键 $cache_key = zib_bbs_generate_ranking_cache_key($tab_key, $tab_args, $instance); $cache_time = !empty($instance['cache_time']) ? (int)$instance['cache_time'] : 300; // 尝试从缓存获取 $cached_data = get_transient($cache_key); $cache_hit = false; $debug_info = ''; if ($cached_data !== false && !empty($cached_data['content'])) { $cache_hit = true; $content = $cached_data['content']; $debug_info = '缓存命中 | 剩余: ' . (int)((time() + $cache_time - $cached_data['timestamp']) / 60) . '分钟'; } else { // 缓存未命中,生成新内容 $content = zib_bbs_generate_ranking_content($tab_args, $instance); // 保存到缓存 $cache_data = array( 'content' => $content, 'timestamp' => time(), 'version' => '1.0' ); set_transient($cache_key, $cache_data, $cache_time); $debug_info = '缓存生成 | 有效期: ' . (int)($cache_time / 60) . '分钟'; } return array( 'content' => $content, 'debug_info' => $debug_info, 'cache_hit' => $cache_hit ); } /** * 生成缓存键 */ function zib_bbs_generate_ranking_cache_key($tab_key, $tab_args, $instance) { $key_data = array( 'tab_key' => $tab_key, 'tab_args' => $tab_args, 'paged_size' => $instance['paged_size'], 'version' => '1.0' ); // 包含当前版块ID(如果适用) if (!empty($tab_args['current_plate'])) { $key_data['current_plate'] = zib_bbs_get_the_plate_id(); } return 'zib_ranking_' . md5(serialize($key_data)); } /** * 生成排行榜内容 */ function zib_bbs_generate_ranking_content($tab_args, $instance) { // 处理日期范围 if (!empty($tab_args['date_range'])) { $days = 0; switch ($tab_args['date_range']) { case '1': $days = 1; break; case '7': $days = 7; break; case '30': $days = 30; break; case '365': $days = 365; break; } if ($days > 0) { $tab_args['date_query'] = array( 'after' => $days . ' days ago', ); } } // 构建查询参数 $posts_args = array( 'plate' => isset($tab_args['include_plate']) ? $tab_args['include_plate'] : '', 'plate_exclude' => isset($tab_args['exclude_plate']) ? $tab_args['exclude_plate'] : '', 'topic' => isset($tab_args['include_topic']) ? $tab_args['include_topic'] : '', 'tag' => isset($tab_args['include_tag']) ? $tab_args['include_tag'] : '', 'orderby' => isset($tab_args['orderby']) ? $tab_args['orderby'] : 'date', 'bbs_type' => isset($tab_args['bbs_type']) ? $tab_args['bbs_type'] : '', 'filter' => isset($tab_args['filter']) ? $tab_args['filter'] : '', 'paged' => 1, 'paged_size' => $instance['paged_size'], ); // 添加日期查询 if (!empty($tab_args['date_query'])) { $posts_args['date_query'] = $tab_args['date_query']; } // 处理当前版块 if (!empty($_REQUEST['current_plate'])) { $posts_args['plate'] = $_REQUEST['current_plate']; } $posts = zib_bbs_get_posts_query($posts_args); return zib_bbs_get_ranking_posts_html($posts); } /** * 生成帖子列表HTML */ function zib_bbs_get_ranking_posts_html($posts) { $lists = ''; $post_index = 1; if ($posts->have_posts()) { while ($posts->have_posts()): $posts->the_post(); $post_title = get_the_title(); $post_url = get_permalink(); $post_image = zib_bbs_get_post_first_image_enhanced(); $post_id = get_the_ID(); // 生成排名样式class $rank_class = ''; $rank_class_x = ''; if ($post_index <= 3) { $rank_class = 'nex_recons_num_' . $post_index; $rank_class_x = 'nex_recons_numx_' . $post_index; } $lists .= '
    ' . $post_index . ' ' . esc_html($post_title) . '
    ' . $post_index . ' ' . esc_attr($post_title) . '
    ' . esc_html($post_title) . '
    '; $post_index++; endwhile; wp_reset_query(); } if (!$lists) { $lists = '
    -暂无内容
    '; } return $lists; } /** * 获取CSS和JS样式(静态变量确保只加载一次) */ function zib_bbs_get_ranking_css_js() { static $assets_loaded = false; if ($assets_loaded) { return ''; } $assets_loaded = true; return ' ';
    Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/moedes_com/wp-content/themes/zibll/inc/functions/bbs/widgets/ranking-widget.php:157) in /www/wwwroot/moedes_com/wp-includes/sitemaps/class-wp-sitemaps-renderer.php on line 126
    https://moedes.com/wp-sitemap-posts-page-1.xmlhttps://moedes.com/wp-sitemap-posts-plate-1.xmlhttps://moedes.com/wp-sitemap-posts-forum_post-1.xmlhttps://moedes.com/wp-sitemap-taxonomies-plate_cat-1.xmlhttps://moedes.com/wp-sitemap-taxonomies-forum_topic-1.xmlhttps://moedes.com/wp-sitemap-taxonomies-forum_tag-1.xmlhttps://moedes.com/wp-sitemap-users-1.xml