fix(admin): 修复后台文章列表互动数据列无数据
后台「文章 → 所有文章」列表的「互动数据」列四项计数(浏览/点赞/ 收藏/下载)恒为 0。原因是列回调读取的 meta key 与主题实际写入的 键不一致:读取用的是 'views' / 'like' / 'favorite' / 'download_count',而全仓库的写入方只写 aksrt_post_views / _aksrt_like_count / _aksrt_favorite_count / _aksrt_res_download_count, 'views' / 'like' / 'favorite' 从未被任何代码写过。 改为复用既有的 aksrt_get_post_views() / aksrt_get_post_likes() / aksrt_get_post_favorites() 读取(helpers 在 bootstrap 中先于 core-hooks.php 加载,仍保留 function_exists 兜底与原始键回退), 下载量走 DomainModel::RESOURCE_DOWNLOAD_COUNT_KEY,并对历史 download_count 键做一次回退读取。 同时修正两个相邻问题: - 缩写口径与前台不一致:列内用的是前台从未使用的「万」后缀, 改为统一的 aksrt_format_number()(≥10000 → 1.2W+, ≥1000 → 1.5K),与前台列表、文章详情页保持同一套。 - pre_get_posts 的按计数排序同样拿用户可见的排序键当 meta_key 使用,现映射到真实存储键。 资源下载走普通外链,主题内没有下载计数的写入方,下载项仍只在 >0 时出现,此为现状而非本次回归。 新增 tests/widget-system/admin-stats-column-contract.test.cjs (7 项)锁定列注册、读取键与写入键一致、缩写口径、排序键映射, 以及下载键的写入方状态,避免再次改名时静默失效。 验证:node --test "tests/*/*.test.cjs" 515 项全部通过 (原有 508 项 + 新增 7 项);php -l inc/core-hooks.php 无语法错误。
This commit is contained in:
1 parent
2595bb02db
commit
50fac36169
2 files changed
+122
-8
No files matched your search
+23
-8
@@ -482,13 +482,23 @@ add_action('admin_enqueue_scripts', function ($hook) {
|
||||
add_action('manage_post_posts_custom_column', function ($column, $post_id) {
|
||||
if ($column !== 'aksrt_stats') return;
|
||||
|
||||
$views = (int) get_post_meta($post_id, 'views', true);
|
||||
$likes = (int) get_post_meta($post_id, 'like', true);
|
||||
$favorites = (int) get_post_meta($post_id, 'favorite', true);
|
||||
$downloads = (int) get_post_meta($post_id, 'download_count', true);
|
||||
$views = function_exists('aksrt_get_post_views') ? (int) aksrt_get_post_views($post_id) : (int) get_post_meta($post_id, \AKSRT\Core\DomainModel::POST_VIEW_COUNT_KEY, true);
|
||||
$likes = function_exists('aksrt_get_post_likes') ? (int) aksrt_get_post_likes($post_id) : (int) get_post_meta($post_id, '_aksrt_like_count', true);
|
||||
$favorites = function_exists('aksrt_get_post_favorites') ? (int) aksrt_get_post_favorites($post_id) : (int) get_post_meta($post_id, '_aksrt_favorite_count', true);
|
||||
$downloads = (int) get_post_meta($post_id, \AKSRT\Core\DomainModel::RESOURCE_DOWNLOAD_COUNT_KEY, true);
|
||||
|
||||
if ($downloads <= 0) {
|
||||
$legacy_downloads = (int) get_post_meta($post_id, 'download_count', true);
|
||||
if ($legacy_downloads > 0) {
|
||||
$downloads = $legacy_downloads;
|
||||
}
|
||||
}
|
||||
|
||||
$format = function (int $n): string {
|
||||
if ($n >= 10000) return round($n / 10000, 1) . '万';
|
||||
if (function_exists('aksrt_format_number')) {
|
||||
return (string) aksrt_format_number($n);
|
||||
}
|
||||
if ($n >= 10000) return round($n / 10000, 1) . 'W+';
|
||||
if ($n >= 1000) return round($n / 1000, 1) . 'K';
|
||||
return (string) $n;
|
||||
};
|
||||
@@ -510,10 +520,15 @@ add_action('pre_get_posts', function ($query) {
|
||||
if (!is_admin() || !$query->is_main_query()) return;
|
||||
|
||||
$orderby = $query->get('orderby');
|
||||
$allowed = ['views', 'like', 'favorite', 'download_count'];
|
||||
$allowed = [
|
||||
'views' => \AKSRT\Core\DomainModel::POST_VIEW_COUNT_KEY,
|
||||
'like' => '_aksrt_like_count',
|
||||
'favorite' => '_aksrt_favorite_count',
|
||||
'download_count' => \AKSRT\Core\DomainModel::RESOURCE_DOWNLOAD_COUNT_KEY,
|
||||
];
|
||||
|
||||
if (in_array($orderby, $allowed, true)) {
|
||||
$query->set('meta_key', $orderby);
|
||||
if (isset($allowed[$orderby])) {
|
||||
$query->set('meta_key', $allowed[$orderby]);
|
||||
$query->set('orderby', 'meta_value_num');
|
||||
}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user