feat(wpmc S3): ShortcodeDmr::render resolves text by current town
This commit is contained in:
@@ -26,7 +26,42 @@ final class ShortcodeDmr
|
|||||||
|
|
||||||
public static function render($atts = [], $content = null): string
|
public static function render($atts = [], $content = null): string
|
||||||
{
|
{
|
||||||
// Implementation in Task 13.
|
$atts = \shortcode_atts(['id' => 0], (array) $atts);
|
||||||
|
$id = (int) $atts['id'];
|
||||||
|
if ($id <= 0) {
|
||||||
return '';
|
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] = '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,4 +27,29 @@ final class ShortcodeDmrTest extends TestCase
|
|||||||
|
|
||||||
self::assertTrue(true);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ abstract class TestCase extends BaseTestCase
|
|||||||
parent::setUp();
|
parent::setUp();
|
||||||
Monkey\setUp();
|
Monkey\setUp();
|
||||||
Monkey\Functions\stubTranslationFunctions();
|
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();
|
Helpers::reset_cache();
|
||||||
ShortcodeDmr::reset_cache();
|
ShortcodeDmr::reset_cache();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user