PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.0
Import WP – CSV & XML Import Export for WordPress v2.7.0
2.15.2 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 All 145 releases
jc-importer / class / Common / Migration / Migrations.php

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

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