feat(wpmc S4): Helpers::page_unic_swap with per-request cache
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -265,4 +265,50 @@ final class HelpersTest extends TestCase
|
||||
|
||||
self::assertFalse(Helpers::is_routable_path());
|
||||
}
|
||||
|
||||
public function test_page_unic_swap_returns_clone_id_when_row_matches(): void
|
||||
{
|
||||
$term = new \WP_Term();
|
||||
$term->term_id = 11;
|
||||
$term->slug = 'tyumen';
|
||||
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
->with('page_unic', 'option')
|
||||
->andReturn([
|
||||
['town' => 10, 'page_old' => 5, 'page_new' => 50],
|
||||
['town' => 11, 'page_old' => 7, 'page_new' => 70],
|
||||
]);
|
||||
|
||||
self::assertSame(70, Helpers::page_unic_swap(7, 'tyumen'));
|
||||
}
|
||||
|
||||
public function test_page_unic_swap_returns_null_when_no_row_matches(): void
|
||||
{
|
||||
$term = new \WP_Term();
|
||||
$term->term_id = 11;
|
||||
$term->slug = 'tyumen';
|
||||
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
->andReturn([['town' => 10, 'page_old' => 5, 'page_new' => 50]]);
|
||||
|
||||
self::assertNull(Helpers::page_unic_swap(7, 'tyumen'));
|
||||
}
|
||||
|
||||
public function test_page_unic_swap_caches_within_request(): void
|
||||
{
|
||||
$term = new \WP_Term();
|
||||
$term->term_id = 11;
|
||||
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
||||
\Brain\Monkey\Functions\expect('get_field')
|
||||
->once()
|
||||
->andReturn([['town' => 11, 'page_old' => 7, 'page_new' => 70]]);
|
||||
|
||||
Helpers::page_unic_swap(7, 'tyumen');
|
||||
Helpers::page_unic_swap(7, 'tyumen');
|
||||
Helpers::page_unic_swap(7, 'tyumen');
|
||||
|
||||
self::assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user