Avoid PHP warning when REMOTE_ADDR is unavailable
-
Title: Avoid PHP warning when REMOTE_ADDR is unavailable
The plugin logs every outgoing email through the
wp_mailfilter. This filter can also run outside a normal HTTP request, for example from WP-CLI, a server-side cron job, or another background process.In these contexts,
$_SERVER['REMOTE_ADDR']may not be defined. The current direct array access ininc/email-logging.phpproduces a PHP warning:PHP Warning: Undefined array key “REMOTE_ADDR”
The warning occurs before
sanitize_text_field()can process the value, because the array key is accessed first. Email logging itself can continue, but the warning adds unnecessary noise to the PHP error log.Suggested fix:
$log['ip_address'] = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ?? '' );Alternatively, use an explicit fallback such as
UNKNOWNif the log should distinguish an unavailable address from an intentionally empty value:$log['ip_address'] = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ?? 'UNKNOWN' );
This preserves the IP address for regular HTTP requests while allowing email logging to work cleanly in non-HTTP execution contexts.
You must be logged in to reply to this topic.