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:
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user