PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.15.0
Import WP – CSV & XML Import Export for WordPress v2.15.0
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Importer / State / ImporterState.php

ImporterState.php in Import WP – CSV & XML Import Export for WordPress 2.15.0, at class/Common/Importer/State/ImporterState.php

322 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImportWP\Common\Importer\State;
4
5 use ImportWP\Common\Util\Logger;
6
7 class ImporterState
8 {
9 private $data = [];
10 private $importer_id;
11 protected static $object_type = 'importer';
12
13 public function __construct($importer_id, $user = '')
14 {
15 $this->importer_id = $importer_id;
16 }
17
18 protected function default($session_id)
19 {
20 return [
21 'id' => $session_id,
22 'status' => 'init',
23 'version' => 2,
24 'message' => '',
25 'timestamp' => time(),
26 'duration' => 0,
27 'section' => 'import',
28 'progress' => [
29 'import' => [
30 'start' => 0,
31 'end' => 0,
32 'current_row' => 0,
33 ],
34 'delete' => [
35 'start' => 0,
36 'end' => 0,
37 'current_row' => 0,
38 ]
39 ],
40 ];
41 }
42
43 public function init($session_id)
44 {
45 // get current or set default to new session_id
46 $state = self::get_state($this->importer_id);
47 if (!$state) {
48 $state = $this->default($session_id);
49 $this->set_state($this->importer_id, $state);
50 }
51
52 if (!$state || !isset($state['status'])) {
53 throw new \Exception(__("Invalid state", 'jc-importer'));
54 }
55
56 $this->populate($state);
57
58 if (!$this->validate($session_id)) {
59 throw new \Exception(__("Session has changed", 'jc-importer'));
60 }
61 }
62
63
64 public function populate($state_data)
65 {
66 foreach ($state_data as $name => $value) {
67 $this->data[$name] = $value;
68 }
69 }
70
71 public function has_status($status)
72 {
73 if (!isset($this->data['status'])) {
74 return false;
75 }
76
77 if (is_array($status)) {
78 return in_array($this->data['status'], $status);
79 }
80
81 return $this->data['status'] == $status;
82 }
83
84 public function get_session()
85 {
86 return isset($this->data['id']) ? $this->data['id'] : false;
87 }
88
89 public function update($callback)
90 {
91 $raw = [];
92 if (is_callable($callback)) {
93 $raw = call_user_func($callback, $this->data);
94 }
95
96 $this->populate($raw);
97
98 $this->set_state($this->importer_id, $this->data);
99
100 return $this;
101 }
102
103 public function error($error)
104 {
105 $this->update(function ($state) use ($error) {
106 $state['status'] = 'error';
107 if (is_wp_error($error)) {
108 /**
109 * @var \WP_Error $error
110 */
111 $state['message'] = $error->get_error_message();
112 } else if ($error instanceof \Exception) {
113 /**
114 * @var \Exception $error
115 */
116 $state['message'] = $error->getMessage();
117 }
118 return $state;
119 });
120 return $this;
121 }
122
123 public function validate($session_id)
124 {
125 $valid = $this->has_status('init') || $this->get_session() == $session_id;
126 if (!$valid) {
127 Logger::write("state -invalid -check={$session_id} -current={$this->get_session()}");
128 }
129 return $valid;
130 }
131
132 public function get_raw()
133 {
134 return $this->data;
135 }
136
137 public static function wait_for_lock($importer_id, $user, $callback)
138 {
139 $result = null;
140 if (is_callable($callback)) {
141 $result = call_user_func($callback);
142 }
143
144 return $result;
145 }
146
147 /**
148 * Update importer state
149 *
150 * @param mixed $state State to overwrite
151 *
152 * @return void
153 */
154 public static function set_state($id, $state)
155 {
156 $state['updated'] = time();
157 do_action('iwp/' . static::$object_type . '/status/save', $state);
158 self::update_option('iwp_' . static::$object_type . '_state_' . $id, maybe_serialize($state));
159 }
160
161 /**
162 * Get last importer state
163 *
164 * @return array Importer state
165 */
166 public static function get_state($id, $default = false)
167 {
168 $state = self::get_option('iwp_' . static::$object_type . '_state_' . $id);
169 if (!$state) {
170 $state = $default;
171 if ($state !== false) {
172 self::set_state($id, $state);
173 }
174 }
175
176 return $state;
177 }
178
179 public static function get_option($key, $default = false)
180 {
181 /**
182 * @var \WPDB $wpdb
183 */
184 global $wpdb;
185
186 $query = $wpdb->prepare("SELECT option_id as id, option_value as `data` FROM {$wpdb->options} WHERE option_name=%s LIMIT 1", [$key]);
187
188 $result = $wpdb->get_row($query, ARRAY_A);
189 if (!$result) {
190 return $default;
191 }
192
193 if (is_serialized($result['data'])) {
194 return unserialize($result['data']);
195 }
196
197 return $result['data'];
198 }
199
200 public static function update_option($key, $value = '')
201 {
202 /**
203 * @var \WPDB $wpdb
204 */
205 global $wpdb;
206
207 $result = $wpdb->update($wpdb->options, ['option_value' => $value], ['option_name' => $key], ['%s'], ['%s']);
208 if (intval($result) < 1) {
209 $result = $wpdb->insert($wpdb->options, ['option_value' => $value, 'option_name' => $key], ['%s', '%s']);
210 }
211
212 return $result;
213 }
214
215 public static function clear_options($id)
216 {
217
218 // clear existing
219 delete_option('iwp_' . static::$object_type . '_config_' . $id);
220 delete_option('iwp_' . static::$object_type . '_state_' . $id);
221 delete_option('iwp_' . static::$object_type . '_lock_' . $id);
222 delete_option('iwp_' . static::$object_type . '_lock_timestamp_' . $id);
223 delete_option('iwp_' . static::$object_type . '_flag_' . $id);
224
225 /**
226 * @var \WPDB $wpdb
227 */
228 global $wpdb;
229 $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE 'iwp\_" . static::$object_type . "\_state\_" . $id . "\_%'");
230 }
231
232 public function get_section()
233 {
234 if (!isset($this->data['section'])) {
235 return false;
236 }
237
238 return $this->data['section'];
239 }
240
241 public function get_progress($section = null)
242 {
243 if (is_null($section)) {
244 $section = $this->get_section();
245 }
246
247 return $this->data['progress'][$section];
248 }
249
250 function update_importer_stats($stats)
251 {
252 if (!isset($this->data['stats'])) {
253 $this->data['stats'] = [
254 'inserts' => 0,
255 'updates' => 0,
256 'deletes' => 0,
257 'skips' => 0,
258 'errors' => 0,
259 ];
260 }
261
262 $this->data['stats']['inserts'] += $stats['inserts'];
263 $this->data['stats']['updates'] += $stats['updates'];
264 $this->data['stats']['deletes'] += $stats['deletes'];
265 $this->data['stats']['skips'] += $stats['skips'];
266 $this->data['stats']['errors'] += $stats['errors'];
267 }
268
269 function get_stats()
270 {
271 if (!isset($this->data['stats'])) {
272 $this->data['stats'] = [
273 'inserts' => 0,
274 'updates' => 0,
275 'deletes' => 0,
276 'skips' => 0,
277 'errors' => 0,
278 ];
279 }
280
281 return $this->data['stats'];
282 }
283
284 function increment_current_row($section = null)
285 {
286 if (is_null($section)) {
287 $section = $this->get_section();
288 }
289 $this->data['progress'][$section]['current_row']++;
290 }
291
292 static function get_flag($id)
293 {
294 return self::get_option('iwp_' . static::$object_type . '_flag_' . $id);
295 }
296
297 static function is_paused($flag)
298 {
299 return $flag == 'paused';
300 }
301
302 static function is_cancelled($flag)
303 {
304 return $flag == 'cancelled';
305 }
306
307 static function set_paused($id)
308 {
309 self::update_option('iwp_' . static::$object_type . '_flag_' . $id, 'paused');
310 }
311
312 static function set_cancelled($id)
313 {
314 self::update_option('iwp_' . static::$object_type . '_flag_' . $id, 'cancelled');
315 }
316
317 static function clear_flag($id)
318 {
319 self::update_option('iwp_' . static::$object_type . '_flag_' . $id, '');
320 }
321 }
322