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
|
||||
{
|
||||
// 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] = '';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user