From 0be1a5a08f2b7bd407f343ae833f191c8ae42022 Mon Sep 17 00:00:00 2001 From: Vladimir Bryzgalov Date: Tue, 12 May 2026 20:07:01 +0500 Subject: [PATCH] feat(wpmc S3): ShortcodeDmr::render resolves text by current town --- includes/class-shortcode-dmr.php | 39 ++++++++++++++++++++++++++++++-- tests/ShortcodeDmrTest.php | 25 ++++++++++++++++++++ tests/TestCase.php | 3 +++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/includes/class-shortcode-dmr.php b/includes/class-shortcode-dmr.php index f0474bc..4fb459d 100644 --- a/includes/class-shortcode-dmr.php +++ b/includes/class-shortcode-dmr.php @@ -26,7 +26,42 @@ final class ShortcodeDmr public static function render($atts = [], $content = null): string { - // Implementation in Task 13. - return ''; + $atts = \shortcode_atts(['id' => 0], (array) $atts); + $id = (int) $atts['id']; + if ($id <= 0) { + return ''; + } + + $town_slug = Helpers::current_town(); + if ($town_slug === null) { + return ''; + } + + $cache_key = $id . '|' . $town_slug; + if (array_key_exists($cache_key, self::$render_cache)) { + return self::$render_cache[$cache_key]; + } + + $term = Helpers::get_term_by_slug($town_slug); + if ($term === null) { + return self::$render_cache[$cache_key] = ''; + } + + $rows = \get_field('list_shortcode', $id); + if (!is_array($rows) || $rows === []) { + return self::$render_cache[$cache_key] = ''; + } + + foreach ($rows as $row) { + if (!is_array($row) || !isset($row['town'])) { + continue; + } + if ((int) $row['town'] === (int) $term->term_id) { + $text = isset($row['text']) ? (string) $row['text'] : ''; + return self::$render_cache[$cache_key] = $text; + } + } + + return self::$render_cache[$cache_key] = ''; } } diff --git a/tests/ShortcodeDmrTest.php b/tests/ShortcodeDmrTest.php index a11b8bb..2f4d23d 100644 --- a/tests/ShortcodeDmrTest.php +++ b/tests/ShortcodeDmrTest.php @@ -27,4 +27,29 @@ final class ShortcodeDmrTest extends TestCase self::assertTrue(true); } + + public function test_render_returns_text_when_row_matches_current_town(): void + { + $_SERVER['REQUEST_URI'] = '/tyumen/'; + \Brain\Monkey\Functions\when('get_terms')->justReturn([10 => 'spb', 11 => 'tyumen']); + + $term = new \WP_Term(); + $term->term_id = 11; + $term->slug = 'tyumen'; + \Brain\Monkey\Functions\when('get_term_by')->alias(function ($field, $slug, $tax) use ($term) { + return ($field === 'slug' && $slug === 'tyumen' && $tax === 'town') ? $term : false; + }); + + \Brain\Monkey\Functions\expect('get_field') + ->once() + ->with('list_shortcode', 42) + ->andReturn([ + ['town' => 10, 'text' => 'СПб-текст'], + ['town' => 11, 'text' => 'Тюмень-текст'], + ]); + + $result = ShortcodeDmr::render(['id' => 42]); + + self::assertSame('Тюмень-текст', $result); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 55ef283..27b871a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -15,6 +15,9 @@ abstract class TestCase extends BaseTestCase parent::setUp(); Monkey\setUp(); Monkey\Functions\stubTranslationFunctions(); + Monkey\Functions\when('shortcode_atts')->alias(function (array $defaults, array $atts): array { + return array_merge($defaults, array_intersect_key($atts, $defaults)); + }); Helpers::reset_cache(); ShortcodeDmr::reset_cache(); }