New: Display the number of data to migrate

Several fixes following the code review:
- For styles we use Stylus. (http://stylus-lang.com/)
  In Stylus styles curly brackets and terminating semicolons are unnecessary. Indentation is sufficient to denote blocks of styles
  You can also use variables to avoid duplication (e.g. mentioning the same color multiple times)
  Vendor prefixes are not necessary. We use the Nib mixin for Stylus to handle those (http://tj.github.io/nib/)
  => DONE

- `admin.js` bundle is included on all admin pages, so we need to ensure that migration fires only on the migration page and nowhere else.
  You can even create a separate bundle only for migration files, as they won't be necessary on other admin pages.
  => DONE

- MP2MigratorAPI => MP2Migrator endpoint would be just as fine
  => DONE

- For storing migration files, you can use the `Env::$temp_path` path
  => DONE

- `proposeMigration()` the method name disagrees with the comment.
  Comment suggests it tests if migration can be or should be performed
  Method name suggests that it proposes doing the migration (to the user?) - not very clear
  => DONE: the new name is isMigrationNeeded()

  And not only does it test, it may also update the `mailpoet_migration_complete` option, which is confusing and is an unexpected side-effect.
  => DONE

  The migration class itself `MP2Migrator` shouldn't even care about _GET, _REQUEST or _POST arguments. It should only work with what is passed to it.
  => DONE

- In views, please make sure all human-friendly texts use WP's gettext functions for translations (e.g. __('text'))
=> DONE
This commit is contained in:
fred
2017-04-18 19:15:12 +02:00
parent a1ea56f505
commit c984ac7a66
7 changed files with 203 additions and 69 deletions

View File

@ -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

View File

@ -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);

View File

@ -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()
));
}
}
}

View File

@ -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;
}
}

View File

@ -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,

View File

@ -5,18 +5,18 @@
<div class="wrap about-wrap">
<h1><%= __('Welcome to MailPoet') %> <%= settings.version %></h1>
<p class="about-text"><strong>This new version is quite an upgrade.</strong> Since this new version is completely new, we first need to update your database before we begin.</p>
<p class="about-text"><strong><%= __('This new version is quite an upgrade.') %></strong> <%= __('Since this new version is completely new, we first need to update your database before we begin.') %></p>
<div style="position: absolute; top: .2em; right: 0;"><img style="border: 0 !important;" src="<%= image_url('welcome_template/mailpoet-logo.png') %>" alt="<%= __('MailPoet Logo') %>" /></div>
<h3>What will be kept in MailPoet 3</h3>
<h3><%= __('What will be kept in MailPoet 3') %></h3>
<ul>
<li><strong>Subscribers and lists <img draggable="false" class="emoji" alt="✔" src="https://s.w.org/images/core/emoji/2.2.1/svg/2714.svg"></strong></li>
<li>Settings (soon!)</li>
<li>Forms (soon!)</li>
<li>Newsletters (soon!)</li>
<li>Archive of sent newsletters (soon!)</li>
<li>Statistics (soon!)</li>
<li><strong><%= __('Subscribers and lists') %> <img draggable="false" class="emoji" alt="✔" src="https://s.w.org/images/core/emoji/2.2.1/svg/2714.svg"></strong></li>
<li><%= __('Settings') %> (<%= __('soon') %>!)</li>
<li><%= __('Forms') %> (<%= __('soon') %>!)</li>
<li><%= __('Newsletters') %> (<%= __('soon') %>!)</li>
<li><%= __('Archive of sent newsletters') %> (<%= __('soon') %>!)</li>
<li><%= __('Statistics') %> (<%= __('soon') %>!)</li>
</ul>
<div style="position: absolute; top: .2em; right: 0;"></div>
@ -25,9 +25,9 @@
<div class="feature-section one-col">
<br />
<p style="/* text-align: center */">
<input type="submit" name="import" id="import" class="button button-primary" value="Start upgrade" />
<input type="submit" name="stop-import" id="stop-import" class="button button-secondary" value="Pause" style="display: none"/>
<small> <a href="?page=mailpoet-welcome&nomigrate=1">No thanks, I'll skip and start from scratch.</a></small>
<input type="submit" name="import" id="import" class="button button-primary" value="<%= __('Start upgrade') %>" />
<input type="submit" name="stop-import" id="stop-import" class="button button-secondary" value="<%= __('Pause') %>" style="display: none"/>
<small> <a id="skip-import" href="#"><%= __("No thanks, I'll skip and start from scratch.") %></a></small>
</p>
<div id="progressbar" class="mailpoet_progress mailpoet_progress_complete">
@ -35,7 +35,7 @@
</div>
<br /><br />
<small><strong>Log...</strong></small>
<small><strong><%= __('Log...') %></strong></small>
<div id="logger"></div>
</div>
@ -51,8 +51,8 @@
<% block translations %>
<%= localize({
'import_complete' : __( 'IMPORT COMPLETE'),
'importing' : __( 'Importing…'),
'import_stopped_by_user' : __( 'IMPORT STOPPED BY USER'),
'import_complete' : __('IMPORT COMPLETE'),
'importing' : __('Importing…'),
'import_stopped_by_user' : __('IMPORT STOPPED BY USER'),
}) %>
<% endblock %>

View File

@ -159,8 +159,7 @@ config.push(_.extend({}, baseConfig, {
'settings/tabs.js',
'subscribers/importExport/import.js',
'subscribers/importExport/export.js',
'helpscout',
'mp2migrator.js'
'helpscout'
],
form_editor: [
'form_editor/form_editor.js',
@ -239,6 +238,19 @@ config.push(_.extend({}, baseConfig, {
}
}));
// mp2migrator
config.push(_.extend({}, baseConfig, {
name: 'mp2migrator',
entry: {
mp2migrator: [
'mp2migrator.js'
]
},
externals: {
'jquery': 'jQuery'
}
}));
// Test
config.push(_.extend({}, baseConfig, {
name: 'test',