From b7fb46ea3857059a673470ea19849d0c3f83ebad Mon Sep 17 00:00:00 2001 From: Vladimir Bryzgalov Date: Sat, 16 May 2026 22:33:39 +0500 Subject: [PATCH] feat(wpmc S5): LinkFilter scaffold + register 3 URL filters Co-Authored-By: Claude Sonnet 4.6 --- includes/class-link-filter.php | 38 ++++++++ tests/LinkFilterTest.php | 162 +++++++++++++++++++++++++++++++++ 2 files changed, 200 insertions(+) create mode 100644 includes/class-link-filter.php create mode 100644 tests/LinkFilterTest.php diff --git a/includes/class-link-filter.php b/includes/class-link-filter.php new file mode 100644 index 0000000..d6a6872 --- /dev/null +++ b/includes/class-link-filter.php @@ -0,0 +1,38 @@ +justReturn([11 => $town_slug]); + \Brain\Monkey\Functions\when('get_field')->alias(function ($name) use ($main) { + if ($name === 'main_town_slug') return $main; + return false; // page_no_rep empty, page_unic empty + }); + } + + public function test_add_town_prefix_no_change_when_current_town_equals_main(): void + { + $this->set_routing_context('spb', 'spb'); + + self::assertSame('/about/', LinkFilter::add_town_prefix('/about/')); + } + + public function test_add_town_prefix_no_change_when_current_town_is_null(): void + { + $_SERVER['REQUEST_URI'] = '/random/'; + $_SERVER['HTTP_HOST'] = 'site.com'; + \Brain\Monkey\Functions\when('get_terms')->justReturn([10 => 'spb']); + \Brain\Monkey\Functions\when('get_field')->justReturn(false); + + self::assertSame('/about/', LinkFilter::add_town_prefix('/about/')); + } + + public function test_add_town_prefix_prepends_town_to_path_only_url(): void + { + $this->set_routing_context(); + + self::assertSame('/tyumen/about/', LinkFilter::add_town_prefix('/about/')); + } + + public function test_add_town_prefix_skips_path_already_prefixed(): void + { + $this->set_routing_context(); + + self::assertSame('/tyumen/about/', LinkFilter::add_town_prefix('/tyumen/about/')); + self::assertSame('/tyumen', LinkFilter::add_town_prefix('/tyumen')); + } + + public function test_add_town_prefix_full_url_same_host(): void + { + $this->set_routing_context(); + + self::assertSame( + 'https://site.com/tyumen/about/', + LinkFilter::add_town_prefix('https://site.com/about/') + ); + self::assertSame( + 'https://site.com/tyumen/', + LinkFilter::add_town_prefix('https://site.com/') + ); + } + + public function test_add_town_prefix_full_url_already_prefixed_no_double_prefix(): void + { + $this->set_routing_context(); + + self::assertSame( + 'https://site.com/tyumen/about/', + LinkFilter::add_town_prefix('https://site.com/tyumen/about/') + ); + } + + public function test_add_town_prefix_external_host_no_change(): void + { + $this->set_routing_context(); + + self::assertSame( + 'https://external.com/foo/', + LinkFilter::add_town_prefix('https://external.com/foo/') + ); + } + + public function test_add_town_prefix_non_http_scheme_no_change(): void + { + $this->set_routing_context(); + + self::assertSame('mailto:x@y.com', LinkFilter::add_town_prefix('mailto:x@y.com')); + self::assertSame('tel:+79991234567', LinkFilter::add_town_prefix('tel:+79991234567')); + } + + public function test_add_town_prefix_respects_page_no_rep_exclusions(): void + { + $_SERVER['REQUEST_URI'] = '/tyumen/'; + $_SERVER['HTTP_HOST'] = 'site.com'; + \Brain\Monkey\Functions\when('get_terms')->justReturn([11 => 'tyumen']); + \Brain\Monkey\Functions\when('get_field')->alias(function ($name) { + if ($name === 'main_town_slug') return 'spb'; + if ($name === 'page_no_rep') return [['link' => '/privacy/']]; + return false; + }); + + self::assertSame('/privacy/', LinkFilter::add_town_prefix('/privacy/')); + } + + public function test_add_town_prefix_returns_unchanged_when_bypassed(): void + { + $this->set_routing_context(); + + $result = \WPMultiCity\Helpers::without_url_filters(function () { + return LinkFilter::add_town_prefix('/about/'); + }); + + self::assertSame('/about/', $result); + } + + public function test_filter_acf_load_value_returns_array_unchanged(): void + { + $this->set_routing_context(); + + self::assertSame( + ['a', 'b'], + LinkFilter::filter_acf_load_value(['a', 'b']) + ); + } +}