Files
wordpress-theme-aksrt/inc/helpers/aksrt-forum.php
T
kate522 780c8cb4f7 feat(forum): frontend 1:1 list/tabs/urls/enqueue
Wire Zibll-style board sort tabs, shared topic rows, page-template
URLs, forum asset enqueue on home/plate, home synthesis load-more,
and topic last_reply meta for last-reply ordering. No dual-read.
2026-07-12 08:38:31 +08:00

99 lines
2.6 KiB
PHP

<?php
/**
* Forum frontend URL helpers.
*
* Page-template assignment is the source of truth for forum home / compose URLs.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_get_page_url_by_template')) :
/**
* Resolve the first published page that uses a given page template file.
*
* @param string $template_file Relative theme template path (e.g. templates/pages/page-forums.php).
*/
function aksrt_get_page_url_by_template(string $template_file): string {
$template_file = ltrim(str_replace('\\', '/', $template_file), '/');
if ($template_file === '') {
return '';
}
static $cache = [];
if (array_key_exists($template_file, $cache)) {
return $cache[$template_file];
}
$pages = get_posts([
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'menu_order title',
'order' => 'ASC',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'meta_key' => '_wp_page_template',
'meta_value' => $template_file,
]);
$url = '';
if (!empty($pages[0]) && $pages[0] instanceof WP_Post) {
$permalink = get_permalink($pages[0]);
if (is_string($permalink) && $permalink !== '') {
$url = $permalink;
}
}
$cache[$template_file] = $url;
return $url;
}
endif;
if (!function_exists('aksrt_get_forum_home_url')) :
/**
* Public forum home URL (page template AKSRT - 论坛).
*/
function aksrt_get_forum_home_url(): string {
$url = aksrt_get_page_url_by_template('templates/pages/page-forums.php');
return $url !== '' ? $url : home_url('/');
}
endif;
if (!function_exists('aksrt_get_forum_compose_url')) :
/**
* Public forum compose URL (page template AKSRT - 发帖).
*
* @param array<string, scalar> $args Optional query args (board, plate, …).
*/
function aksrt_get_forum_compose_url(array $args = []): string {
$url = aksrt_get_page_url_by_template('templates/forum/forum-compose.php');
if ($url === '') {
$url = home_url('/');
}
// Normalize plate → board for compose form contract.
if (isset($args['plate']) && !isset($args['board'])) {
$args['board'] = $args['plate'];
unset($args['plate']);
}
$clean = [];
foreach ($args as $key => $value) {
$key = sanitize_key((string) $key);
if ($key === '') {
continue;
}
if (is_bool($value)) {
$clean[$key] = $value ? '1' : '0';
} else {
$clean[$key] = (string) $value;
}
}
return !empty($clean) ? add_query_arg($clean, $url) : $url;
}
endif;