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