hardening 2.3: 2meet-data-optimizer 1.3.0 (flat-table DDL outside web requests, routed count cache, audit fixes) #7
@@ -48,7 +48,7 @@ class TMDO_Logger {
|
|||||||
/**
|
/**
|
||||||
* Log an INFO-level event (event-based signature, used by v2.0.0 engine code).
|
* Log an INFO-level event (event-based signature, used by v2.0.0 engine code).
|
||||||
*
|
*
|
||||||
* Writes only to PHP error_log; does NOT touch wpdo_errors (which is reserved
|
* Writes only to PHP error_log, and only when debug logging is on; does NOT touch wpdo_errors (which is reserved
|
||||||
* for actual errors). Mode changes / cache flushes / audit prunes call here
|
* for actual errors). Mode changes / cache flushes / audit prunes call here
|
||||||
* many times per request — keeping them out of the DB error table.
|
* many times per request — keeping them out of the DB error table.
|
||||||
*
|
*
|
||||||
@@ -57,6 +57,22 @@ class TMDO_Logger {
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function info( string $event, array $context = array() ): void {
|
public static function info( string $event, array $context = array() ): void {
|
||||||
|
// Routine events: only with WP_DEBUG_LOG (filter `tmdo_log_info`), or every
|
||||||
|
// CLI run and production request would flood the log.
|
||||||
|
if ( ! apply_filters( 'tmdo_log_info', defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self::write_info( $event, $context );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write an INFO line to the PHP error log.
|
||||||
|
*
|
||||||
|
* @param string $event Event name.
|
||||||
|
* @param array<string, mixed> $context Structured context.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private static function write_info( string $event, array $context ): void {
|
||||||
error_log( sprintf( '[WPDO][INFO][%s] %s', $event, $context ? wp_json_encode( $context, JSON_UNESCAPED_UNICODE ) : '' ) );
|
error_log( sprintf( '[WPDO][INFO][%s] %s', $event, $context ? wp_json_encode( $context, JSON_UNESCAPED_UNICODE ) : '' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +113,7 @@ class TMDO_Logger {
|
|||||||
*/
|
*/
|
||||||
public static function debug( string $event, array $context = array() ): void {
|
public static function debug( string $event, array $context = array() ): void {
|
||||||
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
|
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
|
||||||
self::info( $event, $context );
|
self::write_info( $event, $context );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -130,4 +130,38 @@ class LoggerTest extends TestCase {
|
|||||||
$deleted = WPDO_Logger::purge( 30 );
|
$deleted = WPDO_Logger::purge( 30 );
|
||||||
$this->assertSame( 5, $deleted );
|
$this->assertSame( 5, $deleted );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** error_log() output of $fn. */
|
||||||
|
private function logged( callable $fn ): string {
|
||||||
|
$file = tempnam( sys_get_temp_dir(), 'tmdo-log' );
|
||||||
|
$old = ini_set( 'error_log', $file );
|
||||||
|
try {
|
||||||
|
$fn();
|
||||||
|
} finally {
|
||||||
|
ini_set( 'error_log', (string) $old );
|
||||||
|
}
|
||||||
|
$out = (string) file_get_contents( $file );
|
||||||
|
unlink( $file );
|
||||||
|
return $out;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_info_and_notice_stay_out_of_the_log_unless_debug_logging_is_on(): void {
|
||||||
|
$GLOBALS['_wp_filter_callbacks']['tmdo_log_info'] = array();
|
||||||
|
|
||||||
|
$this->assertSame( '', $this->logged( static function () {
|
||||||
|
WPDO_Logger::info( 'cache_flushed' );
|
||||||
|
WPDO_Logger::notice( 'mode_changed' );
|
||||||
|
} ) );
|
||||||
|
|
||||||
|
add_filter( 'tmdo_log_info', static fn() => true );
|
||||||
|
try {
|
||||||
|
$this->assertStringContainsString( '[WPDO][INFO][cache_flushed]', $this->logged( static fn() => WPDO_Logger::info( 'cache_flushed' ) ) );
|
||||||
|
} finally {
|
||||||
|
$GLOBALS['_wp_filter_callbacks']['tmdo_log_info'] = array();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_warnings_are_always_logged(): void {
|
||||||
|
$this->assertStringContainsString( '[WPDO][WARN][odd_return]', $this->logged( static fn() => WPDO_Logger::warning( 'odd_return' ) ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user