fix(wpmc 1.0.1): guard ACF helpers with did_action('acf/init')

Add private Helpers::acf_ready() that checks did_action('acf/init') > 0,
then guard the five ACF-dependent methods (alt_get_field, index_town_alt_page_id,
main_town_slug, page_unic_swap, is_routable_path) so they return safe defaults
(null/true) before ACF initialises. Fixes the acf_get_value "called too early"
notice triggered when home_url filter fires before acf/init.

Update existing tests in HelpersTest, LinkFilterTest, RedirectTest, RouterTest
to stub did_action => 1 where ACF-ready behaviour is expected. Add four new
guard tests covering before/after acf/init for main_town_slug and is_routable_path.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Vladimir Bryzgalov
2026-05-17 17:01:19 +05:00
parent 949fb19e95
commit 82d2345d12
5 changed files with 105 additions and 0 deletions
+20
View File
@@ -11,6 +11,11 @@ final class Helpers
private static array $page_unic_cache = [];
private static int $bypass_depth = 0;
private static function acf_ready(): bool
{
return \did_action('acf/init') > 0;
}
public static function reset_cache(): void
{
self::$current_town_memo = null;
@@ -80,6 +85,9 @@ final class Helpers
public static function alt_get_field(string $selector, $post_id = false, bool $format_value = true)
{
if (!self::acf_ready()) {
return null;
}
$post_id = \apply_filters('wpmc_alt_get_field_post_id', $post_id, $selector);
if (!\function_exists('get_field')) {
return null;
@@ -106,6 +114,9 @@ final class Helpers
public static function index_town_alt_page_id(): ?int
{
if (!self::acf_ready()) {
return null;
}
if (!\function_exists('get_field')) {
return null;
}
@@ -116,6 +127,9 @@ final class Helpers
public static function main_town_slug(): ?string
{
if (!self::acf_ready()) {
return null;
}
if (!\function_exists('get_field')) {
return null;
}
@@ -132,6 +146,9 @@ final class Helpers
if (array_key_exists($key, self::$page_unic_cache)) {
return self::$page_unic_cache[$key];
}
if (!self::acf_ready()) {
return null;
}
$term = self::get_term_by_slug($town_slug);
if ($term === null) {
return self::$page_unic_cache[$key] = null;
@@ -161,6 +178,9 @@ final class Helpers
if ($path === null) {
$path = $_SERVER['REQUEST_URI'] ?? '/';
}
if (!self::acf_ready()) {
return true;
}
if (!\function_exists('get_field')) {
return true;
}