| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace ImportWP\Common\Model; |
| 4 | 4 | |
| 5 | +use ImportWP\Common\Filesystem\Filesystem; | |
| 5 | 6 | use ImportWP\Common\Util\Logger; |
| 6 | 7 | |
| 7 | 8 | class ImporterModel |
| 8 | 9 | { |
| @@ -98,8 +99,14 @@ | ||
| 98 | 99 | */ |
| 99 | 100 | protected $settings = []; |
| 100 | 101 | |
| 101 | 102 | /** |
| 103 | + * @var int | |
| 104 | + */ | |
| 105 | + protected $version; | |
| 106 | + protected $version_latest = 2; | |
| 107 | + | |
| 108 | + /** | |
| 102 | 109 | * @var bool |
| 103 | 110 | */ |
| 104 | 111 | protected $debug; |
| 105 | 112 | |
| @@ -133,8 +140,9 @@ | ||
| 133 | 140 | $this->permissions = isset($data['permissions']) ? $data['permissions'] : null; |
| 134 | 141 | $this->start_row = isset($data['settings'], $data['settings']['start_row']) ? $data['settings']['start_row'] : null; |
| 135 | 142 | $this->max_row = isset($data['settings'], $data['settings']['max_row']) ? $data['settings']['max_row'] : null; |
| 136 | 143 | $this->settings = isset($data['settings']) ? $data['settings'] : []; |
| 144 | + $this->version = isset($data['version']) ? $data['version'] : 0; | |
| 137 | 145 | } elseif (!is_null($data)) { |
| 138 | 146 | |
| 139 | 147 | $post = false; |
| 140 | 148 | |
| @@ -150,9 +158,14 @@ | ||
| 150 | 158 | } |
| 151 | 159 | |
| 152 | 160 | if ($post && $post->post_type === IWP_POST_TYPE) { |
| 153 | 161 | |
| 162 | + // Fix broken importer issue. | |
| 154 | 163 | $json = maybe_unserialize($post->post_content, true); |
| 164 | + if (!is_array($json)) { | |
| 165 | + $json = []; | |
| 166 | + } | |
| 167 | + | |
| 155 | 168 | $this->id = $post->ID; |
| 156 | 169 | $this->name = $post->post_title; |
| 157 | 170 | $this->user_id = $post->post_author; |
| 158 | 171 | |
| @@ -180,8 +193,9 @@ | ||
| 180 | 193 | |
| 181 | 194 | $this->start_row = isset($json['settings'], $json['settings']['start_row']) ? $json['settings']['start_row'] : null; |
| 182 | 195 | $this->max_row = isset($json['settings'], $json['settings']['max_row']) ? $json['settings']['max_row'] : null; |
| 183 | 196 | $this->settings = isset($json['settings']) ? $json['settings'] : []; |
| 197 | + $this->version = isset($json['version']) ? $json['version'] : 0; | |
| 184 | 198 | } |
| 185 | 199 | } |
| 186 | 200 | |
| 187 | 201 | Logger::setId($this->getId()); |
| @@ -191,9 +205,13 @@ | ||
| 191 | 205 | { |
| 192 | 206 | $files = $this->getFiles(); |
| 193 | 207 | $file = $this->getFile(); |
| 194 | 208 | if ($view === 'public' && false === $this->debug) { |
| 195 | - $file = basename($file); | |
| 209 | + | |
| 210 | + if ($file) { | |
| 211 | + $file = basename($file); | |
| 212 | + } | |
| 213 | + | |
| 196 | 214 | foreach ($files as &$tmp_file) { |
| 197 | 215 | $tmp_file = basename($tmp_file) . ' - (Added: ' . date(get_option('date_format') . ' \a\t ' . get_option('time_format'), filemtime($tmp_file)) . ')'; |
| 198 | 216 | } |
| 199 | 217 | } else { |
| @@ -231,9 +249,10 @@ | ||
| 231 | 249 | 'datasource' => $datasource_data, |
| 232 | 250 | 'map' => (object) $this->map, |
| 233 | 251 | 'enabled' => (object) $this->enabled, |
| 234 | 252 | 'permissions' => (object) $this->getPermissions(), |
| 235 | - 'settings' => (object) $settings | |
| 253 | + 'settings' => (object) $settings, | |
| 254 | + 'version' => $this->version, | |
| 236 | 255 | ); |
| 237 | 256 | |
| 238 | 257 | if (true === $this->debug) { |
| 239 | 258 | global $wpdb; |
| @@ -256,28 +275,44 @@ | ||
| 256 | 275 | 'start_row' => $this->start_row, |
| 257 | 276 | 'max_row' => $this->max_row, |
| 258 | 277 | ]); |
| 259 | 278 | |
| 279 | + $post_content = array( | |
| 280 | + 'template' => $this->template, | |
| 281 | + 'template_type' => $this->template_type, | |
| 282 | + 'file' => [ | |
| 283 | + 'id' => $this->file_id, | |
| 284 | + 'settings' => $this->file_settings | |
| 285 | + ], | |
| 286 | + 'datasource' => [ | |
| 287 | + 'type' => $this->datasource, | |
| 288 | + 'settings' => $this->getDatasourceSettings() | |
| 289 | + ], | |
| 290 | + 'parser' => $this->parser, | |
| 291 | + 'map' => $this->map, | |
| 292 | + 'enabled' => $this->enabled, | |
| 293 | + 'permissions' => $this->permissions, | |
| 294 | + 'settings' => $settings, | |
| 295 | + 'version' => $this->version | |
| 296 | + ); | |
| 297 | + | |
| 298 | + if (is_null($this->id)) { | |
| 299 | + | |
| 300 | + if ($this->template === 'jet-engine-cct') { | |
| 301 | + | |
| 302 | + // do not force the new permissions interface. | |
| 303 | + | |
| 304 | + } else { | |
| 305 | + | |
| 306 | + // set defaults on new importers | |
| 307 | + $post_content['version'] = $this->version_latest; | |
| 308 | + $post_content['settings']['unique_identifier_type'] = 'field'; | |
| 309 | + } | |
| 310 | + } | |
| 311 | + | |
| 260 | 312 | $postarr = array( |
| 261 | 313 | 'post_title' => $this->name, |
| 262 | - 'post_content' => serialize(array( | |
| 263 | - 'template' => $this->template, | |
| 264 | - 'template_type' => $this->template_type, | |
| 265 | - 'file' => [ | |
| 266 | - 'id' => $this->file_id, | |
| 267 | - 'settings' => $this->file_settings | |
| 268 | - ], | |
| 269 | - 'datasource' => [ | |
| 270 | - 'type' => $this->datasource, | |
| 271 | - 'settings' => $this->getDatasourceSettings() | |
| 272 | - ], | |
| 273 | - 'parser' => $this->parser, | |
| 274 | - 'map' => $this->map, | |
| 275 | - 'enabled' => $this->enabled, | |
| 276 | - 'permissions' => $this->permissions, | |
| 277 | - | |
| 278 | - 'settings' => $settings | |
| 279 | - )), | |
| 314 | + 'post_content' => wp_slash(serialize($post_content)), | |
| 280 | 315 | ); |
| 281 | 316 | |
| 282 | 317 | if (is_null($this->id)) { |
| 283 | 318 | $postarr['post_type'] = IWP_POST_TYPE; |
| @@ -310,9 +345,9 @@ | ||
| 310 | 345 | public function getAllowedFileTypes() |
| 311 | 346 | { |
| 312 | 347 | |
| 313 | 348 | $parser = $this->getParser(); |
| 314 | - $allowed_file_types = apply_filters('iwp/importer/datasource/allowed_file_types', ['xml', 'csv'], $this); | |
| 349 | + $allowed_file_types = apply_filters('iwp/importer/datasource/allowed_file_types', ['xml', 'csv', 'json'], $this); | |
| 315 | 350 | |
| 316 | 351 | if (!is_null($parser)) { |
| 317 | 352 | $allowed_file_types = [$parser]; |
| 318 | 353 | } |
| @@ -358,12 +393,19 @@ | ||
| 358 | 393 | 'base_path' => null, |
| 359 | 394 | 'setup' => false |
| 360 | 395 | ]; |
| 361 | 396 | break; |
| 397 | + case 'json': | |
| 398 | + return [ | |
| 399 | + 'base_path' => null, | |
| 400 | + 'setup' => false | |
| 401 | + ]; | |
| 402 | + break; | |
| 362 | 403 | case 'csv': |
| 363 | 404 | return [ |
| 364 | 405 | 'enclosure' => '"', |
| 365 | 406 | 'delimiter' => ',', |
| 407 | + 'escape' => '\\', | |
| 366 | 408 | 'show_headings' => true, |
| 367 | 409 | 'setup' => false |
| 368 | 410 | ]; |
| 369 | 411 | break; |
| @@ -449,11 +491,20 @@ | ||
| 449 | 491 | foreach ($results as $result) { |
| 450 | 492 | $key = $result['meta_key']; |
| 451 | 493 | $id = intval(str_replace('_importer_file_', '', $key)); |
| 452 | 494 | $value = $result['meta_value']; |
| 453 | - if (file_exists($value)) { | |
| 454 | - $files[$id] = $value; | |
| 495 | + $resolved = Filesystem::resolve_importer_file_path($value); | |
| 496 | + if (!$resolved) { | |
| 497 | + continue; | |
| 455 | 498 | } |
| 499 | + | |
| 500 | + $files[$id] = $resolved; | |
| 501 | + | |
| 502 | + // Soft-migrate legacy absolute paths to uploads-relative storage. | |
| 503 | + $relative = Filesystem::to_uploads_relative_path($resolved); | |
| 504 | + if ($relative !== '' && $relative !== $value) { | |
| 505 | + update_post_meta($this->getId(), $key, $relative); | |
| 506 | + } | |
| 456 | 507 | } |
| 457 | 508 | |
| 458 | 509 | return $files; |
| 459 | 510 | } |
| @@ -550,11 +601,11 @@ | ||
| 550 | 601 | { |
| 551 | 602 | return $this->file_settings; |
| 552 | 603 | } |
| 553 | 604 | |
| 554 | - public function getFileSetting($key) | |
| 605 | + public function getFileSetting($key, $default = null) | |
| 555 | 606 | { |
| 556 | - return isset($this->file_settings[$key]) ? $this->file_settings[$key] : null; | |
| 607 | + return isset($this->file_settings[$key]) ? $this->file_settings[$key] : $default; | |
| 557 | 608 | } |
| 558 | 609 | |
| 559 | 610 | public function setFileSetting($key, $value) |
| 560 | 611 | { |
| @@ -570,8 +621,28 @@ | ||
| 570 | 621 | { |
| 571 | 622 | $this->map[$key] = $value; |
| 572 | 623 | } |
| 573 | 624 | |
| 625 | + /** | |
| 626 | + * Replace the entire field map (skips null values from deleted repeater rows). | |
| 627 | + * | |
| 628 | + * @param array $map | |
| 629 | + */ | |
| 630 | + public function replaceMap($map) | |
| 631 | + { | |
| 632 | + $this->map = []; | |
| 633 | + if (!is_array($map)) { | |
| 634 | + return; | |
| 635 | + } | |
| 636 | + | |
| 637 | + foreach ($map as $key => $value) { | |
| 638 | + if ($value === null) { | |
| 639 | + continue; | |
| 640 | + } | |
| 641 | + $this->map[$key] = $value; | |
| 642 | + } | |
| 643 | + } | |
| 644 | + | |
| 574 | 645 | public function getEnabled() |
| 575 | 646 | { |
| 576 | 647 | return $this->enabled; |
| 577 | 648 | } |
| @@ -642,6 +713,31 @@ | ||
| 642 | 713 | |
| 643 | 714 | public function getUserId() |
| 644 | 715 | { |
| 645 | 716 | return intval($this->user_id) > 0 ? intval($this->user_id) : false; |
| 717 | + } | |
| 718 | + | |
| 719 | + public function getVersion() | |
| 720 | + { | |
| 721 | + return $this->version; | |
| 722 | + } | |
| 723 | + | |
| 724 | + public function get_iwp_reference_meta_key() | |
| 725 | + { | |
| 726 | + return '_iwp_ref_uid'; | |
| 727 | + } | |
| 728 | + | |
| 729 | + public function has_custom_unique_identifier() | |
| 730 | + { | |
| 731 | + return $this->getSetting('unique_identifier_type') === 'custom'; | |
| 732 | + } | |
| 733 | + | |
| 734 | + public function has_field_unique_identifier() | |
| 735 | + { | |
| 736 | + return $this->getSetting('unique_identifier_type') === 'field'; | |
| 737 | + } | |
| 738 | + | |
| 739 | + public function has_legacy_unique_identifier() | |
| 740 | + { | |
| 741 | + return !in_array($this->getSetting('unique_identifier_type'), ['custom', 'field']); | |
| 646 | 742 | } |
| 647 | 743 | } |