一并修掉上一轮列出的 5 处侧栏挂件遗留问题:
1. 标题不读 $args(4 个挂件标题在后台改不了)
新增 yang_widget_title($args, $instance, $fallback, $id_base),5 个挂件
统一走它:读 $instance['title'] → 空则回落 $fallback → 过 widget_title
过滤器 → 输出用 $args['before_title']/after_title → 仍为空则整个节点不输出。
四个挂件的 update() 补 title,form() 补输入框 + 「留空则不显示标题」说明。
widget_tags 留空回落「标签聚合」,老站点显示不变。
2. 标签列数随屏宽反转(1440 一列、1180 反而两列)
逐视口实测:真实断点在 1311→1310,@media(max-width:1310px) 只把格子压成
110px 而不改侧栏宽度(侧栏 992px 以上恒为容器 1/3),于是格子变小反而能
塞下 3 个。改为 .w-tags .item a / .widget_links ul li 用
width: calc(50% - 2px) + 父容器 font-size:0(消掉 inline-block 之间的
空白宽度),并在 1310 断点里删掉这两条 110px 规则(分类/归档的 110px
不是网格,保留)。判据守的是「列数单调不增」而不是「某视口恰好两列」。
3. 重复 id nav-tab(桌面一份、手机一份)
两份导航合并为一份,id 全部带实例后缀:yang_widget_uid($this) 取
$widget->id(如 widget_posts-2),输出 {uid}-tab / {uid}-new 等,
aria-controls / aria-labelledby 同步指向真实 id,两实例并用也不撞。
4. 搜索框固定 id(searchform / search / searchsubmit)
同样改为 {uid}-form / {uid}-field / {uid}-submit,并补
<label class="sr-only" for="{uid}-field">搜索</label>。
5. 图片 URL 未加引号 + 未转义
src/href 一律加引号 + esc_url(),alt 用 wp_strip_all_tags + esc_attr,
简介用 nl2br(esc_html())。背景图踩了一个自己写出来的坑:一开始写成
esc_url() 套在 esc_attr() 里 = 双重转义,& 变 &amp;,CSS 里拿到
字面量 amp,背景图 404。改为 url() 内部用 esc_url_raw()(只做协议/字符
清洗,不入实体),整个 style 属性交给 esc_attr() 统一转义一次。
顺带把侧栏标签页图标在窄屏隐藏(<1200px),避免三个 tab 挤在一起。
验证:
- _widgets_consistency.php 68 通过 / 0 失败
- _widgets_consistency_falsify.php 24 通过 / 0 失败(基线1·自检3·注入20)
- _widgets_hot_test.php 33 / 0
- _widgets_hot_falsify.php 20 / 0
- _widgets_hot_geom.js 24 / 0(真实浏览器几何)
- _widgets_ids_dom.js 5 / 0(真实 DOM 查重复 id)
- _widgets_visual20.js 30 / 0(像素级:图片 naturalWidth>0、
标题装饰线、标签等宽、tab 点击)
- _widgets_icon_wide.js 7 / 0(断点精确在 1200)
- _widgets_tags_geom.js 5 / 0(列数单调性)
- 发布脚本 R1~R10 十组注入全部真红
750 lines
36 KiB
PHP
750 lines
36 KiB
PHP
<?php
|
||
/**
|
||
* 侧栏小工具
|
||
* @author Yongquan Yang <yangyq.net>
|
||
* @license MIT License
|
||
* @version 2020.08.03
|
||
*/
|
||
|
||
// 添加小工具
|
||
function widgets_init()
|
||
{
|
||
register_sidebar(array(
|
||
'name' => __('侧边栏工具', 'kratos'),
|
||
'id' => 'sidebar_tool',
|
||
'before_widget' => '<div class="widget %2$s">',
|
||
'after_widget' => '</div>',
|
||
'before_title' => '<div class="title">',
|
||
'after_title' => '</div>',
|
||
));
|
||
// 去掉默认小工具
|
||
$wp_widget = array(
|
||
'WP_Widget_Pages',
|
||
'WP_Widget_Meta',
|
||
'WP_Widget_Recent_Posts',
|
||
'WP_Widget_Recent_Comments',
|
||
'WP_Widget_RSS',
|
||
'WP_Widget_Search',
|
||
'WP_Widget_Tag_Cloud',
|
||
'WP_Nav_Menu_Widget',
|
||
);
|
||
|
||
foreach ($wp_widget as $wp_widget) {
|
||
unregister_widget($wp_widget);
|
||
}
|
||
}
|
||
add_action('widgets_init', 'widgets_init');
|
||
|
||
/* ==========================================================================
|
||
* 挂件标题与外框(3.2.50)
|
||
*
|
||
* ── 为什么要有这两个函数 ──────────────────────────────────────────────
|
||
* register_sidebar() 里已经声明了版式:
|
||
*
|
||
* 'before_title' => '<div class="title">',
|
||
* 'after_title' => '</div>',
|
||
*
|
||
* 但主题自己的挂件**全都绕开了它**:`widget_tags` 把「标签聚合」硬编码在
|
||
* 代码里,`widget_ad` / `widget_about` / `widget_posts` 干脆没有标题字段,
|
||
* `widget_search` 有字段却自己手写了一个 `<div class="title">`。
|
||
*
|
||
* 结果是:在后台改侧栏版式(换标题标签、加装饰)对这四个挂件无效,
|
||
* 而且用户根本没地方给它们起标题。这两个函数把「标题」收敛到一处,
|
||
* 保证所有挂件走同一条路径。
|
||
*
|
||
* 🔴 空标题必须**不输出任何节点**,而不是输出空的 `<div class="title"></div>` ——
|
||
* 后者会留下一条只有内边距的空白条,视觉上像"多了一块间距"。
|
||
* ====================================================================== */
|
||
|
||
if (!function_exists('yang_widget_title')):
|
||
|
||
/**
|
||
* 输出挂件标题(走 sidebar 注册的 before_title / after_title)。
|
||
*
|
||
* @param array $args widget() 收到的 $args
|
||
* @param string $instance 实例里的 title
|
||
* @param string $fallback 没设置标题时的默认值;传 '' 表示默认不显示标题
|
||
* @param string $id_base 挂件 id_base,仅用于 widget_title 过滤器
|
||
*/
|
||
function yang_widget_title($args, $instance, $fallback = '', $id_base = '')
|
||
{
|
||
$title = isset($instance['title']) ? trim((string) $instance['title']) : '';
|
||
if ($title === '') {
|
||
$title = $fallback;
|
||
}
|
||
|
||
/* 兼容 WP 惯例:主题/插件可以用 widget_title 过滤器改标题。
|
||
* 这里保留它,否则装过「改挂件标题」类插件的站点行为会变。 */
|
||
if (function_exists('apply_filters')) {
|
||
$title = apply_filters('widget_title', $title, (array) $instance, $id_base);
|
||
$title = trim((string) $title);
|
||
}
|
||
|
||
if ($title === '') {
|
||
return;
|
||
}
|
||
|
||
$before = isset($args['before_title']) ? $args['before_title'] : '<div class="title">';
|
||
$after = isset($args['after_title']) ? $args['after_title'] : '</div>';
|
||
|
||
echo $before . esc_html($title) . $after;
|
||
}
|
||
|
||
endif;
|
||
|
||
if (!function_exists('yang_widget_uid')):
|
||
|
||
/**
|
||
* 生成挂件内 HTML id 的唯一前缀。
|
||
*
|
||
* 🔴 为什么不能用写死的 id:挂件可以添加多个实例,同一页面上
|
||
* 两个「文章聚合」就会产生两套重复 id(HTML 非法,
|
||
* aria-labelledby 也只能指到第一个)。
|
||
* `$this->id` 由 WP_Widget 分配(形如 `widget_posts-2`),
|
||
* 同一请求内唯一;但在单元测试/旧版环境里可能不存在,所以做兜底。
|
||
*
|
||
* @param object $widget 挂件实例
|
||
* @return string 形如 `yang-widget_posts-2`
|
||
*/
|
||
function yang_widget_uid($widget)
|
||
{
|
||
$id = '';
|
||
if (is_object($widget) && isset($widget->id) && is_string($widget->id) && $widget->id !== '') {
|
||
$id = $widget->id;
|
||
} elseif (is_object($widget) && isset($widget->id_base) && is_string($widget->id_base) && $widget->id_base !== '') {
|
||
$id = $widget->id_base;
|
||
} else {
|
||
$id = 'widget';
|
||
}
|
||
|
||
return 'yang-' . preg_replace('/[^a-z0-9_-]+/i', '-', $id);
|
||
}
|
||
|
||
endif;
|
||
|
||
if (!function_exists('yang_widget_open')):
|
||
|
||
/**
|
||
* 挂件外框起始标签。
|
||
*
|
||
* 侧栏注册的 before_widget 是 `<div class="widget %2$s">`,其中 %2$s 是
|
||
* 挂件类名(classname)。这里故意**不用**它,而是自己拼类名:
|
||
* 主题的挂件样式全部写成 `.k-main .sidebar .w-about` 这种以 `w-` 开头的
|
||
* 短类名,用 classname 会变成 `widget_widget_about` 之类,样式一条都不生效。
|
||
* 保留 `widget` 这个基类(主题里 `.k-main .sidebar .widget` 提供白底与内边距)。
|
||
*
|
||
* @param string $modifier 形如 'w-about'
|
||
* @return string
|
||
*/
|
||
function yang_widget_class($modifier)
|
||
{
|
||
return 'widget ' . $modifier;
|
||
}
|
||
|
||
endif;
|
||
|
||
/* ==========================================================================
|
||
* 热度算法(3.2.49)
|
||
*
|
||
* ── 为什么废弃旧的 most_comm_posts() ──────────────────────────────────
|
||
* 旧实现的候选池是「最近 N 天(默认 30)**发布**的文章」:
|
||
*
|
||
* WHERE post_date BETWEEN '$daysago' AND '$today'
|
||
* ORDER BY comment_count DESC LIMIT 6
|
||
*
|
||
* 对日更站点没问题,对低频更新的站点直接失效。本站实测(2026-10-26):
|
||
* 7 天内发布 0 篇 / 30 天内 1 篇 / 90 天内 1 篇 / 180 天内 6 篇
|
||
* 30 天候选池只有 1 篇 ⇒ LIMIT 6 也只能返回 1 条。
|
||
* 这就是侧栏「热点」永远只显示一条的真正原因 —— **不是排序错,是池子空**。
|
||
*
|
||
* 而且「最近 30 天发布」本身就不是「热」的定义:热是累计互动,不是新鲜度。
|
||
*
|
||
* ── 现在的算法 ────────────────────────────────────────────────────────
|
||
* 候选池:全站所有已发布 post(不含 page —— 「留言本」这种页面 views 214 万,
|
||
* 它的热度对"最热文章"没有意义;`post_type='post'` 天然挡掉)。
|
||
* 打分:三个指标各取 log10 后加权求和
|
||
*
|
||
* score = 3*log10(views+1) + 5*log10(love+1) + 4*log10(comments+1)
|
||
*
|
||
* 为什么取 log10:本站 views 中位数 4981、最大 56075(差 11 倍),
|
||
* 但 love 最大只有 40、评论最大 93。不取对数的话 views 一个指标就吃掉
|
||
* 全部权重,点赞和评论等于没参与。取对数后三者量级被拉平到可比的区间。
|
||
*
|
||
* 为什么权重是 3 / 5 / 4(不是常见的 1 / 3 / 5):
|
||
* 用通用值实测,前 8 名有 6 篇是 2009 年的老随笔 —— 因为那批文章评论
|
||
* 80~93 条,仅评论一项(1.97×5=9.86)就压过 views 最大值的贡献(4.75×1=4.75),
|
||
* 结果变成"评论数排行榜"。把 views 权重提到 3 之后三个指标影响力均衡:
|
||
* 点赞最高(5)—— 点👍 是比评论更强的认可信号,且本站只有 197 篇有点赞,
|
||
* 稀缺性让它更有区分度;评论次之(4)—— 要付出打字成本;浏览最轻(3)。
|
||
*
|
||
* 实测这组权重的排名(前 8):Anaconda多用户安装(love=40) · kratos主题发布(love=9,评论37)
|
||
* · LaTex上划线(love=19) · 那一年2009 · LaTex字体(love=12) · Windows7自带WMP(评论93)
|
||
* → 技术文章与高互动随笔混合出现,2009 年那批也没有被完全埋掉。
|
||
*
|
||
* ── 有意不加时间衰减 ──────────────────────────────────────────────────
|
||
* 「全站历史最热」是累计榜,不是时效榜。Hacker News / Reddit 那套
|
||
* gravity 衰减是给「新内容榜」用的,加上去会把老文章压到看不见,
|
||
* 那就又回到"只有最近几条"的老问题。要时效榜是另一个功能,不塞在这里。
|
||
*
|
||
* ── 顺手修掉的两个坑 ──────────────────────────────────────────────────
|
||
* ① 旧代码里 `date_default_timezone_set("PRC")` 直接改 **PHP 全局时区**,
|
||
* 影响同一请求里之后所有时间函数。已删。
|
||
* ② 旧代码把 `$title` 原样插进 `title="..."` 属性,标题里有双引号就会
|
||
* 截断属性(甚至注入)。已改用 esc_attr()。
|
||
* ====================================================================== */
|
||
|
||
if (!function_exists('yang_hot_score')):
|
||
|
||
/**
|
||
* 单篇文章的热度分。
|
||
*
|
||
* @param int $views 浏览数
|
||
* @param int $love 点赞数
|
||
* @param int $comments 评论数
|
||
* @return float
|
||
*/
|
||
function yang_hot_score($views, $love, $comments)
|
||
{
|
||
return 3 * log10((int) $views + 1)
|
||
+ 5 * log10((int) $love + 1)
|
||
+ 4 * log10((int) $comments + 1);
|
||
}
|
||
|
||
endif;
|
||
|
||
if (!function_exists('yang_hot_posts')):
|
||
|
||
/**
|
||
* 取热度最高的文章。
|
||
*
|
||
* 为什么在 PHP 里算分、不在 SQL 里 ORDER BY:
|
||
* 分值是三项 log 加权和,SQL 里要写三段 LOG10 + COALESCE + 两个
|
||
* LEFT JOIN 到 postmeta,可读性极差,而且没法复用 yang_hot_score()
|
||
* (测试和产品必须是同一份实现,不能各写一遍)。
|
||
* 代价是要把候选行取回来 —— 本站 967 篇 post,一次取 4 个字段
|
||
* 内存占用可忽略;如果将来文章上万,再加 LIMIT / 缓存。
|
||
* 为防止极端情况,SQL 侧先用 views 粗排取前 N*10(对数排序里
|
||
* 真正的高分不会来自 views 极低的文章,这个粗排是安全的)。
|
||
*
|
||
* @param int $nums 返回条数
|
||
* @return array 文章对象数组,每项含 ID/post_title/views/love/comments/score
|
||
*/
|
||
function yang_hot_posts($nums = 6)
|
||
{
|
||
global $wpdb;
|
||
|
||
$nums = max(1, (int) $nums);
|
||
// 粗排窗口:给足余量。views 前 200 名以外不可能靠 love/评论翻盘
|
||
// (log10 域下,love 满分 1.61×5=8.05,补不回 views 差 1 个数量级的 3 分)。
|
||
$pool = max(200, $nums * 20);
|
||
|
||
$sql = $wpdb->prepare(
|
||
"SELECT p.ID, p.post_title, p.post_date, p.comment_count,
|
||
COALESCE(CAST(mv.meta_value AS UNSIGNED), 0) AS views,
|
||
COALESCE(CAST(ml.meta_value AS UNSIGNED), 0) AS love
|
||
FROM {$wpdb->posts} p
|
||
LEFT JOIN {$wpdb->postmeta} mv ON mv.post_id = p.ID AND mv.meta_key = 'views'
|
||
LEFT JOIN {$wpdb->postmeta} ml ON ml.post_id = p.ID AND ml.meta_key = 'love'
|
||
WHERE p.post_status = 'publish'
|
||
AND p.post_type = 'post'
|
||
ORDER BY views DESC
|
||
LIMIT %d",
|
||
$pool
|
||
);
|
||
$rows = $wpdb->get_results($sql);
|
||
if (empty($rows)) {
|
||
return array();
|
||
}
|
||
|
||
$scored = array();
|
||
foreach ($rows as $r) {
|
||
$r->views = (int) $r->views;
|
||
$r->love = (int) $r->love;
|
||
$r->comments = (int) $r->comment_count;
|
||
$r->score = yang_hot_score($r->views, $r->love, $r->comments);
|
||
$scored[] = $r;
|
||
}
|
||
|
||
usort($scored, function ($a, $b) {
|
||
if ($a->score == $b->score) {
|
||
// 同分按浏览数兜底,保证顺序稳定(否则每次刷新顺序会跳)
|
||
return $b->views <=> $a->views;
|
||
}
|
||
return $b->score <=> $a->score;
|
||
});
|
||
|
||
return array_slice($scored, 0, $nums);
|
||
}
|
||
|
||
endif;
|
||
|
||
/**
|
||
* 小工具文章聚合 —— 热点文章
|
||
*
|
||
* ⚠️ 这个函数名和签名保留着,因为 `widget_posts::widget()` 里有一句
|
||
* `if (function_exists('most_comm_posts'))` 调用它。留着旧名不破兼容,
|
||
* 但内部已经完全换成 yang_hot_posts()。
|
||
*
|
||
* @param int $days 已废弃。旧实现用它当"最近 N 天发布"的时间窗,
|
||
* 那正是只显示一条的根因。保留参数只为不破坏调用签名。
|
||
* @param int $nums 返回条数
|
||
*/
|
||
function most_comm_posts($days = 30, $nums = 6)
|
||
{
|
||
$posts = yang_hot_posts($nums);
|
||
|
||
$output = '';
|
||
foreach ($posts as $p) {
|
||
$output .= '<a class="bookmark-item" title="' . esc_attr($p->post_title) . '"'
|
||
. ' href="' . esc_url(get_permalink($p->ID)) . '" rel="bookmark">'
|
||
. '<i class="kicon i-book"></i>'
|
||
. esc_html(wp_strip_all_tags($p->post_title))
|
||
. '</a>';
|
||
}
|
||
echo $output;
|
||
}
|
||
|
||
class widget_search extends WP_Widget {
|
||
|
||
public function __construct() {
|
||
$widget_ops = array(
|
||
'classname' => 'widget_search',
|
||
'description' => __( 'A search form for your site.' ),
|
||
'customize_selective_refresh' => true,
|
||
);
|
||
parent::__construct( 'search', _x( 'Search', 'Search widget' ), $widget_ops );
|
||
}
|
||
|
||
public function widget( $args, $instance ) {
|
||
/* 🔴 id 必须带实例后缀。
|
||
* 原来写死 id="searchform" / id="search" / id="searchsubmit",
|
||
* 挂两个搜索挂件时全撞;主题别处或核心再放一个搜索表单也会撞。 */
|
||
$uid = yang_widget_uid($this);
|
||
|
||
echo '<div class="' . esc_attr(yang_widget_class('w-search')) . '">';
|
||
yang_widget_title($args, $instance, '', $this->id_base);
|
||
?>
|
||
<div class="item">
|
||
<form role="search" method="get" id="<?php echo esc_attr($uid); ?>-form"
|
||
class="searchform" action="<?php echo esc_url(home_url('/')); ?>">
|
||
<div class="input-group mt-2 mb-2">
|
||
<label class="sr-only" for="<?php echo esc_attr($uid); ?>-field"><?php _e('搜索', 'kratos'); ?></label>
|
||
<input type="text" name="s" id="<?php echo esc_attr($uid); ?>-field" class="form-control"
|
||
placeholder="<?php esc_attr_e('搜点什么呢?', 'kratos'); ?>">
|
||
<div class="input-group-append">
|
||
<button class="btn btn-primary btn-search" type="submit" id="<?php echo esc_attr($uid); ?>-submit"><?php _e('搜索', 'kratos'); ?></button>
|
||
</div>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
<?php
|
||
echo '</div>';
|
||
}
|
||
|
||
public function form( $instance ) {
|
||
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
|
||
$title = $instance['title'];
|
||
?>
|
||
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
|
||
<?php
|
||
}
|
||
|
||
public function update( $new_instance, $old_instance ) {
|
||
$instance = $old_instance;
|
||
$new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '' ) );
|
||
$instance['title'] = sanitize_text_field( $new_instance['title'] );
|
||
return $instance;
|
||
}
|
||
|
||
}
|
||
|
||
class widget_ad extends WP_Widget
|
||
{
|
||
public function __construct()
|
||
{
|
||
add_action('admin_enqueue_scripts', array($this, 'scripts'));
|
||
|
||
$widget_ops = array(
|
||
'name' => __('图片广告', 'kratos'),
|
||
'description' => __('显示自定义图片广告的工具', 'kratos'),
|
||
);
|
||
|
||
parent::__construct(false, false, $widget_ops);
|
||
}
|
||
|
||
public function scripts()
|
||
{
|
||
wp_enqueue_script('media-upload');
|
||
wp_enqueue_media();
|
||
wp_enqueue_script('widget_scripts', ASSET_PATH . '/assets/js/widget.min.js', array('jquery'));
|
||
wp_enqueue_style('widget_css', ASSET_PATH . '/assets/css/widget.min.css', array());
|
||
}
|
||
|
||
public function widget($args, $instance)
|
||
{
|
||
$subtitle = !empty($instance['subtitle']) ? $instance['subtitle'] : __('广告', 'kratos');
|
||
$image = !empty($instance['image']) ? $instance['image'] : '';
|
||
$url = !empty($instance['url']) ? $instance['url'] : '';
|
||
|
||
/* 🔴 原来这三处都是裸拼:
|
||
* '<a href="' . $url . '"...><img src="' . $image . '"><div class="prompt">' . $subtitle . '</div></a>'
|
||
* ① $url 没 esc_url —— 后台填个带引号的地址就能截断 href 属性;
|
||
* ② img 的 src 值**没有引号** —— 图片 URL 里只要有一个空格,
|
||
* 后半截就会被解析成另一个属性(常见于文件名带空格的媒体库图片);
|
||
* ③ $subtitle 没转义 —— 后台填 < 或 & 会破坏结构。
|
||
* 下面全部补齐;alt 取副标题,避免读屏软件只念文件名。 */
|
||
echo '<div class="' . esc_attr(yang_widget_class('w-ad')) . '">';
|
||
yang_widget_title($args, $instance, '', $this->id_base);
|
||
|
||
$imgAlt = wp_strip_all_tags($subtitle);
|
||
echo '<a href="' . esc_url($url) . '" target="_blank" rel="noreferrer noopener">'
|
||
. '<img src="' . esc_url($image) . '" alt="' . esc_attr($imgAlt) . '">'
|
||
. '<div class="prompt">' . esc_html($subtitle) . '</div>'
|
||
. '</a>';
|
||
echo '</div>';
|
||
}
|
||
|
||
public function update($new_instance, $old_instance)
|
||
{
|
||
$instance = array();
|
||
|
||
$instance['title'] = (!empty($new_instance['title'])) ? sanitize_text_field($new_instance['title']) : '';
|
||
$instance['subtitle'] = (!empty($new_instance['subtitle'])) ? sanitize_text_field($new_instance['subtitle']) : '';
|
||
$instance['image'] = (!empty($new_instance['image'])) ? esc_url_raw($new_instance['image']) : '';
|
||
$instance['url'] = (!empty($new_instance['url'])) ? esc_url_raw($new_instance['url']) : '';
|
||
|
||
return $instance;
|
||
}
|
||
|
||
public function form($instance)
|
||
{
|
||
$title = !empty($instance['title']) ? $instance['title'] : '';
|
||
$subtitle = !empty($instance['subtitle']) ? $instance['subtitle'] : __('广告', 'kratos');
|
||
$image = !empty($instance['image']) ? $instance['image'] : '';
|
||
$url = !empty($instance['url']) ? $instance['url'] : '';
|
||
?>
|
||
<div class="media-widget-control">
|
||
<p>
|
||
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('标题:', 'kratos');?></label>
|
||
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>">
|
||
<span class="description"><?php _e('留空则不显示标题。', 'kratos');?></span>
|
||
</p>
|
||
<p>
|
||
<label for="<?php echo $this->get_field_id('subtitle'); ?>"><?php _e('副标题:', 'kratos');?></label>
|
||
<input class="widefat" id="<?php echo $this->get_field_id('subtitle'); ?>" name="<?php echo $this->get_field_name('subtitle'); ?>" type="text" value="<?php echo esc_attr($subtitle); ?>">
|
||
</p>
|
||
<p>
|
||
<label for="<?php echo $this->get_field_id('url'); ?>"><?php _e('链接地址:', 'kratos');?></label>
|
||
<input class="widefat" id="<?php echo $this->get_field_id('url'); ?>" name="<?php echo $this->get_field_name('url'); ?>" type="text" value="<?php echo esc_attr($url); ?>">
|
||
</p>
|
||
<p>
|
||
<label for="<?php echo $this->get_field_id('image'); ?>"><?php _e('广告图片:', 'kratos');?></label>
|
||
<input class="widefat" id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" type="text" value="<?php echo esc_url($image); ?>" />
|
||
<button type="button" class="button-update-media upload_ad"><?php _e('选择图片', 'kratos');?></button>
|
||
</p>
|
||
</div>
|
||
<?php
|
||
}
|
||
}
|
||
|
||
class widget_about extends WP_Widget
|
||
{
|
||
public function __construct()
|
||
{
|
||
add_action('admin_enqueue_scripts', array($this, 'scripts'));
|
||
|
||
$widget_ops = array(
|
||
'name' => __('个人简介', 'kratos'),
|
||
'description' => __('可跳转后台的个人简介展示工具', 'kratos'),
|
||
);
|
||
|
||
parent::__construct(false, false, $widget_ops);
|
||
}
|
||
|
||
public function scripts()
|
||
{
|
||
wp_enqueue_script('media-upload');
|
||
wp_enqueue_media();
|
||
wp_enqueue_script('widget_scripts', ASSET_PATH . '/assets/js/widget.min.js', array('jquery'));
|
||
wp_enqueue_style('widget_css', ASSET_PATH . '/assets/css/widget.min.css', array());
|
||
}
|
||
|
||
public function widget($args, $instance)
|
||
{
|
||
$introduce = kratos_option('a_about', __('保持饥渴的专注,追求最佳的品质', 'kratos'));
|
||
$username = kratos_option('a_nickname', __('Kratos', 'kratos'));
|
||
$avatar = yang_option_image('a_gravatar');
|
||
$background = !empty($instance['background']) ? $instance['background'] : ASSET_PATH . '/assets/img/about-background.png';
|
||
|
||
/* 🔴 原来写的是 `background:url(' . $background . ') no-repeat ...`
|
||
* —— url() 里的地址**没有引号**。只要路径含空格、括号、单引号,
|
||
* CSS 解析器就会在那里截断,整条声明作废(背景变白)。
|
||
* 媒体库文件名带空格很常见。这里补引号。
|
||
*
|
||
* 🔴 这里**只拼一次转义**,而且拼完还要整体 esc_attr()。
|
||
* 第一版写的是 esc_url($background) 再套 esc_attr($bg) ——
|
||
* esc_url() 已经做过 HTML 实体化,esc_attr() 又来一次 ⇒
|
||
* `&` 被编成 `&amp;`,CSS 里拿到的是字面量 "amp",
|
||
* 背景图直接 404。测试里那条「URL 完整」判据抓到的就是它。
|
||
*
|
||
* 正确做法:URL 只做**协议/字符清洗**(esc_url_raw,不入实体),
|
||
* url() 内部用双引号包住,最后整个 style 属性交给 esc_attr()
|
||
* 统一转义一次。这样 `&` 只会被编成 `&`(正确),
|
||
* 浏览器解析回属性值后仍是原始的 `&`。
|
||
*
|
||
* 🔴 $introduce 来自主题选项 textarea,$username 来自 text,
|
||
* 原来是裸 echo。个人简介里写个 < 就把后面的结构吃掉了。
|
||
* 简介保留换行(textarea 的自然语义),其余全部转义。 */
|
||
$bg = 'background-image:url("' . esc_url_raw($background) . '");'
|
||
. 'background-repeat:no-repeat;'
|
||
. 'background-position:center center;'
|
||
. 'background-size:cover;';
|
||
|
||
echo '<div class="' . esc_attr(yang_widget_class('w-about')) . '">';
|
||
yang_widget_title($args, $instance, '', $this->id_base);
|
||
|
||
echo '<div class="background" style="' . esc_attr($bg) . '"></div><div class="wrapper text-center">';
|
||
if (current_user_can('manage_options')) {
|
||
echo '<a href="' . esc_url(admin_url()) . '">';
|
||
} else {
|
||
echo '<a href="' . esc_url(wp_login_url()) . '">';
|
||
}
|
||
echo '<img src="' . esc_url($avatar) . '" alt="' . esc_attr(wp_strip_all_tags($username)) . '"></a>';
|
||
echo '</div><div class="textwidget text-center">'
|
||
. '<p class="username">' . esc_html($username) . '</p>'
|
||
. '<p class="about">' . nl2br(esc_html($introduce)) . '</p>'
|
||
. '</div>';
|
||
echo '</div>';
|
||
}
|
||
|
||
public function update($new_instance, $old_instance)
|
||
{
|
||
$instance = array();
|
||
|
||
$instance['title'] = (!empty($new_instance['title'])) ? sanitize_text_field($new_instance['title']) : '';
|
||
$instance['background'] = (!empty($new_instance['background'])) ? esc_url_raw($new_instance['background']) : '';
|
||
|
||
return $instance;
|
||
}
|
||
|
||
public function form($instance)
|
||
{
|
||
$title = !empty($instance['title']) ? $instance['title'] : '';
|
||
$background = !empty($instance['background']) ? $instance['background'] : ASSET_PATH . '/assets/img/about-background.png';
|
||
?>
|
||
<div class="media-widget-control">
|
||
<p>
|
||
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('标题:', 'kratos');?></label>
|
||
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>">
|
||
<span class="description"><?php _e('留空则不显示标题。', 'kratos');?></span>
|
||
</p>
|
||
<p>
|
||
<label for="<?php echo $this->get_field_id('background'); ?>"><?php _e('背景图片:', 'kratos');?></label>
|
||
<input class="widefat" id="<?php echo $this->get_field_id('background'); ?>" name="<?php echo $this->get_field_name('background'); ?>" type="text" value="<?php echo esc_attr($background); ?>">
|
||
<button type="button" class="button-update-media upload_background"><?php _e('选择图片', 'kratos');?></button>
|
||
</p>
|
||
</div>
|
||
<?php
|
||
}
|
||
}
|
||
|
||
class widget_tags extends WP_Widget
|
||
{
|
||
public function __construct()
|
||
{
|
||
$widget_ops = array(
|
||
'name' => __('标签聚合', 'kratos'),
|
||
'description' => __('文章标签的展示工具', 'kratos'),
|
||
);
|
||
|
||
parent::__construct(false, false, $widget_ops);
|
||
}
|
||
|
||
public function widget($args, $instance)
|
||
{
|
||
$number = !empty($instance['number']) ? $instance['number'] : '8';
|
||
$order = !empty($instance['order']) ? $instance['order'] : 'RAND';
|
||
$tags = wp_tag_cloud(array(
|
||
'unit' => 'px',
|
||
'smallest' => 14,
|
||
'largest' => 14,
|
||
'number' => $number,
|
||
'format' => 'flat',
|
||
'orderby' => 'count',
|
||
'order' => $order,
|
||
'echo' => false,
|
||
)
|
||
);
|
||
/* 🔴 标题原来是硬编码的 __('标签聚合'),后台没有输入框也改不了。
|
||
* 现在走实例的 title,没设才回落到「标签聚合」——
|
||
* 这样老站点的显示**一点不变**,但用户可以改了。 */
|
||
echo '<div class="' . esc_attr(yang_widget_class('w-tags')) . '">';
|
||
yang_widget_title($args, $instance, __('标签聚合', 'kratos'), $this->id_base);
|
||
echo '<div class="item">' . $tags . '</div>';
|
||
echo '</div>';
|
||
}
|
||
|
||
public function update($new_instance, $old_instance)
|
||
{
|
||
$instance = array();
|
||
|
||
$instance['title'] = (!empty($new_instance['title'])) ? sanitize_text_field($new_instance['title']) : '';
|
||
$instance['number'] = (!empty($new_instance['number'])) ? (int) $new_instance['number'] : 8;
|
||
$instance['order'] = (!empty($new_instance['order'])) ? $new_instance['order'] : 'RAND';
|
||
|
||
return $instance;
|
||
}
|
||
|
||
public function form($instance)
|
||
{
|
||
global $wpdb;
|
||
$title = !empty($instance['title']) ? $instance['title'] : '';
|
||
$number = !empty($instance['number']) ? $instance['number'] : '8';
|
||
$order = !empty($instance['order']) ? $instance['order'] : 'RAND';
|
||
?>
|
||
<div class="media-widget-control">
|
||
<p>
|
||
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('标题:', 'kratos');?></label>
|
||
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" placeholder="<?php echo esc_attr__('标签聚合', 'kratos'); ?>">
|
||
<span class="description"><?php _e('留空则仍显示「标签聚合」。', 'kratos');?></span>
|
||
</p>
|
||
<p>
|
||
<label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('显示数量:', 'kratos');?></label>
|
||
<input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo esc_attr($number); ?>" />
|
||
</p>
|
||
<p>
|
||
<label for="<?php echo $this->get_field_id('order'); ?>"><?php _e('显示排序:', 'kratos');?></label>
|
||
<select name="<?php echo $this->get_field_name("order"); ?>" id='<?php echo $this->get_field_id("order"); ?>'>
|
||
<option value="DESC" <?php echo ($order == 'DESC') ? 'selected' : ''; ?>><?php _e('降序', 'kratos');?></option>
|
||
<option value="ASC" <?php echo ($order == 'ASC') ? 'selected' : ''; ?>><?php _e('升序', 'kratos');?></option>
|
||
<option value="RAND" <?php echo ($order == 'RAND') ? 'selected' : ''; ?>><?php _e('随机', 'kratos');?></option>
|
||
</select>
|
||
</p>
|
||
</div>
|
||
<?php
|
||
}
|
||
}
|
||
|
||
class widget_posts extends WP_Widget
|
||
{
|
||
public function __construct()
|
||
{
|
||
$widget_ops = array(
|
||
'name' => __('文章聚合', 'kratos'),
|
||
'description' => __('展示最热、随机、最新文章的工具', 'kratos'),
|
||
);
|
||
|
||
parent::__construct(false, false, $widget_ops);
|
||
}
|
||
|
||
public function widget($args, $instance)
|
||
{
|
||
$number = !empty($instance['number']) ? (int) $instance['number'] : 6;
|
||
if ($number < 1) {
|
||
$number = 6;
|
||
}
|
||
|
||
/* 🔴 id 唯一化(原来全是写死的)。
|
||
* 老代码把同一套标签页导航写了两份 —— 桌面一份带图标(d-none d-xl-flex)、
|
||
* 手机一份不带图标(d-xl-none)—— 于是 nav-tab / nav-new-tab /
|
||
* nav-hot-tab / nav-random-tab **四个 id 各出现两次**,HTML 非法;
|
||
* `aria-labelledby="nav-new-tab"` 也只能指到第一份。
|
||
* 挂两个「文章聚合」实例时撞得更彻底。
|
||
*
|
||
* 现在只渲染**一份**导航:图标用 CSS 在窄屏隐藏(见 kratos.css 的
|
||
* `.w-recommended .tab-icon`)。这样 id 天然唯一,DOM 也少一半。 */
|
||
$uid = yang_widget_uid($this);
|
||
|
||
echo '<div class="' . esc_attr(yang_widget_class('w-recommended')) . '">';
|
||
yang_widget_title($args, $instance, '', $this->id_base);
|
||
?>
|
||
<div class="nav nav-tabs" id="<?php echo esc_attr($uid); ?>-tab" role="tablist">
|
||
<a class="nav-item nav-link active" id="<?php echo esc_attr($uid); ?>-new-tab" data-toggle="tab" href="#<?php echo esc_attr($uid); ?>-new" role="tab" aria-controls="<?php echo esc_attr($uid); ?>-new" aria-selected="true"><i class="kicon i-tabnew tab-icon"></i><?php _e('最新', 'kratos');?></a>
|
||
<a class="nav-item nav-link" id="<?php echo esc_attr($uid); ?>-hot-tab" data-toggle="tab" href="#<?php echo esc_attr($uid); ?>-hot" role="tab" aria-controls="<?php echo esc_attr($uid); ?>-hot" aria-selected="false"><i class="kicon i-tabhot tab-icon"></i><?php _e('热点', 'kratos');?></a>
|
||
<a class="nav-item nav-link" id="<?php echo esc_attr($uid); ?>-random-tab" data-toggle="tab" href="#<?php echo esc_attr($uid); ?>-random" role="tab" aria-controls="<?php echo esc_attr($uid); ?>-random" aria-selected="false"><i class="kicon i-tabrandom tab-icon"></i><?php _e('随机', 'kratos');?></a>
|
||
</div>
|
||
<div class="tab-content" id="<?php echo esc_attr($uid); ?>-tabContent">
|
||
<div class="tab-pane fade show active" id="<?php echo esc_attr($uid); ?>-new" role="tabpanel" aria-labelledby="<?php echo esc_attr($uid); ?>-new-tab">
|
||
<?php
|
||
/* 🔴 原来写的是 `'numberposts=' . $number . ' & offset=0'` ——
|
||
* 字符串里带空格,WP_Query 解析时参数名变成 `' offset'`(前面
|
||
* 有空格),`offset=0` 实际没生效。offset=0 本身是默认值,
|
||
* 所以症状看不出来,但这是错的写法。改成数组传参。 */
|
||
$myposts = get_posts(array(
|
||
'numberposts' => $number,
|
||
'offset' => 0,
|
||
));
|
||
foreach ($myposts as $post): ?>
|
||
<a class="bookmark-item" title="<?php echo esc_attr($post->post_title); ?>" href="<?php echo esc_url(get_permalink($post->ID)); ?>" rel="bookmark"><i class="kicon i-book"></i><?php echo esc_html(wp_strip_all_tags($post->post_title)) ?></a>
|
||
<?php endforeach;?>
|
||
</div>
|
||
<div class="tab-pane fade" id="<?php echo esc_attr($uid); ?>-hot" role="tabpanel" aria-labelledby="<?php echo esc_attr($uid); ?>-hot-tab">
|
||
<?php
|
||
/* 热点:全站累计热度(views/love/评论 三项 log 加权),
|
||
* 不再受「最近 N 天发布」的时间窗限制。原因见 most_comm_posts()
|
||
* 上方的注释块。 */
|
||
if (function_exists('most_comm_posts')) {
|
||
most_comm_posts(0, $number);
|
||
} ?>
|
||
</div>
|
||
<div class="tab-pane fade" id="<?php echo esc_attr($uid); ?>-random" role="tabpanel" aria-labelledby="<?php echo esc_attr($uid); ?>-random-tab">
|
||
<?php
|
||
$myposts = get_posts(array(
|
||
'numberposts' => $number,
|
||
'offset' => 0,
|
||
'orderby' => 'rand',
|
||
));
|
||
foreach ($myposts as $post): ?>
|
||
<a class="bookmark-item" title="<?php echo esc_attr($post->post_title); ?>" href="<?php echo esc_url(get_permalink($post->ID)); ?>" rel="bookmark"><i class="kicon i-book"></i><?php echo esc_html(wp_strip_all_tags($post->post_title)) ?></a>
|
||
<?php endforeach;?>
|
||
</div>
|
||
</div>
|
||
<?php echo '</div>';
|
||
}
|
||
|
||
public function update($new_instance, $old_instance)
|
||
{
|
||
$instance = array();
|
||
|
||
$instance['title'] = (!empty($new_instance['title'])) ? sanitize_text_field($new_instance['title']) : '';
|
||
$instance['number'] = (!empty($new_instance['number'])) ? (int) $new_instance['number'] : 6;
|
||
|
||
/* `days` 已废弃:旧实现拿它当「最近 N 天发布」的时间窗,
|
||
* 那正是热点只显示一条的根因。这里不再保存,
|
||
* 老实例里残留的 days 值会在下次保存时被丢掉。 */
|
||
return $instance;
|
||
}
|
||
public function form($instance)
|
||
{
|
||
$title = !empty($instance['title']) ? $instance['title'] : '';
|
||
$number = !empty($instance['number']) ? $instance['number'] : '6';
|
||
?>
|
||
<div class="media-widget-control">
|
||
<p>
|
||
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('标题:', 'kratos');?></label>
|
||
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>">
|
||
<span class="description"><?php _e('留空则不显示标题。', 'kratos');?></span>
|
||
</p>
|
||
<p>
|
||
<label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('展示数量:', 'kratos');?></label>
|
||
<input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo esc_attr($number); ?>" />
|
||
</p>
|
||
<p class="description">
|
||
<?php _e('「最新」「随机」「热点」三个标签页都用这个数量。', 'kratos'); ?><br>
|
||
<?php _e('「热点」按全站累计热度排序:浏览量、点赞数、评论数三项分别取对数后加权(3 : 5 : 4),不受发布时间限制。', 'kratos'); ?>
|
||
</p>
|
||
</div>
|
||
<?php
|
||
}
|
||
}
|
||
|
||
function register_widgets()
|
||
{
|
||
register_widget('widget_ad');
|
||
register_widget('widget_about');
|
||
register_widget('widget_tags');
|
||
register_widget('widget_search');
|
||
register_widget('widget_posts');
|
||
}
|
||
add_action('widgets_init', 'register_widgets');
|