class-wc-log-handler-db.php
5 years ago
class-wc-log-handler-email.php
5 years ago
class-wc-log-handler-file.php
5 years ago
class-wc-log-handler-db.php
190 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WC_Log_Handler_DB file. |
| 4 | * |
| 5 | * @package WooCommerce\Log Handlers |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Constants; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Handles log entries by writing to database. |
| 16 | * |
| 17 | * @class WC_Log_Handler_DB |
| 18 | * @version 1.0.0 |
| 19 | * @package WooCommerce/Classes/Log_Handlers |
| 20 | */ |
| 21 | class WC_Log_Handler_DB extends WC_Log_Handler { |
| 22 | |
| 23 | /** |
| 24 | * Handle a log entry. |
| 25 | * |
| 26 | * @param int $timestamp Log timestamp. |
| 27 | * @param string $level emergency|alert|critical|error|warning|notice|info|debug. |
| 28 | * @param string $message Log message. |
| 29 | * @param array $context { |
| 30 | * Additional information for log handlers. |
| 31 | * |
| 32 | * @type string $source Optional. Source will be available in log table. |
| 33 | * If no source is provided, attempt to provide sensible default. |
| 34 | * } |
| 35 | * |
| 36 | * @see WC_Log_Handler_DB::get_log_source() for default source. |
| 37 | * |
| 38 | * @return bool False if value was not handled and true if value was handled. |
| 39 | */ |
| 40 | public function handle( $timestamp, $level, $message, $context ) { |
| 41 | |
| 42 | if ( isset( $context['source'] ) && $context['source'] ) { |
| 43 | $source = $context['source']; |
| 44 | } else { |
| 45 | $source = $this->get_log_source(); |
| 46 | } |
| 47 | |
| 48 | return $this->add( $timestamp, $level, $message, $source, $context ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Add a log entry to chosen file. |
| 53 | * |
| 54 | * @param int $timestamp Log timestamp. |
| 55 | * @param string $level emergency|alert|critical|error|warning|notice|info|debug. |
| 56 | * @param string $message Log message. |
| 57 | * @param string $source Log source. Useful for filtering and sorting. |
| 58 | * @param array $context Context will be serialized and stored in database. |
| 59 | * |
| 60 | * @return bool True if write was successful. |
| 61 | */ |
| 62 | protected static function add( $timestamp, $level, $message, $source, $context ) { |
| 63 | global $wpdb; |
| 64 | |
| 65 | $insert = array( |
| 66 | 'timestamp' => date( 'Y-m-d H:i:s', $timestamp ), |
| 67 | 'level' => WC_Log_Levels::get_level_severity( $level ), |
| 68 | 'message' => $message, |
| 69 | 'source' => $source, |
| 70 | ); |
| 71 | |
| 72 | $format = array( |
| 73 | '%s', |
| 74 | '%d', |
| 75 | '%s', |
| 76 | '%s', |
| 77 | '%s', // possible serialized context. |
| 78 | ); |
| 79 | |
| 80 | if ( ! empty( $context ) ) { |
| 81 | $insert['context'] = serialize( $context ); // @codingStandardsIgnoreLine. |
| 82 | } |
| 83 | |
| 84 | return false !== $wpdb->insert( "{$wpdb->prefix}woocommerce_log", $insert, $format ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Clear all logs from the DB. |
| 89 | * |
| 90 | * @return bool True if flush was successful. |
| 91 | */ |
| 92 | public static function flush() { |
| 93 | global $wpdb; |
| 94 | |
| 95 | return $wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_log" ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Clear entries for a chosen handle/source. |
| 100 | * |
| 101 | * @param string $source Log source. |
| 102 | * @return bool |
| 103 | */ |
| 104 | public function clear( $source ) { |
| 105 | global $wpdb; |
| 106 | |
| 107 | return $wpdb->query( |
| 108 | $wpdb->prepare( |
| 109 | "DELETE FROM {$wpdb->prefix}woocommerce_log WHERE source = %s", |
| 110 | $source |
| 111 | ) |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Delete selected logs from DB. |
| 117 | * |
| 118 | * @param int|string|array $log_ids Log ID or array of Log IDs to be deleted. |
| 119 | * |
| 120 | * @return bool |
| 121 | */ |
| 122 | public static function delete( $log_ids ) { |
| 123 | global $wpdb; |
| 124 | |
| 125 | if ( ! is_array( $log_ids ) ) { |
| 126 | $log_ids = array( $log_ids ); |
| 127 | } |
| 128 | |
| 129 | $format = array_fill( 0, count( $log_ids ), '%d' ); |
| 130 | $query_in = '(' . implode( ',', $format ) . ')'; |
| 131 | return $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_log WHERE log_id IN {$query_in}", $log_ids ) ); // @codingStandardsIgnoreLine. |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Delete all logs older than a defined timestamp. |
| 136 | * |
| 137 | * @since 3.4.0 |
| 138 | * @param integer $timestamp Timestamp to delete logs before. |
| 139 | */ |
| 140 | public static function delete_logs_before_timestamp( $timestamp = 0 ) { |
| 141 | if ( ! $timestamp ) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | global $wpdb; |
| 146 | |
| 147 | $wpdb->query( |
| 148 | $wpdb->prepare( |
| 149 | "DELETE FROM {$wpdb->prefix}woocommerce_log WHERE timestamp < %s", |
| 150 | date( 'Y-m-d H:i:s', $timestamp ) |
| 151 | ) |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get appropriate source based on file name. |
| 157 | * |
| 158 | * Try to provide an appropriate source in case none is provided. |
| 159 | * |
| 160 | * @return string Text to use as log source. "" (empty string) if none is found. |
| 161 | */ |
| 162 | protected static function get_log_source() { |
| 163 | static $ignore_files = array( 'class-wc-log-handler-db', 'class-wc-logger' ); |
| 164 | |
| 165 | /** |
| 166 | * PHP < 5.3.6 correct behavior |
| 167 | * |
| 168 | * @see http://php.net/manual/en/function.debug-backtrace.php#refsect1-function.debug-backtrace-parameters |
| 169 | */ |
| 170 | if ( Constants::is_defined( 'DEBUG_BACKTRACE_IGNORE_ARGS' ) ) { |
| 171 | $debug_backtrace_arg = DEBUG_BACKTRACE_IGNORE_ARGS; // phpcs:ignore PHPCompatibility.Constants.NewConstants.debug_backtrace_ignore_argsFound |
| 172 | } else { |
| 173 | $debug_backtrace_arg = false; |
| 174 | } |
| 175 | |
| 176 | $trace = debug_backtrace( $debug_backtrace_arg ); // @codingStandardsIgnoreLine. |
| 177 | foreach ( $trace as $t ) { |
| 178 | if ( isset( $t['file'] ) ) { |
| 179 | $filename = pathinfo( $t['file'], PATHINFO_FILENAME ); |
| 180 | if ( ! in_array( $filename, $ignore_files, true ) ) { |
| 181 | return $filename; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return ''; |
| 187 | } |
| 188 | |
| 189 | } |
| 190 |