refactor(resource): drop aksrt_resource CPT residue, post+meta only

This commit is contained in:
kate522 committed 2026-07-13 23:47:04 +08:00
1 parent 24c135aeec
commit 1af0e9b753
22 files changed
+362 -986

No files matched your search

@@ -20,19 +20,14 @@ if (!in_array($layout, ['card', 'list', 'masonry', 'grid-cms'], true)) {
}
$args = [
'posts_per_page' => $count,
'post_status' => 'publish',
'no_found_rows' => true,
];
if ($cat_id > 0) {
$args['cat'] = $cat_id;
// 分类页通常希望同时展示资源 post,post 已天然包含资源;旧 CPT 兼容时合并
$args['post_type'] = function_exists('aksrt_resource_post_types')
? array_unique(array_merge(['post'], aksrt_resource_post_types()))
: ['post'];
} else {
$args['post_type'] = 'post';
}
'post_type' => 'post',
'posts_per_page' => $count,
'post_status' => 'publish',
'no_found_rows' => true,
];
if ($cat_id > 0) {
$args['cat'] = $cat_id;
}
$query = new WP_Query($args);
if (!$query->have_posts()) {
+30 -21
View File
@@ -3,7 +3,7 @@
* Home Module · Hotspot 热点推荐
*
* 纯文章列表(无图片),仅桌面端显示。
* config: { count, post_type, source, title? }
* config: { count, post_type: 'post'|'resource', source, title? }
*
* source:
* 'auto' → 优先读取 aksrt_recommend meta,不足时用最新补充
@@ -13,36 +13,39 @@
defined('ABSPATH') || exit;
$cfg = get_query_var('aksrt_module_config', []);
if (!is_array($cfg)) $cfg = [];
if (!is_array($cfg)) {
$cfg = [];
}
$count = max(3, min(20, (int) ($cfg['count'] ?? 6)));
$post_type = (string) ($cfg['post_type'] ?? 'post');
$source = (string) ($cfg['source'] ?? 'auto');
$title = (string) ($cfg['title'] ?? '');
$count = max(3, min(20, (int) ($cfg['count'] ?? 6)));
$scope = function_exists('aksrt_normalize_content_scope')
? aksrt_normalize_content_scope($cfg['post_type'] ?? 'post')
: 'post';
$source = (string) ($cfg['source'] ?? 'auto');
$title = (string) ($cfg['title'] ?? '');
if (!in_array($source, ['auto', 'recent', 'views'], true)) {
$source = 'auto';
}
if (!in_array($post_type, ['post', 'aksrt_resource'], true)) {
$post_type = 'post';
}
$is_resource = $post_type === 'aksrt_resource';
$ptypes = $is_resource && function_exists('aksrt_resource_post_types')
? aksrt_resource_post_types()
: [$post_type];
$is_resource = $scope === 'resource';
// 构建查询参数
$args = [
'post_type' => $ptypes,
'post_type' => 'post',
'posts_per_page' => $count,
'post_status' => 'publish',
'no_found_rows' => true,
];
$meta_query = [];
if ($is_resource) {
$meta_query[] = function_exists('aksrt_resource_meta_query')
? aksrt_resource_meta_query()
: ['key' => '_aksrt_is_resource', 'value' => '1', 'compare' => '='];
}
if ($source === 'auto') {
// 优先推荐文章,不足时用最新补充
$args['meta_query'] = [
$meta_query[] = [
'relation' => 'OR',
['key' => 'aksrt_recommend', 'value' => '1', 'compare' => '='],
['key' => 'aksrt_recommend', 'compare' => 'NOT EXISTS'],
@@ -58,6 +61,12 @@ if ($source === 'auto') {
$args['order'] = 'DESC';
}
if (count($meta_query) === 1) {
$args['meta_query'] = $meta_query;
} elseif (count($meta_query) > 1) {
$args['meta_query'] = array_merge(['relation' => 'AND'], $meta_query);
}
$query = new WP_Query($args);
if (!$query->have_posts()) {
wp_reset_postdata();
@@ -81,10 +90,10 @@ $display_title = $title ?: $default_title;
$rank = 1;
while ($query->have_posts()) :
$query->the_post();
$cats = get_the_category();
$cats = get_the_category();
$cat_name = $cats ? $cats[0]->name : '';
$views = (int) get_post_meta(get_the_ID(), '_aksrt_res_view_count', true);
?>
$views = (int) get_post_meta(get_the_ID(), '_aksrt_res_view_count', true);
?>
<li class="hotspot__item">
<span class="hotspot__num hotspot__num--<?php echo $rank <= 3 ? 'top' : 'normal'; ?>"><?php echo (int) $rank; ?></span>
<div class="hotspot__body">
@@ -102,7 +111,7 @@ $display_title = $title ?: $default_title;
</div>
</div>
</li>
<?php
<?php
$rank++;
endwhile;
wp_reset_postdata();
+22 -22
View File
@@ -2,57 +2,57 @@
/**
* Home Module · Ranking
*
* config: { metric: 'views|downloads|comments', count, post_type, title? }
* config: { metric: 'views|downloads|comments', count, post_type: 'post'|'resource', title? }
*/
defined('ABSPATH') || exit;
$cfg = get_query_var('aksrt_module_config', []);
if (!is_array($cfg)) $cfg = [];
if (!is_array($cfg)) {
$cfg = [];
}
$metric = (string) ($cfg['metric'] ?? 'downloads');
$count = max(1, min(20, (int) ($cfg['count'] ?? 6)));
$post_type_cfg = (string) ($cfg['post_type'] ?? 'aksrt_resource');
$title = (string) ($cfg['title'] ?? '');
$metric = (string) ($cfg['metric'] ?? 'downloads');
$count = max(1, min(20, (int) ($cfg['count'] ?? 6)));
$scope = function_exists('aksrt_normalize_content_scope')
? aksrt_normalize_content_scope($cfg['post_type'] ?? 'resource')
: 'resource';
$title = (string) ($cfg['title'] ?? '');
if (!in_array($metric, ['views', 'downloads', 'comments'], true)) {
$metric = 'downloads';
}
$is_resource_module = $post_type_cfg === 'aksrt_resource';
$is_resource_module = $scope === 'resource';
$args = [
'post_type' => 'post',
'posts_per_page' => $count,
'post_status' => 'publish',
'no_found_rows' => true,
];
if ($is_resource_module) {
$args['post_type'] = function_exists('aksrt_resource_post_types') ? aksrt_resource_post_types() : ['post'];
if (!post_type_exists('aksrt_resource')) {
$args['meta_query'] = [[
'key' => '_aksrt_is_resource',
'value' => '1',
'compare' => '=',
]];
}
} else {
$args['post_type'] = 'post';
$args['meta_query'] = [
function_exists('aksrt_resource_meta_query')
? aksrt_resource_meta_query()
: ['key' => '_aksrt_is_resource', 'value' => '1', 'compare' => '='],
];
}
switch ($metric) {
case 'views':
$args['meta_key'] = '_aksrt_res_view_count';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
break;
case 'comments':
$args['orderby'] = 'comment_count';
$args['order'] = 'DESC';
$args['order'] = 'DESC';
break;
case 'downloads':
default:
$args['meta_key'] = '_aksrt_res_download_count';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
break;
}
+20 -24
View File
@@ -2,30 +2,30 @@
/**
* Home Module · Recent
*
* config: { post_type, count, layout, title? }
*
* post_type 取值:
* 'post' → 最新文章
* 'aksrt_resource' → 最新资源(v3.4 起兼容 post + _aksrt_is_resource)
* config: { post_type: 'post'|'resource', count, layout, title? }
*/
defined('ABSPATH') || exit;
$cfg = get_query_var('aksrt_module_config', []);
if (!is_array($cfg)) $cfg = [];
if (!is_array($cfg)) {
$cfg = [];
}
$post_type = (string) ($cfg['post_type'] ?? 'post');
$count = max(1, min(20, (int) ($cfg['count'] ?? 6)));
$layout = (string) ($cfg['layout'] ?? 'card');
$title = (string) ($cfg['title'] ?? '');
$scope = function_exists('aksrt_normalize_content_scope')
? aksrt_normalize_content_scope($cfg['post_type'] ?? 'post')
: ((string) ($cfg['post_type'] ?? 'post') === 'resource' ? 'resource' : 'post');
$count = max(1, min(20, (int) ($cfg['count'] ?? 6)));
$layout = (string) ($cfg['layout'] ?? 'card');
$title = (string) ($cfg['title'] ?? '');
if (!in_array($layout, ['card', 'list', 'masonry', 'grid-cms'], true)) {
$layout = 'card';
}
if (!in_array($post_type, ['post', 'aksrt_resource'], true)) $post_type = 'post';
$is_resource_module = $post_type === 'aksrt_resource';
$is_resource_module = $scope === 'resource';
$args = [
'post_type' => 'post',
'posts_per_page' => $count,
'post_status' => 'publish',
'no_found_rows' => true,
@@ -33,16 +33,11 @@ $args = [
'order' => 'DESC',
];
if ($is_resource_module) {
$args['post_type'] = function_exists('aksrt_resource_post_types') ? aksrt_resource_post_types() : ['post'];
if (!post_type_exists('aksrt_resource')) {
$args['meta_query'] = [[
'key' => '_aksrt_is_resource',
'value' => '1',
'compare' => '=',
]];
}
} else {
$args['post_type'] = 'post';
$args['meta_query'] = [
function_exists('aksrt_resource_meta_query')
? aksrt_resource_meta_query()
: ['key' => '_aksrt_is_resource', 'value' => '1', 'compare' => '='],
];
}
$query = new WP_Query($args);
@@ -53,7 +48,7 @@ if (!$query->have_posts()) {
$default_title = $is_resource_module ? __('最新资源', 'aksrt') : __('最新文章', 'aksrt');
$display_title = $title ?: $default_title;
$archive_link = $is_resource_module
$archive_link = $is_resource_module
? (function_exists('aksrt_resource_archive_url') ? aksrt_resource_archive_url() : home_url('/resources/'))
: get_permalink(get_option('page_for_posts'));
?>
@@ -78,7 +73,8 @@ $archive_link = $is_resource_module
}
set_query_var('aksrt_post_card_layout', '');
set_query_var('aksrt_post_card_scene', '');
wp_reset_postdata();
?>
</div>
</section>
<?php
wp_reset_postdata();
+6 -9
View File
@@ -71,16 +71,13 @@ $active_id = $tabs[0]['id'];
</header>
<?php foreach ($tabs as $i => $t) :
$tab_post_types = function_exists('aksrt_resource_post_types')
? array_unique(array_merge(['post'], aksrt_resource_post_types()))
: ['post'];
$query = new WP_Query([
'cat' => $t['id'],
'post_type' => $tab_post_types,
'posts_per_page' => $count,
'post_status' => 'publish',
'no_found_rows' => true,
]);
'cat' => $t['id'],
'post_type' => 'post',
'posts_per_page' => $count,
'post_status' => 'publish',
'no_found_rows' => true,
]);
?>
<div class="home-module__tab-panel tab-pane<?php echo $i === 0 ? ' is-active' : ''; ?>"
role="tabpanel"
+13 -20
View File
@@ -2,8 +2,7 @@
/**
* Resource Related
*
* 单篇资源页底部的「相关资源」区块。
* 取同分类下其它资源(post + _aksrt_is_resource = 1,兼容旧 aksrt_resource CPT),按下载量倒序。
* 单篇资源页底部「相关资源」:同分类其它资源帖(post + _aksrt_is_resource)。
*
* @package AKSRT
*/
@@ -11,13 +10,13 @@
defined('ABSPATH') || exit;
$current_id = get_the_ID();
$cat_ids = wp_get_post_categories($current_id, ['fields' => 'ids']);
if (empty($cat_ids)) return;
$post_types = function_exists('aksrt_resource_post_types') ? aksrt_resource_post_types() : ['post'];
$cat_ids = wp_get_post_categories($current_id, ['fields' => 'ids']);
if (empty($cat_ids)) {
return;
}
$args = [
'post_type' => $post_types,
'post_type' => 'post',
'posts_per_page' => 6,
'post__not_in' => [$current_id],
'category__in' => array_map('intval', $cat_ids),
@@ -25,17 +24,12 @@ $args = [
'orderby' => 'meta_value_num',
'order' => 'DESC',
'no_found_rows' => true,
'meta_query' => [
function_exists('aksrt_resource_meta_query')
? aksrt_resource_meta_query()
: ['key' => '_aksrt_is_resource', 'value' => '1', 'compare' => '='],
],
];
if (!post_type_exists('aksrt_resource')) {
$args['meta_query'] = [
'relation' => 'AND',
[
'key' => '_aksrt_is_resource',
'value' => '1',
'compare' => '=',
],
];
}
$query = new WP_Query($args);
@@ -49,10 +43,9 @@ if (!$query->have_posts()) {
<header class="resource-related__header">
<h2 class="resource-related__title"><?php esc_html_e('相关资源', 'aksrt'); ?></h2>
</header>
<div class="post-grid post-grid--grid-cms resource-related__grid">
<div class="resource-related__grid post-grid post-grid--card">
<?php
set_query_var('aksrt_post_card_layout', 'grid-cms');
set_query_var('aksrt_post_card_layout', 'card');
while ($query->have_posts()) {
$query->the_post();
get_template_part('templates/parts/content', 'post-card');