diff --git a/assets/js/src/mp2migrator.js b/assets/js/src/mp2migrator.js index ad2cc65d80..52a51a522a 100644 --- a/assets/js/src/mp2migrator.js +++ b/assets/js/src/mp2migrator.js @@ -63,6 +63,8 @@ define('mp2migrator', ['mailpoet', 'jquery'], function(MailPoet, jQuery) { jQuery('#progresslabel').html(progress + '%'); if(Number(result.current) !== 0) { jQuery('#skip-import').hide(); + jQuery('#progressbar').show(); + jQuery('#logger-container').show(); } if(MailPoet.MP2Migrator.is_logging) { MailPoet.MP2Migrator.updateProgressbar_timeout = setTimeout(MailPoet.MP2Migrator.updateProgressbar, 1000); diff --git a/lib/Config/MP2Migrator.php b/lib/Config/MP2Migrator.php index 4bce39c324..3f7197c7fc 100644 --- a/lib/Config/MP2Migrator.php +++ b/lib/Config/MP2Migrator.php @@ -161,6 +161,7 @@ class MP2Migrator { $this->importCustomFields(); $this->importSubscribers(); $this->importForms(); + $this->importSettings(); if(!$this->importStopped()) { Setting::setValue('mailpoet_migration_complete', true); @@ -270,14 +271,6 @@ class MP2Migrator { $total_count += $forms_count; $result .= sprintf(_n('%d form', '%d forms', $forms_count, 'mailpoet'), $forms_count) . "\n"; - // TODO to reactivate during the next phases - /* - // Emails - $emails_count = \ORM::for_table(MP2_EMAIL_TABLE)->count(); - $total_count += $emails_count; - $result .= sprintf(_n('%d newsletter', '%d newsletters', $emails_count, 'mailpoet'), $emails_count) . "\n"; - */ - $this->progressbar->setTotalCount($total_count); return $result; @@ -961,4 +954,190 @@ class MP2Migrator { return $mp3_values; } + /** + * Import the settings + * + */ + private function importSettings() { + $encoded_options = get_option('wysija'); + $options = unserialize(base64_decode($encoded_options)); + + // Sender + $sender = Setting::getValue('sender'); + $sender['name'] = isset($options['from_name']) ? $options['from_name'] : ''; + $sender['address'] = isset($options['from_email']) ? $options['from_email'] : ''; + Setting::setValue('sender', $sender); + + // Reply To + $reply_to = Setting::getValue('reply_to'); + $reply_to['name'] = isset($options['replyto_name']) ? $options['replyto_name'] : ''; + $reply_to['address'] = isset($options['replyto_email']) ? $options['replyto_email'] : ''; + Setting::setValue('reply_to', $reply_to); + + // Bounce + $bounce = Setting::getValue('bounce'); + $bounce['address'] = isset($options['bounce_email']) ? $options['bounce_email'] : ''; + Setting::setValue('bounce', $bounce); + + // Notification + $notification = Setting::getValue('notification'); + $notification['address'] = isset($options['emails_notified']) ? $options['emails_notified'] : ''; + Setting::setValue('notification', $notification); + + // Subscribe + $subscribe = Setting::getValue('subscribe'); + $subscribe['on_comment']['enabled'] = isset($options['commentform']) ? $options['commentform'] : '0'; + $subscribe['on_comment']['label'] = isset($options['commentform_linkname']) ? $options['commentform_linkname'] : ''; + $subscribe['on_comment']['segments'] = isset($options['commentform_lists'])? $this->getMappedSegmentIds($options['commentform_lists']) : array(); + $subscribe['on_register']['enabled'] = isset($options['registerform']) ? $options['registerform'] : '0'; + $subscribe['on_register']['label'] = isset($options['registerform_linkname']) ? $options['registerform_linkname'] : ''; + $subscribe['on_register']['segments'] = isset($options['registerform_lists'])? $this->getMappedSegmentIds($options['registerform_lists']) : array(); + Setting::setValue('subscribe', $subscribe); + + // Subscription + $subscription = Setting::getValue('subscription'); + $subscription['pages']['unsubscribe'] = isset($options['unsubscribe_page']) ? $options['unsubscribe_page'] : ''; + $subscription['pages']['confirmation'] = isset($options['confirmation_page']) ? $options['confirmation_page'] : ''; + $subscription['pages']['manage'] = isset($options['subscriptions_page']) ? $options['subscriptions_page'] : ''; + $subscription['segments'] = isset($options['manage_subscriptions_lists'])? $this->getMappedSegmentIds($options['manage_subscriptions_lists']) : array(); + Setting::setValue('subscription', $subscription); + + // Confirmation email + $signup_confirmation = Setting::getValue('signup_confirmation'); + $signup_confirmation['enabled'] = isset($options['confirm_dbleoptin']) && ($options['confirm_dbleoptin'] == 0) ? 0 : 1; + if(isset($options['confirm_email_id'])) { + $confirm_email_id = $options['confirm_email_id']; + $confirm_email = $this->getEmail($confirm_email_id); + if(!empty($confirm_email)) { + $signup_confirmation['from']['name'] = isset($confirm_email['from_name']) ? $confirm_email['from_name'] : ''; + $signup_confirmation['from']['address'] = isset($confirm_email['from_email']) ? $confirm_email['from_email'] : ''; + $signup_confirmation['reply_to']['name'] = isset($confirm_email['replyto_name']) ? $confirm_email['replyto_name'] : ''; + $signup_confirmation['reply_to']['address'] = isset($confirm_email['replyto_email']) ? $confirm_email['replyto_email'] : ''; + $signup_confirmation['subject'] = isset($confirm_email['subject']) ? $confirm_email['subject'] : ''; + $signup_confirmation['body'] = isset($confirm_email['body']) ? $confirm_email['body'] : ''; + } + } + Setting::setValue('signup_confirmation', $signup_confirmation); + + // Analytics + $analytics = Setting::getValue('analytics'); + $analytics['enabled'] = isset($options['analytics']) ? $options['analytics'] : ''; + Setting::setValue('analytics', $analytics); + + // MTA + $mta_group = Setting::getValue('mta_group'); + $mta_group = isset($options['sending_method']) && ($options['sending_method'] == 'smtp') ? 'smtp' : 'website'; + Setting::setValue('mta_group', $mta_group); + + $mta = Setting::getValue('mta'); + $mta['method'] = (isset($options['smtp_host']) && ($options['smtp_host'] == 'smtp.sendgrid.net')) ? 'SendGrid' : (isset($options['sending_method']) && ($options['sending_method'] == 'smtp') ? 'SMTP' : 'PHPMail'); + $sending_emails_number = isset($options['sending_emails_number']) ? $options['sending_emails_number'] : ''; + $sending_emails_each = isset($options['sending_emails_each']) ? $options['sending_emails_each'] : ''; + $mta['frequency']['emails'] = $this->mapFrequencyEmails($sending_emails_number, $sending_emails_each); + $mta['frequency']['interval'] = $this->mapFrequencyInterval($sending_emails_each); + $mta['host'] = isset($options['smtp_host']) ? $options['smtp_host'] : ''; + $mta['port'] = isset($options['smtp_port']) ? $options['smtp_port'] : ''; + $mta['login'] = isset($options['smtp_login']) ? $options['smtp_login'] : ''; + $mta['password'] = isset($options['smtp_password']) ? $options['smtp_password'] : ''; + $mta['encryption'] = isset($options['smtp_secure']) ? $options['smtp_secure'] : ''; + $mta['authentication'] = !isset($options['smtp_auth']) ? '1' : '-1'; + Setting::setValue('mta', $mta); + + // SMTP Provider + if($mta['method'] == 'SendGrid') { + Setting::setValue('smtp_provider', 'SendGrid'); + } + + // Installation date + if(isset($options['installed_time'])) { + $datetime = new \MailPoet\WP\DateTime(); + $installed_at = $datetime->formatTime($options['installed_time'], \MailPoet\WP\DateTime::DEFAULT_DATE_TIME_FORMAT); + Setting::setValue('installed_at', $installed_at); + } + + $this->log(__("Settings imported", 'mailpoet')); + } + + /** + * Get an email + * + * @global object $wpdb + * @param int $email_id + * @return array Email + */ + private function getEmail($email_id) { + global $wpdb; + $email = array(); + + $table = MP2_EMAIL_TABLE; + $sql = " + SELECT e.* + FROM `$table` e + WHERE e.email_id = '$email_id' + "; + $email = $wpdb->get_row($sql, ARRAY_A); + + return $email; + } + + /** + * Map the Email frequency interval + * + * @param string $interval_str Interval + * @return string Interval + */ + private function mapFrequencyInterval($interval_str) { + switch($interval_str) { + case 'one_min': + $interval = 1; + break; + + case 'two_min': + $interval = 2; + break; + + case 'five_min': + $interval = 5; + break; + + case 'ten_min': + $interval = 10; + break; + + default: + $interval = 15; + } + return (string)$interval; + } + + /** + * Map the Email frequency number + * + * @param int $emails_number Emails number + * @param string $interval_str Interval + * @return int Emails number + */ + private function mapFrequencyEmails($emails_number, $interval_str) { + if(empty($emails_number)) { + $emails_number = 70; + } else { + switch($interval_str) { + case 'thirty_min': + $emails_number /= 2; + break; + + case 'hourly': + case '': + $emails_number /= 4; + break; + + case 'two_hours': + $emails_number /= 8; + break; + } + $emails_number = round($emails_number); + } + return $emails_number; + } + } diff --git a/lib/Util/ProgressBar.php b/lib/Util/ProgressBar.php index cd7b2bbfbe..bdaf0d8603 100644 --- a/lib/Util/ProgressBar.php +++ b/lib/Util/ProgressBar.php @@ -65,7 +65,7 @@ if(!class_exists('ProgressBar', false)) { * @param int $count Count */ public function setTotalCount($count) { - if($count != $this->total_count) { + if(($count != $this->total_count) || ($count == 0)) { $this->total_count = $count; $this->current_count = 0; $this->saveProgress(); @@ -93,6 +93,14 @@ if(!class_exists('ProgressBar', false)) { ))); } + /** + * Delete the progress file + * + */ + public function deleteProgressFile() { + unlink($this->filename); + } + } } diff --git a/tests/_support/Helper/Database.php b/tests/_support/Helper/Database.php index 441f73a3f8..0ced865e98 100644 --- a/tests/_support/Helper/Database.php +++ b/tests/_support/Helper/Database.php @@ -13,9 +13,12 @@ class Database extends \Codeception\Module * @param string $filename Filename without extension */ static public function loadSQL($filename) { + global $wpdb; + $db = \ORM::getDb(); $full_filename = Env::$path . '/tests/_data/' . $filename . '.sql'; $sql = file_get_contents($full_filename); + $sql = preg_replace('/`wp_/', '`' . $wpdb->prefix, $sql); // Use the current database prefix $db->exec($sql); } diff --git a/tests/unit/Config/MP2MigratorTest.php b/tests/unit/Config/MP2MigratorTest.php index e7c153bcf8..577527204f 100644 --- a/tests/unit/Config/MP2MigratorTest.php +++ b/tests/unit/Config/MP2MigratorTest.php @@ -16,6 +16,7 @@ class MP2MigratorTest extends MailPoetTest { } public function _after() { + $this->MP2Migrator->progressbar->setTotalCount(0); } /** @@ -497,4 +498,214 @@ class MP2MigratorTest extends MailPoetTest { expect($result)->equals($expected_lists); } + /** + * Test the mapFrequencyInterval function + * + */ + public function testMapFrequencyInterval() { + $result = $this->invokeMethod($this->MP2Migrator, 'mapFrequencyInterval', array('one_min')); + expect($result)->equals(1); + + $result = $this->invokeMethod($this->MP2Migrator, 'mapFrequencyInterval', array('two_min')); + expect($result)->equals(2); + + $result = $this->invokeMethod($this->MP2Migrator, 'mapFrequencyInterval', array('five_min')); + expect($result)->equals(5); + + $result = $this->invokeMethod($this->MP2Migrator, 'mapFrequencyInterval', array('ten_min')); + expect($result)->equals(10); + + $result = $this->invokeMethod($this->MP2Migrator, 'mapFrequencyInterval', array('fifteen_min')); + expect($result)->equals(15); + + $result = $this->invokeMethod($this->MP2Migrator, 'mapFrequencyInterval', array('thirty_min')); + expect($result)->equals(15); + + $result = $this->invokeMethod($this->MP2Migrator, 'mapFrequencyInterval', array('hourly')); + expect($result)->equals(15); + + $result = $this->invokeMethod($this->MP2Migrator, 'mapFrequencyInterval', array('two_hours')); + expect($result)->equals(15); + } + + /** + * Test the importSettings function + * + */ + public function testImportSettings() { + $this->loadMP2OptionsFixtures(); + + $this->invokeMethod($this->MP2Migrator, 'importSettings'); + + $sender = Setting::getValue('sender'); + expect($sender['name'])->equals('Sender'); + expect($sender['address'])->equals('sender@email.com'); + + $reply_to = Setting::getValue('reply_to'); + expect($reply_to['name'])->equals('Reply'); + expect($reply_to['address'])->equals('reply@email.com'); + + $bounce = Setting::getValue('bounce'); + expect($bounce['address'])->equals('bounce@email.com'); + + $notification = Setting::getValue('notification'); + expect($notification['address'])->equals('notification@email.com'); + + $subscribe = Setting::getValue('subscribe'); + expect($subscribe['on_comment']['enabled'])->equals(1); + expect($subscribe['on_comment']['label'])->equals('Oui, ajoutez moi à votre liste de diffusion !!!'); + expect($subscribe['on_register']['enabled'])->equals(1); + expect($subscribe['on_register']['label'])->equals('Oui, ajoutez moi à votre liste de diffusion 2'); + + $subscription = Setting::getValue('subscription'); + expect($subscription['pages']['unsubscribe'])->equals(2); + expect($subscription['pages']['confirmation'])->equals(4); + expect($subscription['pages']['manage'])->equals(4); + + $signup_confirmation = Setting::getValue('signup_confirmation'); + expect($signup_confirmation['enabled'])->equals(1); + + $analytics = Setting::getValue('analytics'); + expect($analytics['enabled'])->equals(1); + + $mta_group = Setting::getValue('mta_group'); + expect($mta_group)->equals('smtp'); + + $mta = Setting::getValue('mta'); + expect($mta['method'])->equals('SMTP'); + expect($mta['frequency']['emails'])->equals(25); + expect($mta['frequency']['interval'])->equals(5); + expect($mta['host'])->equals('smtp.mondomaine.com'); + expect($mta['port'])->equals(25); + expect($mta['login'])->equals('login'); + expect($mta['password'])->equals('password'); + expect($mta['encryption'])->equals('ssl'); + expect($mta['authentication'])->equals(1); + } + + /** + * Load some MP2 fixtures + * + */ + private function loadMP2OptionsFixtures() { + $wysija_options = array ( + 'from_name' => 'Sender', + 'replyto_name' => 'Reply', + 'emails_notified' => 'notification@email.com', + 'from_email' => 'sender@email.com', + 'replyto_email' => 'reply@email.com', + 'default_list_id' => 1, + 'total_subscribers' => '1262', + 'importwp_list_id' => 2, + 'confirm_email_link' => 4, + 'uploadfolder' => '', + 'uploadurl' => '', + 'confirm_email_id' => 2, + 'installed' => true, + 'manage_subscriptions' => 1, + 'installed_time' => 1486319877, + 'wysija_db_version' => '2.7.7', + 'dkim_domain' => 'localhost', + 'wysija_whats_new' => '2.7.10', + 'ignore_msgs' => + array ( + 'ctaupdate' => 1, + ), + 'queue_sends_slow' => 1, + 'emails_notified_when_sub' => 1, + 'emails_notified_when_bounce' => false, + 'emails_notified_when_dailysummary' => 1, + 'bounce_process_auto' => false, + 'ms_bounce_process_auto' => false, + 'sharedata' => false, + 'dkim_active' => false, + 'commentform' => 1, + 'smtp_rest' => false, + 'ms_smtp_rest' => false, + 'debug_log_cron' => false, + 'debug_log_post_notif' => false, + 'debug_log_query_errors' => false, + 'debug_log_queue_process' => false, + 'debug_log_manual' => false, + 'company_address' => 'mon adresse postale', + 'commentform_lists' => + array ( + 0 => '15', + 1 => '3', + ), + 'unsubscribe_page' => '2', + 'confirmation_page' => '4', + 'smtp_host' => 'smtp.mondomaine.com', + 'smtp_login' => 'login', + 'smtp_password' => 'password', + 'smtp_port' => '25', + 'smtp_secure' => 'ssl', + 'test_mails' => 'test@email.com', + 'bounce_email' => 'bounce@email.com', + 'subscriptions_page' => '4', + 'html_source' => '1', + 'industry' => 'e-commerce', + 'archive_linkname' => '[wysija_archive]', + 'subscribers_count_linkname' => '[wysija_subscribers_count]', + 'archive_lists' => + array ( + 0 => '15', + ), + 'commentform_linkname' => 'Oui, ajoutez moi à votre liste de diffusion !!!', + 'registerform' => 1, + 'registerform_linkname' => 'Oui, ajoutez moi à votre liste de diffusion 2', + 'registerform_lists' => + array ( + 0 => '12', + 1 => '11', + 2 => '8', + ), + 'viewinbrowser_linkname' => 'Problèmes d\'affichage ?? [link]Affichez cette newsletter dans votre navigateur.[/link]', + 'unsubscribe_linkname' => 'Se désabonner...', + 'analytics' => '1', + 'subscribers_count_lists' => + array ( + 0 => '15', + ), + 'premium_key' => '', + 'premium_val' => '', + 'last_save' => 1498810541, + 'sending_emails_each' => 'five_min', + 'sending_emails_number' => '25', + 'sending_method' => 'smtp', + 'manage_subscriptions_lists' => + array ( + 0 => '3', + 1 => '12', + 2 => '11', + ), + 'rolescap---administrator---newsletters' => false, + 'rolescap---editor---newsletters' => false, + 'rolescap---author---newsletters' => false, + 'rolescap---contributor---newsletters' => false, + 'rolescap---subscriber---newsletters' => false, + 'rolescap---administrator---subscribers' => false, + 'rolescap---editor---subscribers' => false, + 'rolescap---author---subscribers' => false, + 'rolescap---contributor---subscribers' => false, + 'rolescap---subscriber---subscribers' => false, + 'rolescap---administrator---config' => false, + 'rolescap---editor---config' => false, + 'rolescap---author---config' => false, + 'rolescap---contributor---config' => false, + 'rolescap---subscriber---config' => false, + 'rolescap---administrator---theme_tab' => false, + 'rolescap---editor---theme_tab' => false, + 'rolescap---author---theme_tab' => false, + 'rolescap---contributor---theme_tab' => false, + 'rolescap---subscriber---theme_tab' => false, + 'rolescap---administrator---style_tab' => false, + 'rolescap---editor---style_tab' => false, + 'rolescap---author---style_tab' => false, + 'rolescap---contributor---style_tab' => false, + 'rolescap---subscriber---style_tab' => false, + ); + update_option('wysija', base64_encode(serialize($wysija_options))); + } + } diff --git a/views/mp2migration.html b/views/mp2migration.html index e37edd316e..601a3d9702 100644 --- a/views/mp2migration.html +++ b/views/mp2migration.html @@ -8,13 +8,14 @@

<%= __('This new version is quite an upgrade.') %> <%= __('Since this new version is completely new, we first need to update your database before we begin.') %>

<%= __('What will be kept in MailPoet 3') %>

+

<%= __('All the data from your previous MailPoet will be preserved and left intact, rest assured. Get in touch with us if you have issues with the upgrade process by using the form %s at the bottom right of all your MailPoet pages.') | format('Beacon') | raw %>

-

@@ -28,18 +29,21 @@
-
+
- <%= __('Log...') %> -
+