feat(wpmc S4): Router::route root URL uses main_page_alt term meta
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,4 +91,33 @@ final class RouterTest extends TestCase
|
||||
|
||||
self::assertArrayNotHasKey('wp_query', $GLOBALS);
|
||||
}
|
||||
|
||||
public function test_route_uses_main_page_alt_term_meta_for_root_url(): void
|
||||
{
|
||||
\Brain\Monkey\Functions\when('is_admin')->justReturn(false);
|
||||
$_SERVER['REQUEST_URI'] = '/tyumen/';
|
||||
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => 'tyumen']);
|
||||
|
||||
$term = new \WP_Term();
|
||||
$term->term_id = 11;
|
||||
$term->slug = 'tyumen';
|
||||
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
||||
|
||||
\Brain\Monkey\Functions\when('get_field')->alias(function ($name, $where = null) {
|
||||
if ($name === 'main_town_slug') return 'spb';
|
||||
if ($name === 'page_no_rep') return false;
|
||||
if ($name === 'main_page_alt' && $where === 'town_11') return 555;
|
||||
return false;
|
||||
});
|
||||
\Brain\Monkey\Functions\when('get_post')->justReturn((object) ['ID' => 555]);
|
||||
\Brain\Monkey\Functions\when('status_header')->justReturn(null);
|
||||
|
||||
Router::route();
|
||||
|
||||
self::assertInstanceOf(\WP_Query::class, $GLOBALS['wp_query']);
|
||||
self::assertSame(555, $GLOBALS['wp_query']->constructor_args['page_id']);
|
||||
self::assertSame('tyumen', $GLOBALS['wp_query']->constructor_args['town']);
|
||||
self::assertTrue($GLOBALS['wp_query']->is_front_page);
|
||||
self::assertTrue($GLOBALS['wp_query']->is_page);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user