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