| 1 |
<?php |
| 2 |
|
| 3 |
if (! defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} // Exit if accessed directly |
| 6 |
|
| 7 |
if (class_exists('WCPN_Log')) { |
| 8 |
return; |
| 9 |
} |
| 10 |
|
| 11 |
class WCPN_Log |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* Log data if the error logging setting is enabled. |
| 16 |
* |
| 17 |
* @param string ...$messages |
| 18 |
*/ |
| 19 |
public static function add(string ...$messages): void |
| 20 |
{ |
| 21 |
if (! WCPOST()->setting_collection->isEnabled(WCPOST_Settings::SETTING_ERROR_LOGGING)) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
$message = implode("\n", $messages); |
| 26 |
|
| 27 |
// Starting with WooCommerce 2.7, logging can be grouped by context and severity. |
| 28 |
if (class_exists("WC_Logger") && version_compare(WOOCOMMERCE_VERSION, "2.7", ">=")) { |
| 29 |
try { |
| 30 |
(wc_get_logger())->debug($message, ["source" => "wc-postnl"]); |
| 31 |
} catch (Exception $e) { |
| 32 |
exit($e); |
| 33 |
} |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
if (class_exists("WC_Logger")) { |
| 38 |
$wc_logger = function_exists("wc_get_logger") ? wc_get_logger() : new WC_Logger(); |
| 39 |
$wc_logger->add("wc-postnl", $message); |
| 40 |
|
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// Old WC versions didn't have a logger |
| 45 |
// add file in upload folder - wp-content/uploads |
| 46 |
$upload_dir = wp_upload_dir(); |
| 47 |
$upload_base = trailingslashit($upload_dir["basedir"]); |
| 48 |
$log_file = $upload_base . "postnl_log.txt"; |
| 49 |
$current_date_time = date("Y-m-d H:i:s"); |
| 50 |
$message = $current_date_time . " " . $message . "n"; |
| 51 |
file_put_contents($log_file, $message, FILE_APPEND); |
| 52 |
|
| 53 |
return; |
| 54 |
} |
| 55 |
} |
| 56 |
|