PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.14.23
Import WP – CSV & XML Import Export for WordPress v2.14.23
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
jc-importer / class / Common / Migration / Migrations.php

Migrations.php in Import WP – CSV & XML Import Export for WordPress 2.14.23, at class/Common/Migration/Migrations.php

947 lines 38.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImportWP\Common\Migration;
4
5 use ImportWP\Common\Importer\ImporterManager;
6 use ImportWP\Common\Model\ImporterModel;
7 use ImportWP\Common\Util\Logger;
8 use ImportWP\Container;
9
10 class Migrations
11 {
12 private $_version = 0;
13 private $_migrations = array();
14
15 public function __construct()
16 {
17
18 $starting_version = intval(get_option('iwp_db_version', 0));
19 if ($starting_version <= 3 && $starting_version > 0) {
20 // v1 migrations
21 $this->_migrations[] = array($this, 'migration_01');
22 $this->_migrations[] = array($this, 'migration_02');
23 $this->_migrations[] = array($this, 'migration_03');
24 $this->_migrations[] = array($this, 'migration_04_migrate_v1_to_v2_data');
25 } else {
26 // skip v1 migrations
27 $this->_migrations[] = null;
28 $this->_migrations[] = null;
29 $this->_migrations[] = null;
30 $this->_migrations[] = null;
31 }
32
33 // v2 migrations
34 $this->_migrations[] = array($this, 'migration_05_multiple_crons');
35 $this->_migrations[] = array($this, 'migration_06_cron_update');
36 $this->_migrations[] = array($this, 'migration_07_add_session_table');
37 $this->_migrations[] = array($this, 'migration_08_migrate_taxonomy_settings');
38 $this->_migrations[] = array($this, 'migration_09_migrate_attachment_settings');
39
40 $this->_version = count($this->_migrations);
41 }
42
43 public function isSetup()
44 {
45 $version = intval(get_option('iwp_db_version', get_option('jci_db_version', 0)));
46 if ($version < count($this->_migrations)) {
47 return false;
48 }
49 return true;
50 }
51
52 public function install()
53 {
54
55 //run through schema migrations only
56 $this->migrate(false);
57 }
58
59 public function uninstall()
60 {
61 global $wpdb;
62 $wpdb->query("DROP TABLE IF EXISTS `" . $wpdb->prefix . "importer_log`;");
63 $wpdb->query("DROP TABLE IF EXISTS `" . $wpdb->prefix . "importer_files`;");
64 delete_option('iwp_db_version');
65 delete_option('jci_db_version');
66 delete_option('iwp_is_migrating');
67 }
68
69 public function migrate($migrate_data = true)
70 {
71
72 $verion_key = 'iwp_db_version';
73 $version = intval(get_option('iwp_db_version', get_option('jci_db_version', 0)));
74 $migrating = get_option('iwp_is_migrating', 'no');
75 if ('yes' === $migrating) {
76 return;
77 }
78
79 if ($version < count($this->_migrations)) {
80
81 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
82
83 for ($i = 0; $i < count($this->_migrations); $i++) {
84
85 $migration_version = $i + 1;
86 if ($version < $migration_version) {
87
88 update_option('iwp_is_migrating', 'yes');
89
90 set_time_limit(0);
91
92 // Run migration
93 if (!is_null($this->_migrations[$i])) {
94 call_user_func($this->_migrations[$i], $migrate_data);
95 }
96
97 // Flag as migrated
98 update_option($verion_key, $migration_version);
99 update_option('iwp_is_migrating', 'no');
100 }
101 }
102 }
103
104 // update_option('iwp_is_setup', 'yes');
105 }
106
107 public function get_charset()
108 {
109
110 global $wpdb;
111 $charset_collate = "";
112
113 if (!empty($wpdb->charset)) {
114 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
115 }
116 if (!empty($wpdb->collate)) {
117 $charset_collate .= " COLLATE $wpdb->collate";
118 }
119 return $charset_collate;
120 }
121
122 public function migration_01($migrate_data = true)
123 {
124
125 global $wpdb;
126 $charset_collate = $this->get_charset();
127
128 $sql = "CREATE TABLE `" . $wpdb->prefix . "importer_log` (
129 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
130 `importer_name` varchar(255) DEFAULT NULL,
131 `object_id` int(11) DEFAULT NULL,
132 `template` varchar(255) DEFAULT NULL,
133 `type` varchar(255) DEFAULT NULL,
134 `file` varchar(255) DEFAULT NULL,
135 `version` int(11) DEFAULT NULL,
136 `row` int(11) DEFAULT NULL,
137 `src` text,
138 `value` text,
139 `created` datetime DEFAULT NULL,
140 `import_settings` TEXT NULL,
141 `mapped_fields` TEXT NULL,
142 `attachments` TEXT NULL,
143 `taxonomies` TEXT NULL,
144 `parser_settings` TEXT NULL,
145 `template_settings` TEXT NULL,
146 PRIMARY KEY (`id`)
147 ) $charset_collate; ";
148
149 dbDelta($sql);
150
151 $sql = "CREATE TABLE `" . $wpdb->prefix . "importer_files`(
152 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
153 `importer_id` INT(11),
154 `author_id` INT(11),
155 `mime_type` VARCHAR(255),
156 `name` VARCHAR(255),
157 `src` VARCHAR(255),
158 `created` DATETIME,
159 PRIMARY KEY (`id`)
160 ) $charset_collate;";
161 dbDelta($sql);
162 }
163
164 public function migration_02($migrate_data = true)
165 {
166
167 if (!$migrate_data) {
168 return;
169 }
170
171 global $wpdb;
172
173 // return list of importer file links
174 $importer_file_ids = $this->migration_02_get_importer_file_ids();
175
176 // get ids of all importers and fetch all their import files
177 $importers = $wpdb->get_col("SELECT id FROM " . $wpdb->posts . " WHERE post_type = 'jc-imports'");
178
179 //
180 $or_query = '';
181 if (!empty($importers)) {
182 $or_query = "
183 OR (
184 post_type = 'attachment'
185 AND post_parent IN ( " . implode(',', $importers) . ")
186 )
187 ";
188 }
189
190 $results = $wpdb->get_results("
191 SELECT ID, guid, post_parent, post_author, post_mime_type, post_name, post_date
192 FROM " . $wpdb->posts . "
193 WHERE
194 (post_type = 'jc-import-files')
195 " . $or_query . "
196 ");
197
198
199 if (!empty($results)) {
200
201 // print_r($importer_attachments);
202 $upload_dir = wp_upload_dir();
203 $baseurl = $upload_dir['baseurl'];
204 $records = array();
205
206 foreach ($results as $importer) {
207
208 $src = $importer->guid;
209 if (strpos($src, $baseurl) === 0) {
210 $src = substr($src, strlen($baseurl));
211 }
212 // $record = array(
213 $importer_id = $importer->post_parent;
214 $author_id = $importer->post_author;
215 $mime = $importer->post_mime_type;
216 $name = $importer->post_name;
217 $attachment_src = $src;
218 $created = $importer->post_date;
219 // );
220
221 $query_result = $wpdb->query($wpdb->prepare("INSERT INTO `" . $wpdb->prefix . "importer_files`(importer_id, author_id, mime_type, name, src, created) VALUES(%d, %d, %s, %s, %s, %s)", $importer_id, $author_id, $mime, $name, $attachment_src, $created));
222
223 // check if importer_file_id exists in importer settings array
224 if (is_array($importer_file_ids) && array_key_exists($importer->ID, $importer_file_ids)) {
225 $importer_file_ids[$importer->ID] = $wpdb->insert_id;
226 set_transient('jci_db_import_file_ids', $importer_file_ids);
227 }
228
229 if ($query_result) {
230 wp_delete_post($importer->ID, true);
231 }
232 }
233 }
234
235 // loop through all importer_file meta data
236 $importer_settings = $wpdb->get_results("SELECT * FROM `" . $wpdb->prefix . "postmeta` WHERE meta_key='_import_settings'");
237 if ($importer_settings) {
238
239 foreach ($importer_settings as $settings) {
240
241 $post_id = $settings->post_id;
242 $value = maybe_unserialize($settings->meta_value);
243
244 if (is_array($importer_file_ids) && array_key_exists($value['import_file'], $importer_file_ids)) {
245 $value['import_file'] = $importer_file_ids[$value['import_file']];
246 update_post_meta($post_id, '_import_settings', $value);
247 continue;
248 }
249 }
250 }
251 }
252
253 /**
254 * Fetch list of current importer_file id's
255 * @return array
256 */
257 private function migration_02_get_importer_file_ids()
258 {
259 global $wpdb;
260
261 $transient = get_transient('jci_db_import_file_ids');
262 if (get_transient('jci_db_import_file_ids') === false) {
263
264 $importer_files = array();
265
266 $importer_settings = $wpdb->get_results("SELECT * FROM `" . $wpdb->prefix . "postmeta` WHERE meta_key='_import_settings'");
267 if ($importer_settings) {
268
269 foreach ($importer_settings as $settings) {
270 $value = maybe_unserialize($settings->meta_value);
271 $importer_files[$value['import_file']] = null;
272 }
273 }
274 set_transient('jci_db_import_file_ids', $importer_files);
275
276 return $importer_files;
277 } else {
278 return $transient;
279 }
280 }
281
282 /**
283 * Migration 03
284 * Refactor logs table, so duplication of data.
285 *
286 * @since 1.1.0
287 */
288 public function migration_03($migrate_data = true)
289 {
290
291 global $wpdb;
292 $wpdb->query("ALTER TABLE `" . $wpdb->prefix . "importer_log`
293 DROP COLUMN importer_name,
294 DROP COLUMN src,
295 DROP COLUMN template,
296 DROP COLUMN type,
297 DROP COLUMN import_settings,
298 DROP COLUMN mapped_fields,
299 DROP COLUMN attachments,
300 DROP COLUMN taxonomies,
301 DROP COLUMN parser_settings,
302 DROP COLUMN template_settings");
303 }
304
305 public function migration_04_migrate_v1_to_v2_data($migrate_data = true)
306 {
307 global $wpdb;
308 // remove log table
309 // $wpdb->query("DROP TABLE `" . $wpdb->prefix . "importer_log`");
310
311 $query = new \WP_Query([
312 'post_type' => 'jc-imports',
313 'posts_per_page' => -1
314 ]);
315
316 /**
317 * @var ImporterManager $importer_manager
318 */
319 $importer_manager = Container::getInstance()->get('importer_manager');
320
321 if ($query->have_posts()) {
322 foreach ($query->posts as $post) {
323 $v2_id = get_post_meta($post->ID, '_iwp_v2_importer', true);
324 $import_settings = get_post_meta($post->ID, '_import_settings', true);
325 $mapped_fields = get_post_meta($post->ID, '_mapped_fields', true);
326 $attachments = get_post_meta($post->ID, '_attachments', true);
327 $taxonomies = get_post_meta($post->ID, '_taxonomies', true);
328 $parser_settings = get_post_meta($post->ID, '_parser_settings', true);
329 $field_permissions = get_post_meta($post->ID, 'field_permissions', true);
330
331 $import_file_id = $import_settings['import_file'];
332
333
334 $file_result = $wpdb->get_var($wpdb->prepare("SELECT src FROM {$wpdb->prefix}importer_files WHERE id=%d", [$import_file_id]));
335 $filepath = false;
336 if ($file_result) {
337 $wp_upload_dir = wp_upload_dir();
338 $filepath = $wp_upload_dir['basedir'] . $file_result;
339 }
340
341 $post_type = '';
342 $template = $import_settings['template'];
343 if ($template === 'taxonomy') {
344 // Skip taxonomy template as this is now term (same but without the taxonomy column)
345 continue;
346 }
347
348 if ($template === 'page') {
349 $post_type = 'page';
350 } elseif ($template === 'post') {
351 $post_type = 'post';
352 } elseif ($template === 'custom-post-type') {
353 $post_type = $import_settings['general']['custom_post_type'];
354 }
355
356 $settings = [
357 'post_type' => $post_type,
358 'max_row' => intval($import_settings['row_count']) > 0 ? intval($import_settings['row_count']) : '',
359 'start_row' => intval($import_settings['start_line']) > 1 ? intval($import_settings['start_line']) : '',
360 ];
361
362 $converted_mapped_fields = [];
363 $enabled = [];
364 foreach ($mapped_fields as $group => $field_data) {
365 foreach ($field_data as $k => $v) {
366
367 $group_id = $group;
368 if ($group === 'page') {
369 $group_id = 'post';
370 }
371
372 switch ($k) {
373
374 // TODO: Migrate user fields
375 case 'generate_pass':
376 $settings['generate_pass'] = boolval($v) === true ? true : false;
377 break;
378 case 'notify_reg':
379 $settings['notify_users'] = boolval($v) === true ? true : false;
380 break;
381 case 'enable_user_nicename':
382 $enabled[$group_id . '.user_nicename'] = boolval($v) === true ? true : false;
383 break;
384 case 'enable_display_name':
385 $enabled[$group_id . '.display_name'] = boolval($v) === true ? true : false;
386 break;
387 case 'enable_description':
388 $enabled[$group_id . '.description'] = boolval($v) === true ? true : false;
389 break;
390 case 'enable_role':
391 $enabled[$group_id . '.role'] = boolval($v) === true ? true : false;
392 break;
393 case 'enable_pass':
394 $enabled[$group_id . '.user_pass'] = boolval($v) === true ? true : false;
395 break;
396 case 'user_url':
397 if (!empty($v)) {
398 $enabled[$group_id . '.user_url'] = true;
399 $converted_mapped_fields[$group_id . '.' . $k] = $v;
400 }
401 break;
402
403 // post_type
404 case 'post_author':
405 $converted_mapped_fields[$group_id . '._author.post_author'] = $v;
406 break;
407 case 'post_author_field_type':
408 $converted_mapped_fields[$group_id . '._author._author_type'] = $v;
409 break;
410 case 'post_parent':
411 $converted_mapped_fields[$group_id . '._parent.parent'] = $v;
412 break;
413 case 'post_parent_field_type':
414 $converted_mapped_fields[$group_id . '._parent._parent_type'] = $v;
415 break;
416 case 'post_parent_ref':
417 $converted_mapped_fields[$group_id . '._parent._parent_ref'] = $v;
418 break;
419 case 'post_excerpt':
420 $converted_mapped_fields[$group_id . '.' . $k] = $v;
421 if (!empty($v)) {
422 $enabled[$group_id . '.post_excerpt'] = true;
423 }
424 break;
425 case 'post_name':
426 $converted_mapped_fields[$group_id . '.' . $k] = $v;
427 if (!empty($v)) {
428 $enabled[$group_id . '.post_name'] = true;
429 }
430 break;
431 // Enable Fields
432 case 'enable_post_parent':
433 $enabled[$group_id . '._parent'] = boolval($v) === true ? true : false;
434 break;
435 case 'enable_post_status':
436 $enabled[$group_id . '.post_status'] = boolval($v) === true ? true : false;
437 break;
438 case 'enable_post_author':
439 $enabled[$group_id . '._author'] = boolval($v) === true ? true : false;
440 break;
441 case 'enable_menu_order':
442 $enabled[$group_id . '.menu_order'] = boolval($v) === true ? true : false;
443 break;
444 case 'enable_post_password':
445 $enabled[$group_id . '.post_password'] = boolval($v) === true ? true : false;
446 break;
447 case 'enable_post_date':
448 $enabled[$group_id . '.post_date'] = boolval($v) === true ? true : false;
449 break;
450 case 'enable_comment_status':
451 $enabled[$group_id . '.comment_status'] = boolval($v) === true ? true : false;
452 break;
453 case 'enable_ping_status':
454 $enabled[$group_id . '.ping_status'] = boolval($v) === true ? true : false;
455 break;
456 case 'enable_page_template':
457 $enabled[$group_id . '._wp_page_template'] = boolval($v) === true ? true : false;
458 break;
459 default:
460 $converted_mapped_fields[$group_id . '.' . $k] = $v;
461 break;
462 }
463 }
464 }
465
466 if ($taxonomies) {
467 foreach ($taxonomies as $group => $field_data) {
468 foreach ($field_data['tax'] as $row_id => $tax) {
469
470 $term = $field_data['term'][$row_id];
471
472 // TODO: what do we do with permissions
473 $permissions = $field_data['permissions'][$row_id];
474
475 $converted_mapped_fields['taxonomies.' . $row_id . '.tax'] = $tax;
476 $converted_mapped_fields['taxonomies.' . $row_id . '.term'] = $term;
477 }
478 }
479
480 $converted_mapped_fields['taxonomies._index'] = count($taxonomies);
481 }
482
483 if ($attachments) {
484 foreach ($attachments as $group => $field_data) {
485
486 // attachments used to be gloablly set ftp|remote|local
487 $type = $field_data['type'];
488 $ftp_server = $field_data['ftp']['server'];
489 $ftp_user = $field_data['ftp']['user'];
490 $ftp_pass = $field_data['ftp']['pass'];
491 $local_base_path = $field_data['local']['base_path'];
492
493 if ($type === 'url') {
494 $type = 'remote';
495 }
496
497 foreach ($field_data['location'] as $row_id => $location) {
498 $alt = $field_data['alt'][$row_id];
499 $title = $field_data['title'][$row_id];
500 $caption = $field_data['caption'][$row_id];
501 $description = $field_data['description'][$row_id];
502 $permissions = $field_data['permissions'][$row_id];
503 $featured_image = $field_data['featured_image'][$row_id];
504
505 $converted_mapped_fields['attachments.' . $row_id . '.location'] = $location;
506 $converted_mapped_fields['attachments.' . $row_id . '._featured'] = $featured_image == 1 ? 'yes' : 'no';
507 $converted_mapped_fields['attachments.' . $row_id . '._download'] = $type;
508 $converted_mapped_fields['attachments.' . $row_id . '._ftp_host'] = $ftp_server;
509 $converted_mapped_fields['attachments.' . $row_id . '._ftp_user'] = $ftp_user;
510 $converted_mapped_fields['attachments.' . $row_id . '._ftp_pass'] = $ftp_pass;
511 $converted_mapped_fields['attachments.' . $row_id . '._ftp_path'] = '';
512 $converted_mapped_fields['attachments.' . $row_id . '._remote_url'] = '';
513 $converted_mapped_fields['attachments.' . $row_id . '._local_url'] = $local_base_path;
514
515 $converted_mapped_fields['attachments.' . $row_id . '._meta._enabled'] = 'yes';
516 $converted_mapped_fields['attachments.' . $row_id . '._meta._alt'] = $alt;
517 $converted_mapped_fields['attachments.' . $row_id . '._meta._title'] = $title;
518 $converted_mapped_fields['attachments.' . $row_id . '._meta._caption'] = $caption;
519 $converted_mapped_fields['attachments.' . $row_id . '._meta._description'] = $description;
520 }
521 }
522
523 $converted_mapped_fields['attachments._index'] = count($attachments);
524 }
525
526 // custom fields
527 $custom_fields = isset($import_settings['_custom_fields'], $import_settings['_custom_fields'][$template]) ? $import_settings['_custom_fields'][$template] : [];
528 if (!empty($custom_fields)) {
529 $row_id = 0;
530 foreach ($custom_fields as $custom_field) {
531
532 $cf_prefix = 'custom_fields.' . $row_id . '.';
533 $converted_mapped_fields[$cf_prefix . 'key'] = $custom_field['key'];
534 $converted_mapped_fields[$cf_prefix . 'value'] = $custom_field['value'];
535 $converted_mapped_fields[$cf_prefix . '_field_type'] = $custom_field['type'];
536
537 if ('attachment' === $custom_field['type']) {
538 $converted_mapped_fields[$cf_prefix . '_return'] = $custom_field['settings']['attachment_return'];
539
540 $converted_mapped_fields[$cf_prefix . '_ftp_host'] = $custom_field['settings']['attachment_ftp_server'];
541 $converted_mapped_fields[$cf_prefix . '_ftp_user'] = $custom_field['settings']['attachment_ftp_user'];
542 $converted_mapped_fields[$cf_prefix . '_ftp_pass'] = $custom_field['settings']['attachment_ftp_pass'];
543
544
545 $converted_mapped_fields[$cf_prefix . '_ftp_path'] = '';
546 $converted_mapped_fields[$cf_prefix . '_remote_url'] = '';
547 $converted_mapped_fields[$cf_prefix . '_local_url'] = '';
548
549 switch ($custom_field['settings']['attachment_download']) {
550 case 'ftp':
551 $converted_mapped_fields[$cf_prefix . '_download'] = 'ftp';
552 $converted_mapped_fields[$cf_prefix . '_ftp_path'] = $custom_field['settings']['attachment_base_url'];
553 break;
554 case 'url':
555 $converted_mapped_fields[$cf_prefix . '_download'] = 'remote';
556 $converted_mapped_fields[$cf_prefix . '_remote_url'] = $custom_field['settings']['attachment_base_url'];
557 break;
558 case 'local':
559 $converted_mapped_fields[$cf_prefix . '_download'] = 'local';
560 $converted_mapped_fields[$cf_prefix . '_local_url'] = $custom_field['settings']['attachment_base_url'];
561 break;
562 }
563
564 $converted_mapped_fields[$cf_prefix . '_enabled'] = 'no';
565 $converted_mapped_fields[$cf_prefix . '_alt'] = '';
566 $converted_mapped_fields[$cf_prefix . '_title'] = '';
567 $converted_mapped_fields[$cf_prefix . '_caption'] = '';
568 $converted_mapped_fields[$cf_prefix . '_description'] = '';
569 }
570 $row_id++;
571 }
572
573 $converted_mapped_fields['custom_fields._index'] = $row_id;
574 }
575
576 // TODO: Migrate cron
577 // _jci_cron_enabled: yes
578 // _jci_cron_minutes: 60
579 // _jci_cron_last_ran: 1579960020
580 // _cron_last_updated: 1579960020
581
582 $cron_enabled = get_post_meta($post->ID, '_jci_cron_enabled', true);
583 if ($cron_enabled === 'yes') {
584 $settings['import_method'] = 'schedule';
585 $settings['cron_day'] = 0;
586 $settings['cron_hour'] = 0;
587 $settings['cron_minute'] = 0;
588
589 $minutes = intval(get_post_meta($post->ID, '_jci_cron_minutes', true));
590 if ($minutes < 60) {
591 // Hourly
592 $settings['cron_schedule'] = 'hour';
593 } elseif ($minutes < 60 * 24) {
594 $settings['cron_schedule'] = 'day';
595 } elseif ($minutes < 60 * 24 * 7) {
596 $settings['cron_schedule'] = 'week';
597 } else {
598 $settings['cron_schedule'] = 'month';
599 }
600 } else {
601 $settings['import_method'] = 'run';
602 }
603
604
605
606 $importer_model_data = [
607 'name' => $post->post_title,
608 'template' => $import_settings['template'],
609 'template_type' => '',
610 'parser' => $import_settings['template_type'],
611 'permissions' => [
612 'create' => [
613 'enabled' => $import_settings['permissions']['create'] === 1 ? true : false,
614 'type' => $field_permissions['create_type'],
615 'fields' => $field_permissions['create_fields']
616 ],
617 'update' => [
618 'enabled' => $import_settings['permissions']['update'] === 1 ? true : false,
619 'type' => $field_permissions['update_type'],
620 'fields' => $field_permissions['update_fields']
621 ],
622 'remove' => [
623 'enabled' => $import_settings['permissions']['delete'] === 1 ? true : false
624 ],
625 ],
626 'datasource' => [
627 'type' => $import_settings['import_type'],
628 'settings' => [
629 'remoute_url' => '',
630 'local_url' => ''
631 ]
632 ],
633 'settings' => $settings,
634 'file' => [
635 'id' => null,
636 'settings' => [
637 'count' => 0,
638 'processed' => false,
639 'setup' => false,
640 // csv
641 'delimiter' => stripslashes($parser_settings['csv_delimiter']),
642 'enclosure' => stripslashes($parser_settings['csv_enclosure']),
643 'show_headings' => false,
644 // xml
645 'base_path' => $parser_settings['import_base'],
646 'nodes' => [],
647 ]
648 ],
649 'map' => $converted_mapped_fields,
650 'enabled' => $enabled
651 ];
652
653 if (intval($v2_id) > 0) {
654 $importer_model_data['id'] = intval($v2_id);
655 }
656
657 $importer_model = new ImporterModel($importer_model_data);
658 $result = $importer_model->save();
659
660 if (!is_wp_error($result)) {
661
662 // clear existing importer files
663 $query = "DELETE FROM {$wpdb->postmeta} WHERE post_id={$result} AND meta_key LIKE '_importer_file%'";
664 $wpdb->query($query);
665
666 if ($filepath && file_exists($filepath)) {
667 $file_id = $importer_manager->link_importer_file($result, $filepath);
668 $importer_model->setFileId($file_id);
669 $importer_model->save();
670 }
671
672 update_post_meta($result, '_iwp_v1_importer', $post->ID);
673 update_post_meta($post->ID, '_iwp_v2_importer', $result);
674 }
675 }
676 }
677 }
678
679 public function migration_05_multiple_crons()
680 {
681
682 /**
683 * @var \wpdb $wpdb
684 */
685 global $wpdb;
686
687 // TODO: loop through serialsed post_content, switching from single cron to array
688 $importers = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE post_type='" . IWP_POST_TYPE . "'", ARRAY_A);
689 foreach ($importers as $importer) {
690 $id = $importer['ID'];
691 $data = unserialize($importer['post_content']);
692 if ($data['settings']['import_method'] !== 'schedule') {
693 continue;
694 }
695
696 $cron = [[
697 'setting_cron_schedule' => $data['settings']['cron_schedule'],
698 'setting_cron_day' => $data['settings']['cron_day'],
699 'setting_cron_hour' => $data['settings']['cron_hour'],
700 'setting_cron_minute' => $data['settings']['cron_minute'],
701 'setting_cron_disabled' => $data['settings']['cron_disabled'],
702 ]];
703
704 unset($data['settings']['cron_schedule']);
705 unset($data['settings']['cron_day']);
706 unset($data['settings']['cron_hour']);
707 unset($data['settings']['cron_minute']);
708 unset($data['settings']['cron_disabled']);
709
710 $data['settings']['cron'] = $cron;
711
712 remove_filter('content_save_pre', 'wp_filter_post_kses');
713 wp_update_post(['ID' => $id, 'post_content' => serialize($data)]);
714 add_filter('content_save_pre', 'wp_filter_post_kses');
715 }
716 }
717
718 public function migration_06_cron_update()
719 {
720 wp_unschedule_hook('iwp_runner');
721
722 /**
723 * @var \wpdb $wpdb
724 */
725 global $wpdb;
726
727 // TODO: loop through serialsed post_content, switching from single cron to array
728 $importers = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE post_type='" . IWP_POST_TYPE . "'", ARRAY_A);
729 foreach ($importers as $importer) {
730
731 $id = $importer['ID'];
732 $data = unserialize($importer['post_content']);
733 if ($data['settings']['import_method'] !== 'schedule') {
734 continue;
735 }
736
737 delete_post_meta($id, '_iwp_session');
738 delete_post_meta($id, '_iwp_cron_updated');
739 delete_post_meta($id, '_iwp_cron_status');
740 delete_post_meta($id, '_iwp_cron_version');
741 wp_update_post(['ID' => $id, 'post_excerpt' => '']);
742
743 Logger::write(__CLASS__ . '::migration_06_cron_update -rest', $id);
744 }
745 }
746
747 public function migration_07_add_session_table($migrate_data = true)
748 {
749 /**
750 * @var \WPDB $wpdb
751 */
752 global $wpdb;
753 $charset_collate = $this->get_charset();
754
755 $sql = "CREATE TABLE `" . $wpdb->prefix . "iwp_sessions` (
756 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
757 `site_id` int(11) DEFAULT NULL,
758 `importer_id` int(11) DEFAULT NULL,
759 `item_id` int(11) DEFAULT NULL,
760 `item_type` varchar(255) DEFAULT NULL,
761 `session` varchar(255) DEFAULT NULL,
762 PRIMARY KEY (`id`)
763 ) $charset_collate; ";
764
765 dbDelta($sql);
766
767 if (!$migrate_data) {
768 return;
769 }
770
771 // Migrate post sessions
772 $posts = $wpdb->get_results("SELECT {$wpdb->postmeta}.*, {$wpdb->posts}.post_type FROM {$wpdb->postmeta} INNER JOIN {$wpdb->posts} ON {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID WHERE meta_key LIKE '\_iwp\_session\_%'", ARRAY_A);
773 if (!empty($posts)) {
774 foreach ($posts as $post) {
775
776 if (preg_match('/_iwp_session_(\d+)/', $post['meta_key'], $matches) !== 1) {
777 continue;
778 }
779
780 $data = [
781 'importer_id' => $matches[1],
782 'item_id' => $post['post_id'],
783 'item_type' => 'pt-' . $post['post_type'],
784 'session' => $post['meta_value']
785 ];
786 $format = ['%d', '%d', '%s', '%s'];
787
788 // TODO: is this needed as blog_id is more relevant since there is a sessions table per site?
789 if (is_multisite()) {
790 $data['site_id'] = $wpdb->siteid;
791 $format[] = '%d';
792 }
793
794 $wpdb->insert($wpdb->prefix . 'iwp_sessions', $data, $format);
795 }
796 }
797
798 // Migrate term sessions
799 $terms = $wpdb->get_results("SELECT {$wpdb->termmeta}.*, {$wpdb->term_taxonomy}.taxonomy FROM {$wpdb->termmeta} INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->termmeta}.term_id = {$wpdb->term_taxonomy}.term_id WHERE meta_key LIKE '\_iwp\_session\_%'", ARRAY_A);
800 if (!empty($terms)) {
801 foreach ($terms as $post) {
802
803 if (preg_match('/_iwp_session_(\d+)/', $post['meta_key'], $matches) !== 1) {
804 continue;
805 }
806
807 $data = [
808 'importer_id' => $matches[1],
809 'item_id' => $post['term_id'],
810 'item_type' => 't-' . $post['taxonomy'],
811 'session' => $post['meta_value']
812 ];
813 $format = ['%d', '%d', '%s', '%s'];
814
815 // TODO: is this needed as blog_id is more relevant since there is a sessions table per site?
816 if (is_multisite()) {
817 $data['site_id'] = $wpdb->siteid;
818 $format[] = '%d';
819 }
820
821 $wpdb->insert($wpdb->prefix . 'iwp_sessions', $data, $format);
822 }
823 }
824
825 // Migrate user sessions
826 $users = $wpdb->get_results("SELECT * FROM {$wpdb->usermeta} WHERE meta_key LIKE '\_iwp\_session\_%'", ARRAY_A);
827 if (!empty($users)) {
828 foreach ($users as $post) {
829
830 if (preg_match('/_iwp_session_(\d+)/', $post['meta_key'], $matches) !== 1) {
831 continue;
832 }
833
834 $data = [
835 'importer_id' => $matches[1],
836 'item_id' => $post['user_id'],
837 'item_type' => 'user',
838 'session' => $post['meta_value']
839 ];
840 $format = ['%d', '%d', '%s', '%s'];
841
842 // TODO: is this needed as blog_id is more relevant since there is a sessions table per site?
843 if (is_multisite()) {
844 $data['site_id'] = $wpdb->siteid;
845 $format[] = '%d';
846 }
847
848 $wpdb->insert($wpdb->prefix . 'iwp_sessions', $data, $format);
849 }
850 }
851 }
852
853 public function migration_08_migrate_taxonomy_settings($migrate_data = true)
854 {
855 /**
856 * @var \wpdb $wpdb
857 */
858 global $wpdb;
859
860 // TODO: loop through serialsed post_content, switching from single cron to array
861 $importers = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE post_type='" . IWP_POST_TYPE . "'", ARRAY_A);
862
863 foreach ($importers as $importer) {
864
865 $data = maybe_unserialize($importer['post_content']);
866 $modified = false;
867
868 $tmp = [];
869 foreach ($data['map'] as $field_id => $field_value) {
870 $count = 0;
871 $tmp[preg_replace('/^(taxonomies\.\d+\.)(_.*?)$/', '$1settings.$2', $field_id, -1, $count)] = $field_value;
872 if ($count > 0) {
873 $modified = true;
874 }
875 }
876
877 if (!$modified) {
878 continue;
879 }
880
881 $data['map'] = $tmp;
882
883
884 remove_filter('content_save_pre', 'wp_filter_post_kses');
885 wp_update_post(['ID' => $importer['ID'], 'post_content' => serialize($data)]);
886 add_filter('content_save_pre', 'wp_filter_post_kses');
887 }
888 }
889
890 // TODO: do we need this? can we get around this with manipulating the data if it exists?
891 public function migration_09_migrate_attachment_settings($migrate_data = true)
892 {
893 /**
894 * @var \wpdb $wpdb
895 */
896 global $wpdb;
897
898 $importers = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE post_type='" . IWP_POST_TYPE . "'", ARRAY_A);
899
900 foreach ($importers as $importer) {
901
902 $data = maybe_unserialize($importer['post_content']);
903 $modified = false;
904
905 $tmp = [];
906 foreach ($data['map'] as $field_id => $field_value) {
907 $count = 0;
908
909 $ends_with = [
910 '_download',
911 '_enable_image_hash',
912 '_featured',
913 '_ftp_host',
914 '_ftp_pass',
915 '_ftp_path',
916 '_ftp_user',
917 '_local_url',
918 '_meta\._alt',
919 '_meta\._caption',
920 '_meta\._description',
921 '_meta\._enabled',
922 '_meta\._title',
923 '_remote_url',
924 '_return'
925 ];
926
927 $tmp[preg_replace('/^(.+)(?<!\.settings)\.(' . implode('|', $ends_with) . ')$/', '$1.settings.$2', $field_id, -1, $count)] = $field_value;
928
929 if ($count > 0) {
930 $modified = true;
931 }
932 }
933
934 if (!$modified) {
935 continue;
936 }
937
938 $data['map'] = $tmp;
939
940
941 remove_filter('content_save_pre', 'wp_filter_post_kses');
942 wp_update_post(['ID' => $importer['ID'], 'post_content' => serialize($data)]);
943 add_filter('content_save_pre', 'wp_filter_post_kses');
944 }
945 }
946 }
947