| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Exporter\State; |
| 4 |
|
| 5 |
use ImportWP\Common\Util\Logger; |
| 6 |
|
| 7 |
class ExporterState |
| 8 |
{ |
| 9 |
private $data = []; |
| 10 |
private $exporter_id; |
| 11 |
protected static $object_type = 'exporter'; |
| 12 |
|
| 13 |
public function __construct($id, $user = '') |
| 14 |
{ |
| 15 |
$this->exporter_id = $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' => 'export', |
| 28 |
'progress' => [ |
| 29 |
'export' => [ |
| 30 |
'start' => 0, |
| 31 |
'end' => 0, |
| 32 |
'current_row' => 0, |
| 33 |
] |
| 34 |
], |
| 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, ''); |
| 314 |
} |
| 315 |
} |
| 316 |
|