diff --git a/assets/css/src/mp2migrator.styl b/assets/css/src/mp2migrator.styl index 1cf9893166..f47733152f 100644 --- a/assets/css/src/mp2migrator.styl +++ b/assets/css/src/mp2migrator.styl @@ -1,29 +1,31 @@ -#logger { - width: 100%; - height: 300px; - background-color: #fff; - border: 1px solid #000; - padding: 2px; - overflow: scroll; - resize: both; -} -#progressbar { - width: 50%; - background-color: #d8d8d8; -} -.ui-progressbar .ui-progressbar-value { - height: 100%; - background-color: #fecf23; - background-image: -webkit-linear-gradient(top, #fecf23, #fd9215); - background-image: -moz-linear-gradient(top, #fecf23, #fd9215); - background-image: -o-linear-gradient(top, #fecf23, #fd9215); - background-image: -ms-linear-gradient(top, #fecf23, #fd9215); - background-image: linear-gradient(to bottom, #fecf23, #fd9215); -} -.error_msg { - color: #f00; -} -.complete_msg { - color: #008000; - font-weight: bold; -} +#logger + width: 100% + height: 300px + background-color: #fff + border: 1px solid #000 + padding: 2px + overflow: scroll + resize: both + +#progressbar + width: 50% + background-color: #d8d8d8 + +progressbar_color = #fecf23 +progressbar_gradient_to_color = #fd9215 + +.ui-progressbar .ui-progressbar-value + height: 100% + background-color: progressbar_color + background-image: -webkit-linear-gradient(top, progressbar_color, progressbar_gradient_to_color) + background-image: -moz-linear-gradient(top, progressbar_color, progressbar_gradient_to_color) + background-image: -o-linear-gradient(top, progressbar_color, progressbar_gradient_to_color) + background-image: -ms-linear-gradient(top, progressbar_color, progressbar_gradient_to_color) + background-image: linear-gradient(to bottom, progressbar_color, progressbar_gradient_to_color) + +.error_msg + color: #f00 + +.complete_msg + color: #008000 + font-weight: bold diff --git a/assets/js/src/mp2migrator.js b/assets/js/src/mp2migrator.js index f38056de2f..4695b74b32 100644 --- a/assets/js/src/mp2migrator.js +++ b/assets/js/src/mp2migrator.js @@ -103,7 +103,7 @@ // Run the import MailPoet.Ajax.post({ - endpoint: 'MP2MigratorAPI', + endpoint: 'MP2Migrator', action: 'import', data: { } @@ -118,10 +118,10 @@ }).fail(function (response) { if(response.errors.length > 0) { MailPoet.Notice.error( - response.errors.map(function (error) { - return error.message; - }), - {scroll: true} + response.errors.map(function (error) { + return error.message; + }), + {scroll: true} ); } }); @@ -138,7 +138,7 @@ }, /** - * Stop import + * Stop the import * * @returns {Boolean} */ @@ -146,7 +146,7 @@ $('#stop-import').attr('disabled', 'disabled'); // Stop the import MailPoet.Ajax.post({ - endpoint: 'MP2MigratorAPI', + endpoint: 'MP2Migrator', action: 'stopImport', data: { } @@ -157,6 +157,23 @@ }); that.stop_logger(); return false; + }, + + /** + * Skip the import + * + * @returns {Boolean} + */ + skip_import: function () { + MailPoet.Ajax.post({ + endpoint: 'MP2Migrator', + action: 'skipImport', + data: { + } + }).done(function () { + window.location.reload(); + }); + return false; } }; @@ -174,6 +191,9 @@ // Stop import button $('#stop-import').click(that.stop_import); + + // Skip import link + $('#skip-import').click(that.skip_import); }); })(jQuery); diff --git a/lib/API/Endpoints/MP2MigratorAPI.php b/lib/API/Endpoints/MP2Migrator.php similarity index 70% rename from lib/API/Endpoints/MP2MigratorAPI.php rename to lib/API/Endpoints/MP2Migrator.php index b01fe6b0cc..054e62a7ec 100644 --- a/lib/API/Endpoints/MP2MigratorAPI.php +++ b/lib/API/Endpoints/MP2Migrator.php @@ -4,7 +4,7 @@ use MailPoet\API\Endpoint as APIEndpoint; if(!defined('ABSPATH')) exit; -class MP2MigratorAPI extends APIEndpoint { +class MP2Migrator extends APIEndpoint { public function __construct() { $this->MP2Migrator = new \MailPoet\Config\MP2Migrator(); @@ -44,4 +44,21 @@ class MP2MigratorAPI extends APIEndpoint { } } + /** + * Skip import end point + * + * @param object $data + * @return object + */ + public function skipImport($data) { + try { + $process = $this->MP2Migrator->skipImport(); + return $this->successResponse($process); + } catch(\Exception $e) { + return $this->errorResponse(array( + $e->getCode() => $e->getMessage() + )); + } + } + } diff --git a/lib/Config/MP2Migrator.php b/lib/Config/MP2Migrator.php index 85ea020d2a..894c21987f 100644 --- a/lib/Config/MP2Migrator.php +++ b/lib/Config/MP2Migrator.php @@ -13,23 +13,18 @@ class MP2Migrator { public $progressbar; public function __construct() { - $log_filename = Env::$plugin_name . '-mp2migration.log'; - $upload_dir = wp_upload_dir(); - $this->log_file = $upload_dir['basedir'] . '/' . $log_filename; - $this->log_file_url = $upload_dir['baseurl'] . '/' . $log_filename; + $log_filename = 'mp2migration.log'; + $this->log_file = Env::$temp_path . '/' . $log_filename; + $this->log_file_url = Env::$temp_url . '/' . $log_filename; $this->progressbar = new ProgressBar('mp2migration'); } /** - * Test if the migration is proposed + * Test if the migration is needed * * @return boolean */ - public function proposeMigration() { - if(isset($_REQUEST['nomigrate'])) { - // Store the user's choice if he doesn't want to migrate from MP2 - update_option('mailpoet_migration_complete', true); - } + public function isMigrationNeeded() { if(get_option('mailpoet_migration_complete')) { return false; } else { @@ -37,6 +32,14 @@ class MP2Migrator { } } + /** + * Store the "Skip import" choice + * + */ + public function skipImport() { + update_option('mailpoet_migration_complete', true); + } + /** * Test if a table exists * @@ -71,7 +74,8 @@ class MP2Migrator { * */ private function enqueueScripts() { - wp_enqueue_script('jquery-ui-progressbar'); + wp_register_script('mp2migrator', Env::$assets_url . '/js/mp2migrator.js', array('jquery-ui-progressbar')); + wp_enqueue_script('mp2migrator'); } /** @@ -89,8 +93,13 @@ class MP2Migrator { * @return boolean Result */ public function import() { - $this->log('START IMPORT'); + ob_start(); + $this->emptyLog(); + $this->log(sprintf("=== START IMPORT %s ===", date('Y-m-d H:i:s'))); update_option('mailpoet_stopImport', false, false); // Reset the stop import action + + $this->displayDataToMigrate(); + // TODO to remove, for testing only $this->progressbar->setTotalCount(0); $this->progressbar->setTotalCount(10); @@ -102,16 +111,27 @@ class MP2Migrator { } } - $this->log('END IMPORT'); + $this->log(sprintf("=== END IMPORT %s ===", date('Y-m-d H:i:s'))); + $result = ob_get_contents(); + ob_clean(); + return $result; } + /** + * Empty the log file + * + */ + private function emptyLog() { + file_put_contents($this->log_file, ''); + } + /** * Stop the import * */ public function stopImport() { update_option('mailpoet_stopImport', true); - $this->log('IMPORT STOPPED BY USER'); + $this->log(__('IMPORT STOPPED BY USER', Env::$plugin_name)); } /** @@ -123,4 +143,67 @@ class MP2Migrator { return get_option('mailpoet_stopImport'); } + /** + * Display the number of data to migrate + * + */ + private function displayDataToMigrate() { + $data = $this->getDataToMigrate(); + $this->log($data); + } + + /** + * Get the data to migrate + * + * @return string Data to migrate + */ + private function getDataToMigrate() { + $result = ''; + $totalCount = 0; + + $this->progressbar->setTotalCount(0); + + $result .= __('MailPoet 2 data found:', Env::$plugin_name) . "\n"; + + // Users + $usersCount = $this->rowsCount('wysija_user'); + $totalCount += $usersCount; + $result .= sprintf(_n('%d subscriber', '%d subscribers', $usersCount, Env::$plugin_name), $usersCount) . "\n"; + + // User Lists + $usersListsCount = $this->rowsCount('wysija_user_list'); + $totalCount += $usersListsCount; + $result .= sprintf(_n('%d subscribers list', '%d subscribers lists', $usersListsCount, Env::$plugin_name), $usersListsCount) . "\n"; + + // Emails + $emailsCount = $this->rowsCount('wysija_email'); + $totalCount += $emailsCount; + $result .= sprintf(_n('%d newsletter', '%d newsletters', $emailsCount, Env::$plugin_name), $emailsCount) . "\n"; + + // Forms + $formsCount = $this->rowsCount('wysija_form'); + $totalCount += $formsCount; + $result .= sprintf(_n('%d form', '%d forms', $formsCount, Env::$plugin_name), $formsCount) . "\n"; + + $this->progressbar->setTotalCount($totalCount); + + return $result; + } + + /** + * Count the number of rows in a table + * + * @param string $table Table + * @return int Number of rows found + */ + private function rowsCount($table) { + global $wpdb; + + $table = $wpdb->prefix . $table; + $sql = "SELECT COUNT(*) FROM `$table`"; + $count = $wpdb->get_var($sql); + + return $count; + } + } diff --git a/lib/Config/Menu.php b/lib/Config/Menu.php index 51606b653d..874656535a 100644 --- a/lib/Config/Menu.php +++ b/lib/Config/Menu.php @@ -272,7 +272,7 @@ class Menu { } $mp2Migrator = new MP2Migrator(); - if($mp2Migrator->proposeMigration()) { + if($mp2Migrator->isMigrationNeeded()) { $mp2Migrator->init(); $data = array( 'log_file_url' => $mp2Migrator->log_file_url, diff --git a/views/mp2migration.html b/views/mp2migration.html index c4d3b1c79e..c56013523c 100644 --- a/views/mp2migration.html +++ b/views/mp2migration.html @@ -5,18 +5,18 @@
This new version is quite an upgrade. Since this new version is completely new, we first need to update your database before we begin.
+<%= __('This new version is quite an upgrade.') %> <%= __('Since this new version is completely new, we first need to update your database before we begin.') %>
- - - No thanks, I'll skip and start from scratch. + + + <%= __("No thanks, I'll skip and start from scratch.") %>