class-wc-log-handler-db.php 4.7 KB

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