| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Stores resumable import checkpoints keyed by uploaded CSV content hash. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_ImportProgressStore { |
| 11 |
|
| 12 |
/** @var string */ |
| 13 |
private $optionKey; |
| 14 |
|
| 15 |
function __construct(string $optionKey) { |
| 16 |
$this->optionKey = $optionKey; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* @param string $contentHash |
| 21 |
* @return array<string, mixed>|null |
| 22 |
*/ |
| 23 |
function getResumeProgress($contentHash) { |
| 24 |
if ($contentHash === '' || !function_exists('get_option')) { |
| 25 |
return null; |
| 26 |
} |
| 27 |
$progress = get_option($this->optionKey, null); |
| 28 |
if (!is_array($progress) || !isset($progress['hash']) || !is_string($progress['hash'])) { |
| 29 |
return null; |
| 30 |
} |
| 31 |
if ($progress['hash'] !== $contentHash) { |
| 32 |
return null; |
| 33 |
} |
| 34 |
/** @var array<string, mixed> $progress */ |
| 35 |
return $progress; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @param string $contentHash |
| 40 |
* @param array<string, mixed> $state |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
function persistImportProgress($contentHash, $state) { |
| 44 |
if ($contentHash === '' || !function_exists('update_option')) { |
| 45 |
return; |
| 46 |
} |
| 47 |
$state['hash'] = $contentHash; |
| 48 |
update_option($this->optionKey, $state); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
function clearImportProgress() { |
| 55 |
if (function_exists('delete_option')) { |
| 56 |
delete_option($this->optionKey); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @param array<string, mixed> $progress |
| 62 |
* @param string $key |
| 63 |
* @param int $default |
| 64 |
* @return int |
| 65 |
*/ |
| 66 |
static function progressInt(array $progress, string $key, int $default): int { |
| 67 |
if (!isset($progress[$key])) { |
| 68 |
return $default; |
| 69 |
} |
| 70 |
$v = $progress[$key]; |
| 71 |
if (is_int($v)) { |
| 72 |
return $v; |
| 73 |
} |
| 74 |
if (is_numeric($v)) { |
| 75 |
return (int)$v; |
| 76 |
} |
| 77 |
return $default; |
| 78 |
} |
| 79 |
} |
| 80 |
|