diff --git a/includes/class-helpers.php b/includes/class-helpers.php index 0fb6688..76f0263 100644 --- a/includes/class-helpers.php +++ b/includes/class-helpers.php @@ -11,7 +11,7 @@ final class Helpers private static array $page_unic_cache = []; private static int $bypass_depth = 0; - private static function acf_ready(): bool + public static function acf_ready(): bool { return \did_action('acf/init') > 0; } diff --git a/includes/class-redirect.php b/includes/class-redirect.php index b66ff38..44a3f95 100644 --- a/includes/class-redirect.php +++ b/includes/class-redirect.php @@ -30,12 +30,11 @@ final class Redirect } $candidates = \get_posts([ - 'name' => $slug, - 'post_type' => 'page', - 'post_status' => ['private', 'publish'], - 'posts_per_page' => 1, - 'no_found_rows' => true, - 'suppress_filters' => false, + 'name' => $slug, + 'post_type' => 'page', + 'post_status' => ['private', 'publish'], + 'posts_per_page' => 1, + 'no_found_rows' => true, ]); if (empty($candidates)) { return; @@ -43,7 +42,7 @@ final class Redirect $candidate_id = (int) $candidates[0]->ID; - if (!\function_exists('get_field')) { + if (!Helpers::acf_ready() || !\function_exists('get_field')) { return; } $rows = \get_field('page_unic', 'option'); @@ -71,7 +70,7 @@ final class Redirect static fn () => \get_permalink($orig_id) ); if (!is_string($orig_permalink) || $orig_permalink === '') { - return; + continue; } $path_only = Helpers::remove_domain_link($orig_permalink); $redirect_to = '/' . $term->slug . $path_only; diff --git a/tests/ZRedirectDoingAjaxTest.php b/tests/RedirectDoingAjaxTest.php similarity index 59% rename from tests/ZRedirectDoingAjaxTest.php rename to tests/RedirectDoingAjaxTest.php index bc32ad8..775f1e9 100644 --- a/tests/ZRedirectDoingAjaxTest.php +++ b/tests/RedirectDoingAjaxTest.php @@ -8,12 +8,17 @@ use WPMultiCity\Redirect; /** * Isolated test for the DOING_AJAX bail-out in Redirect::handle(). * - * Must be in a separate file sorted AFTER RouterTest so that defining the - * DOING_AJAX constant does not contaminate Router::route() tests in RouterTest, - * which also bails early when DOING_AJAX is true. + * Isolation is achieved via @runInSeparateProcess on the test method, which + * forks a fresh PHP process so that defining the DOING_AJAX constant cannot + * contaminate other tests (e.g. RouterTest). The previous Z-prefix ordering + * hack is no longer needed. */ -final class ZRedirectDoingAjaxTest extends TestCase +final class RedirectDoingAjaxTest extends TestCase { + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ public function test_bail_when_doing_ajax(): void { if (!\defined('DOING_AJAX')) {