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
+3
View File
@@ -44,6 +44,7 @@ final class LinkFilterTest extends TestCase
{
$_SERVER['REQUEST_URI'] = '/' . $town_slug . '/';
$_SERVER['HTTP_HOST'] = $host;
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => $town_slug]);
\Brain\Monkey\Functions\when('get_field')->alias(function ($name) use ($main) {
if ($name === 'main_town_slug') return $main;
@@ -62,6 +63,7 @@ final class LinkFilterTest extends TestCase
{
$_SERVER['REQUEST_URI'] = '/random/';
$_SERVER['HTTP_HOST'] = 'site.com';
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
\Brain\Monkey\Functions\when('get_terms')->justReturn([10 => 'spb']);
\Brain\Monkey\Functions\when('get_field')->justReturn(false);
@@ -129,6 +131,7 @@ final class LinkFilterTest extends TestCase
{
$_SERVER['REQUEST_URI'] = '/tyumen/';
$_SERVER['HTTP_HOST'] = 'site.com';
\Brain\Monkey\Functions\stubs(['did_action' => 1]);
\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';