Skip to content

Commit

Permalink
Added respecting WP_DEBUG for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
timoj committed Nov 19, 2018
1 parent aaa3100 commit 06c8132
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion assets/js/parcelshop.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var map;
var parcelshop_address;

// If parcelshop is selected in shipment. Adds a button to choose a parcelshop
if (document.getElementById('shipping_method_0_wuunder_parcelshop').checked) {
if (document.getElementById('shipping_method_0_wuunder_parcelshop') && document.getElementById('shipping_method_0_wuunder_parcelshop').checked) {
var node = document.createElement("div");
node.className += "chooseParcelshop";
node.innerHTML = '<div id="parcelshopsSelectedContainer" onclick="showParcelshopPicker()"><a href="#/" id="selectParcelshop">Klik hier om een parcelshop te kiezen</a></div>';
Expand Down
9 changes: 2 additions & 7 deletions includes/parcelshop.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

function parcelShopLocator()
{
include_once('wcwuunder-shipping-method.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);
// error_reporting(E_ALL);
// ini_set('display_errors', 1);
$status = get_option('wc_wuunder_api_status');
$apiKey = ($status == 'productie' ? get_option('wc_wuunder_api') : get_option('wc_wuunder_test_api'));

Expand All @@ -25,9 +23,6 @@ function parcelShopLocator()
$parcelshopsRequest = $connector->getParcelshopsByAddress();
$parcelshopsConfig = new \Wuunder\Api\Config\ParcelshopsConfig();

$logger = wc_get_logger();
$context = array('source' => "wuunder_connector");

$parcelshopsConfig->setProviders(get_option('woocommerce_wuunder_parcelshop_settings')['select_carriers']);
$parcelshopsConfig->setAddress($shipping_address);

Expand Down
37 changes: 23 additions & 14 deletions includes/wcwuunder-create.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ public function sample_admin_notice__error()
*/
private function setBookingConfig($orderId)
{
$logger = wc_get_logger();
$context = array('source' => "wuunder_connector");
$logger->log('info', "Filling the booking config", $context);
if (WP_DEBUG) {
$logger = wc_get_logger();
$context = array('source' => "wuunder_connector");
$logger->log('info', "Filling the booking config", $context);
}

$orderItems = $this->get_order_items($orderId);

Expand Down Expand Up @@ -153,31 +155,33 @@ public function generateBookingUrl()
$context = array('source' => "wuunder_connector");

if (isset($_REQUEST['order']) && $_REQUEST['action'] === "bookorder") {
$logger->log('info', "Generating the booking url", $context);
if (WP_DEBUG)
$logger->log('info', "Generating the booking url", $context);
$order_id = $_REQUEST['order'];

$status = get_option('wc_wuunder_api_status');
$apiKey = ($status == 'productie' ? get_option('wc_wuunder_api') : get_option('wc_wuunder_test_api'));



$connector = new Wuunder\Connector($apiKey, $status !== 'productie');
$booking = $connector->createBooking();
$bookingConfig = $this->setBookingConfig($order_id);

if ($bookingConfig->validate()) {
$booking->setConfig($bookingConfig);
$logger->log('info', "Going to fire for bookingurl", $context);
if (WP_DEBUG)
$logger->log('info', "Going to fire for bookingurl", $context);
if ($booking->fire()) {
$url = $booking->getBookingResponse()->getBookingUrl();
} else {
} else if (WP_DEBUG) {
$logger->log('error', $booking->getBookingResponse()->getError(), $context);
}
} else {
} else if (WP_DEBUG) {
$logger->log('error', "Bookingconfig not complete", $context);
}

$logger->log('info', "Handling response", $context);
if (WP_DEBUG)
$logger->log('info', "Handling response", $context);

if (isset($url)) {
update_post_meta($order_id, '_wuunder_label_booking_url', $url);
Expand Down Expand Up @@ -257,7 +261,8 @@ public function get_company_address()
if ($pickupAddress->validate()) {
return $pickupAddress;
} else {
$logger->log('error', "Invalid pickup address. There are mistakes or missing fields.", $context);
if (WP_DEBUG)
$logger->log('error', "Invalid pickup address. There are mistakes or missing fields.", $context);
return $pickupAddress;
}
}
Expand Down Expand Up @@ -366,7 +371,8 @@ public function get_customer_address($orderid)
if ($deliveryAddress->validate()) {
return $deliveryAddress;
} else {
$logger->log('error', "Invalid delivery address. There are mistakes or missing fields.", $context);
if (WP_DEBUG)
$logger->log('error', "Invalid delivery address. There are mistakes or missing fields.", $context);
return $deliveryAddress;
}
}
Expand All @@ -384,17 +390,20 @@ public function get_base64_image($imagepath)

try {
$fileSize = (substr($imagepath, 0, 4) === "http") ? $this->remote_filesize($imagepath) : filesize($imagepath);
$logger->log("Handling a image of size: " . $fileSize, $context);
if (WP_DEBUG)
$logger->log("Handling a image of size: " . $fileSize, $context);
if ($fileSize > 0 && $fileSize <= 2097152) { //smaller or equal to 2MB
$logger->log("Base64 encoding image", $context);
if (WP_DEBUG)
$logger->log("Base64 encoding image", $context);
$imagedata = file_get_contents($imagepath);
$image = base64_encode($imagedata);
} else {
$image = "";
}
return $image;
} catch (Exception $e) {
$logger->log('error', $e, $context);
if (WP_DEBUG)
$logger->log('error', $e, $context);
return "";
}
}
Expand Down
12 changes: 7 additions & 5 deletions woocommerce-wuunder.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ public function add_admin_styles_scripts()

public function webhook()
{
$logger = wc_get_logger();
$context = array('source' => "wuunder_connector");
if (WP_DEBUG) {
$logger = wc_get_logger();
$context = array('source' => "wuunder_connector");

$logger->log('info', "Test webhook", $context);
$logger->log('info', $_REQUEST['order'], $context);
$logger->log('info', $_REQUEST['token'], $context);
$logger->log('info', "Test webhook", $context);
$logger->log('info', $_REQUEST['order'], $context);
$logger->log('info', $_REQUEST['token'], $context);
}

if (!isset($_REQUEST['order']) || !isset($_REQUEST['token'])) {
wp_redirect("", 500);
Expand Down

0 comments on commit 06c8132

Please sign in to comment.