Migration from Mailpoet 2 to Mailpoet 3 : phase 0
Interface
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -19,3 +19,4 @@ assets/js/*.js
|
||||
.vagrant
|
||||
lang
|
||||
.mp_svn
|
||||
/nbproject/
|
@ -22,3 +22,5 @@
|
||||
@require 'progress_bar'
|
||||
|
||||
@require 'subscribers'
|
||||
|
||||
@require 'mp2migrator'
|
||||
|
29
assets/css/src/mp2migrator.styl
Normal file
29
assets/css/src/mp2migrator.styl
Normal file
@ -0,0 +1,29 @@
|
||||
#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;
|
||||
}
|
187
assets/js/src/mp2migrator.js
Normal file
187
assets/js/src/mp2migrator.js
Normal file
@ -0,0 +1,187 @@
|
||||
(function( $ ) {
|
||||
'use strict';
|
||||
|
||||
var that;
|
||||
|
||||
var mailpoet_import = {
|
||||
|
||||
fatal_error: '',
|
||||
is_logging: false,
|
||||
|
||||
/**
|
||||
* Start the logger
|
||||
*/
|
||||
start_logger: function() {
|
||||
that.is_logging = true;
|
||||
clearTimeout(that.display_logs_timeout);
|
||||
clearTimeout(that.update_progressbar_timeout);
|
||||
clearTimeout(that.update_wordpress_info_timeout);
|
||||
that.update_display();
|
||||
},
|
||||
|
||||
/**
|
||||
* Stop the logger
|
||||
*/
|
||||
stop_logger: function() {
|
||||
that.is_logging = false;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Update the display
|
||||
*/
|
||||
update_display: function() {
|
||||
that.display_logs();
|
||||
that.update_progressbar();
|
||||
},
|
||||
|
||||
/**
|
||||
* Display the logs
|
||||
*/
|
||||
display_logs: function() {
|
||||
$.ajax({
|
||||
url: objectPlugin.log_file_url,
|
||||
cache: false
|
||||
}).done(function(result) {
|
||||
$('#action_message').html(''); // Clear the action message
|
||||
$("#logger").html('');
|
||||
result.split("\n").forEach(function(row) {
|
||||
if ( row.substr(0, 7) === '[ERROR]' || row.substr(0, 9) === '[WARNING]' || row === 'IMPORT STOPPED BY USER') {
|
||||
row = '<span class="error_msg">' + row + '</span>'; // Mark the errors in red
|
||||
}
|
||||
// Test if the import is complete
|
||||
else if ( row === 'IMPORT COMPLETE' ) {
|
||||
row = '<span class="complete_msg">' + row + '</span>'; // Mark the complete message in green
|
||||
$('#action_message').html(MailPoet.I18n.t('import_complete'))
|
||||
.removeClass('failure').addClass('success');
|
||||
}
|
||||
$("#logger").append(row + "<br />\n");
|
||||
|
||||
});
|
||||
$("#logger").append('<span class="error_msg">' + that.fatal_error + '</span>' + "<br />\n");
|
||||
}).always(function() {
|
||||
if ( that.is_logging ) {
|
||||
that.display_logs_timeout = setTimeout(that.display_logs, 1000);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Update the progressbar
|
||||
*/
|
||||
update_progressbar: function() {
|
||||
$.ajax({
|
||||
url: objectPlugin.progress_url,
|
||||
cache: false,
|
||||
dataType: 'json'
|
||||
}).always(function(result) {
|
||||
// Move the progress bar
|
||||
var progress = Number(result.current) / Number(result.total) * 100;
|
||||
$('#progressbar').progressbar('option', 'value', progress);
|
||||
$('#progresslabel').html(progress + '%');
|
||||
if ( that.is_logging ) {
|
||||
that.update_progressbar_timeout = setTimeout(that.update_progressbar, 1000);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Start the import
|
||||
*
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
start_import: function() {
|
||||
that.fatal_error = '';
|
||||
// Start displaying the logs
|
||||
that.start_logger();
|
||||
|
||||
// Disable the import button
|
||||
that.import_button_label = $('#import').val();
|
||||
$('#import').val(MailPoet.I18n.t('importing')).attr('disabled', 'disabled');
|
||||
// Show the stop button
|
||||
$('#stop-import').show();
|
||||
// Clear the action message
|
||||
$('#action_message').html('');
|
||||
|
||||
// Run the import
|
||||
MailPoet.Ajax.post({
|
||||
endpoint: 'MP2MigratorAPI',
|
||||
action: 'import',
|
||||
data: {
|
||||
}
|
||||
}).always(function() {
|
||||
that.stop_logger();
|
||||
that.update_display(); // Get the latest information after the import was stopped
|
||||
that.reactivate_import_button();
|
||||
}).done(function(response) {
|
||||
if (response) {
|
||||
that.fatal_error = response.data;
|
||||
}
|
||||
}).fail(function(response) {
|
||||
if (response.errors.length > 0) {
|
||||
MailPoet.Notice.error(
|
||||
response.errors.map(function(error) { return error.message; }),
|
||||
{ scroll: true }
|
||||
);
|
||||
}
|
||||
});
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Reactivate the import button
|
||||
*
|
||||
*/
|
||||
reactivate_import_button: function() {
|
||||
$('#import').val(that.import_button_label).removeAttr('disabled');
|
||||
$('#stop-import').hide();
|
||||
},
|
||||
|
||||
/**
|
||||
* Stop import
|
||||
*
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
stop_import: function() {
|
||||
$('#stop-import').attr('disabled', 'disabled');
|
||||
// Stop the import
|
||||
MailPoet.Ajax.post({
|
||||
endpoint: 'MP2MigratorAPI',
|
||||
action: 'stopImport',
|
||||
data: {
|
||||
}
|
||||
}).always(function() {
|
||||
$('#stop-import').removeAttr('disabled'); // Enable the button
|
||||
that.reactivate_import_button();
|
||||
that.update_display(); // Get the latest information after the import was stopped
|
||||
});
|
||||
that.stop_logger();
|
||||
return false;
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Actions to run when the DOM is ready
|
||||
*/
|
||||
$(function() {
|
||||
that = mailpoet_import;
|
||||
|
||||
$('#progressbar').progressbar({value : 0});
|
||||
|
||||
// Import button
|
||||
$('#import').click(that.start_import);
|
||||
|
||||
// Stop import button
|
||||
$('#stop-import').click(that.stop_import);
|
||||
});
|
||||
|
||||
/**
|
||||
* Actions to run when the window is loaded
|
||||
*/
|
||||
$( window ).load(function() {
|
||||
|
||||
});
|
||||
|
||||
})( jQuery );
|
||||
|
47
lib/API/Endpoints/MP2MigratorAPI.php
Normal file
47
lib/API/Endpoints/MP2MigratorAPI.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace MailPoet\API\Endpoints;
|
||||
use MailPoet\API\Endpoint as APIEndpoint;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class MP2MigratorAPI extends APIEndpoint {
|
||||
|
||||
public function __construct() {
|
||||
$this->MP2Migrator = new \MailPoet\Config\MP2Migrator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Import end point
|
||||
*
|
||||
* @param object $data
|
||||
* @return object
|
||||
*/
|
||||
public function import($data) {
|
||||
try {
|
||||
$process = $this->MP2Migrator->import(json_decode($data, true));
|
||||
return $this->successResponse($process);
|
||||
} catch(\Exception $e) {
|
||||
return $this->errorResponse(array(
|
||||
$e->getCode() => $e->getMessage()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop import end point
|
||||
*
|
||||
* @param object $data
|
||||
* @return object
|
||||
*/
|
||||
public function stopImport($data) {
|
||||
try {
|
||||
$process = $this->MP2Migrator->stop_import();
|
||||
return $this->successResponse($process);
|
||||
} catch(\Exception $e) {
|
||||
return $this->errorResponse(array(
|
||||
$e->getCode() => $e->getMessage()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
124
lib/Config/MP2Migrator.php
Normal file
124
lib/Config/MP2Migrator.php
Normal file
@ -0,0 +1,124 @@
|
||||
<?php
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\Util\ProgressBar;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class MP2Migrator {
|
||||
|
||||
private $log_file;
|
||||
public $log_file_url;
|
||||
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;
|
||||
$this->progressbar = new ProgressBar('mp2migration');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the migration is proposed
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
if ( get_option('mailpoet_migration_complete') ) {
|
||||
return false;
|
||||
} else {
|
||||
return $this->table_exists('wysija_campaign'); // Check if the MailPoet 2 tables exist
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a table exists
|
||||
*
|
||||
* @param string $table Table name
|
||||
* @return boolean
|
||||
*/
|
||||
public function table_exists($table) {
|
||||
global $wpdb;
|
||||
|
||||
try {
|
||||
$sql = "SHOW TABLES LIKE '{$wpdb->prefix}{$table}'";
|
||||
$result = $wpdb->query($sql);
|
||||
return !empty($result);
|
||||
} catch ( Exception $e ) {}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the migration page
|
||||
*
|
||||
*/
|
||||
public function init() {
|
||||
$this->enqueue_scripts();
|
||||
$this->log('INIT');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the JavaScript for the admin area.
|
||||
*
|
||||
*/
|
||||
private function enqueue_scripts() {
|
||||
wp_enqueue_script('jquery-ui-progressbar');
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a message in the log file
|
||||
*
|
||||
* @param string $message
|
||||
*/
|
||||
public function log($message) {
|
||||
file_put_contents($this->log_file, "$message\n", FILE_APPEND);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import the data from MailPoet 2
|
||||
*
|
||||
* @return boolean Result
|
||||
*/
|
||||
public function import() {
|
||||
$this->log('START IMPORT');
|
||||
update_option('mailpoet_stop_import', false, false); // Reset the stop import action
|
||||
|
||||
// TODO to remove, for testing only
|
||||
$this->progressbar->set_total_count(0);
|
||||
$this->progressbar->set_total_count(10);
|
||||
for ( $i = 0; $i < 10; $i++ ) {
|
||||
$this->progressbar->increment_current_count(1);
|
||||
usleep(300000);
|
||||
if ( $this->import_stopped() ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->log('END IMPORT');
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the import
|
||||
*
|
||||
*/
|
||||
public function stop_import() {
|
||||
update_option('mailpoet_stop_import', true);
|
||||
$this->log('IMPORT STOPPED BY USER');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the import must stop
|
||||
*
|
||||
* @return boolean Import must stop or not
|
||||
*/
|
||||
public function import_stopped() {
|
||||
return get_option('mailpoet_stop_import');
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\Config\MP2Migrator;
|
||||
use MailPoet\Cron\CronTrigger;
|
||||
use MailPoet\Form\Block;
|
||||
use MailPoet\Form\Renderer as FormRenderer;
|
||||
@ -270,6 +271,17 @@ class Menu {
|
||||
$redirect_url = admin_url('admin.php?page=mailpoet-newsletters');
|
||||
}
|
||||
|
||||
$mp2Migrator = new MP2Migrator();
|
||||
if ($mp2Migrator->proposeMigration()) {
|
||||
$mp2Migrator->init();
|
||||
$data = array(
|
||||
'log_file_url' => $mp2Migrator->log_file_url,
|
||||
'progress_url' => $mp2Migrator->progressbar->url,
|
||||
);
|
||||
$this->displayPage('mp2migration.html', $data);
|
||||
|
||||
} else {
|
||||
|
||||
$data = array(
|
||||
'settings' => Setting::getAll(),
|
||||
'current_user' => wp_get_current_user(),
|
||||
@ -278,6 +290,7 @@ class Menu {
|
||||
);
|
||||
$this->displayPage('welcome.html', $data);
|
||||
}
|
||||
}
|
||||
|
||||
function update() {
|
||||
global $wp;
|
||||
|
97
lib/Util/ProgressBar.php
Normal file
97
lib/Util/ProgressBar.php
Normal file
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
namespace MailPoet\Util;
|
||||
|
||||
use MailPoet\Config\Env;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
if ( !class_exists('ProgressBar', false) ) {
|
||||
|
||||
/**
|
||||
* The Progress Bar class
|
||||
*
|
||||
*/
|
||||
class ProgressBar {
|
||||
|
||||
private $total_count = 0;
|
||||
private $current_count = 0;
|
||||
private $filename;
|
||||
public $url;
|
||||
|
||||
/**
|
||||
* Initialize the class and set its properties.
|
||||
*
|
||||
*/
|
||||
public function __construct($progress_bar_id) {
|
||||
$upload_dir = wp_upload_dir();
|
||||
$filename = Env::$plugin_name . '-' . $progress_bar_id . '-progress.json';
|
||||
$this->filename = $upload_dir['basedir'] . '/' . $filename;
|
||||
$this->url = $upload_dir['baseurl'] . '/' . $filename;
|
||||
$counters = $this->read_progress();
|
||||
if ( isset($counters->total) ) {
|
||||
$this->total_count = $counters->total;
|
||||
}
|
||||
if ( isset($counters->current) ) {
|
||||
$this->current_count = $counters->current;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the progress file URL
|
||||
*
|
||||
* @return string Progress file URL
|
||||
*/
|
||||
public function get_url() {
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the progress counters
|
||||
*
|
||||
* @return array|false Array of counters
|
||||
*/
|
||||
private function read_progress() {
|
||||
if ( file_exists($this->filename) ) {
|
||||
$json_content = file_get_contents($this->filename);
|
||||
return json_decode($json_content);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the total count
|
||||
*
|
||||
* @param int $count Count
|
||||
*/
|
||||
public function set_total_count($count) {
|
||||
if ( $count != $this->total_count ) {
|
||||
$this->total_count = $count;
|
||||
$this->current_count = 0;
|
||||
$this->save_progress();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment the current count
|
||||
*
|
||||
* @param int $count Count
|
||||
*/
|
||||
public function increment_current_count($count) {
|
||||
$this->current_count += $count;
|
||||
$this->save_progress();
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the progress counters
|
||||
*
|
||||
*/
|
||||
private function save_progress() {
|
||||
file_put_contents($this->filename, json_encode(array(
|
||||
'total' => $this->total_count,
|
||||
'current' => $this->current_count,
|
||||
)));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
58
views/mp2migration.html
Normal file
58
views/mp2migration.html
Normal file
@ -0,0 +1,58 @@
|
||||
<% extends 'layout.html' %>
|
||||
|
||||
<% block content %>
|
||||
|
||||
<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>
|
||||
|
||||
<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>
|
||||
<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>
|
||||
</ul>
|
||||
|
||||
<div style="position: absolute; top: .2em; right: 0;"></div>
|
||||
<hr />
|
||||
|
||||
<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>
|
||||
</p>
|
||||
|
||||
<div id="progressbar" class="mailpoet_progress mailpoet_progress_complete">
|
||||
<div id="progresslabel" class="mailpoet_progress_label">0%</div>
|
||||
</div>
|
||||
<br /><br />
|
||||
|
||||
<small><strong>Log...</strong></small>
|
||||
<div id="logger"></div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
var objectPlugin = {
|
||||
log_file_url: '<%= log_file_url %>',
|
||||
progress_url: '<%= progress_url %>'
|
||||
};
|
||||
</script>
|
||||
<% endblock %>
|
||||
|
||||
<% block translations %>
|
||||
<%= localize({
|
||||
'import_complete' : __( 'IMPORT COMPLETE'),
|
||||
'importing' : __( 'Importing…'),
|
||||
'import_stopped_by_user' : __( 'IMPORT STOPPED BY USER'),
|
||||
}) %>
|
||||
<% endblock %>
|
@ -159,7 +159,8 @@ config.push(_.extend({}, baseConfig, {
|
||||
'settings/tabs.js',
|
||||
'subscribers/importExport/import.js',
|
||||
'subscribers/importExport/export.js',
|
||||
'helpscout'
|
||||
'helpscout',
|
||||
'mp2migrator.js'
|
||||
],
|
||||
form_editor: [
|
||||
'form_editor/form_editor.js',
|
||||
|
Reference in New Issue
Block a user