refactor(resource): drop aksrt_resource CPT residue, post+meta only
This commit is contained in:
1 parent
24c135aeec
commit
1af0e9b753
22 files changed
+362
-986
No files matched your search
+25
-78
@@ -261,16 +261,13 @@ function aksrt_category_meta_admin_js() {
|
||||
add_action('admin_footer', 'aksrt_category_meta_admin_js');
|
||||
|
||||
// ============================================================
|
||||
// 资源归档:/resources 重写规则 + 排序参数注入
|
||||
// 站内搜索:包含「资源」(post + _aksrt_is_resource)
|
||||
// 资源列表:/resources 虚拟归档(post + _aksrt_is_resource)
|
||||
// ============================================================
|
||||
|
||||
// 注册 /resources 虚拟归档
|
||||
add_action('init', function () {
|
||||
add_rewrite_rule('^resources/?$', 'index.php?aksrt_is_resource_archive=1', 'top');
|
||||
add_rewrite_rule('^resources/page/([0-9]+)/?$', 'index.php?aksrt_is_resource_archive=1&paged=$matches[1]', 'top');
|
||||
|
||||
// 兼容历史 /resource/{slug}:301 到 /{slug}(默认 post 永久链接)
|
||||
// 旧单篇路径 /resource/{slug} → 301 到 post 永久链接
|
||||
add_rewrite_rule('^resource/([^/]+)/?$', 'index.php?aksrt_legacy_resource=$matches[1]', 'top');
|
||||
});
|
||||
|
||||
@@ -281,15 +278,12 @@ add_filter('query_vars', function ($vars) {
|
||||
return $vars;
|
||||
});
|
||||
|
||||
// 旧资源链接 301 跳转
|
||||
add_action('template_redirect', function () {
|
||||
$slug = get_query_var('aksrt_legacy_resource');
|
||||
if (!$slug) return;
|
||||
// 先按 post 查
|
||||
$page = get_page_by_path((string) $slug, OBJECT, 'post');
|
||||
if (!$page && post_type_exists('aksrt_resource')) {
|
||||
$page = get_page_by_path((string) $slug, OBJECT, 'aksrt_resource');
|
||||
if (!$slug) {
|
||||
return;
|
||||
}
|
||||
$page = get_page_by_path((string) $slug, OBJECT, 'post');
|
||||
if ($page) {
|
||||
wp_redirect(get_permalink($page->ID), 301);
|
||||
exit;
|
||||
@@ -297,14 +291,12 @@ add_action('template_redirect', function () {
|
||||
});
|
||||
|
||||
add_action('pre_get_posts', function ($query) {
|
||||
if (is_admin() || !$query->is_main_query()) return;
|
||||
if (is_admin() || !$query->is_main_query()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$is_resource_archive = (bool) $query->get('aksrt_is_resource_archive')
|
||||
|| (post_type_exists('aksrt_resource') && $query->is_post_type_archive('aksrt_resource'));
|
||||
|
||||
// 资源归档页:把查询锁定到「post + _aksrt_is_resource = 1」(含历史 CPT 兼容)
|
||||
if ($is_resource_archive) {
|
||||
$query->set('post_type', function_exists('aksrt_resource_post_types') ? aksrt_resource_post_types() : ['post']);
|
||||
if ((bool) $query->get('aksrt_is_resource_archive')) {
|
||||
$query->set('post_type', 'post');
|
||||
$query->set('post_status', 'publish');
|
||||
|
||||
$orderby = isset($_GET['orderby']) ? sanitize_key((string) $_GET['orderby']) : 'date';
|
||||
@@ -318,30 +310,19 @@ add_action('pre_get_posts', function ($query) {
|
||||
$query->set('order', 'DESC');
|
||||
}
|
||||
|
||||
// 价格筛选:?price=free | paid | all
|
||||
// 价格区间:?price_min=0&price_max=99
|
||||
$price_filter = isset($_GET['price']) ? sanitize_key((string) $_GET['price']) : '';
|
||||
$price_min = isset($_GET['price_min']) && $_GET['price_min'] !== '' ? (float) $_GET['price_min'] : null;
|
||||
$price_max = isset($_GET['price_max']) && $_GET['price_max'] !== '' ? (float) $_GET['price_max'] : null;
|
||||
|
||||
// 「是资源」基础约束:_aksrt_is_resource=1(兼容旧 CPT 时由 post_type 兜底)
|
||||
$meta_query = [
|
||||
'relation' => 'AND',
|
||||
[
|
||||
'relation' => 'OR',
|
||||
// 新形态:post + meta=1
|
||||
[
|
||||
'key' => AKSRT_Domain_Model::RESOURCE_MARKER_KEY,
|
||||
'value' => '1',
|
||||
'compare' => '=',
|
||||
],
|
||||
// 历史 CPT 兼容:通过 post_type 已被包含,meta 缺失也算资源
|
||||
[
|
||||
'key' => AKSRT_Domain_Model::RESOURCE_MARKER_KEY,
|
||||
'compare' => 'NOT EXISTS',
|
||||
],
|
||||
'key' => AKSRT_Domain_Model::RESOURCE_MARKER_KEY,
|
||||
'value' => '1',
|
||||
'compare' => '=',
|
||||
],
|
||||
];
|
||||
|
||||
if ($price_filter === 'free') {
|
||||
$meta_query[] = [
|
||||
'relation' => 'OR',
|
||||
@@ -380,7 +361,7 @@ add_action('pre_get_posts', function ($query) {
|
||||
'compare' => '>=',
|
||||
'type' => 'NUMERIC',
|
||||
];
|
||||
} elseif ($price_max !== null) {
|
||||
} else {
|
||||
$meta_query[] = [
|
||||
'key' => AKSRT_Domain_Model::PAY_PRICE_KEY,
|
||||
'value' => $price_max,
|
||||
@@ -390,29 +371,16 @@ add_action('pre_get_posts', function ($query) {
|
||||
}
|
||||
}
|
||||
|
||||
// 仅在新形态(post)下加 _aksrt_is_resource=1 强约束;
|
||||
// 旧 CPT 仍在站点时,post_type 已含 aksrt_resource,配合上面 OR 兼容逻辑即可。
|
||||
if (!post_type_exists('aksrt_resource')) {
|
||||
// 替换 OR 兼容片段为强等于
|
||||
$meta_query[0] = [
|
||||
'key' => AKSRT_Domain_Model::RESOURCE_MARKER_KEY,
|
||||
'value' => '1',
|
||||
'compare' => '=',
|
||||
];
|
||||
$existing = $query->get('meta_query');
|
||||
if (!is_array($existing)) {
|
||||
$existing = [];
|
||||
}
|
||||
|
||||
if (!empty($meta_query)) {
|
||||
$existing = $query->get('meta_query');
|
||||
if (!is_array($existing)) $existing = [];
|
||||
$query->set('meta_query', array_merge($existing, $meta_query));
|
||||
}
|
||||
|
||||
// 资源归档不要分类/标签限制
|
||||
$query->set('meta_query', array_merge($existing, $meta_query));
|
||||
$query->set('cat', 0);
|
||||
$query->set('tag', '');
|
||||
}
|
||||
|
||||
// 站内搜索:post 已默认覆盖(资源就是 post),仅在仍有旧 CPT 时合并
|
||||
// 站内搜索:分类/标签名搜索(内容类型始终为 post)
|
||||
if ($query->is_search() && empty($query->get('post_type'))) {
|
||||
$search_type = sanitize_key((string) $query->get('aksrt_search_type'));
|
||||
|
||||
@@ -438,41 +406,20 @@ add_action('pre_get_posts', function ($query) {
|
||||
]]);
|
||||
}
|
||||
}
|
||||
|
||||
if (post_type_exists('aksrt_resource')) {
|
||||
// 单一数据选择策略:搜索的内容类型与索引/推送共用同一 helper。
|
||||
$search_types = function_exists('aksrt_searchable_post_types')
|
||||
? aksrt_searchable_post_types()
|
||||
: ['post'];
|
||||
if (count($search_types) > 1) {
|
||||
$query->set('post_type', $search_types);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 分类 / 标签归档:post 已自然包含资源(meta 标记),仅当历史 CPT 仍在时才需合并 post_type
|
||||
if (($query->is_category() || $query->is_tag()) && post_type_exists('aksrt_resource')) {
|
||||
$existing = $query->get('post_type');
|
||||
if (empty($existing)) {
|
||||
$query->set('post_type', ['post', 'aksrt_resource']);
|
||||
}
|
||||
}
|
||||
|
||||
// v4 起主题统一为 CMS / 资源站形态,所有归档都允许包含资源帖。
|
||||
// 历史的 blog/cms 双流分流(按 site_profile + term flow 排除资源)已移除。
|
||||
});
|
||||
|
||||
// 资源归档页:套 archive-aksrt_resource.php 模板(v3.4 起作为「资源虚拟归档」复用)
|
||||
add_filter('template_include', function ($template) {
|
||||
if (!get_query_var('aksrt_is_resource_archive')) return $template;
|
||||
$candidate = locate_template(['archive-aksrt_resource.php']);
|
||||
if (!get_query_var('aksrt_is_resource_archive')) {
|
||||
return $template;
|
||||
}
|
||||
$candidate = locate_template(['archive-resources.php']);
|
||||
return $candidate ?: $template;
|
||||
});
|
||||
|
||||
// 资源归档页:标题
|
||||
add_filter('document_title_parts', function ($parts) {
|
||||
if (get_query_var('aksrt_is_resource_archive')) {
|
||||
$parts['title'] = '资源';
|
||||
$parts['title'] = __('资源', 'aksrt');
|
||||
}
|
||||
return $parts;
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user