Skip to content

Commit 578c32c

Browse files
fix: only clear logs when logs are turned on
* release: 3.6.10 * Only clear logs when enabled and previously not enabled * Add tests for clear logs * rewrite test assertion message * Format --------- Co-authored-by: Sertii <36940685+Sreini@users.noreply.github.com>
1 parent 5f2bd55 commit 578c32c

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/class-tiny-logger.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,19 @@ public function get_log_file_path() {
108108
/**
109109
* Triggered when log_enabled is saved
110110
* - set the setting on the instance
111-
* - if turn on, clear the old logs
111+
* - will clear logs when turned on
112+
*
113+
* Hooked to `pre_update_option_tinypng_logging_enabled` filter
114+
*
115+
* @return bool true if enabled
112116
*/
113117
public static function on_save_log_enabled( $log_enabled, $old, $option ) {
114118
$instance = self::get_instance();
115-
$instance->log_enabled = 'on' === $log_enabled;
116-
if ( $instance->get_log_enabled() ) {
119+
$was_enabled = 'on' === $old;
120+
$is_now_enabled = 'on' === $log_enabled;
121+
$instance->log_enabled = $is_now_enabled;
122+
123+
if ( ! $was_enabled && $is_now_enabled ) {
117124
self::clear_logs();
118125
}
119126

test/unit/TinyLoggerTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,42 @@ public function test_removes_full_log_and_creates_new()
113113

114114
assertTrue(filesize($logger->get_log_file_path()) < 1048576, 'log file rotated and less than 1MB');
115115
}
116+
117+
public function test_clears_logs_when_turned_on() {
118+
$log_dir_path = 'wp-content/uploads/tiny-compress-logs';
119+
vfsStream::newDirectory($log_dir_path)->at($this->vfs);
120+
$log_dir = $this->vfs->getChild($log_dir_path);
121+
vfsStream::newFile('tiny-compress.log')
122+
->withContent('Some existing log content')
123+
->at($log_dir);
124+
125+
$logger = Tiny_Logger::get_instance();
126+
$log_path = $logger->get_log_file_path();
127+
128+
assertTrue(file_exists($log_path), 'log file should exist');
129+
130+
Tiny_Logger::on_save_log_enabled( 'on', 'off', null );
131+
132+
assertFalse( file_exists($log_path), 'log file should be deleted after turning on logging' );
133+
}
134+
135+
public function test_keeps_logs_when_unchanged() {
136+
$log_dir_path = 'wp-content/uploads/tiny-compress-logs';
137+
vfsStream::newDirectory($log_dir_path)->at($this->vfs);
138+
$log_dir = $this->vfs->getChild($log_dir_path);
139+
vfsStream::newFile('tiny-compress.log')
140+
->withContent('Some existing log content')
141+
->at($log_dir);
142+
143+
$logger = Tiny_Logger::get_instance();
144+
$log_path = $logger->get_log_file_path();
145+
146+
assertTrue(file_exists($log_path), 'log file should exist');
147+
148+
Tiny_Logger::on_save_log_enabled( 'on', 'on', null );
149+
150+
assertTrue( file_exists($log_path), 'log file should still exist when settings remain unchanged' );
151+
152+
unlink($log_path);
153+
}
116154
}

0 commit comments

Comments
 (0)