Completed
Push — master ( be0293...e72af4 )
by Claudio
08:45
created

WC_Logger   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 145
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __destruct() 0 7 3
B open() 0 20 5
A close() 0 10 2
A add() 0 12 3
A clear() 0 18 3
B remove() 0 17 5
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 ( ! file_exists( wc_get_log_file_path( $handle ) ) ) {
57
			$temphandle = @fopen( wc_get_log_file_path( $handle ), 'w+' );
58
			@fclose( $temphandle );
59
60
			if ( defined( 'FS_CHMOD_FILE' ) ) {
61
				@chmod( wc_get_log_file_path( $handle ), FS_CHMOD_FILE );
62
			}
63
		}
64
65
		if ( $this->_handles[ $handle ] = @fopen( wc_get_log_file_path( $handle ), $mode ) ) {
66
			return true;
67
		}
68
69
		return false;
70
	}
71
72
	/**
73
	 * Close a handle.
74
	 *
75
	 * @param string $handle
76
	 * @return bool success
77
	 */
78
	protected function close( $handle ) {
79
		$result = false;
80
81
		if ( is_resource( $this->_handles[ $handle ] ) ) {
82
			$result = fclose( $this->_handles[ $handle ] );
83
			unset( $this->_handles[ $handle ] );
84
		}
85
86
		return $result;
87
	}
88
89
	/**
90
	 * Add a log entry to chosen file.
91
	 *
92
	 * @param string $handle
93
	 * @param string $message
94
	 *
95
	 * @return bool
96
	 */
97
	public function add( $handle, $message ) {
98
		$result = false;
99
100
		if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) {
101
			$time   = date_i18n( 'm-d-Y @ H:i:s -' ); // Grab Time
102
			$result = fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" );
103
		}
104
105
		do_action( 'woocommerce_log_add', $handle, $message );
106
107
		return false !== $result;
108
	}
109
110
	/**
111
	 * Clear entries from chosen file.
112
	 *
113
	 * @param string $handle
114
	 *
115
	 * @return bool
116
	 */
117
	public function clear( $handle ) {
118
		$result = false;
119
120
		// Close the file if it's already open.
121
		$this->close( $handle );
122
123
		/**
124
		 * $this->open( $handle, 'w' ) == Open the file for writing only. Place the file pointer at the beginning of the file,
125
		 * and truncate the file to zero length.
126
		 */
127
		if ( $this->open( $handle, 'w' ) && is_resource( $this->_handles[ $handle ] ) ) {
128
			$result = true;
129
		}
130
131
		do_action( 'woocommerce_log_clear', $handle );
132
133
		return $result;
134
	}
135
136
	/**
137
	 * Remove/delete the chosen file.
138
	 *
139
	 * @param string $handle
140
	 *
141
	 * @return bool
142
	 */
143
	public function remove( $handle ) {
144
		$removed = false;
145
		$file    = wc_get_log_file_path( $handle );
146
147
		if ( is_file( $file ) && is_writable( $file ) ) {
148
			// Close first to be certain no processes keep it alive after it is unlinked.
149
			$this->close( $handle );
150
			$removed = unlink( $file );
151
		} elseif ( is_file( trailingslashit( WC_LOG_DIR ) . $handle . '.log' ) && is_writable( trailingslashit( WC_LOG_DIR ) . $handle . '.log' ) ) {
152
			$this->close( $handle );
153
			$removed = unlink( trailingslashit( WC_LOG_DIR ) . $handle . '.log' );
154
		}
155
156
		do_action( 'woocommerce_log_remove', $handle, $removed );
157
158
		return $removed;
159
	}
160
}
161