diff --git a/assets/js/src/mp2migrator.js b/assets/js/src/mp2migrator.js index 9985004c2b..f38056de2f 100644 --- a/assets/js/src/mp2migrator.js +++ b/assets/js/src/mp2migrator.js @@ -1,187 +1,179 @@ -(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') { +(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 = '' + row + ''; // Mark the errors in red - } - // Test if the import is complete - else if ( row === 'IMPORT COMPLETE' ) { + } + // Test if the import is complete + else if(row === 'IMPORT COMPLETE') { row = '' + row + ''; // Mark the complete message in green $('#action_message').html(MailPoet.I18n.t('import_complete')) - .removeClass('failure').addClass('success'); - } - $("#logger").append(row + "
\n"); + .removeClass('failure').addClass('success'); + } + $("#logger").append(row + "
\n"); - }); - $("#logger").append('' + that.fatal_error + '' + "
\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); - } }); - }, + $("#logger").append('' + that.fatal_error + '' + "
\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(); - /** - * 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(''); - // 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 - }); + // Run the import + MailPoet.Ajax.post({ + endpoint: 'MP2MigratorAPI', + action: 'import', + data: { + } + }).always(function () { that.stop_logger(); - return false; - }, - - }; - - /** - * Actions to run when the DOM is ready - */ - $(function() { - that = mailpoet_import; - - $('#progressbar').progressbar({value : 0}); + 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; + } - // 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() { - - }); + /** + * Actions to run when the DOM is ready + */ + $(function () { + that = mailpoet_import; -})( jQuery ); + $('#progressbar').progressbar({value: 0}); + // Import button + $('#import').click(that.start_import); + + // Stop import button + $('#stop-import').click(that.stop_import); + }); + +})(jQuery); diff --git a/lib/API/Endpoints/MP2MigratorAPI.php b/lib/API/Endpoints/MP2MigratorAPI.php index 3c70d52747..b01fe6b0cc 100644 --- a/lib/API/Endpoints/MP2MigratorAPI.php +++ b/lib/API/Endpoints/MP2MigratorAPI.php @@ -35,7 +35,7 @@ class MP2MigratorAPI extends APIEndpoint { */ public function stopImport($data) { try { - $process = $this->MP2Migrator->stop_import(); + $process = $this->MP2Migrator->stopImport(); return $this->successResponse($process); } catch(\Exception $e) { return $this->errorResponse(array( diff --git a/lib/Config/MP2Migrator.php b/lib/Config/MP2Migrator.php index bb41513ba6..85ea020d2a 100644 --- a/lib/Config/MP2Migrator.php +++ b/lib/Config/MP2Migrator.php @@ -1,4 +1,5 @@ log_file = $upload_dir['basedir'] . '/' . $log_filename; + $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']) ) { + 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') ) { + if(get_option('mailpoet_migration_complete')) { return false; } else { - return $this->table_exists('wysija_campaign'); // Check if the MailPoet 2 tables exist + return $this->tableExists('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) { + private function tableExists($table) { global $wpdb; try { $sql = "SHOW TABLES LIKE '{$wpdb->prefix}{$table}'"; $result = $wpdb->query($sql); return !empty($result); - } catch ( Exception $e ) {} - + } catch (Exception $e) { + // Do nothing + } + return false; } - + /** * Initialize the migration page * */ public function init() { - $this->enqueue_scripts(); + $this->enqueueScripts(); $this->log('INIT'); } - + /** * Register the JavaScript for the admin area. * */ - private function enqueue_scripts() { + private function enqueueScripts() { wp_enqueue_script('jquery-ui-progressbar'); } - /** + /** * Write a message in the log file * * @param string $message */ - public function log($message) { + private function log($message) { file_put_contents($this->log_file, "$message\n", FILE_APPEND); } @@ -87,28 +90,27 @@ class MP2Migrator { */ public function import() { $this->log('START IMPORT'); - update_option('mailpoet_stop_import', false, false); // Reset the stop import action - + update_option('mailpoet_stopImport', 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); + $this->progressbar->setTotalCount(0); + $this->progressbar->setTotalCount(10); + for($i = 0; $i < 10; $i++) { + $this->progressbar->incrementCurrentCount(1); usleep(300000); - if ( $this->import_stopped() ) { + if($this->importStopped()) { return; } } - + $this->log('END IMPORT'); } - + /** * Stop the import * */ - public function stop_import() { - update_option('mailpoet_stop_import', true); + public function stopImport() { + update_option('mailpoet_stopImport', true); $this->log('IMPORT STOPPED BY USER'); } @@ -117,8 +119,8 @@ class MP2Migrator { * * @return boolean Import must stop or not */ - public function import_stopped() { - return get_option('mailpoet_stop_import'); + private function importStopped() { + return get_option('mailpoet_stopImport'); } - + } diff --git a/lib/Config/Menu.php b/lib/Config/Menu.php index 6c27b48590..51606b653d 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->proposeMigration()) { $mp2Migrator->init(); $data = array( 'log_file_url' => $mp2Migrator->log_file_url, diff --git a/lib/Util/ProgressBar.php b/lib/Util/ProgressBar.php index 773242b8b6..3632886732 100644 --- a/lib/Util/ProgressBar.php +++ b/lib/Util/ProgressBar.php @@ -1,97 +1,99 @@ filename = $upload_dir['basedir'] . '/' . $filename; + $this->url = $upload_dir['baseurl'] . '/' . $filename; + $counters = $this->readProgress(); + 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 getUrl() { + return $this->url; + } + + /** + * Read the progress counters + * + * @return array|false Array of counters + */ + private function readProgress() { + 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 setTotalCount($count) { + if($count != $this->total_count) { + $this->total_count = $count; + $this->current_count = 0; + $this->saveProgress(); + } + } + + /** + * Increment the current count + * + * @param int $count Count + */ + public function incrementCurrentCount($count) { + $this->current_count += $count; + $this->saveProgress(); + } + + /** + * Save the progress counters + * + */ + private function saveProgress() { + file_put_contents($this->filename, json_encode(array( + 'total' => $this->total_count, + 'current' => $this->current_count, + ))); + } + + } - /** - * 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, - ))); - - } - } }