Coding standard fixes

This commit is contained in:
fred
2017-04-15 19:35:52 +02:00
parent dd7f959731
commit a1ea56f505
5 changed files with 294 additions and 298 deletions

View File

@ -1,17 +1,16 @@
(function( $ ) { (function ($) {
'use strict'; 'use strict';
var that; var that;
var mailpoet_import = { var mailpoet_import = {
fatal_error: '', fatal_error: '',
is_logging: false, is_logging: false,
/** /**
* Start the logger * Start the logger
*/ */
start_logger: function() { start_logger: function () {
that.is_logging = true; that.is_logging = true;
clearTimeout(that.display_logs_timeout); clearTimeout(that.display_logs_timeout);
clearTimeout(that.update_progressbar_timeout); clearTimeout(that.update_progressbar_timeout);
@ -22,15 +21,14 @@
/** /**
* Stop the logger * Stop the logger
*/ */
stop_logger: function() { stop_logger: function () {
that.is_logging = false; that.is_logging = false;
}, },
/** /**
* Update the display * Update the display
*/ */
update_display: function() { update_display: function () {
that.display_logs(); that.display_logs();
that.update_progressbar(); that.update_progressbar();
}, },
@ -38,19 +36,19 @@
/** /**
* Display the logs * Display the logs
*/ */
display_logs: function() { display_logs: function () {
$.ajax({ $.ajax({
url: objectPlugin.log_file_url, url: objectPlugin.log_file_url,
cache: false cache: false
}).done(function(result) { }).done(function (result) {
$('#action_message').html(''); // Clear the action message $('#action_message').html(''); // Clear the action message
$("#logger").html(''); $("#logger").html('');
result.split("\n").forEach(function(row) { result.split("\n").forEach(function (row) {
if ( row.substr(0, 7) === '[ERROR]' || row.substr(0, 9) === '[WARNING]' || row === 'IMPORT STOPPED BY USER') { 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 row = '<span class="error_msg">' + row + '</span>'; // Mark the errors in red
} }
// Test if the import is complete // Test if the import is complete
else if ( row === 'IMPORT COMPLETE' ) { else if(row === 'IMPORT COMPLETE') {
row = '<span class="complete_msg">' + row + '</span>'; // Mark the complete message in green row = '<span class="complete_msg">' + row + '</span>'; // Mark the complete message in green
$('#action_message').html(MailPoet.I18n.t('import_complete')) $('#action_message').html(MailPoet.I18n.t('import_complete'))
.removeClass('failure').addClass('success'); .removeClass('failure').addClass('success');
@ -59,8 +57,8 @@
}); });
$("#logger").append('<span class="error_msg">' + that.fatal_error + '</span>' + "<br />\n"); $("#logger").append('<span class="error_msg">' + that.fatal_error + '</span>' + "<br />\n");
}).always(function() { }).always(function () {
if ( that.is_logging ) { if(that.is_logging) {
that.display_logs_timeout = setTimeout(that.display_logs, 1000); that.display_logs_timeout = setTimeout(that.display_logs, 1000);
} }
}); });
@ -69,17 +67,17 @@
/** /**
* Update the progressbar * Update the progressbar
*/ */
update_progressbar: function() { update_progressbar: function () {
$.ajax({ $.ajax({
url: objectPlugin.progress_url, url: objectPlugin.progress_url,
cache: false, cache: false,
dataType: 'json' dataType: 'json'
}).always(function(result) { }).always(function (result) {
// Move the progress bar // Move the progress bar
var progress = Number(result.current) / Number(result.total) * 100; var progress = Number(result.current) / Number(result.total) * 100;
$('#progressbar').progressbar('option', 'value', progress); $('#progressbar').progressbar('option', 'value', progress);
$('#progresslabel').html(progress + '%'); $('#progresslabel').html(progress + '%');
if ( that.is_logging ) { if(that.is_logging) {
that.update_progressbar_timeout = setTimeout(that.update_progressbar, 1000); that.update_progressbar_timeout = setTimeout(that.update_progressbar, 1000);
} }
}); });
@ -90,7 +88,7 @@
* *
* @returns {Boolean} * @returns {Boolean}
*/ */
start_import: function() { start_import: function () {
that.fatal_error = ''; that.fatal_error = '';
// Start displaying the logs // Start displaying the logs
that.start_logger(); that.start_logger();
@ -109,19 +107,21 @@
action: 'import', action: 'import',
data: { data: {
} }
}).always(function() { }).always(function () {
that.stop_logger(); that.stop_logger();
that.update_display(); // Get the latest information after the import was stopped that.update_display(); // Get the latest information after the import was stopped
that.reactivate_import_button(); that.reactivate_import_button();
}).done(function(response) { }).done(function (response) {
if (response) { if(response) {
that.fatal_error = response.data; that.fatal_error = response.data;
} }
}).fail(function(response) { }).fail(function (response) {
if (response.errors.length > 0) { if(response.errors.length > 0) {
MailPoet.Notice.error( MailPoet.Notice.error(
response.errors.map(function(error) { return error.message; }), response.errors.map(function (error) {
{ scroll: true } return error.message;
}),
{scroll: true}
); );
} }
}); });
@ -132,7 +132,7 @@
* Reactivate the import button * Reactivate the import button
* *
*/ */
reactivate_import_button: function() { reactivate_import_button: function () {
$('#import').val(that.import_button_label).removeAttr('disabled'); $('#import').val(that.import_button_label).removeAttr('disabled');
$('#stop-import').hide(); $('#stop-import').hide();
}, },
@ -142,7 +142,7 @@
* *
* @returns {Boolean} * @returns {Boolean}
*/ */
stop_import: function() { stop_import: function () {
$('#stop-import').attr('disabled', 'disabled'); $('#stop-import').attr('disabled', 'disabled');
// Stop the import // Stop the import
MailPoet.Ajax.post({ MailPoet.Ajax.post({
@ -150,24 +150,24 @@
action: 'stopImport', action: 'stopImport',
data: { data: {
} }
}).always(function() { }).always(function () {
$('#stop-import').removeAttr('disabled'); // Enable the button $('#stop-import').removeAttr('disabled'); // Enable the button
that.reactivate_import_button(); that.reactivate_import_button();
that.update_display(); // Get the latest information after the import was stopped that.update_display(); // Get the latest information after the import was stopped
}); });
that.stop_logger(); that.stop_logger();
return false; return false;
}, }
}; };
/** /**
* Actions to run when the DOM is ready * Actions to run when the DOM is ready
*/ */
$(function() { $(function () {
that = mailpoet_import; that = mailpoet_import;
$('#progressbar').progressbar({value : 0}); $('#progressbar').progressbar({value: 0});
// Import button // Import button
$('#import').click(that.start_import); $('#import').click(that.start_import);
@ -176,12 +176,4 @@
$('#stop-import').click(that.stop_import); $('#stop-import').click(that.stop_import);
}); });
/** })(jQuery);
* Actions to run when the window is loaded
*/
$( window ).load(function() {
});
})( jQuery );

View File

@ -35,7 +35,7 @@ class MP2MigratorAPI extends APIEndpoint {
*/ */
public function stopImport($data) { public function stopImport($data) {
try { try {
$process = $this->MP2Migrator->stop_import(); $process = $this->MP2Migrator->stopImport();
return $this->successResponse($process); return $this->successResponse($process);
} catch(\Exception $e) { } catch(\Exception $e) {
return $this->errorResponse(array( return $this->errorResponse(array(

View File

@ -1,4 +1,5 @@
<?php <?php
namespace MailPoet\Config; namespace MailPoet\Config;
use MailPoet\Util\ProgressBar; use MailPoet\Util\ProgressBar;
@ -25,14 +26,14 @@ class MP2Migrator {
* @return boolean * @return boolean
*/ */
public function proposeMigration() { 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 // Store the user's choice if he doesn't want to migrate from MP2
update_option('mailpoet_migration_complete', true); update_option('mailpoet_migration_complete', true);
} }
if ( get_option('mailpoet_migration_complete') ) { if(get_option('mailpoet_migration_complete')) {
return false; return false;
} else { } 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
} }
} }
@ -42,14 +43,16 @@ class MP2Migrator {
* @param string $table Table name * @param string $table Table name
* @return boolean * @return boolean
*/ */
public function table_exists($table) { private function tableExists($table) {
global $wpdb; global $wpdb;
try { try {
$sql = "SHOW TABLES LIKE '{$wpdb->prefix}{$table}'"; $sql = "SHOW TABLES LIKE '{$wpdb->prefix}{$table}'";
$result = $wpdb->query($sql); $result = $wpdb->query($sql);
return !empty($result); return !empty($result);
} catch ( Exception $e ) {} } catch (Exception $e) {
// Do nothing
}
return false; return false;
} }
@ -59,7 +62,7 @@ class MP2Migrator {
* *
*/ */
public function init() { public function init() {
$this->enqueue_scripts(); $this->enqueueScripts();
$this->log('INIT'); $this->log('INIT');
} }
@ -67,7 +70,7 @@ class MP2Migrator {
* Register the JavaScript for the admin area. * Register the JavaScript for the admin area.
* *
*/ */
private function enqueue_scripts() { private function enqueueScripts() {
wp_enqueue_script('jquery-ui-progressbar'); wp_enqueue_script('jquery-ui-progressbar');
} }
@ -76,7 +79,7 @@ class MP2Migrator {
* *
* @param string $message * @param string $message
*/ */
public function log($message) { private function log($message) {
file_put_contents($this->log_file, "$message\n", FILE_APPEND); file_put_contents($this->log_file, "$message\n", FILE_APPEND);
} }
@ -87,15 +90,14 @@ class MP2Migrator {
*/ */
public function import() { public function import() {
$this->log('START 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 // TODO to remove, for testing only
$this->progressbar->set_total_count(0); $this->progressbar->setTotalCount(0);
$this->progressbar->set_total_count(10); $this->progressbar->setTotalCount(10);
for ( $i = 0; $i < 10; $i++ ) { for($i = 0; $i < 10; $i++) {
$this->progressbar->increment_current_count(1); $this->progressbar->incrementCurrentCount(1);
usleep(300000); usleep(300000);
if ( $this->import_stopped() ) { if($this->importStopped()) {
return; return;
} }
} }
@ -107,8 +109,8 @@ class MP2Migrator {
* Stop the import * Stop the import
* *
*/ */
public function stop_import() { public function stopImport() {
update_option('mailpoet_stop_import', true); update_option('mailpoet_stopImport', true);
$this->log('IMPORT STOPPED BY USER'); $this->log('IMPORT STOPPED BY USER');
} }
@ -117,8 +119,8 @@ class MP2Migrator {
* *
* @return boolean Import must stop or not * @return boolean Import must stop or not
*/ */
public function import_stopped() { private function importStopped() {
return get_option('mailpoet_stop_import'); return get_option('mailpoet_stopImport');
} }
} }

View File

@ -272,7 +272,7 @@ class Menu {
} }
$mp2Migrator = new MP2Migrator(); $mp2Migrator = new MP2Migrator();
if ($mp2Migrator->proposeMigration()) { if($mp2Migrator->proposeMigration()) {
$mp2Migrator->init(); $mp2Migrator->init();
$data = array( $data = array(
'log_file_url' => $mp2Migrator->log_file_url, 'log_file_url' => $mp2Migrator->log_file_url,

View File

@ -1,11 +1,12 @@
<?php <?php
namespace MailPoet\Util; namespace MailPoet\Util;
use MailPoet\Config\Env; use MailPoet\Config\Env;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
if ( !class_exists('ProgressBar', false) ) { if(!class_exists('ProgressBar', false)) {
/** /**
* The Progress Bar class * The Progress Bar class
@ -27,11 +28,11 @@ if ( !class_exists('ProgressBar', false) ) {
$filename = Env::$plugin_name . '-' . $progress_bar_id . '-progress.json'; $filename = Env::$plugin_name . '-' . $progress_bar_id . '-progress.json';
$this->filename = $upload_dir['basedir'] . '/' . $filename; $this->filename = $upload_dir['basedir'] . '/' . $filename;
$this->url = $upload_dir['baseurl'] . '/' . $filename; $this->url = $upload_dir['baseurl'] . '/' . $filename;
$counters = $this->read_progress(); $counters = $this->readProgress();
if ( isset($counters->total) ) { if(isset($counters->total)) {
$this->total_count = $counters->total; $this->total_count = $counters->total;
} }
if ( isset($counters->current) ) { if(isset($counters->current)) {
$this->current_count = $counters->current; $this->current_count = $counters->current;
} }
} }
@ -41,7 +42,7 @@ if ( !class_exists('ProgressBar', false) ) {
* *
* @return string Progress file URL * @return string Progress file URL
*/ */
public function get_url() { public function getUrl() {
return $this->url; return $this->url;
} }
@ -50,8 +51,8 @@ if ( !class_exists('ProgressBar', false) ) {
* *
* @return array|false Array of counters * @return array|false Array of counters
*/ */
private function read_progress() { private function readProgress() {
if ( file_exists($this->filename) ) { if(file_exists($this->filename)) {
$json_content = file_get_contents($this->filename); $json_content = file_get_contents($this->filename);
return json_decode($json_content); return json_decode($json_content);
} else { } else {
@ -64,11 +65,11 @@ if ( !class_exists('ProgressBar', false) ) {
* *
* @param int $count Count * @param int $count Count
*/ */
public function set_total_count($count) { public function setTotalCount($count) {
if ( $count != $this->total_count ) { if($count != $this->total_count) {
$this->total_count = $count; $this->total_count = $count;
$this->current_count = 0; $this->current_count = 0;
$this->save_progress(); $this->saveProgress();
} }
} }
@ -77,21 +78,22 @@ if ( !class_exists('ProgressBar', false) ) {
* *
* @param int $count Count * @param int $count Count
*/ */
public function increment_current_count($count) { public function incrementCurrentCount($count) {
$this->current_count += $count; $this->current_count += $count;
$this->save_progress(); $this->saveProgress();
} }
/** /**
* Save the progress counters * Save the progress counters
* *
*/ */
private function save_progress() { private function saveProgress() {
file_put_contents($this->filename, json_encode(array( file_put_contents($this->filename, json_encode(array(
'total' => $this->total_count, 'total' => $this->total_count,
'current' => $this->current_count, 'current' => $this->current_count,
))); )));
}
} }
}
} }