PluginProbe
YayMail – WooCommerce Email Customizer / 4.3.3
YayMail – WooCommerce Email Customizer v4.3.3
4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 3.4.2 3.4.3 All 55 releases
yaymail / src / Utils / Logger.php

Logger.php in YayMail – WooCommerce Email Customizer 4.3.3, at src/Utils/Logger.php

142 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\Utils;
4
5 /**
6 * Logger
7 */
8 class Logger {
9 private $log_directory;
10 private $wp_filesystem;
11 private $max_log_files;
12
13 /**
14 * Constructor for the Logger class.
15 *
16 * Initializes the logger with a specified log directory, prefix for log entries,
17 * and the maximum number of log files to keep. Ensures the log directory exists and
18 * performs cleanup of old log files.
19 */
20 public function __construct( $log_directory = null, $max_log_files = 30 ) {
21 if ( ! $log_directory ) {
22 $log_directory = WP_CONTENT_DIR . '/yaymail-logs';
23 }
24 $this->log_directory = $log_directory;
25 $this->max_log_files = $max_log_files;
26 $this->initialize_filesystem();
27
28 // Ensure the log directory exists (only if filesystem is available)
29 if ( $this->wp_filesystem && ! $this->wp_filesystem->is_dir( $this->log_directory ) ) {
30 $this->wp_filesystem->mkdir( $this->log_directory, 0755 );
31 }
32
33 // Clean up old log files (only if filesystem is available)
34 if ( $this->wp_filesystem ) {
35 $this->cleanup_old_logs();
36 }
37 }
38
39 private function initialize_filesystem() {
40 global $wp_filesystem;
41
42 // Initialize the WordPress filesystem
43 require_once ABSPATH . 'wp-admin/includes/file.php';
44
45 // Try to initialize WP_Filesystem, fallback to direct method if failed
46 if ( ! WP_Filesystem() || ! $wp_filesystem ) {
47 // Fallback to direct filesystem access
48 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
49 $this->wp_filesystem = new \WP_Filesystem_Direct( null );
50 } else {
51 $this->wp_filesystem = $wp_filesystem;
52 }
53 }
54
55 private function cleanup_old_logs() {
56 // Return early if filesystem is not available
57 if ( ! $this->wp_filesystem ) {
58 return;
59 }
60
61 // Get all log files in the directory
62 $files = $this->wp_filesystem->dirlist( $this->log_directory );
63
64 // Filter only log files
65 $log_files = array_filter(
66 $files,
67 function( $file ) {
68 return strpos( $file['name'], '.log' ) !== false;
69 }
70 );
71
72 // Sort log files by name (assuming the name is in the format 'Y-m-d.log')
73 usort(
74 $log_files,
75 function( $a, $b ) {
76 return strcmp( $b['name'], $a['name'] );
77 }
78 );
79
80 // If there are more log files than the allowed maximum, delete the oldest
81 if ( count( $log_files ) > $this->max_log_files ) {
82 $files_to_delete = array_slice( $log_files, $this->max_log_files );
83
84 foreach ( $files_to_delete as $file ) {
85 $this->wp_filesystem->delete( $this->log_directory . '/' . $file['name'] );
86 }
87 }
88 }
89
90 public function log( $message ) {
91 // Return early if filesystem is not available
92 if ( ! $this->wp_filesystem ) {
93 return;
94 }
95
96 // Get the current date to create a log file for each day, based on local time
97 $date = current_time( 'Y-m-d' );
98 $log_file = $this->log_directory . '/' . $date . '.log';
99
100 // Construct the log message with a timestamp based on local time
101 $timestamp = current_time( 'Y-m-d H:i:s' );
102 $log_message = "[$timestamp] $message" . PHP_EOL;
103
104 // Append the log message to the log file
105 if ( $this->wp_filesystem->exists( $log_file ) ) {
106 $existing_content = $this->wp_filesystem->get_contents( $log_file );
107 $log_message = $existing_content . $log_message;
108 }
109
110 $this->wp_filesystem->put_contents( $log_file, $log_message, FS_CHMOD_FILE );
111 }
112
113 /**
114 * Logs an exception message and sends a JSON error response.
115 * Message will display in folder wp-content/yaymail-logs
116 */
117 public function log_exception_message( $ex, $log_type = 'error', $additional_data = null ) {
118 $prefix = ( $log_type === 'warning' ) ? __( 'WARNING:', 'yaymail' ) : __( 'SYSTEM ERROR:', 'yaymail' );
119
120 $message = $prefix . ' ' . $ex->getCode() . ' : ' . $ex->getMessage();
121 $message .= PHP_EOL . $ex->getFile() . '(' . $ex->getLine() . ')';
122 $message .= PHP_EOL . $ex->getTraceAsString();
123
124 // Add additional data if provided
125 if ( $additional_data !== null ) {
126 $message .= PHP_EOL . 'Additional data: ';
127 if ( is_array( $additional_data ) || is_object( $additional_data ) ) {
128 $message .= PHP_EOL . print_r( $additional_data, true );
129 } else {
130 $message .= $additional_data;
131 }
132 }
133
134 $this->log( $message );
135
136 // Only send JSON response for errors, not for warnings
137 if ( $log_type === 'error' ) {
138 wp_send_json_error( [ 'mess' => $message ] );
139 }
140 }
141 }
142