1
0
Fork 0
mirror of synced 2025-04-02 21:36:14 +03:00

Review fixes

This commit is contained in:
Alex Komarichev 2024-09-26 18:42:38 +03:00
parent 293cdd8660
commit cfc5c2c274
12 changed files with 243 additions and 411 deletions

View file

@ -67,103 +67,77 @@ if (!class_exists('WC_Retailcrm_Proxy')) :
$called = sprintf('%s::%s', get_class($this->retailcrm), $method);
try {
WC_Retailcrm_Logger::info(
$method,
empty($arguments) ? '[no params]' : '[with arguments]',
['arguments' => $arguments],
WC_Retailcrm_Logger::TYPE['req']
);
/** @var \WC_Retailcrm_Response $response */
$response = call_user_func_array(array($this->retailcrm, $method), $arguments);
$response = $this->getResponse($method, $arguments);
if (is_string($response)) {
WC_Retailcrm_Logger::info($method, $response, [], WC_Retailcrm_Logger::TYPE['res']);
WC_Retailcrm_Logger::info($method, $response, [], WC_Retailcrm_Logger::RESPONSE);
return $response;
}
if (empty($response)) {
if (!$response instanceof WC_Retailcrm_Response) {
WC_Retailcrm_Logger::error(
$method,
sprintf("[%s] null (no response whatsoever)", $called),
[],
WC_Retailcrm_Logger::TYPE['res']
WC_Retailcrm_Logger::RESPONSE
);
return null;
}
if ($response->isSuccessful()) {
if (in_array(
$called,
$this->methodsWithoutFullLog()
)) {
WC_Retailcrm_Logger::info(
$method,
'Ok',
['body' => 'request was successful, but response is omitted'],
WC_Retailcrm_Logger::TYPE['res']
);
} else {
WC_Retailcrm_Logger::info(
$method,
'Ok',
['body' => json_decode($response->getRawResponse(), true)],
WC_Retailcrm_Logger::TYPE['res']
);
}
} else {
WC_Retailcrm_Logger::error(
$method,
sprintf(
"Error: [HTTP-code %s] %s",
$response->getStatusCode(),
$response->getErrorString()
),
['response' => json_decode($response->getRawResponse(), true)],
WC_Retailcrm_Logger::TYPE['res']
);
}
} catch (WC_Retailcrm_Exception_Curl $exception) {
WC_Retailcrm_Logger::error(
$method,
sprintf(
'%s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
} catch (WC_Retailcrm_Exception_Json $exception) {
WC_Retailcrm_Logger::error(
$method,
sprintf(
'%s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
} catch (InvalidArgumentException $exception) {
WC_Retailcrm_Logger::error(
$method,
sprintf(
'%s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
$this->logResponse($response, $method, $called);
} catch (WC_Retailcrm_Exception_Curl|WC_Retailcrm_Exception_Json|InvalidArgumentException $exception) {
WC_Retailcrm_Logger::exception($method, $exception);
}
return !empty($response) ? $response : new WC_Retailcrm_Response(900, '{}');
return $response instanceof WC_Retailcrm_Response ? $response : new WC_Retailcrm_Response(900, '{}');
}
private function getResponse($method, $arguments)
{
WC_Retailcrm_Logger::info(
$method,
count($arguments) === 0 ? '[no params]' : '[with params]',
['params' => $arguments],
WC_Retailcrm_Logger::REQUEST
);
return call_user_func_array(array($this->retailcrm, $method), $arguments);
}
private function logResponse(WC_Retailcrm_Response $response, $method, $called): void
{
if ($response->isSuccessful()) {
if (in_array($called, $this->methodsWithoutFullLog())) {
WC_Retailcrm_Logger::info(
$method,
'Ok',
['body' => 'request was successful, but response is omitted'],
WC_Retailcrm_Logger::RESPONSE
);
} else {
WC_Retailcrm_Logger::info(
$method,
'Ok',
['body' => json_decode($response->getRawResponse(), true)],
WC_Retailcrm_Logger::RESPONSE
);
}
} else {
WC_Retailcrm_Logger::error(
$method,
sprintf(
"Error: [HTTP-code %s] %s",
$response->getStatusCode(),
$response->getErrorString()
),
['response' => json_decode($response->getRawResponse(), true)],
WC_Retailcrm_Logger::RESPONSE
);
}
}
}
endif;

View file

@ -182,7 +182,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function init_settings_fields()
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
$this->init_form_fields();
$this->init_settings();
}
@ -194,7 +194,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function api_sanitized($settings)
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
WC_Retailcrm_Logger::info(
__METHOD__,
'Module settings',
@ -292,7 +292,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function clear_cron_tasks()
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
wp_clear_scheduled_hook('retailcrm_icml');
wp_clear_scheduled_hook('retailcrm_history');
wp_clear_scheduled_hook('retailcrm_inventories');
@ -309,7 +309,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function generate_icml()
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
/*
* A temporary solution.
* We have rebranded the module and changed the name of the ICML file.
@ -414,7 +414,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function retailcrm_history_get()
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
$retailcrm_history = new WC_Retailcrm_History($this->apiClient);
$retailcrm_history->getHistory();
}
@ -426,7 +426,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function retailcrm_process_order($order_id)
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__, $order_id);
WC_Retailcrm_Logger::setHook(current_action(), $order_id);
$this->orders->orderCreate($order_id);
}
@ -437,7 +437,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function load_stocks()
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
$inventories = new WC_Retailcrm_Inventories($this->apiClient);
$inventories->updateQuantity();
@ -452,7 +452,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function upload_selected_orders()
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
$this->uploader->uploadSelectedOrders();
}
@ -463,7 +463,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function upload_to_crm()
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
$page = filter_input(INPUT_POST, 'Step');
$entity = filter_input(INPUT_POST, 'Entity');
@ -486,7 +486,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function create_customer($customerId)
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__, $customerId);
WC_Retailcrm_Logger::setHook(current_action(), $customerId);
if (WC_Retailcrm_Plugin::history_running() === true) {
return;
@ -516,7 +516,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function update_customer($customerId)
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__, $customerId);
WC_Retailcrm_Logger::setHook(current_action(), $customerId);
if (WC_Retailcrm_Plugin::history_running() === true) {
WC_Retailcrm_Logger::info(__METHOD__, 'History in progress, skip');
@ -542,7 +542,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function create_order($order_id)
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__, $order_id);
WC_Retailcrm_Logger::setHook(current_action(), $order_id);
if (is_admin()) {
WC_Retailcrm_Logger::info(__METHOD__, 'Creation is from admin panel');
@ -563,7 +563,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
{
global $woocommerce;
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
try {
$site = $this->apiClient->getSingleSiteForKey();
@ -597,17 +597,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
}
}
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'%s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
}
}
@ -626,7 +616,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
{
global $woocommerce;
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
try {
$site = $this->apiClient->getSingleSiteForKey();
@ -659,23 +649,13 @@ if (!class_exists('WC_Retailcrm_Base')) {
}
}
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'%s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
}
}
public function update_order($orderId)
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__, $orderId);
WC_Retailcrm_Logger::setHook(current_action(), $orderId);
if (WC_Retailcrm_Plugin::history_running() === true) {
WC_Retailcrm_Logger::info(__METHOD__, 'History in progress, skip');
@ -697,7 +677,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function take_update_order($order_id)
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__, $order_id);
WC_Retailcrm_Logger::setHook(current_action(), $order_id);
if (
WC_Retailcrm_Plugin::history_running() === true
@ -717,7 +697,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
public function update_order_loyalty()
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
if ($this->updatedOrderId !== []) {
foreach ($this->updatedOrderId as $orderId) {
@ -728,14 +708,14 @@ if (!class_exists('WC_Retailcrm_Base')) {
public function update_order_items($orderId)
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__, $orderId);
WC_Retailcrm_Logger::setHook(current_action(), $orderId);
$this->orders->updateOrder($orderId);
}
public function trash_order_action($id)
{
if ('shop_order' == get_post_type($id)) {
WC_Retailcrm_Logger::setEntry(__FUNCTION__, $id);
WC_Retailcrm_Logger::setHook(current_action(), $id);
$this->orders->updateOrder($id, true);
}
}
@ -873,7 +853,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
public function coupon_info()
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
try {
$result = $this->loyalty->createLoyaltyCoupon();
@ -889,111 +869,61 @@ if (!class_exists('WC_Retailcrm_Base')) {
wp_enqueue_script('retailcrm-loyalty-cart', $jsScriptPath, '', '', true);
wp_localize_script('retailcrm-loyalty-cart', 'AdminUrl', $wpAdminUrl);
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'%s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
}
}
public function refresh_loyalty_coupon()
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
try {
$this->loyalty->createLoyaltyCoupon(true);
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'%s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
}
}
public function clear_loyalty_coupon()
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
try {
$this->loyalty->clearLoyaltyCoupon();
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'%s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
}
}
public function remove_coupon($couponCode)
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
try {
if (!$this->loyalty->deleteLoyaltyCoupon($couponCode)) {
$this->loyalty->createLoyaltyCoupon(true);
}
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'%s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
}
}
public function apply_coupon($couponCode)
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
try {
if (!$this->loyalty->isLoyaltyCoupon($couponCode)) {
$this->loyalty->createLoyaltyCoupon(true);
}
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'%s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
}
}
public function reviewCreditBonus()
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
$resultHtml = $this->loyalty->getCreditBonuses();
if ($resultHtml) {
@ -1172,7 +1102,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
return null;
}
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
$orderMetaData = $this->getMetaData('order');
$customerMetaData = $this->getMetaData('user');
@ -1210,7 +1140,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
public function add_loyalty_item($items)
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
$items['loyalty'] = __('Loyalty program', 'retailcrm');
return $items;
@ -1229,7 +1159,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
return;
}
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
$jsScript = 'retailcrm-loyalty-actions';
$loyaltyUrl = ['url' => get_admin_url()];
@ -1335,7 +1265,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function deactivate()
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::setHook(current_action());
$api_client = $this->getApiClient();
$clientId = get_option('retailcrm_client_id');

View file

@ -80,17 +80,7 @@ if (!class_exists('WC_Retailcrm_Carts')) :
$setResponse = $this->apiClient->cartSet($crmCart, $site);
$isSuccessful = $setResponse->isSuccessful() && !empty($setResponse['success']);
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'Error process cart: %s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
}
return $isSuccessful;
@ -107,17 +97,7 @@ if (!class_exists('WC_Retailcrm_Carts')) :
$isSuccessful = $clearResponse->isSuccessful() && !empty($clearResponse['success']);
}
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'Error clear cart: %s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
}
return $isSuccessful;

View file

@ -184,7 +184,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Process WC_Customer ' . $customer->get_id(),
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($customer)]
['wc_customer' => WC_Retailcrm_Logger::formatWcObject($customer)]
);
$this->processCustomer($customer, $order);
$response = $this->retailcrm->customersCreate($this->customer);
@ -217,7 +217,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Update WC_Customer ' . $customer->get_id(),
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($customer)]
['wc_customer' => WC_Retailcrm_Logger::formatWcObject($customer)]
);
$this->processCustomer($customer);
$this->retailcrm->customersEdit($this->customer);
@ -247,7 +247,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Update WC_Customer by CRM_Customer ID: ' . $crmCustomerId,
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($customer)]
['wc_customer' => WC_Retailcrm_Logger::formatWcObject($customer)]
);
$this->processCustomer($customer);
$this->customer['id'] = $crmCustomerId;
@ -395,7 +395,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Processing for upload WC_Customer ' . $customer->get_id(),
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($customer)]
['wc_customer' => WC_Retailcrm_Logger::formatWcObject($customer)]
);
$this->processCustomer($customer);
}
@ -707,7 +707,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Build new customer from order data',
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($new_customer)]
['wc_customer' => WC_Retailcrm_Logger::formatWcObject($new_customer)]
);
return $new_customer;

View file

@ -83,17 +83,7 @@ if (!class_exists('WC_Retailcrm_History')) :
$this->ordersHistory();
// @codeCoverageIgnoreStart
} catch (\Exception $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'%s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
}
// @codeCoverageIgnoreEnd
}
@ -186,22 +176,12 @@ if (!class_exists('WC_Retailcrm_History')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Updated WC_Customer ' . $crmCustomer['externalId'],
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($wcCustomer)]
['wc_customer' => WC_Retailcrm_Logger::formatWcObject($wcCustomer)]
);
// @codeCoverageIgnoreStart
} catch (Exception $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'%s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
}
// @codeCoverageIgnoreEnd
}
@ -315,21 +295,11 @@ if (!class_exists('WC_Retailcrm_History')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Result WC_Order ' . $wcOrder->get_id(),
['wc_order' => WC_Retailcrm_Logger::formatWCObject($wcOrder)]
['wc_order' => WC_Retailcrm_Logger::formatWcObject($wcOrder)]
);
}
} catch (Exception $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'%s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
continue;
}
@ -434,7 +404,7 @@ if (!class_exists('WC_Retailcrm_History')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Updating WC_Order ' . $wcOrder->get_id(),
['wc_order' => WC_Retailcrm_Logger::formatWCObject($wcOrder)]
['wc_order' => WC_Retailcrm_Logger::formatWcObject($wcOrder)]
);
if (isset($order['status']) && isset($options[$order['status']])) {
@ -1175,8 +1145,8 @@ if (!class_exists('WC_Retailcrm_History')) :
$crmProduct['id'] ?? 'id empty'
),
[
'wc_order' => WC_Retailcrm_Logger::formatWCObject($wcOrder),
'wc_product' => WC_Retailcrm_Logger::formatWCObject($wcProduct),
'wc_order' => WC_Retailcrm_Logger::formatWcObject($wcOrder),
'wc_product' => WC_Retailcrm_Logger::formatWcObject($wcProduct),
'crm_product' => $crmProduct,
]
);
@ -1235,7 +1205,7 @@ if (!class_exists('WC_Retailcrm_History')) :
__METHOD__,
'Updating order product WC_Order_Item, CRM_Product',
[
'wc_order_item' => WC_Retailcrm_Logger::formatWCObject($wcOrderItem),
'wc_order_item' => WC_Retailcrm_Logger::formatWcObject($wcOrderItem),
'crm_product' => $crmProduct,
]
);
@ -1357,24 +1327,16 @@ if (!class_exists('WC_Retailcrm_History')) :
$handled = true;
// @codeCoverageIgnoreStart
} catch (\Exception $exception) {
$errorMessage = sprintf(
'Error switching order externalId=%s to customer id=%s (new company: id=%s %s). Reason: %s' .
' - Exception in file %s on line %s'
,
$order['externalId'],
$newCustomerId,
isset($order['company']) ? $order['company']['id'] : '',
isset($order['company']) ? $order['company']['name'] : '',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
);
WC_Retailcrm_Logger::error(
WC_Retailcrm_Logger::exception(
__METHOD__,
$errorMessage,
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
$exception,
sprintf(
'Error switching order externalId=%s to customer id=%s (new company: id=%s %s). Reason: ',
$order['externalId'],
$newCustomerId,
isset($order['company']) ? $order['company']['id'] : '',
isset($order['company']) ? $order['company']['name'] : ''
)
);
$handled = false;
}

View file

@ -48,16 +48,10 @@ if (!class_exists('WC_Retailcrm_Loyalty')) :
try {
$response = $this->getLoyaltyAccounts($userId);
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
WC_Retailcrm_Logger::exception(
__METHOD__,
sprintf(
'Exception get loyalty accounts: %s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
$exception,
'Exception get loyalty accounts: '
);
return $result;
@ -102,16 +96,10 @@ if (!class_exists('WC_Retailcrm_Loyalty')) :
return $response->isSuccessful();
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
WC_Retailcrm_Logger::exception(
__METHOD__,
sprintf(
'Exception while registering in the loyalty program: %s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
$exception,
'Exception while registering in the loyalty program: '
);
return false;
@ -133,17 +121,7 @@ if (!class_exists('WC_Retailcrm_Loyalty')) :
return $response->isSuccessful();
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'Exception while activate loyalty account: %s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
return false;
}
@ -379,16 +357,10 @@ if (!class_exists('WC_Retailcrm_Loyalty')) :
try {
$response = $this->getLoyaltyAccounts($wcCustomer->get_id());
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
WC_Retailcrm_Logger::exception(
__METHOD__,
sprintf(
'Exception get loyalty accounts: %s - Exception in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
$exception,
'Exception get loyalty accounts: '
);
return false;

View file

@ -98,8 +98,8 @@ if (!class_exists('WC_Retailcrm_Orders')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Start order creating ' . is_int($orderId) ? $orderId : '',
['wc_order' => WC_Retailcrm_Logger::formatWCObject($orderId)]
'Start order creating ' . (is_int($orderId) ? $orderId : ''),
['wc_order' => WC_Retailcrm_Logger::formatWcObject($orderId)]
);
try {
@ -135,7 +135,7 @@ if (!class_exists('WC_Retailcrm_Orders')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Create WC_Order ' . $wcOrder->get_id(),
['wc_order' => WC_Retailcrm_Logger::formatWCObject($wcOrder)]
['wc_order' => WC_Retailcrm_Logger::formatWcObject($wcOrder)]
);
$this->processOrder($wcOrder);
@ -156,17 +156,7 @@ if (!class_exists('WC_Retailcrm_Orders')) :
$this->loyalty->applyLoyaltyDiscount($wcOrder, $response['order'], $discountLp);
}
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'Error message: %s - Exception in file: %s on line: %s.',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
return null;
}
@ -343,7 +333,7 @@ if (!class_exists('WC_Retailcrm_Orders')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Update WC_Order ' . $wcOrder->get_id(),
['wc_order' => WC_Retailcrm_Logger::formatWCObject($wcOrder)]
['wc_order' => WC_Retailcrm_Logger::formatWcObject($wcOrder)]
);
$needRecalculate = false;
@ -380,17 +370,7 @@ if (!class_exists('WC_Retailcrm_Orders')) :
}
}
} catch (Throwable $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'Error message: %s - Exception in file: %s on line: %s.',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
return null;
}
@ -551,7 +531,7 @@ if (!class_exists('WC_Retailcrm_Orders')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Process WC_Order_Item_Product ' . $id,
['wc_order_item_product' => WC_Retailcrm_Logger::formatWCObject($item)]
['wc_order_item_product' => WC_Retailcrm_Logger::formatWcObject($item)]
);
$crmItem = $crmItems[$id] ?? null;
$orderItems[] = $this->order_item->build($item, $crmItem)->getData();

View file

@ -107,17 +107,7 @@ if (!class_exists('WC_Retailcrm_Upload_Discount_Price')):
unset($chunks);
} catch (\Throwable $exception) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf(
'Exception: %s in file %s on line %s',
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
WC_Retailcrm_Logger::TYPE['exc']
);
WC_Retailcrm_Logger::exception(__METHOD__, $exception);
return;
}

View file

@ -17,13 +17,10 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) :
class WC_Retailcrm_Logger
{
/** @var string */
const HANDLE = 'retailcrm';
const TYPE = [
'req' => 'REQUEST',
'res' => 'RESPONSE',
'exc' => 'EXCEPTION',
];
private const HANDLE = 'retailcrm';
public const REQUEST = 'REQUEST';
public const RESPONSE = 'RESPONSE';
public CONST EXCEPTION = 'EXCEPTION';
/**
* @var \WC_Logger_Interface $instance
@ -41,11 +38,11 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) :
private static $logIdentifier;
/**
* First called action name
* Current called hook name
*
* @var string $entrypoint
* @var string $currentHook
*/
private static $entrypoint;
private static $currentHook;
/**
* First called action time
@ -66,7 +63,7 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) :
*/
private static function getInstance(): WC_Logger_Interface
{
if (empty(static::$instance)) {
if (!static::$instance instanceof WC_Logger) {
static::$instance = new WC_Logger(self::$additionalHandlers);
}
@ -88,16 +85,14 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) :
* @param $id
* @return void
*/
public static function setEntry(string $action, $id = null)
public static function setHook(string $action, $id = null)
{
if (empty(static::$entrypoint)) {
static::$entrypoint = $id === null ? $action : sprintf('%s-%s', $action, $id);
}
static::$currentHook = $id === null ? $action : sprintf('%s-%d', $action, (int) $id);
}
private static function getIdentifier(): string
{
if (empty(static::$logIdentifier)) {
if (!is_string(static::$logIdentifier)) {
static::$logIdentifier = substr(wp_generate_uuid4(), 0, 8);
}
@ -106,13 +101,37 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) :
private static function getStartTime(): float
{
if (empty(static::$startTime)) {
if (!is_float(static::$startTime)) {
static::$startTime = microtime(true);
}
return static::$startTime;
}
/**
* Exception logging
*
* @param string $method
* @param Throwable $exception
* @param string $additionalMessage
* @return void
*/
public static function exception(string $method, Throwable $exception, string $additionalMessage = '')
{
self::error(
$method,
sprintf(
'%s%s - Exception in file %s on line %s',
$additionalMessage,
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
),
['trace' => $exception->getTraceAsString()],
self::EXCEPTION
);
}
/**
* Error logging
*
@ -157,7 +176,7 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) :
$message = sprintf(
'%s [%s] <%s> %s=> %s',
self::getIdentifier(),
self::$entrypoint,
self::$currentHook,
$method,
$type ? $type . ' ' : '',
$message
@ -166,58 +185,83 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) :
self::getInstance()->log($level, $message, $context);
}
public static function formatWCObject($object): array
/**
* Extracts information useful for logs from an object
*
* @param $object
* @return array
*/
public static function formatWcObject($object): array
{
if ($object instanceof WC_Order) {
return [
'id' => $object->get_id(),
'status' => $object->get_status(),
'date_modified' => $object->get_date_modified(),
'total' => $object->get_total(),
'shipping' => [
'first_name' => $object->get_shipping_first_name(),
'last_name' => $object->get_shipping_last_name(),
'company' => $object->get_shipping_company(),
'address_1' => $object->get_shipping_address_1(),
'address_2' => $object->get_shipping_address_2(),
'city' => $object->get_shipping_city(),
'state' => $object->get_shipping_state(),
'postcode' => $object->get_shipping_postcode(),
'country' => $object->get_shipping_country(),
'phone' => method_exists($object, 'get_shipping_phone')
? $object->get_shipping_phone() : $object->get_billing_phone(),
],
'email' => $object->get_billing_email(),
'payment_method_title' => $object->get_payment_method_title(),
'date_paid' => $object->get_date_paid(),
];
return self::formatWcOrder($object);
}
if ($object instanceof WC_Customer) {
return [
'id' => $object->get_id(),
'date_modified' => $object->get_date_modified(),
'email' => $object->get_email(),
'display_name' => $object->get_display_name(),
'role' => $object->get_role(),
'username' => $object->get_username(),
'shipping' => [
'first_name' => $object->get_shipping_first_name(),
'last_name' => $object->get_shipping_last_name(),
'company' => $object->get_shipping_company(),
'address_1' => $object->get_shipping_address_1(),
'address_2' => $object->get_shipping_address_2(),
'city' => $object->get_shipping_city(),
'state' => $object->get_shipping_state(),
'postcode' => $object->get_shipping_postcode(),
'country' => $object->get_shipping_country(),
'phone' => method_exists($object, 'get_shipping_phone')
? $object->get_shipping_phone() : $object->get_billing_phone(),
],
];
return self::formatWcCustomer($object);
}
return method_exists($object, 'get_data') ? (array_filter($object->get_data())) : [$object];
if (is_object($object)) {
return method_exists($object, 'get_data') ? (array_filter($object->get_data())) : [$object];
}
return [$object];
}
public static function formatWcOrder(WC_Order $order) {
return [
'id' => $order->get_id(),
'status' => $order->get_status(),
'date_modified' => $order->get_date_modified(),
'total' => $order->get_total(),
'shipping' => [
'first_name' => $order->get_shipping_first_name(),
'last_name' => $order->get_shipping_last_name(),
'company' => $order->get_shipping_company(),
'address_1' => $order->get_shipping_address_1(),
'address_2' => $order->get_shipping_address_2(),
'city' => $order->get_shipping_city(),
'state' => $order->get_shipping_state(),
'postcode' => $order->get_shipping_postcode(),
'country' => $order->get_shipping_country(),
'phone' => method_exists($order, 'get_shipping_phone') ? $order->get_shipping_phone() : '',
],
'billing' => [
'phone' => $order->get_billing_phone()
],
'email' => $order->get_billing_email(),
'payment_method_title' => $order->get_payment_method_title(),
'date_paid' => $order->get_date_paid(),
];
}
public static function formatWcCustomer(WC_Customer $customer)
{
return [
'id' => $customer->get_id(),
'date_modified' => $customer->get_date_modified(),
'firstName' => $customer->get_first_name(),
'lastName' => $customer->get_last_name(),
'email' => $customer->get_email(),
'display_name' => $customer->get_display_name(),
'role' => $customer->get_role(),
'username' => $customer->get_username(),
'shipping' => [
'first_name' => $customer->get_shipping_first_name(),
'last_name' => $customer->get_shipping_last_name(),
'company' => $customer->get_shipping_company(),
'address_1' => $customer->get_shipping_address_1(),
'address_2' => $customer->get_shipping_address_2(),
'city' => $customer->get_shipping_city(),
'state' => $customer->get_shipping_state(),
'postcode' => $customer->get_shipping_postcode(),
'country' => $customer->get_shipping_country(),
'phone' => method_exists($customer, 'get_shipping_phone') ? $customer->get_shipping_phone() : '',
],
'billing' => [
'phone' => $customer->get_billing_phone()
],
];
}
}
endif;

View file

@ -36,7 +36,7 @@ class WC_Retailcrm_Customer_Address extends WC_Retailcrm_Abstracts_Address
WC_Retailcrm_Logger::error(
__METHOD__,
'Error: Customer address is empty',
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($customer)]
['wc_customer' => WC_Retailcrm_Logger::formatWcObject($customer)]
);
}

View file

@ -54,7 +54,7 @@ class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Add
WC_Retailcrm_Logger::error(
__METHOD__,
'Error: Corporate Customer address is empty.',
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($customer)]
['wc_customer' => WC_Retailcrm_Logger::formatWcObject($customer)]
);
}

View file

@ -67,8 +67,8 @@ class WC_Retailcrm_Customer_Switcher_Result
__METHOD__,
'Saving WC_Customer and WC_Order',
[
'wc_customer' => WC_Retailcrm_Logger::formatWCObject($this->wcCustomer),
'wc_order' => WC_Retailcrm_Logger::formatWCObject($this->wcOrder),
'wc_customer' => WC_Retailcrm_Logger::formatWcObject($this->wcCustomer),
'wc_order' => WC_Retailcrm_Logger::formatWcObject($this->wcOrder),
]
);