2016-11-20 14:17:55 +00:00
|
|
|
<?php
|
2018-03-07 13:49:17 +00:00
|
|
|
/**
|
|
|
|
* Class WC_Log_Handler_DB file.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Log Handlers
|
|
|
|
*/
|
|
|
|
|
2016-11-20 14:17:55 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2018-03-07 13:49:17 +00:00
|
|
|
exit; // Exit if accessed directly.
|
2016-11-20 14:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles log entries by writing to database.
|
|
|
|
*
|
|
|
|
* @class WC_Log_Handler_DB
|
|
|
|
* @version 1.0.0
|
|
|
|
* @package WooCommerce/Classes/Log_Handlers
|
|
|
|
*/
|
|
|
|
class WC_Log_Handler_DB extends WC_Log_Handler {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a log entry.
|
|
|
|
*
|
2018-03-07 13:30:00 +00:00
|
|
|
* @param int $timestamp Log timestamp.
|
2018-03-07 13:49:17 +00:00
|
|
|
* @param string $level emergency|alert|critical|error|warning|notice|info|debug.
|
2016-11-20 14:17:55 +00:00
|
|
|
* @param string $message Log message.
|
2018-03-07 13:30:00 +00:00
|
|
|
* @param array $context {
|
|
|
|
* Additional information for log handlers.
|
2016-11-20 14:17:55 +00:00
|
|
|
*
|
2017-01-08 20:38:37 +00:00
|
|
|
* @type string $source Optional. Source will be available in log table.
|
|
|
|
* If no source is provided, attempt to provide sensible default.
|
2016-11-20 14:17:55 +00:00
|
|
|
* }
|
|
|
|
*
|
2017-01-08 20:38:37 +00:00
|
|
|
* @see WC_Log_Handler_DB::get_log_source() for default source.
|
|
|
|
*
|
2016-12-15 14:34:48 +00:00
|
|
|
* @return bool False if value was not handled and true if value was handled.
|
2016-11-20 14:17:55 +00:00
|
|
|
*/
|
|
|
|
public function handle( $timestamp, $level, $message, $context ) {
|
|
|
|
|
2016-12-21 19:15:19 +00:00
|
|
|
if ( isset( $context['source'] ) && $context['source'] ) {
|
|
|
|
$source = $context['source'];
|
2016-11-20 14:17:55 +00:00
|
|
|
} else {
|
2017-01-08 20:38:37 +00:00
|
|
|
$source = $this->get_log_source();
|
2016-11-20 14:17:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 19:15:19 +00:00
|
|
|
return $this->add( $timestamp, $level, $message, $source, $context );
|
2016-11-20 14:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a log entry to chosen file.
|
|
|
|
*
|
2018-03-07 13:30:00 +00:00
|
|
|
* @param int $timestamp Log timestamp.
|
2018-03-07 13:49:17 +00:00
|
|
|
* @param string $level emergency|alert|critical|error|warning|notice|info|debug.
|
2016-11-20 14:17:55 +00:00
|
|
|
* @param string $message Log message.
|
2016-12-21 19:15:19 +00:00
|
|
|
* @param string $source Log source. Useful for filtering and sorting.
|
2018-03-07 13:49:17 +00:00
|
|
|
* @param array $context Context will be serialized and stored in database.
|
2016-11-20 14:17:55 +00:00
|
|
|
*
|
|
|
|
* @return bool True if write was successful.
|
|
|
|
*/
|
2016-12-21 19:15:19 +00:00
|
|
|
protected static function add( $timestamp, $level, $message, $source, $context ) {
|
2016-11-20 14:17:55 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$insert = array(
|
|
|
|
'timestamp' => date( 'Y-m-d H:i:s', $timestamp ),
|
2018-03-07 13:30:00 +00:00
|
|
|
'level' => WC_Log_Levels::get_level_severity( $level ),
|
|
|
|
'message' => $message,
|
|
|
|
'source' => $source,
|
2016-11-20 14:17:55 +00:00
|
|
|
);
|
|
|
|
|
2016-12-11 17:59:12 +00:00
|
|
|
$format = array(
|
|
|
|
'%s',
|
|
|
|
'%d',
|
|
|
|
'%s',
|
|
|
|
'%s',
|
2018-03-07 13:49:17 +00:00
|
|
|
'%s', // possible serialized context.
|
2016-12-11 17:59:12 +00:00
|
|
|
);
|
|
|
|
|
2016-11-20 14:17:55 +00:00
|
|
|
if ( ! empty( $context ) ) {
|
2018-05-01 15:37:30 +00:00
|
|
|
$insert['context'] = serialize( $context ); // @codingStandardsIgnoreLine.
|
2016-11-20 14:17:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 17:59:12 +00:00
|
|
|
return false !== $wpdb->insert( "{$wpdb->prefix}woocommerce_log", $insert, $format );
|
2016-11-20 14:17:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-26 19:05:59 +00:00
|
|
|
/**
|
|
|
|
* Clear all logs from the DB.
|
|
|
|
*
|
|
|
|
* @return bool True if flush was successful.
|
|
|
|
*/
|
|
|
|
public static function flush() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
return $wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_log" );
|
|
|
|
}
|
|
|
|
|
2018-04-30 17:04:52 +00:00
|
|
|
/**
|
|
|
|
* Clear entries for a chosen handle/source.
|
|
|
|
*
|
|
|
|
* @param string $source Log source.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function clear( $source ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
return $wpdb->query(
|
|
|
|
$wpdb->prepare(
|
|
|
|
"DELETE FROM {$wpdb->prefix}woocommerce_log WHERE source = %s",
|
|
|
|
$source
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-15 21:03:12 +00:00
|
|
|
/**
|
|
|
|
* Delete selected logs from DB.
|
|
|
|
*
|
2018-03-07 13:49:17 +00:00
|
|
|
* @param int|string|array $log_ids Log ID or array of Log IDs to be deleted.
|
2016-12-15 21:03:12 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function delete( $log_ids ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
if ( ! is_array( $log_ids ) ) {
|
|
|
|
$log_ids = array( $log_ids );
|
|
|
|
}
|
|
|
|
|
|
|
|
$format = array_fill( 0, count( $log_ids ), '%d' );
|
|
|
|
|
|
|
|
$query_in = '(' . implode( ',', $format ) . ')';
|
|
|
|
|
2018-05-01 15:37:30 +00:00
|
|
|
return $wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_log WHERE log_id IN {$query_in}" ); // @codingStandardsIgnoreLine.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all logs older than a defined timestamp.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
* @param integer $timestamp Timestamp to delete logs before.
|
|
|
|
*/
|
|
|
|
public static function delete_logs_before_timestamp( $timestamp = 0 ) {
|
|
|
|
if ( ! $timestamp ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$wpdb->query(
|
2018-03-07 13:49:17 +00:00
|
|
|
$wpdb->prepare(
|
2018-05-01 15:37:30 +00:00
|
|
|
"DELETE FROM {$wpdb->prefix}woocommerce_log WHERE timestamp < %d",
|
|
|
|
$timestamp
|
2018-03-07 13:49:17 +00:00
|
|
|
)
|
2016-12-15 21:03:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-08 20:38:37 +00:00
|
|
|
/**
|
|
|
|
* Get appropriate source based on file name.
|
|
|
|
*
|
|
|
|
* Try to provide an appropriate source in case none is provided.
|
|
|
|
*
|
|
|
|
* @return string Text to use as log source. "" (empty string) if none is found.
|
|
|
|
*/
|
|
|
|
protected static function get_log_source() {
|
|
|
|
static $ignore_files = array( 'class-wc-log-handler-db', 'class-wc-logger' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PHP < 5.3.6 correct behavior
|
2018-03-07 13:30:00 +00:00
|
|
|
*
|
2017-01-08 20:38:37 +00:00
|
|
|
* @see http://php.net/manual/en/function.debug-backtrace.php#refsect1-function.debug-backtrace-parameters
|
|
|
|
*/
|
|
|
|
if ( defined( 'DEBUG_BACKTRACE_IGNORE_ARGS' ) ) {
|
2018-03-07 13:49:17 +00:00
|
|
|
$debug_backtrace_arg = DEBUG_BACKTRACE_IGNORE_ARGS; // phpcs:ignore PHPCompatibility.PHP.NewConstants.debug_backtrace_ignore_argsFound
|
2017-01-08 20:38:37 +00:00
|
|
|
} else {
|
|
|
|
$debug_backtrace_arg = false;
|
|
|
|
}
|
|
|
|
|
2018-05-01 15:37:30 +00:00
|
|
|
$trace = debug_backtrace( $debug_backtrace_arg ); // @codingStandardsIgnoreLine.
|
2017-01-08 20:38:37 +00:00
|
|
|
foreach ( $trace as $t ) {
|
|
|
|
if ( isset( $t['file'] ) ) {
|
|
|
|
$filename = pathinfo( $t['file'], PATHINFO_FILENAME );
|
2018-03-07 13:49:17 +00:00
|
|
|
if ( ! in_array( $filename, $ignore_files, true ) ) {
|
2017-01-08 20:38:37 +00:00
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-11-20 14:17:55 +00:00
|
|
|
}
|