| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Model; |
| 4 |
|
| 5 |
use ImportWP\Common\Util\Logger; |
| 6 |
|
| 7 |
class ExporterModel |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @var int $id |
| 11 |
*/ |
| 12 |
protected $id; |
| 13 |
|
| 14 |
/** |
| 15 |
* @var string $name |
| 16 |
*/ |
| 17 |
protected $name; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var string $type |
| 21 |
*/ |
| 22 |
protected $type; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var string $file_type |
| 26 |
*/ |
| 27 |
protected $file_type; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var string $file_settings |
| 31 |
*/ |
| 32 |
protected $file_settings; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var string $unique_identifier |
| 36 |
*/ |
| 37 |
protected $unique_identifier; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var array $fields |
| 41 |
*/ |
| 42 |
protected $fields; |
| 43 |
|
| 44 |
/** |
| 45 |
* @var array $filters |
| 46 |
*/ |
| 47 |
protected $filters; |
| 48 |
|
| 49 |
/** |
| 50 |
* @var array $cron |
| 51 |
*/ |
| 52 |
protected $cron; |
| 53 |
|
| 54 |
/** |
| 55 |
* @var string |
| 56 |
*/ |
| 57 |
protected $export_method = 'run'; |
| 58 |
|
| 59 |
/** |
| 60 |
* @var bool |
| 61 |
*/ |
| 62 |
protected $debug; |
| 63 |
|
| 64 |
public function __construct($data = null, $debug = false) |
| 65 |
{ |
| 66 |
$this->debug = $debug; |
| 67 |
$this->setup_data($data); |
| 68 |
} |
| 69 |
|
| 70 |
private function setup_data($data) |
| 71 |
{ |
| 72 |
|
| 73 |
if (is_array($data)) { |
| 74 |
|
| 75 |
// fetch data from array |
| 76 |
$this->id = isset($data['id']) && intval($data['id']) > 0 ? intval($data['id']) : null; |
| 77 |
$this->name = $data['name']; |
| 78 |
$this->type = $data['type']; |
| 79 |
$this->file_type = $data['file_type']; |
| 80 |
$this->file_settings = isset($data['file_settings']) ? $data['file_settings'] : []; |
| 81 |
$this->fields = $data['fields']; |
| 82 |
$this->filters = $data['filters']; |
| 83 |
$this->unique_identifier = $data['unique_identifier']; |
| 84 |
$this->export_method = isset($data['export_method']) ? $data['export_method'] : 'run'; |
| 85 |
$this->cron = isset($data['cron']) ? $data['cron'] : []; |
| 86 |
} elseif (!is_null($data)) { |
| 87 |
|
| 88 |
$post = false; |
| 89 |
|
| 90 |
if ($data instanceof \WP_Post) { |
| 91 |
|
| 92 |
// fetch data from post |
| 93 |
$post = $data; |
| 94 |
} elseif (intval($data) > 0) { |
| 95 |
|
| 96 |
// fetch data from id |
| 97 |
$this->id = intval($data); |
| 98 |
$post = get_post($this->id); |
| 99 |
} |
| 100 |
|
| 101 |
if ($post && $post->post_type === EWP_POST_TYPE) { |
| 102 |
|
| 103 |
$json = maybe_unserialize($post->post_content, true); |
| 104 |
$this->id = $post->ID; |
| 105 |
$this->name = $post->post_title; |
| 106 |
$this->type = $json['type']; |
| 107 |
$this->fields = (array) $json['fields']; |
| 108 |
$this->file_type = $json['file_type']; |
| 109 |
$this->file_settings = isset($json['file_settings']) ? $json['file_settings'] : []; |
| 110 |
$this->filters = $json['filters']; |
| 111 |
$this->unique_identifier = $json['unique_identifier']; |
| 112 |
$this->export_method = isset($json['export_method']) ? $json['export_method'] : 'run'; |
| 113 |
$this->cron = isset($json['cron']) ? $json['cron'] : []; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
Logger::setId($this->getId()); |
| 118 |
} |
| 119 |
|
| 120 |
public function data($view = 'public') |
| 121 |
{ |
| 122 |
$result = array( |
| 123 |
'id' => $this->id, |
| 124 |
'name' => '' . $this->getName(), |
| 125 |
'type' => '' . $this->type, |
| 126 |
'file_type' => '' . $this->file_type, |
| 127 |
'file_settings' => $this->file_settings, |
| 128 |
'fields' => $this->fields, |
| 129 |
'filters' => $this->filters, |
| 130 |
'unique_identifier' => '' . $this->unique_identifier, |
| 131 |
'export_method' => '' . $this->export_method, |
| 132 |
'cron' => $this->cron |
| 133 |
); |
| 134 |
|
| 135 |
if (true === $this->debug) { |
| 136 |
global $wpdb; |
| 137 |
$content = $wpdb->get_var($wpdb->prepare("SELECT post_content from {$wpdb->posts} WHERE post_type='%s' AND ID=%d", EWP_POST_TYPE, $this->getId())); |
| 138 |
$result['debug'] = [ |
| 139 |
'settings' => base64_encode($content), |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
return $result; |
| 144 |
} |
| 145 |
|
| 146 |
public function save() |
| 147 |
{ |
| 148 |
// Match what happens in wp-rest. |
| 149 |
remove_filter('content_save_pre', 'wp_filter_post_kses'); |
| 150 |
|
| 151 |
$postarr = array( |
| 152 |
'post_title' => $this->name, |
| 153 |
'post_content' => wp_slash(serialize(array( |
| 154 |
'type' => $this->type, |
| 155 |
'fields' => (array) $this->fields, |
| 156 |
'file_type' => $this->file_type, |
| 157 |
'file_settings' => $this->file_settings, |
| 158 |
'filters' => (array)$this->filters, |
| 159 |
'unique_identifier' => '' . $this->unique_identifier, |
| 160 |
'export_method' => '' . $this->export_method, |
| 161 |
'cron' => $this->cron |
| 162 |
))), |
| 163 |
); |
| 164 |
|
| 165 |
if (is_null($this->id)) { |
| 166 |
$postarr['post_type'] = EWP_POST_TYPE; |
| 167 |
$postarr['post_status'] = 'publish'; |
| 168 |
|
| 169 |
$result = wp_insert_post($postarr, true); |
| 170 |
} else { |
| 171 |
$postarr['ID'] = $this->id; |
| 172 |
$result = wp_update_post($postarr, true); |
| 173 |
} |
| 174 |
|
| 175 |
// Match what happens in wp-rest. |
| 176 |
add_filter('content_save_pre', 'wp_filter_post_kses'); |
| 177 |
|
| 178 |
|
| 179 |
if (!is_wp_error($result)) { |
| 180 |
$this->setup_data($result); |
| 181 |
} |
| 182 |
|
| 183 |
return $result; |
| 184 |
} |
| 185 |
|
| 186 |
public function delete() |
| 187 |
{ |
| 188 |
if (get_post_type($this->getId()) === EWP_POST_TYPE) { |
| 189 |
wp_delete_post($this->getId(), true); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
public function getId() |
| 194 |
{ |
| 195 |
return $this->id; |
| 196 |
} |
| 197 |
|
| 198 |
public function getName() |
| 199 |
{ |
| 200 |
return $this->name; |
| 201 |
} |
| 202 |
|
| 203 |
public function setName($name) |
| 204 |
{ |
| 205 |
$this->name = $name; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* @return string |
| 210 |
*/ |
| 211 |
public function getType() |
| 212 |
{ |
| 213 |
return $this->type; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* @param string $type |
| 218 |
*/ |
| 219 |
public function setType($type) |
| 220 |
{ |
| 221 |
$this->type = $type; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* @return string |
| 226 |
*/ |
| 227 |
public function getFileType() |
| 228 |
{ |
| 229 |
return $this->file_type; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* @param string $file_type |
| 234 |
*/ |
| 235 |
public function setFileType($file_type) |
| 236 |
{ |
| 237 |
$this->file_type = $file_type; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* @return array |
| 242 |
*/ |
| 243 |
public function getFields($public = false) |
| 244 |
{ |
| 245 |
$output = $this->fields; |
| 246 |
|
| 247 |
if (!$public) { |
| 248 |
// nest files, make sure there is a main loop |
| 249 |
if ($this->getFileType() !== 'csv' && !empty($output)) { |
| 250 |
$found = false; |
| 251 |
foreach ($output as $row) { |
| 252 |
if (isset($row['loop']) && $row['loop'] == true && $row['loop'] == 'true' && $row['selection'] == 'main') { |
| 253 |
$found = true; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
if (!$found) { |
| 258 |
foreach ($output as $i => $row) { |
| 259 |
if ($output[$i]['parent'] == 0) { |
| 260 |
$output[$i]['parent'] = 2; |
| 261 |
} else { |
| 262 |
$output[$i]['parent'] = intval($row['id']) + 2; |
| 263 |
} |
| 264 |
|
| 265 |
$output[$i]['id'] = intval($row['id']) + 2; |
| 266 |
} |
| 267 |
|
| 268 |
if ($this->getFileType() === 'xml') { |
| 269 |
$output[] = ['id' => 2, 'parent' => 1, 'selection' => 'main', 'loop' => true, 'label' => 'record']; |
| 270 |
$output[] = ['id' => 1, 'parent' => 0, 'selection' => '', 'loop' => false, 'label' => 'records']; |
| 271 |
} elseif ($this->getFileType() === 'json') { |
| 272 |
$output[] = ['id' => 2, 'parent' => 0, 'selection' => 'main', 'loop' => true, 'label' => 'records']; |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
// make sure there is unique identifier within the main loop / flat file |
| 278 |
|
| 279 |
$unique_identifier = $this->getUniqueIdentifier(); |
| 280 |
|
| 281 |
$found = array_reduce($output, function ($carry, $item) use ($unique_identifier) { |
| 282 |
|
| 283 |
if (isset($item['selection']) && $item['selection'] == $unique_identifier) { |
| 284 |
return true; |
| 285 |
} |
| 286 |
|
| 287 |
return $carry; |
| 288 |
}, false); |
| 289 |
|
| 290 |
if (!$found) { |
| 291 |
switch ($this->getFileType()) { |
| 292 |
case 'csv': |
| 293 |
$output[] = ['id' => -1, 'parent' => 0, 'selection' => $unique_identifier]; |
| 294 |
break; |
| 295 |
default: |
| 296 |
// search fields for main loop and insert. |
| 297 |
$tmp = false; |
| 298 |
foreach ($output as $row) { |
| 299 |
if (isset($row['loop']) && $row['loop'] == true && $row['loop'] == 'true' && $row['selection'] == 'main') { |
| 300 |
$tmp = ['id' => -1, 'parent' => $row['id'], 'selection' => $unique_identifier]; |
| 301 |
break; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
if ($tmp) { |
| 306 |
array_unshift($output, $tmp); |
| 307 |
} |
| 308 |
break; |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
return $output; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* @param array $fields |
| 318 |
*/ |
| 319 |
public function setFields($fields) |
| 320 |
{ |
| 321 |
$this->fields = $fields; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* @return string |
| 326 |
*/ |
| 327 |
public function getUniqueIdentifier() |
| 328 |
{ |
| 329 |
return $this->unique_identifier; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* @param string $unique_identifier |
| 334 |
*/ |
| 335 |
public function setUniqueIdentifier($unique_identifier) |
| 336 |
{ |
| 337 |
$this->unique_identifier = $unique_identifier; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* @param array $filters |
| 342 |
*/ |
| 343 |
public function setFilters($filters) |
| 344 |
{ |
| 345 |
$this->filters = $filters; |
| 346 |
} |
| 347 |
|
| 348 |
public function getFilters() |
| 349 |
{ |
| 350 |
$output = []; |
| 351 |
$filter_data = $this->filters; |
| 352 |
if (!is_array($filter_data)) { |
| 353 |
return $output; |
| 354 |
} |
| 355 |
|
| 356 |
$max_groups = isset($filter_data['filters._index']) ? intval($filter_data['filters._index']) : 0; |
| 357 |
if ($max_groups <= 0) { |
| 358 |
return $output; |
| 359 |
} |
| 360 |
|
| 361 |
for ($i = 0; $i < $max_groups; $i++) { |
| 362 |
$group_data = []; |
| 363 |
$max_rows = isset($filter_data["filters.{$i}._index"]) ? intval($filter_data["filters.{$i}._index"]) : 0; |
| 364 |
if ($max_groups <= 0) { |
| 365 |
continue; |
| 366 |
} |
| 367 |
|
| 368 |
for ($j = 0; $j < $max_rows; $j++) { |
| 369 |
|
| 370 |
$group_data[] = [ |
| 371 |
'left' => isset($filter_data["filters.{$i}.{$j}.left"]) ? $filter_data["filters.{$i}.{$j}.left"] : "", |
| 372 |
'condition' => isset($filter_data["filters.{$i}.{$j}.condition"]) ? $filter_data["filters.{$i}.{$j}.condition"] : "equal", |
| 373 |
'right' => isset($filter_data["filters.{$i}.{$j}.right"]) ? $filter_data["filters.{$i}.{$j}.right"] : "", |
| 374 |
]; |
| 375 |
} |
| 376 |
|
| 377 |
$output[] = $group_data; |
| 378 |
} |
| 379 |
|
| 380 |
return $output; |
| 381 |
} |
| 382 |
|
| 383 |
public function get_status() |
| 384 |
{ |
| 385 |
clean_post_cache($this->getId()); |
| 386 |
$status = get_post_meta($this->getId(), '_ewp_status', true); |
| 387 |
if (!$status) { |
| 388 |
$status = $this->set_status('created'); |
| 389 |
} |
| 390 |
return $status; |
| 391 |
} |
| 392 |
|
| 393 |
public function set_status($status, $counter = 0, $total = 0) |
| 394 |
{ |
| 395 |
|
| 396 |
$progress = $total > 0 ? round(($counter / $total) * 100, 2) : 0; |
| 397 |
$data = array('status' => $status, 'date' => date('Y-m-d H:i:s'), 'progress' => $progress, 'count' => $counter, 'total' => $total); |
| 398 |
update_post_meta($this->getId(), '_ewp_status', $data); |
| 399 |
|
| 400 |
return $data; |
| 401 |
} |
| 402 |
|
| 403 |
public function setExportMethod($method) |
| 404 |
{ |
| 405 |
$this->export_method = $method; |
| 406 |
} |
| 407 |
|
| 408 |
public function getExportMethod() |
| 409 |
{ |
| 410 |
return $this->export_method; |
| 411 |
} |
| 412 |
|
| 413 |
public function setCron($settings) |
| 414 |
{ |
| 415 |
$this->cron = $settings; |
| 416 |
} |
| 417 |
|
| 418 |
public function getCron() |
| 419 |
{ |
| 420 |
return $this->cron; |
| 421 |
} |
| 422 |
|
| 423 |
public function getFileSettings() |
| 424 |
{ |
| 425 |
return $this->file_settings; |
| 426 |
} |
| 427 |
|
| 428 |
public function setFileSettings($settings) |
| 429 |
{ |
| 430 |
$this->file_settings = $settings; |
| 431 |
} |
| 432 |
|
| 433 |
public function setFileSetting($name, $value) |
| 434 |
{ |
| 435 |
$this->file_settings[$name] = $value; |
| 436 |
} |
| 437 |
|
| 438 |
public function getFileSetting($name, $default = null) |
| 439 |
{ |
| 440 |
return isset($this->file_settings[$name]) ? $this->file_settings[$name] : $default; |
| 441 |
} |
| 442 |
} |
| 443 |
|