1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; // Exit if accessed directly |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Allows log files to be written to for debugging purposes |
9
|
|
|
* |
10
|
|
|
* @class WC_Logger |
11
|
|
|
* @version 1.6.4 |
12
|
|
|
* @package WooCommerce/Classes |
13
|
|
|
* @category Class |
14
|
|
|
* @author WooThemes |
15
|
|
|
*/ |
16
|
|
|
class WC_Logger { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Stores open file _handles. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
* @access private |
23
|
|
|
*/ |
24
|
|
|
private $_handles; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor for the logger. |
28
|
|
|
*/ |
29
|
|
|
public function __construct() { |
30
|
|
|
$this->_handles = array(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Destructor. |
35
|
|
|
*/ |
36
|
|
|
public function __destruct() { |
37
|
|
|
foreach ( $this->_handles as $handle ) { |
38
|
|
|
if ( is_resource( $handle ) ) { |
39
|
|
|
fclose( $handle ); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Open log file for writing. |
46
|
|
|
* |
47
|
|
|
* @param string $handle |
48
|
|
|
* @param string $mode |
49
|
|
|
* @return bool success |
50
|
|
|
*/ |
51
|
|
|
protected function open( $handle, $mode = 'a' ) { |
52
|
|
|
if ( isset( $this->_handles[ $handle ] ) ) { |
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ( $this->_handles[ $handle ] = @fopen( wc_get_log_file_path( $handle ), $mode ) ) { |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Close a handle. |
65
|
|
|
* |
66
|
|
|
* @param string $handle |
67
|
|
|
* @return bool success |
68
|
|
|
*/ |
69
|
|
|
protected function close( $handle ) { |
70
|
|
|
$result = false; |
71
|
|
|
|
72
|
|
|
if ( is_resource( $this->_handles[ $handle ] ) ) { |
73
|
|
|
$result = fclose( $this->_handles[ $handle ] ); |
74
|
|
|
unset( $this->_handles[ $handle ] ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $result; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Add a log entry to chosen file. |
82
|
|
|
* |
83
|
|
|
* @param string $handle |
84
|
|
|
* @param string $message |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function add( $handle, $message ) { |
89
|
|
|
$result = false; |
90
|
|
|
|
91
|
|
|
if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) { |
92
|
|
|
$time = date_i18n( 'm-d-Y @ H:i:s -' ); // Grab Time |
93
|
|
|
$result = fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
do_action( 'woocommerce_log_add', $handle, $message ); |
97
|
|
|
|
98
|
|
|
return false !== $result; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Clear entries from chosen file. |
103
|
|
|
* |
104
|
|
|
* @param string $handle |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public function clear( $handle ) { |
109
|
|
|
$result = false; |
110
|
|
|
|
111
|
|
|
// Close the file if it's already open. |
112
|
|
|
$this->close( $handle ); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* $this->open( $handle, 'w' ) == Open the file for writing only. Place the file pointer at the beginning of the file, |
116
|
|
|
* and truncate the file to zero length. |
117
|
|
|
*/ |
118
|
|
|
if ( $this->open( $handle, 'w' ) && is_resource( $this->_handles[ $handle ] ) ) { |
119
|
|
|
$result = true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
do_action( 'woocommerce_log_clear', $handle ); |
123
|
|
|
|
124
|
|
|
return $result; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.