Files
WooList/woolist-phplist/includes/class-woolist-hooks.php
Malin f4c9e39493 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>
2026-02-19 16:25:33 +01:00

122 lines
3.7 KiB
PHP

<?php
/**
* WooCommerce and WordPress action hooks for WooList.
*
* @package WooList
*/
defined( 'ABSPATH' ) || exit;
class WooList_Hooks {
/** @var WooList_API */
private WooList_API $api;
public function __construct( WooList_API $api ) {
$this->api = $api;
add_action( 'woocommerce_order_status_completed', [ $this, 'on_order_completed' ] );
add_action( 'woocommerce_order_status_cancelled', [ $this, 'on_order_cancelled' ] );
add_action( 'user_register', [ $this, 'on_user_register' ] );
}
/**
* Sync billing email to phpList when an order is marked completed.
*
* @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 ) {
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 );
}
/**
* Sync billing email to phpList when an order is cancelled.
*
* @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 ) {
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 );
}
/**
* Sync new user email to phpList when a WordPress account is created.
*
* @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 ) {
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 );
}
}