2011-11-15 13:08:56 +00:00
|
|
|
<?php
|
2015-11-06 09:22:19 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* Allows log files to be written to for debugging purposes
|
2011-11-15 13:08:56 +00:00
|
|
|
*
|
2016-04-13 06:46:30 +00:00
|
|
|
* @class WC_Logger
|
|
|
|
* @version 1.6.4
|
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
2011-11-15 13:08:56 +00:00
|
|
|
*/
|
2012-01-27 16:38:39 +00:00
|
|
|
class WC_Logger {
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/**
|
2016-01-06 15:24:47 +00:00
|
|
|
* Stores open file _handles.
|
|
|
|
*
|
|
|
|
* @var array
|
2012-08-15 17:08:42 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2012-12-14 21:57:23 +00:00
|
|
|
private $_handles;
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
2012-08-15 17:08:42 +00:00
|
|
|
* Constructor for the logger.
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2012-12-14 21:44:43 +00:00
|
|
|
public function __construct() {
|
2012-12-14 22:00:04 +00:00
|
|
|
$this->_handles = array();
|
2011-11-15 13:08:56 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2012-12-14 21:44:43 +00:00
|
|
|
public function __destruct() {
|
2014-05-28 10:12:35 +00:00
|
|
|
foreach ( $this->_handles as $handle ) {
|
2016-04-19 15:56:41 +00:00
|
|
|
if ( is_resource( $handle ) ) {
|
|
|
|
fclose( $handle );
|
|
|
|
}
|
2014-05-28 10:12:35 +00:00
|
|
|
}
|
2011-11-15 13:08:56 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
/**
|
2012-08-14 19:42:38 +00:00
|
|
|
* Open log file for writing.
|
|
|
|
*
|
2016-04-16 04:26:19 +00:00
|
|
|
* @param string $handle
|
|
|
|
* @param string $mode
|
2012-08-14 19:42:38 +00:00
|
|
|
* @return bool success
|
2011-11-15 13:08:56 +00:00
|
|
|
*/
|
2016-04-19 15:56:41 +00:00
|
|
|
protected function open( $handle, $mode = 'a' ) {
|
2014-05-28 10:12:35 +00:00
|
|
|
if ( isset( $this->_handles[ $handle ] ) ) {
|
2012-08-14 19:42:38 +00:00
|
|
|
return true;
|
2014-05-28 10:12:35 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2016-04-16 04:26:19 +00:00
|
|
|
if ( $this->_handles[ $handle ] = @fopen( wc_get_log_file_path( $handle ), $mode ) ) {
|
2012-08-14 19:42:38 +00:00
|
|
|
return true;
|
2014-05-28 10:12:35 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2016-04-19 15:56:41 +00:00
|
|
|
/**
|
|
|
|
* Close a handle.
|
|
|
|
*
|
|
|
|
* @param string $handle
|
|
|
|
* @return bool success
|
|
|
|
*/
|
|
|
|
protected function close( $handle ) {
|
|
|
|
$result = false;
|
|
|
|
|
|
|
|
if ( is_resource( $this->_handles[ $handle ] ) ) {
|
|
|
|
$result = fclose( $this->_handles[ $handle ] );
|
|
|
|
unset( $this->_handles[ $handle ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
/**
|
2012-08-14 19:42:38 +00:00
|
|
|
* Add a log entry to chosen file.
|
|
|
|
*
|
2015-01-23 14:37:20 +00:00
|
|
|
* @param string $handle
|
|
|
|
* @param string $message
|
2016-04-13 06:46:30 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
2011-11-15 13:08:56 +00:00
|
|
|
*/
|
|
|
|
public function add( $handle, $message ) {
|
2016-04-13 06:46:30 +00:00
|
|
|
$result = false;
|
|
|
|
|
2013-01-10 14:56:07 +00:00
|
|
|
if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) {
|
2016-04-13 06:46:30 +00:00
|
|
|
$time = date_i18n( 'm-d-Y @ H:i:s -' ); // Grab Time
|
2016-04-19 15:56:41 +00:00
|
|
|
$result = fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" );
|
2012-08-14 19:42:38 +00:00
|
|
|
}
|
2015-07-16 19:55:48 +00:00
|
|
|
|
2015-07-10 19:26:30 +00:00
|
|
|
do_action( 'woocommerce_log_add', $handle, $message );
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2016-04-19 13:00:40 +00:00
|
|
|
return false !== $result;
|
2016-04-13 06:46:30 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
/**
|
2013-03-03 17:07:31 +00:00
|
|
|
* Clear entries from chosen file.
|
2012-08-14 19:42:38 +00:00
|
|
|
*
|
2016-04-16 04:26:19 +00:00
|
|
|
* @param string $handle
|
2016-04-13 06:46:30 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
2011-11-15 13:08:56 +00:00
|
|
|
*/
|
|
|
|
public function clear( $handle ) {
|
2016-04-13 06:46:30 +00:00
|
|
|
$result = false;
|
|
|
|
|
2016-04-19 15:31:36 +00:00
|
|
|
// Close the file if it's already open.
|
2016-04-19 15:56:41 +00:00
|
|
|
$this->close( $handle );
|
2016-04-19 15:31:36 +00:00
|
|
|
|
2016-04-16 04:26:19 +00:00
|
|
|
/**
|
|
|
|
* $this->open( $handle, 'w' ) == Open the file for writing only. Place the file pointer at the beginning of the file,
|
|
|
|
* and truncate the file to zero length.
|
|
|
|
*/
|
|
|
|
if ( $this->open( $handle, 'w' ) && is_resource( $this->_handles[ $handle ] ) ) {
|
|
|
|
$result = true;
|
2014-05-28 10:12:35 +00:00
|
|
|
}
|
2015-07-16 19:55:48 +00:00
|
|
|
|
2015-07-10 19:26:30 +00:00
|
|
|
do_action( 'woocommerce_log_clear', $handle );
|
2016-04-13 06:46:30 +00:00
|
|
|
|
2016-04-19 13:00:40 +00:00
|
|
|
return $result;
|
2011-11-15 13:08:56 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-07-03 06:16:11 +00:00
|
|
|
/**
|
|
|
|
* Remove/delete the chosen file.
|
|
|
|
*
|
|
|
|
* @param string $handle
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function remove( $handle ) {
|
|
|
|
$removed = false;
|
|
|
|
$file = wc_get_log_file_path( $handle );
|
|
|
|
|
|
|
|
if ( is_file( $file ) && is_writable( $file ) ) {
|
|
|
|
// Close first to be certain no processes keep it alive after it is unlinked.
|
|
|
|
$this->close( $handle );
|
|
|
|
$removed = unlink( $file );
|
2016-08-02 13:42:27 +00:00
|
|
|
} elseif ( is_file( trailingslashit( WC_LOG_DIR ) . $handle . '.log' ) && is_writable( trailingslashit( WC_LOG_DIR ) . $handle . '.log' ) ) {
|
|
|
|
$this->close( $handle );
|
|
|
|
$removed = unlink( trailingslashit( WC_LOG_DIR ) . $handle . '.log' );
|
2016-07-03 06:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do_action( 'woocommerce_log_remove', $handle, $removed );
|
|
|
|
|
|
|
|
return $removed;
|
|
|
|
}
|
2016-08-02 13:42:27 +00:00
|
|
|
}
|