PluginProbe
PostNL for WooCommerce / trunk
PostNL for WooCommerce vtrunk
5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 4.4.2 All 71 releases
woo-postnl / src / Logger.php

Logger.php in PostNL for WooCommerce trunk, at src/Logger.php

125 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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