feat(wpmc S3): ShortcodeDmr::render resolves text by current town

This commit is contained in:
Vladimir Bryzgalov
2026-05-12 20:07:01 +05:00
parent 464d254f1c
commit 0be1a5a08f
3 changed files with 65 additions and 2 deletions
+37 -2
View File
@@ -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] = '';
}
}