Files
wp-multi-city/includes/class-helpers.php
T
Vladimir Bryzgalov 58838b0c19 feat(wpmc S3): Helpers::get_term_by_slug with per-request cache
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-12 19:56:54 +05:00

29 lines
813 B
PHP

<?php
declare(strict_types=1);
namespace WPMultiCity;
final class Helpers
{
private static ?string $current_town_memo = null;
private static bool $current_town_resolved = false;
private static array $term_cache = [];
public static function reset_cache(): void
{
self::$current_town_memo = null;
self::$current_town_resolved = false;
self::$term_cache = [];
}
public static function get_term_by_slug(string $slug, string $taxonomy = 'town'): ?\WP_Term
{
$key = $taxonomy . '|' . $slug;
if (array_key_exists($key, self::$term_cache)) {
return self::$term_cache[$key];
}
$term = \get_term_by('slug', $slug, $taxonomy);
return self::$term_cache[$key] = ($term instanceof \WP_Term) ? $term : null;
}
}