woocommerce/includes/log-handlers/class-wc-log-handler-db.php

118 lines
2.8 KiB
PHP
Raw Normal View History

2016-11-20 14:17:55 +00:00
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Handles log entries by writing to database.
*
* @class WC_Log_Handler_DB
* @version 1.0.0
* @package WooCommerce/Classes/Log_Handlers
* @category Class
* @author WooThemes
*/
class WC_Log_Handler_DB extends WC_Log_Handler {
/**
* Handle a log entry.
*
* @param int $timestamp Log timestamp.
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
* @param string $message Log message.
* @param array $context {
* Additional information for log handlers.
*
2016-12-21 19:15:19 +00:00
* @type string $source Optional. Source will be available in log table. Default '';
2016-11-20 14:17:55 +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 {
2016-12-21 19:15:19 +00:00
$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.
*
* @param int $timestamp Log timestamp.
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
* @param string $message Log message.
2016-12-21 19:15:19 +00:00
* @param string $source Log source. Useful for filtering and sorting.
2016-11-20 14:17:55 +00:00
* @param array $context {
* Context will be serialized and stored in database.
* }
*
* @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 ),
'level' => WC_Log_Levels::get_level_severity( $level ),
2016-11-20 14:17:55 +00:00
'message' => $message,
2016-12-21 19:15:19 +00:00
'source' => $source,
2016-11-20 14:17:55 +00:00
);
$format = array(
'%s',
'%d',
'%s',
'%s',
'%s', // possible serialized context
);
2016-11-20 14:17:55 +00:00
if ( ! empty( $context ) ) {
$insert['context'] = serialize( $context );
}
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" );
}
/**
* Delete selected logs from DB.
*
* @param int|string|array Log ID or array of Log IDs to be deleted.
*
* @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 ) . ')';
$query = $wpdb->prepare(
"DELETE FROM {$wpdb->prefix}woocommerce_log WHERE log_id IN {$query_in}",
$log_ids
);
return $wpdb->query( $query );
}
2016-11-20 14:17:55 +00:00
}