feat(wpmc S3): Helpers::alt_get_field with do_shortcode + filter

Implements alt_get_field(): applies wpmc_alt_get_field_post_id filter for
post_id remapping, guards against missing ACF, runs do_shortcode on string
results, passes arrays/null through unchanged.  4 new tests; full suite
22 tests / 58 assertions green.

Note: Brain Monkey FunctionStub::__construct calls function_exists()
internally; expect() calls must precede when('function_exists')->alias()
in each test to avoid the alias intercepting the stub-creation check and
causing eval redeclaration errors.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Vladimir Bryzgalov
2026-05-12 20:02:57 +05:00
parent 1f343dfaf2
commit 11bd3a06b0
2 changed files with 69 additions and 0 deletions
+10
View File
@@ -72,4 +72,14 @@ final class Helpers
$parts = array_values(array_filter(explode('/', $path), static fn(string $p) => $p !== '')); $parts = array_values(array_filter(explode('/', $path), static fn(string $p) => $p !== ''));
return $parts[0] ?? null; 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;
}
} }
+59
View File
@@ -129,4 +129,63 @@ final class HelpersTest extends TestCase
self::assertTrue(true); 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'));
}
} }