feat(wpmc S4): Router::route root URL uses main_page_alt term meta

This commit is contained in:
Vladimir Bryzgalov
2026-05-16 19:58:04 +05:00
parent b218f5588b
commit 9af4c2976e
2 changed files with 98 additions and 1 deletions
+69 -1
View File
@@ -19,6 +19,74 @@ final class Router
public static function route(): void
{
// Implementation in Tasks 10-13.
if (\is_admin()) {
return;
}
if (\defined('DOING_AJAX') && \DOING_AJAX) {
return;
}
$town_slug = Helpers::current_town();
if ($town_slug === null) {
return;
}
$main = Helpers::main_town_slug();
if ($main !== null && $town_slug === $main) {
return;
}
$request_uri = $_SERVER['REQUEST_URI'] ?? '/';
if (!Helpers::is_routable_path($request_uri)) {
return;
}
$url_original = self::strip_town_prefix($request_uri, $town_slug);
if (self::is_root($url_original)) {
$term = Helpers::get_term_by_slug($town_slug);
if ($term === null) {
return;
}
$page_id = 0;
if (\function_exists('get_field')) {
$page_id = (int) (\get_field('main_page_alt', 'town_' . $term->term_id) ?? 0);
}
if (!$page_id) {
$fallback = Helpers::index_town_alt_page_id();
if ($fallback === null) {
return;
}
$page_id = $fallback;
}
self::install_query($page_id, $town_slug, true);
return;
}
// Internal page handling lands in Task 13.
}
private static function strip_town_prefix(string $uri, string $town_slug): string
{
$path = strtok($uri, '?');
$query = (strpos($uri, '?') !== false) ? substr($uri, strpos($uri, '?')) : '';
$new_path = preg_replace('#^/' . preg_quote($town_slug, '#') . '(/|$)#', '/', (string) $path);
return ($new_path ?? '/') . $query;
}
private static function is_root(string $url): bool
{
$path = strtok($url, '?');
return $path === '/' || $path === false || $path === '';
}
private static function install_query(int $page_id, string $town_slug, bool $is_front_page): void
{
global $wp_query, $wp_the_query, $post;
$post = \get_post($page_id);
$wp_query = new \WP_Query(['page_id' => $page_id, 'town' => $town_slug]);
$wp_query->is_page = true;
if ($is_front_page) {
$wp_query->is_front_page = true;
}
$wp_the_query = $wp_query;
\status_header(200);
}
}