| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Stores redirect IDs whose destinations recently appeared as 404s. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_RedirectDeadDestinationStore { |
| 11 |
|
| 12 |
public const CACHE_KEY = 'abj404_dead_dest_ids'; |
| 13 |
|
| 14 |
/** @return array<int, string> */ |
| 15 |
public function getIds(): array { |
| 16 |
if (!function_exists('get_transient')) { |
| 17 |
return array(); |
| 18 |
} |
| 19 |
|
| 20 |
$raw = get_transient(self::CACHE_KEY); |
| 21 |
if (!is_array($raw)) { |
| 22 |
return array(); |
| 23 |
} |
| 24 |
|
| 25 |
$ids = array(); |
| 26 |
foreach ($raw as $value) { |
| 27 |
if (is_scalar($value) && (string)$value !== '') { |
| 28 |
$ids[] = (string)$value; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
return array_values(array_unique($ids)); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @param array<int, string> $ids |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function storeIds(array $ids): void { |
| 40 |
if (!function_exists('set_transient')) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
$normalized = array(); |
| 45 |
foreach ($ids as $id) { |
| 46 |
if (is_scalar($id) && (string)$id !== '') { |
| 47 |
$normalized[] = (string)$id; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
$ttl = defined('HOUR_IN_SECONDS') ? 25 * (int) HOUR_IN_SECONDS : 90000; |
| 52 |
// allow-cache-empty: an empty array is the explicit "no dead destinations" cache result. |
| 53 |
set_transient(self::CACHE_KEY, array_values(array_unique($normalized)), $ttl); |
| 54 |
} |
| 55 |
} |
| 56 |
|