| @@ -9,16 +9,14 @@ | ||
| 9 | 9 | private $log_directory; |
| 10 | 10 | private $wp_filesystem; |
| 11 | 11 | private $max_log_files; |
| 12 | 12 | |
| 13 | - private $ready = false; | |
| 14 | - | |
| 15 | 13 | /** |
| 16 | 14 | * Constructor for the Logger class. |
| 17 | 15 | * |
| 18 | - * Only records the log directory and retention limit. Filesystem setup (WP_Filesystem, | |
| 19 | - * mkdir, old-log cleanup) is deferred to the first log() call because Logger instances | |
| 20 | - * are created on every request by classes that rarely log. | |
| 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. | |
| 21 | 19 | */ |
| 22 | 20 | public function __construct( $log_directory = null, $max_log_files = 30 ) { |
| 23 | 21 | if ( ! $log_directory ) { |
| 24 | 22 | $log_directory = WP_CONTENT_DIR . '/yaymail-logs'; |
| @@ -24,18 +22,8 @@ | ||
| 24 | 22 | $log_directory = WP_CONTENT_DIR . '/yaymail-logs'; |
| 25 | 23 | } |
| 26 | 24 | $this->log_directory = $log_directory; |
| 27 | 25 | $this->max_log_files = $max_log_files; |
| 28 | - } | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * Initializes the filesystem, ensures the log directory exists and prunes old logs. Runs once. | |
| 32 | - */ | |
| 33 | - private function ensure_ready() { | |
| 34 | - if ( $this->ready ) { | |
| 35 | - return; | |
| 36 | - } | |
| 37 | - $this->ready = true; | |
| 38 | 26 | $this->initialize_filesystem(); |
| 39 | 27 | |
| 40 | 28 | // Ensure the log directory exists (only if filesystem is available) |
| 41 | 29 | if ( $this->wp_filesystem && ! $this->wp_filesystem->is_dir( $this->log_directory ) ) { |
| @@ -64,10 +52,10 @@ | ||
| 64 | 52 | } |
| 65 | 53 | } |
| 66 | 54 | |
| 67 | 55 | private function cleanup_old_logs() { |
| 68 | - // Return early if filesystem is not available or log directory is not a directory | |
| 69 | - if ( ! $this->wp_filesystem || ! $this->wp_filesystem->is_dir( $this->log_directory ) ) { | |
| 56 | + // Return early if filesystem is not available | |
| 57 | + if ( ! $this->wp_filesystem ) { | |
| 70 | 58 | return; |
| 71 | 59 | } |
| 72 | 60 | |
| 73 | 61 | // Get all log files in the directory |
| @@ -72,12 +60,8 @@ | ||
| 72 | 60 | |
| 73 | 61 | // Get all log files in the directory |
| 74 | 62 | $files = $this->wp_filesystem->dirlist( $this->log_directory ); |
| 75 | 63 | |
| 76 | - if ( ! is_array( $files ) ) { | |
| 77 | - return; | |
| 78 | - } | |
| 79 | - | |
| 80 | 64 | // Filter only log files |
| 81 | 65 | $log_files = array_filter( |
| 82 | 66 | $files, |
| 83 | 67 | function( $file ) { |
| @@ -103,10 +87,8 @@ | ||
| 103 | 87 | } |
| 104 | 88 | } |
| 105 | 89 | |
| 106 | 90 | public function log( $message ) { |
| 107 | - $this->ensure_ready(); | |
| 108 | - | |
| 109 | 91 | // Return early if filesystem is not available |
| 110 | 92 | if ( ! $this->wp_filesystem ) { |
| 111 | 93 | return; |
| 112 | 94 | } |
| @@ -128,14 +110,10 @@ | ||
| 128 | 110 | $this->wp_filesystem->put_contents( $log_file, $log_message, FS_CHMOD_FILE ); |
| 129 | 111 | } |
| 130 | 112 | |
| 131 | 113 | /** |
| 132 | - * Logs an exception message to wp-content/yaymail-logs and returns. | |
| 133 | - * | |
| 134 | - * Does not terminate the request: rendering can be triggered inside another | |
| 135 | - * plugin's request (e.g. WooCommerce checkout AJAX), where dying would break | |
| 136 | - * the host response. Callers that need to abort (YayMail's own AJAX endpoints) | |
| 137 | - * must call wp_send_json_error() themselves. | |
| 114 | + * Logs an exception message and sends a JSON error response. | |
| 115 | + * Message will display in folder wp-content/yaymail-logs | |
| 138 | 116 | */ |
| 139 | 117 | public function log_exception_message( $ex, $log_type = 'error', $additional_data = null ) { |
| 140 | 118 | $prefix = ( $log_type === 'warning' ) ? __( 'WARNING:', 'yaymail' ) : __( 'SYSTEM ERROR:', 'yaymail' ); |
| 141 | 119 | |
| @@ -153,6 +131,11 @@ | ||
| 153 | 131 | } |
| 154 | 132 | } |
| 155 | 133 | |
| 156 | 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 | + } | |
| 157 | 140 | } |
| 158 | 141 | } |