Açıklama Yok

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

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