feat(wpmc S4): Helpers::page_unic_swap with per-request cache

This commit is contained in:
Vladimir Bryzgalov
2026-05-16 19:22:37 +05:00
parent d122bfacb5
commit d724089b2d
2 changed files with 78 additions and 0 deletions
+32
View File
@@ -8,12 +8,14 @@ final class Helpers
private static ?string $current_town_memo = null;
private static bool $current_town_resolved = false;
private static array $term_cache = [];
private static array $page_unic_cache = [];
public static function reset_cache(): void
{
self::$current_town_memo = null;
self::$current_town_resolved = false;
self::$term_cache = [];
self::$page_unic_cache = [];
}
public static function get_term_by_slug(string $slug, string $taxonomy = 'town'): ?\WP_Term
@@ -112,6 +114,36 @@ final class Helpers
return is_string($value) && $value !== '' ? $value : null;
}
public static function page_unic_swap(int $page_id_old, string $town_slug): ?int
{
$key = $page_id_old . '|' . $town_slug;
if (array_key_exists($key, self::$page_unic_cache)) {
return self::$page_unic_cache[$key];
}
$term = self::get_term_by_slug($town_slug);
if ($term === null) {
return self::$page_unic_cache[$key] = null;
}
if (!\function_exists('get_field')) {
return self::$page_unic_cache[$key] = null;
}
$rows = \get_field('page_unic', 'option');
if (!is_array($rows)) {
return self::$page_unic_cache[$key] = null;
}
foreach ($rows as $row) {
if (!is_array($row)) {
continue;
}
if ((int) ($row['town'] ?? 0) === (int) $term->term_id
&& (int) ($row['page_old'] ?? 0) === $page_id_old) {
$new = (int) ($row['page_new'] ?? 0);
return self::$page_unic_cache[$key] = $new > 0 ? $new : null;
}
}
return self::$page_unic_cache[$key] = null;
}
public static function is_routable_path(?string $path = null): bool
{
if ($path === null) {