diff --git a/includes/class-helpers.php b/includes/class-helpers.php index c9db677..3d00ce8 100644 --- a/includes/class-helpers.php +++ b/includes/class-helpers.php @@ -99,4 +99,16 @@ final class Helpers $fragment = isset($parsed['fragment']) ? '#' . $parsed['fragment'] : ''; return $path . $query . $fragment; } + + public static function main_town_slug(): ?string + { + if (!\function_exists('get_field')) { + return null; + } + $value = \get_field('main_town_slug', 'option'); + if ($value instanceof \WP_Term) { + return $value->slug !== '' ? $value->slug : null; + } + return is_string($value) && $value !== '' ? $value : null; + } } diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index 4af6e74..f105749 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -211,4 +211,24 @@ final class HelpersTest extends TestCase // remove_domain_link is pure (no WP function dependencies) — exercise the proxy. self::assertSame('/foo/', wpmc_remove_domain_link('https://site.com/foo/')); } + + public function test_main_town_slug_reads_option_via_acf(): void + { + \Brain\Monkey\Functions\expect('get_field') + ->once() + ->with('main_town_slug', 'option') + ->andReturn('spb'); + + self::assertSame('spb', Helpers::main_town_slug()); + } + + public function test_main_town_slug_returns_null_when_option_empty(): void + { + \Brain\Monkey\Functions\expect('get_field') + ->once() + ->with('main_town_slug', 'option') + ->andReturn(false); + + self::assertNull(Helpers::main_town_slug()); + } }