Skip to content

Commit

Permalink
Add optional debug mode to follow internal happenings of plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
CodaPetri committed Sep 29, 2016
1 parent e9d3b8f commit 821f2fc
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 17 deletions.
16 changes: 15 additions & 1 deletion includes/class-logitrail-shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private function init() {

$this->fallback = !empty( $this->settings['fallback'] ) ? $this->settings['fallback'] : '';
$this->test_server = !empty( $this->settings['test_server'] ) ? $this->settings['test_server'] : '';
$this->debug_mode = !empty( $this->settings['debug_mode'] ) ? $this->settings['debug_mode'] : '';

add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );

Expand Down Expand Up @@ -149,6 +150,13 @@ public function init_form_fields() {
'default' => false,
'desc_tip' => true
),
'debug_mode'=> array(
'title' => __( 'Debug mode', 'logitrail-woocommerce' ),
'label' => __( 'Log debug data which tells more about what\'s happening.<br /><a href="#" class="debug-log">Show Debug log</a>', 'logitrail-woocommerce' ),
'type' => 'checkbox',
'default' => false,
'desc_tip' => true
),
'export_products'=> array(
'title' => __( 'Export products', 'logitrail-woocommerce' ),
'label' => __( "Export all current products to Logitrail's system<br />(will happen once after option is selected, then option is reset to not selected)", 'logitrail-woocommerce' ),
Expand Down Expand Up @@ -178,11 +186,17 @@ public function calculate_shipping( $package ) {
$title = ($type && array_key_exists($type, $shipping_methods) ? $shipping_methods[$type] : 'Toimitustapaa ei ole valittu');
}

$postage = get_transient('logitrail_' . $woocommerce->session->get_session_cookie()[3] . '_price');
$this->add_rate( array(
'id' => $this->id . '_postage',
'label' => $title,
'cost' => get_transient('logitrail_' . $woocommerce->session->get_session_cookie()[3] . '_price'),
'cost' => get_transient('logitrail_' . $postage),
'sort' => 0
) );

$debug_mode = ($this->settings['debug_mode'] === 'yes' ? true : false);
if($debug_mode) {
Logitrail_WooCommerce::logitrail_debug_log('Informing WooCommerce postage as ' . $postage);
}
}
}
50 changes: 50 additions & 0 deletions includes/script.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
jQuery(document).ready(function($) {
function clearDebugLog() {
$.ajax({
url: '/index.php/checkout/?wc-ajax=logitrail_debug_log_clear',
method: 'post',
success:function(data) {
$('#logitrail-debug-log > div').html("<div style='line-height: 17px; width: 100%'>Log cleared.</div>");

$('#logitrail-debug-log > div').append('<div style="width: 100%; text-align: center; line-height: 15px; padding-top: 10px;"><button style="margin-left: 15px;" disabled>Clear log</button> <button style="margin-left: 15px;" onClick="jQuery(\'#logitrail-debug-log\').remove();">Close</button></div>');
},
error: function(errorThrown){
$('#logitrail-export-notify div').css('width', '350px');
$('#logitrail-export-notify div').html('Error exporting products. Try again later.<button style="margin-left: 15px;" onClick="jQuery(\'#logitrail-export-notify\').remove();">Close</button>');
}
});
}


$('.export-now').click(function(e) {
e.preventDefault();

Expand All @@ -18,4 +35,37 @@ jQuery(document).ready(function($) {
});
});

$('.debug-log').click(function(e) {
e.preventDefault();

$('#wpwrap').append('<div id="logitrail-debug-log" style="opacity: 0.6; right: 0px; bottom; 0px; height: 100%; width: 100%; position: absolute; top: 0px; left: 0px; z-index: 10000; background-color: black;">\n\
<div style="position: absolute; opacity: 0.9; padding: 10px; border-radius: 10px; width: 550px; background-color: #ffffff; left: 50%; top: 50%; margin-left: -275px; margin-top: -25px; text-align: left; line-height: 50px;">Loading log, please wait.</div>\n\
</div>');

$.ajax({
url: '/index.php/checkout/?wc-ajax=logitrail_debug_log',
method: 'post',
success:function(data) {
$('#logitrail-debug-log div').html("");

if(data.length > 0) {
data.forEach(function(line) {
$('#logitrail-debug-log > div').append('<div style="line-height: 17px; margin-bottom: 5px; padding-bottom: 3px; border-bottom: 1px solid #eaeaea;">' + line + '</div>');
});
}
else {
$('#logitrail-debug-log > div').append('<div style="line-height: 17px; width: 100%;">Log empty.</div>');
}

$('#logitrail-debug-log > div').append('<div style="width: 100%; text-align: center; line-height: 15px; padding-top: 10px;"><button style="margin-left: 15px;" id="logitrail-debug-log-clear">Clear log</button> <button style="margin-left: 15px;" onClick="jQuery(\'#logitrail-debug-log\').remove();">Close</button></div>');

$('#logitrail-debug-log-clear').click(clearDebugLog);
},
error: function(errorThrown){
$('#logitrail-export-notify div').css('width', '350px');
$('#logitrail-export-notify div').html('Error exporting products. Try again later.<button style="margin-left: 15px;" onClick="jQuery(\'#logitrail-export-notify\').remove();">Close</button>');
}
});

});
});
Loading

0 comments on commit 821f2fc

Please sign in to comment.