diff --git a/includes/class-helpers.php b/includes/class-helpers.php index 3d00ce8..e6de1d6 100644 --- a/includes/class-helpers.php +++ b/includes/class-helpers.php @@ -111,4 +111,28 @@ final class Helpers } return is_string($value) && $value !== '' ? $value : null; } + + public static function is_routable_path(?string $path = null): bool + { + if ($path === null) { + $path = $_SERVER['REQUEST_URI'] ?? '/'; + } + if (!\function_exists('get_field')) { + return true; + } + $exclusions = \get_field('page_no_rep', 'option'); + if (!is_array($exclusions) || $exclusions === []) { + return true; + } + foreach ($exclusions as $row) { + if (!is_array($row) || empty($row['link'])) { + continue; + } + $needle = (string) $row['link']; + if ($needle !== '' && strpos($path, $needle) !== false) { + return false; + } + } + return true; + } } diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index f105749..e24694c 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -231,4 +231,38 @@ final class HelpersTest extends TestCase self::assertNull(Helpers::main_town_slug()); } + + public function test_is_routable_path_returns_true_when_no_exclusions(): void + { + \Brain\Monkey\Functions\expect('get_field') + ->once() + ->with('page_no_rep', 'option') + ->andReturn(false); + + self::assertTrue(Helpers::is_routable_path('/foo/bar/')); + } + + public function test_is_routable_path_returns_false_when_path_matches_exclusion(): void + { + \Brain\Monkey\Functions\expect('get_field') + ->once() + ->with('page_no_rep', 'option') + ->andReturn([ + ['link' => '/privacy/'], + ['link' => '/terms/'], + ]); + + self::assertFalse(Helpers::is_routable_path('/privacy/')); + } + + public function test_is_routable_path_uses_request_uri_when_path_null(): void + { + $_SERVER['REQUEST_URI'] = '/terms/'; + \Brain\Monkey\Functions\expect('get_field') + ->once() + ->with('page_no_rep', 'option') + ->andReturn([['link' => '/terms/']]); + + self::assertFalse(Helpers::is_routable_path()); + } }