From 821f2fcab3a64346fc8d4b4ab8323fa727d673ac Mon Sep 17 00:00:00 2001 From: Petri Kanerva Date: Thu, 29 Sep 2016 09:36:03 +0300 Subject: [PATCH] Add optional debug mode to follow internal happenings of plugin --- includes/class-logitrail-shipping.php | 16 ++- includes/script.js | 50 ++++++++ logitrail-woocommerce.php | 164 +++++++++++++++++++++++--- 3 files changed, 213 insertions(+), 17 deletions(-) diff --git a/includes/class-logitrail-shipping.php b/includes/class-logitrail-shipping.php index 26a0431..c184bac 100644 --- a/includes/class-logitrail-shipping.php +++ b/includes/class-logitrail-shipping.php @@ -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' ) ); @@ -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.
Show Debug log', '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
(will happen once after option is selected, then option is reset to not selected)", 'logitrail-woocommerce' ), @@ -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); + } } } diff --git a/includes/script.js b/includes/script.js index f9221b9..d747d29 100644 --- a/includes/script.js +++ b/includes/script.js @@ -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("
Log cleared.
"); + + $('#logitrail-debug-log > div').append('
'); + }, + error: function(errorThrown){ + $('#logitrail-export-notify div').css('width', '350px'); + $('#logitrail-export-notify div').html('Error exporting products. Try again later.'); + } + }); + } + + $('.export-now').click(function(e) { e.preventDefault(); @@ -18,4 +35,37 @@ jQuery(document).ready(function($) { }); }); + $('.debug-log').click(function(e) { + e.preventDefault(); + + $('#wpwrap').append('
\n\ +
Loading log, please wait.
\n\ +
'); + + $.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('
' + line + '
'); + }); + } + else { + $('#logitrail-debug-log > div').append('
Log empty.
'); + } + + $('#logitrail-debug-log > div').append('
'); + + $('#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.'); + } + }); + + }); }); \ No newline at end of file diff --git a/logitrail-woocommerce.php b/logitrail-woocommerce.php index f348b53..8358f72 100644 --- a/logitrail-woocommerce.php +++ b/logitrail-woocommerce.php @@ -3,8 +3,8 @@ /* Plugin Name: Logitrail Description: Integrate checkout shipping with Logitrail - Version: 0.0.1 - Author: Petri Kanerva petri@codaone.fi + Version: 0.0.13 + Author: Petri Kanerva | Codaone Oy */ if(!defined('ABSPATH')) { @@ -32,26 +32,39 @@ class Logitrail_WooCommerce { + protected static $db_version = '0.1'; + protected static $tables = array( + 'debug' => 'logitrail_debug' + ); + private $merchant_id; private $secret_key; + private $debug_mode; + /** * Constructor */ public function __construct() { + register_activation_hook(__FILE__, array($this, 'logitrail_install')); + register_uninstall_hook(__FILE__, array($this, 'logitrail_uninstall')); + add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'wf_plugin_action_links' ) ); add_action( 'woocommerce_shipping_init', array( $this, 'logitrail_shipping_init') ); add_filter( 'woocommerce_shipping_methods', array( $this, 'logitrail_add_method') ); - add_action( 'woocommerce_checkout_shipping', array( $this, 'logitrail_get_template') ); - add_action( 'woocommerce_payment_complete', array( 'Logitrail_WooCommerce', 'logitrail_payment_complete'), 10, 1 ); + add_action( 'woocommerce_checkout_shipping', array($this, 'logitrail_get_template') ); + add_action( 'woocommerce_payment_complete', array($this, 'logitrail_payment_complete'), 10, 1 ); - add_action( 'woocommerce_order_status_completed', array( 'Logitrail_WooCommerce', 'logitrail_payment_complete'), 10, 1 ); + add_action( 'woocommerce_order_status_completed', array($this, 'logitrail_payment_complete'), 10, 1 ); - add_action( 'wc_ajax_logitrail', array( 'Logitrail_WooCommerce', 'logitrail_get_form' ) ); - add_action( 'wc_ajax_logitrail_setprice', array( 'Logitrail_WooCommerce', 'logitrail_set_price' ) ); + add_action( 'wc_ajax_logitrail', array($this, 'logitrail_get_form' ) ); + add_action( 'wc_ajax_logitrail_setprice', array($this, 'logitrail_set_price')); add_action( 'wc_ajax_logitrail_export_products', array( $this, 'export_products' ) ); + add_action( 'wc_ajax_logitrail_debug_log', array( $this, 'get_debug_log' ) ); + add_action( 'wc_ajax_logitrail_debug_log_clear', array( $this, 'clear_debug_log' ) ); + add_action( 'parse_request', array( $this, 'handle_product_import' ), 0 ); add_filter( 'woocommerce_locate_template', array($this, 'logitrail_woocommerce_locate_template'), 10, 3 ); @@ -70,6 +83,16 @@ public function __construct() { add_action( 'woocommerce_process_product_meta', array($this, 'logitrail_barcode_save')); add_action( 'admin_notices', array($this, 'logitrail_notifications')); + + // add possbile table prefix for db tables to be created + global $wpdb; + foreach (self::$tables as $name => &$table){ + $table = $wpdb->prefix.$table; + } + + $settings = get_option('woocommerce_logitrail_shipping_settings'); + + $this->debug_mode = ($settings['debug_mode'] === 'yes' ? true : false); } /** @@ -126,13 +149,13 @@ function logitrail_woocommerce_locate_template($template, $template_name, $templ $template_path = $woocommerce->template_url; } - $plugin_path = $this->logitrail_plugin_path() . '/woocommerce/'; + $plugin_path = $this->logitrail_plugin_path() . '/woocommerce/'; // Look within passed path within the theme - this is priority - $template = locate_template( - array( - $template_path . $template_name, - $template_name + $template = locate_template( + array( + $template_path . $template_name, + $template_name ) ); @@ -156,7 +179,7 @@ public static function logitrail_get_template() { wc_get_template( 'checkout/form-logitrail.php', $args); } - public static function logitrail_get_form() { + public function logitrail_get_form() { global $woocommerce, $post; $address = $woocommerce->customer->get_shipping_address(); @@ -178,6 +201,10 @@ public static function logitrail_get_form() { $apic->setCustomerInfo('', '', '', '', $address, $postcode, $city); $apic->setOrderId($woocommerce->session->get_session_cookie()[3]); + if($this->debug_mode) { + $this->logitrail_debug_log('Form, creating with data: ' . '""' . ', ' . '""' . ', ' . '""' . ', ' . '""' . ', ' . $address . ', ' . $postcode . ', ' . $city); + } + $cartContent = $woocommerce->cart->get_cart(); foreach($cartContent as $cartItem) { @@ -197,22 +224,43 @@ public static function logitrail_get_form() { } $apic->addProduct($cartItem['data']->get_sku(), $cartItem['data']->get_title(), $cartItem['quantity'], $cartItem['data']->get_weight() * 1000, $cartItem['data']->get_price_including_tax(), $tax); + + if($this->debug_mode) { + $this->logitrail_debug_log('Form, added product with data: ' . '""' . ', ' . '""' . ', ' . '""' . ', ' . '""' . ', ' . $address . ', ' . $postcode . ', ' . $city); + } + } $form = $apic->getForm(); echo $form; + + if($this->debug_mode) { + $this->logitrail_debug_log('Form, returned via ajax'); + } + wp_die(); } - public static function logitrail_set_price() { + /* + * Set price via AJAX call + */ + public function logitrail_set_price() { global $woocommerce; set_transient('logitrail_' . $woocommerce->session->get_session_cookie()[3] . '_price', $_POST['postage']); set_transient('logitrail_' . $woocommerce->session->get_session_cookie()[3] . '_order_id', $_POST['order_id']); set_transient('logitrail_' . $woocommerce->session->get_session_cookie()[3] . '_type', $_POST['delivery_type']); + + if($this->debug_mode) { + $this->logitrail_debug_log('Setting postage to ' . $_POST['postage']); + + $postage = get_transient('logitrail_' . $woocommerce->session->get_session_cookie()[3] . '_price'); + + $this->logitrail_debug_log('Confirming postage value as ' . $postage); + } } - public static function logitrail_payment_complete($this_id) { + public function logitrail_payment_complete($this_id) { global $woocommerce, $post; $settings = get_option('woocommerce_logitrail_shipping_settings'); @@ -237,6 +285,10 @@ public static function logitrail_payment_complete($this_id) { // TODO: translate echo "
Voit seurata toimitustasi osoitteessa: " . $result['tracking_url'] . "
"; + if($this->debug_mode) { + $this->logitrail_debug_log('Confirmed order ' . $order_id . 'with details: ' . $order->shipping_first_name . ', ' . $order->shipping_last_name . ', ' . $order->billing_phone . ', ' . $order->billing_email . ', ' . $order->shipping_address_1 . ' ' . $order->shipping_address_2 . ', ' . $order->shipping_postcode . ', ' . $order->shipping_city); + } + delete_transient('logitrail_' . $woocommerce->session->get_session_cookie()[3] . '_price'); delete_transient('logitrail_' . $woocommerce->session->get_session_cookie()[3] . '_order_id'); delete_transient('logitrail_' . $woocommerce->session->get_session_cookie()[3] . '_type'); @@ -346,6 +398,10 @@ function logitrail_create_product($post_id) { if(count($responses > 1) && $errors > 0) { //wc_add_notice("Virhe " . $errors . " tuotteen kohdalla siirrettäessä " . count('$responses') . " tuotetta Logitrailille"); } + + if($this->debug_mode) { + $this->logitrail_debug_log('Added product with info: ' . $product->get_sku() . ', ' . $product->get_title() . ', ' . 1 . ', ' . $product->get_weight() * 1000 . ', ' . $product->get_price_including_tax() . ', ' . 0 . ', ' . get_post_meta($post_id, 'barcode', true) . ', ' . $product->get_width() * 10 . ', ' . $product->get_height() * 10 . ', ' . $product->get_length() * 10); + } } } @@ -368,7 +424,7 @@ function logitrail_remove_label($label, $method) { * Export all vurrent products to Logitrail. * Called via AJAX */ - public static function export_products() { + public function export_products() { $settings = get_option('woocommerce_logitrail_shipping_settings'); $apic = new Logitrail\Lib\ApiClient(); @@ -390,6 +446,10 @@ public static function export_products() { // weight for Logitrail goes in grams, dimensions in millimeter $apic->addProduct($product->get_sku(), $product->get_title(), 1, $product->get_weight() * 1000, $product->get_price_including_tax(), 0, null, $product->get_width() * 10, $product->get_height() * 10, $product->get_length() * 10); + if($this->debug_mode) { + $this->logitrail_debug_log('Added product with info: ' . $product->get_sku() . ', ' . $product->get_title() . ', ' . 1 . ', ' . $product->get_weight() * 1000 . ', ' . $product->get_price_including_tax() . ', ' . 0 . ', ' . get_post_meta($post_id, 'barcode', true) . ', ' . $product->get_width() * 10 . ', ' . $product->get_height() * 10 . ', ' . $product->get_length() * 10); + } + // create products in batches of 5, so in big shops we don't get // huge amount of products taking memory in ApiClient $productsAdded++; @@ -399,12 +459,20 @@ public static function export_products() { $response = $apic->createProducts(); $apic->clearProducts(); $productsAdded = 0; + + if($this->debug_mode) { + $this->logitrail_debug_log('Created or updated added products and emptied products list,'); + } } } $response = $apic->createProducts(); $apic->clearProducts(); + if($this->debug_mode) { + $this->logitrail_debug_log('Created or updated added products and emptied products list,'); + } + wp_reset_query(); } @@ -436,16 +504,29 @@ function clear_wc_shipping_rates_cache(){ // unset to force recalculation of cart total price when // shipping price is changed + if($this->debug_mode) { + $rates26 = array(); + } + // WooCommerce 2.6 cache $packages = WC()->cart->get_shipping_packages(); foreach ($packages as $key => $value) { $shipping_session = "shipping_for_package_$key"; unset(WC()->session->$shipping_session); + + if($this->debug_mode) { + $rates26[] = print_r(WC()->session->$shipping_session, true); + } + } // WooCommerce 2.5 cache unset(WC()->session->shipping_for_package); + + if($this->debug_mode) { + $this->logitrail_debug_log('Emptied caches: 2.5 (' . print_r(WC()->session->shipping_for_package, true) . '), 2.6 (' . implode(', ', $rates26) . ') ' . $_POST['postage']); + } } function logitrail_notifications() { @@ -457,6 +538,57 @@ function logitrail_notifications() { set_transient('logitrail_' . wp_get_current_user()->ID . '_notifications', array()); } + + + + // Functions related to debug logs + + public static function get_debug_log() { + global $wpdb; + + $sql = "SELECT * FROM `" . self::$tables['debug'] . "` LIMIT 100"; + $results = $wpdb->get_results($sql, ARRAY_A); + + $lines = array(); + foreach($results as $id => $row) { + $lines[] = '' . Date('d.m.Y H:i:s', $row['created_at']) . " {$row['operation']}"; + } + + wp_send_json($lines); + } + + public static function clear_debug_log() { + global $wpdb; + + $delete = $wpdb->query("TRUNCATE TABLE `" . self::$tables['debug'] . "`"); + + wp_send_json(array('status' => 'success')); + } + + public static function logitrail_debug_log($text) { + global $wpdb; + + $wpsess = 'aa'; //WP_Session_Tokens::get(); + $wpdb->insert( self::$tables['debug'], array('session' => $wpsess, 'operation' => $text, 'created_at' => time()) ); + } + + public static function logitrail_install() { + require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); + $tables = self::$tables; + + dbDelta( "CREATE TABLE IF NOT EXISTS `{$tables['debug']}` ( + `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, + `session` varchar(255) NOT NULL, + `operation` varchar(255) NOT NULL, + `created_at` int NOT NULL + ) ENGINE='InnoDB'" ); + + add_option( 'logitrail_db_version', self::$db_version ); + } + + public static function logitrail_uninstall() { + $delete = $wpdb->query("DROP TABLE `" . self::$tables['debug'] . "`"); + } } new Logitrail_WooCommerce();