feat: add structured file-based logging with admin log viewer

- New WooList_Logger class writes to wp-content/uploads/woolist-logs/woolist.log
  - INFO level: subscription events, test connection results (always recorded)
  - ERROR level: API failures, config problems (always recorded + php error_log fallback)
  - DEBUG level: full request URLs (password redacted), raw responses, step-by-step
    flow (only when "Enable debug logging" is checked in settings)
  - Auto-rotates at 1 MB; log directory protected by .htaccess
- API class: logs every request URL (redacted) and raw response body at DEBUG,
  errors at ERROR; subscribe_email_to_list logs each step (lookup/create/add)
- Hooks class: logs hook fire, skip reasons, and sync intent at DEBUG/INFO/ERROR
- Shortcode class: logs AJAX submissions, coupon generation, and failures
- Admin: new Logging section with "Enable debug logging" checkbox;
  log viewer textarea (last 300 lines, dark theme) + Clear Log button
  both visible at bottom of WooCommerce → Settings → phpList tab

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-19 16:25:33 +01:00
parent 0429a282bc
commit f4c9e39493
6 changed files with 309 additions and 28 deletions

View File

@@ -26,26 +26,32 @@ class WooList_Hooks {
* @param int $order_id WooCommerce order ID.
*/
public function on_order_completed( int $order_id ): void {
WooList_Logger::debug( 'Hook fired: order_completed order_id=' . $order_id );
if ( get_option( 'woolist_sync_completed' ) !== 'yes' ) {
WooList_Logger::debug( 'Completed order sync disabled, skipping.' );
return;
}
$list_id = (int) get_option( 'woolist_completed_list_id', 0 );
if ( $list_id < 1 ) {
error_log( '[WooList] Completed order sync enabled but no list ID configured.' );
WooList_Logger::error( 'Completed order sync enabled but no list ID configured order_id=' . $order_id );
return;
}
$order = wc_get_order( $order_id );
if ( ! $order ) {
WooList_Logger::error( 'Could not load order order_id=' . $order_id );
return;
}
$email = $order->get_billing_email();
if ( ! is_email( $email ) ) {
WooList_Logger::error( 'Order has no valid billing email order_id=' . $order_id );
return;
}
WooList_Logger::info( 'Syncing completed order order_id=' . $order_id . ' email=' . $email . ' list_id=' . $list_id );
$this->api->subscribe_email_to_list( $email, $list_id );
}
@@ -55,26 +61,32 @@ class WooList_Hooks {
* @param int $order_id WooCommerce order ID.
*/
public function on_order_cancelled( int $order_id ): void {
WooList_Logger::debug( 'Hook fired: order_cancelled order_id=' . $order_id );
if ( get_option( 'woolist_sync_cancelled' ) !== 'yes' ) {
WooList_Logger::debug( 'Cancelled order sync disabled, skipping.' );
return;
}
$list_id = (int) get_option( 'woolist_cancelled_list_id', 0 );
if ( $list_id < 1 ) {
error_log( '[WooList] Cancelled order sync enabled but no list ID configured.' );
WooList_Logger::error( 'Cancelled order sync enabled but no list ID configured order_id=' . $order_id );
return;
}
$order = wc_get_order( $order_id );
if ( ! $order ) {
WooList_Logger::error( 'Could not load order order_id=' . $order_id );
return;
}
$email = $order->get_billing_email();
if ( ! is_email( $email ) ) {
WooList_Logger::error( 'Order has no valid billing email order_id=' . $order_id );
return;
}
WooList_Logger::info( 'Syncing cancelled order order_id=' . $order_id . ' email=' . $email . ' list_id=' . $list_id );
$this->api->subscribe_email_to_list( $email, $list_id );
}
@@ -84,21 +96,26 @@ class WooList_Hooks {
* @param int $user_id Newly registered user ID.
*/
public function on_user_register( int $user_id ): void {
WooList_Logger::debug( 'Hook fired: user_register user_id=' . $user_id );
if ( get_option( 'woolist_sync_signup' ) !== 'yes' ) {
WooList_Logger::debug( 'Account signup sync disabled, skipping.' );
return;
}
$list_id = (int) get_option( 'woolist_signup_list_id', 0 );
if ( $list_id < 1 ) {
error_log( '[WooList] Account signup sync enabled but no list ID configured.' );
WooList_Logger::error( 'Account signup sync enabled but no list ID configured user_id=' . $user_id );
return;
}
$user = get_userdata( $user_id );
if ( ! $user || ! is_email( $user->user_email ) ) {
WooList_Logger::error( 'Could not load user or invalid email user_id=' . $user_id );
return;
}
WooList_Logger::info( 'Syncing new account user_id=' . $user_id . ' email=' . $user->user_email . ' list_id=' . $list_id );
$this->api->subscribe_email_to_list( $user->user_email, $list_id );
}
}