Files
WooList/woolist-phplist/includes/class-woolist-logger.php
Malin 9b1d653789 feat: WC-native logging + Add to phpList button on order page
Logging:
- Replace custom file logger with wc_get_logger() (source: woolist-phplist)
- Logs now appear in WooCommerce → Status → Logs, no filesystem access needed
- Remove log viewer / Clear Log from settings page (WC UI handles this)
- Keep "Enable debug logging" checkbox to control DEBUG-level verbosity

Order page meta box:
- New "phpList Sync" side meta box on every order edit page
- Works with both classic (shop_order) and HPOS (woocommerce_page_wc-orders)
- Shows billing email + dropdown of all configured lists
- "Add to phpList" button triggers AJAX subscribe, shows inline result
- Result and full API trace logged to WC logs under woolist-phplist source
- woolist-admin.js handles button state and response display

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-20 08:00:05 +01:00

83 lines
2.7 KiB
PHP

<?php
/**
* Logger for WooList — delegates to WooCommerce's built-in logging system.
*
* Logs are visible at WooCommerce → Status → Logs, source: woolist-phplist.
*
* Levels:
* INFO — always written (subscription events, connection test results)
* ERROR — always written (API failures, config problems)
* DEBUG — written only when "Enable debug logging" is checked in settings
*
* @package WooList
*/
defined( 'ABSPATH' ) || exit;
class WooList_Logger {
private const SOURCE = 'woolist-phplist';
/** Whether verbose debug lines are recorded. */
private static bool $debug_enabled = false;
/** Lazy-loaded WC_Logger instance. */
private static ?WC_Logger_Interface $wc_logger = null;
/**
* Initialise: read the debug setting.
* Called on plugins_loaded after options are available.
*/
public static function init(): void {
self::$debug_enabled = ( get_option( 'woolist_enable_debug_log' ) === 'yes' );
}
// ── Public logging methods ───────────────────────────────────────────────
/** Always logged. Use for normal subscription / connection events. */
public static function info( string $message ): void {
self::wc()->info( $message, [ 'source' => self::SOURCE ] );
}
/** Always logged. */
public static function error( string $message ): void {
self::wc()->error( $message, [ 'source' => self::SOURCE ] );
}
/**
* Logged only when debug mode is enabled.
* Use for full request URLs (password redacted), raw responses, flow steps.
*/
public static function debug( string $message ): void {
if ( self::$debug_enabled ) {
self::wc()->debug( $message, [ 'source' => self::SOURCE ] );
}
}
// ── Utility ─────────────────────────────────────────────────────────────
/**
* Strip the password parameter from a phpList URL before logging it.
*
* @param string $url Full API URL.
* @return string URL with password value replaced by ***.
*/
public static function redact_url( string $url ): string {
return preg_replace( '/(\bpassword=)[^&]+/', '$1***', $url );
}
public static function is_debug_enabled(): bool {
return self::$debug_enabled;
}
// ── Internal ─────────────────────────────────────────────────────────────
/** Return (and lazy-load) the WC_Logger instance. */
private static function wc(): WC_Logger_Interface {
if ( self::$wc_logger === null ) {
self::$wc_logger = wc_get_logger();
}
return self::$wc_logger;
}
}