| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Logger file. |
| 4 |
* |
| 5 |
* @package PostNLWooCommerce |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PostNLWooCommerce; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Logger |
| 16 |
* |
| 17 |
* @package PostNLWooCommerce |
| 18 |
*/ |
| 19 |
class Logger { |
| 20 |
|
| 21 |
/** |
| 22 |
* Debug flag. |
| 23 |
* |
| 24 |
* @var debug. |
| 25 |
*/ |
| 26 |
private $debug; |
| 27 |
|
| 28 |
/** |
| 29 |
* Logger constructor. |
| 30 |
* |
| 31 |
* @param boolean $debug debug flag. |
| 32 |
*/ |
| 33 |
public function __construct( $debug ) { |
| 34 |
$this->debug = $debug; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Check if logging is enabled. |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
public function is_enabled() { |
| 43 |
return empty( $this->debug ) || false === is_bool( $this->debug ) ? false : $this->debug; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Write the message to log. |
| 48 |
* |
| 49 |
* @param Mixed $message Message to be written in log. |
| 50 |
*/ |
| 51 |
public function write( $message ) { |
| 52 |
// Check if enabled. |
| 53 |
if ( ! $this->is_enabled() ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$message = apply_filters( 'postnl_logger_write_message', $message ); |
| 58 |
$message = $this->check_pdf_content( $message ); |
| 59 |
|
| 60 |
if ( is_array( $message ) || is_object( $message ) ) { |
| 61 |
$message = print_r( $message, true ); |
| 62 |
} |
| 63 |
|
| 64 |
// Logger object. |
| 65 |
$wc_logger = new \WC_Logger(); |
| 66 |
|
| 67 |
// Add to logger. |
| 68 |
$wc_logger->add( 'PostNLWooCommerce', $message ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Check if the content has PDF binary value. |
| 73 |
* |
| 74 |
* @param Mixed $message Message to be written in log. |
| 75 |
* |
| 76 |
* @return Mixed. |
| 77 |
*/ |
| 78 |
public function check_pdf_content( $message ) { |
| 79 |
if ( ! Utils::is_json( $message ) ) { |
| 80 |
return $message; |
| 81 |
} |
| 82 |
|
| 83 |
$message = json_decode( $message, true ); |
| 84 |
$message_types = Utils::get_label_response_type(); |
| 85 |
|
| 86 |
foreach ( $message_types as $type => $content_type ) { |
| 87 |
if ( empty( $message[ $type ] ) ) { |
| 88 |
continue; |
| 89 |
} |
| 90 |
|
| 91 |
foreach ( $message[ $type ] as $shipment_idx => $shipment_contents ) { |
| 92 |
|
| 93 |
if ( empty( $shipment_contents['Labels'] ) ) { |
| 94 |
continue 2; |
| 95 |
} |
| 96 |
|
| 97 |
foreach ( $shipment_contents['Labels'] as $label_idx => $label_contents ) { |
| 98 |
if ( empty( $label_contents['Content'] ) ) { |
| 99 |
continue 3; |
| 100 |
} |
| 101 |
|
| 102 |
if ( empty( $label_contents[ $content_type['content_type_key'] ] ) ) { |
| 103 |
continue 3; |
| 104 |
} |
| 105 |
|
| 106 |
if ( $content_type['content_type_value'] !== $label_contents[ $content_type['content_type_key'] ] ) { |
| 107 |
continue 3; |
| 108 |
} |
| 109 |
|
| 110 |
$message[ $type ][ $shipment_idx ]['Labels'][ $label_idx ]['Content'] = '[PDF data]'; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return $message; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get log URL. |
| 120 |
*/ |
| 121 |
public static function get_log_url() { |
| 122 |
return admin_url( 'admin.php?page=wc-status&tab=logs' ); |
| 123 |
} |
| 124 |
} |
| 125 |
|