PluginProbe
404 Solution / 4.3.0
404 Solution v4.3.0
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / services / ImportProgressStore.php

ImportProgressStore.php in 404 Solution 4.3.0, at includes/services/ImportProgressStore.php

80 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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