diff --git a/includes/class-helpers.php b/includes/class-helpers.php index cbb2765..2997db0 100644 --- a/includes/class-helpers.php +++ b/includes/class-helpers.php @@ -72,4 +72,14 @@ final class Helpers $parts = array_values(array_filter(explode('/', $path), static fn(string $p) => $p !== '')); return $parts[0] ?? null; } + + public static function alt_get_field(string $selector, $post_id = false, bool $format_value = true) + { + $post_id = \apply_filters('wpmc_alt_get_field_post_id', $post_id, $selector); + if (!\function_exists('get_field')) { + return null; + } + $value = \get_field($selector, $post_id, $format_value); + return is_string($value) ? \do_shortcode($value) : $value; + } } diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index c26865e..75277a1 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -129,4 +129,63 @@ final class HelpersTest extends TestCase self::assertTrue(true); } + + public function test_alt_get_field_applies_do_shortcode_to_string_values(): void + { + // expect() must come before when('function_exists') so Brain Monkey's + // FunctionStub::__construct can eval-define get_field() before the shim + // fakes function_exists('get_field') === true. + \Brain\Monkey\Functions\expect('get_field') + ->once() + ->with('headline', 5, true) + ->andReturn('Hello [bar]'); + \Brain\Monkey\Functions\expect('do_shortcode') + ->once() + ->with('Hello [bar]') + ->andReturn('Hello World'); + \Brain\Monkey\Functions\when('function_exists')->alias(function ($name) { + return $name === 'get_field'; + }); + + self::assertSame('Hello World', Helpers::alt_get_field('headline', 5)); + } + + public function test_alt_get_field_returns_arrays_as_is(): void + { + // expect() before when('function_exists') to avoid FunctionStub seeing + // the shim and skipping the eval-declaration of get_field. + // do_shortcode is intentionally NOT registered: if the implementation + // calls it for a non-string, Brain Monkey throws MissingFunctionExpectations. + \Brain\Monkey\Functions\expect('get_field') + ->once() + ->andReturn(['a', 'b']); + \Brain\Monkey\Functions\when('function_exists')->alias(fn($n) => $n === 'get_field'); + + self::assertSame(['a', 'b'], Helpers::alt_get_field('list')); + } + + public function test_alt_get_field_post_id_filter_overrides_caller_id(): void + { + \Brain\Monkey\Functions\expect('get_field') + ->once() + ->with('title', 99, true) + ->andReturn(null); + \Brain\Monkey\Functions\when('function_exists')->alias(fn($n) => $n === 'get_field'); + \Brain\Monkey\Filters\expectApplied('wpmc_alt_get_field_post_id') + ->once() + ->with(5, 'title') + ->andReturn(99); + + Helpers::alt_get_field('title', 5); + self::assertTrue(true); + } + + public function test_alt_get_field_returns_null_when_acf_get_field_missing(): void + { + \Brain\Monkey\Functions\when('function_exists')->justReturn(false); + // get_field must never be called; not registering it means Brain Monkey + // throws if it is, which is stronger than ->never(). + + self::assertNull(Helpers::alt_get_field('anything')); + } }