- 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>
433 lines
14 KiB
PHP
433 lines
14 KiB
PHP
<?php
|
|
/**
|
|
* WooCommerce Settings tab for WooList — phpList Integration.
|
|
*
|
|
* @package WooList
|
|
*/
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
class WooList_Admin {
|
|
|
|
/** @var WooList_API */
|
|
private WooList_API $api;
|
|
|
|
public function __construct( WooList_API $api ) {
|
|
$this->api = $api;
|
|
|
|
// Register the custom WooCommerce settings tab.
|
|
add_filter( 'woocommerce_settings_tabs_array', [ $this, 'add_settings_tab' ], 50 );
|
|
add_action( 'woocommerce_settings_tabs_woolist', [ $this, 'render_settings' ] );
|
|
add_action( 'woocommerce_update_options_woolist', [ $this, 'save_settings' ] );
|
|
|
|
// Test connection admin-post handler.
|
|
add_action( 'admin_post_woolist_test_connection', [ $this, 'handle_test_connection' ] );
|
|
|
|
// Clear log admin-post handler.
|
|
add_action( 'admin_post_woolist_clear_log', [ $this, 'handle_clear_log' ] );
|
|
|
|
// Enqueue admin JS/CSS only on the WC settings page.
|
|
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] );
|
|
}
|
|
|
|
/**
|
|
* Add "phpList" tab to WooCommerce → Settings.
|
|
*
|
|
* @param array $tabs Existing tabs.
|
|
* @return array
|
|
*/
|
|
public function add_settings_tab( array $tabs ): array {
|
|
$tabs['woolist'] = __( 'phpList', 'woolist-phplist' );
|
|
return $tabs;
|
|
}
|
|
|
|
/**
|
|
* Render the settings tab content.
|
|
*/
|
|
public function render_settings(): void {
|
|
woocommerce_admin_fields( $this->get_settings() );
|
|
$this->render_test_connection_button();
|
|
$this->render_log_viewer();
|
|
}
|
|
|
|
/**
|
|
* Save settings when the form is submitted.
|
|
*/
|
|
public function save_settings(): void {
|
|
woocommerce_update_options( $this->get_settings() );
|
|
}
|
|
|
|
/**
|
|
* Output the "Test Connection" button below the settings form.
|
|
*/
|
|
private function render_test_connection_button(): void {
|
|
$action_url = wp_nonce_url(
|
|
admin_url( 'admin-post.php?action=woolist_test_connection' ),
|
|
'woolist_test_connection'
|
|
);
|
|
|
|
// Display any stored transient notice.
|
|
$notice = get_transient( 'woolist_test_connection_notice_' . get_current_user_id() );
|
|
if ( $notice ) {
|
|
delete_transient( 'woolist_test_connection_notice_' . get_current_user_id() );
|
|
$type = $notice['success'] ? 'updated' : 'error';
|
|
printf(
|
|
'<div class="notice notice-%s inline"><p>%s</p></div>',
|
|
esc_attr( $type ),
|
|
esc_html( $notice['message'] )
|
|
);
|
|
}
|
|
|
|
echo '<p><a href="' . esc_url( $action_url ) . '" class="button button-secondary">'
|
|
. esc_html__( 'Test Connection', 'woolist-phplist' )
|
|
. '</a></p>';
|
|
}
|
|
|
|
/**
|
|
* Render the log viewer panel below all settings.
|
|
*/
|
|
private function render_log_viewer(): void {
|
|
$log_content = WooList_Logger::read_recent( 300 );
|
|
$log_path = WooList_Logger::get_log_path();
|
|
$clear_url = wp_nonce_url(
|
|
admin_url( 'admin-post.php?action=woolist_clear_log' ),
|
|
'woolist_clear_log'
|
|
);
|
|
|
|
$notice = get_transient( 'woolist_clear_log_notice_' . get_current_user_id() );
|
|
if ( $notice ) {
|
|
delete_transient( 'woolist_clear_log_notice_' . get_current_user_id() );
|
|
echo '<div class="notice notice-success inline"><p>' . esc_html( $notice ) . '</p></div>';
|
|
}
|
|
|
|
echo '<hr style="margin:2em 0;">';
|
|
echo '<h2>' . esc_html__( 'Activity Log', 'woolist-phplist' ) . '</h2>';
|
|
echo '<p style="color:#646970;">';
|
|
echo esc_html__( 'Log file: ', 'woolist-phplist' );
|
|
echo '<code>' . esc_html( $log_path ) . '</code>';
|
|
echo '</p>';
|
|
|
|
if ( $log_content === '' ) {
|
|
echo '<p><em>' . esc_html__( 'Log is empty.', 'woolist-phplist' ) . '</em></p>';
|
|
} else {
|
|
echo '<div style="position:relative;">';
|
|
echo '<textarea readonly rows="20" style="width:100%;font-family:monospace;font-size:12px;background:#1e1e1e;color:#d4d4d4;padding:12px;border:1px solid #ccc;border-radius:4px;resize:vertical;white-space:pre;">'
|
|
. esc_textarea( $log_content )
|
|
. '</textarea>';
|
|
echo '</div>';
|
|
}
|
|
|
|
echo '<p>';
|
|
echo '<a href="' . esc_url( $clear_url ) . '" class="button button-secondary" '
|
|
. 'onclick="return confirm(\'' . esc_js( __( 'Clear the entire log file?', 'woolist-phplist' ) ) . '\')">'
|
|
. esc_html__( 'Clear Log', 'woolist-phplist' )
|
|
. '</a>';
|
|
echo '</p>';
|
|
}
|
|
|
|
/**
|
|
* Handle the "Test Connection" form action.
|
|
*/
|
|
public function handle_test_connection(): void {
|
|
check_admin_referer( 'woolist_test_connection' );
|
|
|
|
if ( ! current_user_can( 'manage_woocommerce' ) ) {
|
|
wp_die( esc_html__( 'You do not have permission to perform this action.', 'woolist-phplist' ) );
|
|
}
|
|
|
|
WooList_Logger::info( 'Test Connection triggered by user ID ' . get_current_user_id() );
|
|
|
|
$result = $this->api->lists_get();
|
|
$user_id = get_current_user_id();
|
|
|
|
if ( is_wp_error( $result ) ) {
|
|
WooList_Logger::error( 'Test Connection failed: ' . $result->get_error_message() );
|
|
set_transient(
|
|
'woolist_test_connection_notice_' . $user_id,
|
|
[
|
|
'success' => false,
|
|
'message' => __( 'Connection failed: ', 'woolist-phplist' ) . $result->get_error_message(),
|
|
],
|
|
60
|
|
);
|
|
} else {
|
|
$count = is_array( $result ) ? count( $result ) : '?';
|
|
WooList_Logger::info( 'Test Connection succeeded, found ' . $count . ' list(s).' );
|
|
set_transient(
|
|
'woolist_test_connection_notice_' . $user_id,
|
|
[
|
|
'success' => true,
|
|
'message' => sprintf(
|
|
/* translators: %d: number of lists found */
|
|
__( 'Connection successful! Found %d list(s) in phpList.', 'woolist-phplist' ),
|
|
$count
|
|
),
|
|
],
|
|
60
|
|
);
|
|
}
|
|
|
|
wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=woolist' ) );
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Handle the "Clear Log" admin-post action.
|
|
*/
|
|
public function handle_clear_log(): void {
|
|
check_admin_referer( 'woolist_clear_log' );
|
|
|
|
if ( ! current_user_can( 'manage_woocommerce' ) ) {
|
|
wp_die( esc_html__( 'You do not have permission to perform this action.', 'woolist-phplist' ) );
|
|
}
|
|
|
|
WooList_Logger::clear();
|
|
WooList_Logger::info( 'Log cleared by user ID ' . get_current_user_id() );
|
|
|
|
set_transient(
|
|
'woolist_clear_log_notice_' . get_current_user_id(),
|
|
__( 'Log file cleared.', 'woolist-phplist' ),
|
|
60
|
|
);
|
|
|
|
wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=woolist' ) );
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Enqueue admin-side assets on WC Settings → phpList tab.
|
|
*/
|
|
public function enqueue_admin_assets(): void {
|
|
$screen = get_current_screen();
|
|
if ( ! $screen || strpos( $screen->id, 'woocommerce' ) === false ) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Build and return the WooCommerce settings field definitions.
|
|
*
|
|
* @return array
|
|
*/
|
|
private function get_settings(): array {
|
|
return [
|
|
// ── Section 1: Connection ────────────────────────────────────────
|
|
[
|
|
'title' => __( 'phpList Connection', 'woolist-phplist' ),
|
|
'type' => 'title',
|
|
'id' => 'woolist_section_connection',
|
|
],
|
|
[
|
|
'title' => __( 'phpList Base URL', 'woolist-phplist' ),
|
|
'desc' => __( 'e.g. https://newsletter.example.com', 'woolist-phplist' ),
|
|
'id' => 'woolist_phplist_url',
|
|
'type' => 'text',
|
|
'default' => '',
|
|
'desc_tip' => true,
|
|
],
|
|
[
|
|
'title' => __( 'Login', 'woolist-phplist' ),
|
|
'desc' => __( 'phpList admin username.', 'woolist-phplist' ),
|
|
'id' => 'woolist_phplist_login',
|
|
'type' => 'text',
|
|
'default' => '',
|
|
'desc_tip' => true,
|
|
],
|
|
[
|
|
'title' => __( 'Password', 'woolist-phplist' ),
|
|
'desc' => __( 'phpList admin password.', 'woolist-phplist' ),
|
|
'id' => 'woolist_phplist_password',
|
|
'type' => 'password',
|
|
'default' => '',
|
|
'desc_tip' => true,
|
|
],
|
|
[
|
|
'type' => 'sectionend',
|
|
'id' => 'woolist_section_connection',
|
|
],
|
|
|
|
// ── Section 2: Completed Orders ──────────────────────────────────
|
|
[
|
|
'title' => __( 'Completed Orders', 'woolist-phplist' ),
|
|
'type' => 'title',
|
|
'id' => 'woolist_section_completed',
|
|
],
|
|
[
|
|
'title' => __( 'Enable sync', 'woolist-phplist' ),
|
|
'desc' => __( 'Subscribe customers to phpList when an order is completed.', 'woolist-phplist' ),
|
|
'id' => 'woolist_sync_completed',
|
|
'type' => 'checkbox',
|
|
'default' => 'no',
|
|
],
|
|
[
|
|
'title' => __( 'List ID', 'woolist-phplist' ),
|
|
'desc' => __( 'phpList list ID to subscribe completed-order customers to.', 'woolist-phplist' ),
|
|
'id' => 'woolist_completed_list_id',
|
|
'type' => 'number',
|
|
'default' => '',
|
|
'custom_attributes' => [ 'min' => '1' ],
|
|
'desc_tip' => true,
|
|
],
|
|
[
|
|
'type' => 'sectionend',
|
|
'id' => 'woolist_section_completed',
|
|
],
|
|
|
|
// ── Section 3: Cancelled Orders ──────────────────────────────────
|
|
[
|
|
'title' => __( 'Cancelled Orders', 'woolist-phplist' ),
|
|
'type' => 'title',
|
|
'id' => 'woolist_section_cancelled',
|
|
],
|
|
[
|
|
'title' => __( 'Enable sync', 'woolist-phplist' ),
|
|
'desc' => __( 'Subscribe customers to phpList when an order is cancelled.', 'woolist-phplist' ),
|
|
'id' => 'woolist_sync_cancelled',
|
|
'type' => 'checkbox',
|
|
'default' => 'no',
|
|
],
|
|
[
|
|
'title' => __( 'List ID', 'woolist-phplist' ),
|
|
'desc' => __( 'phpList list ID to subscribe cancelled-order customers to.', 'woolist-phplist' ),
|
|
'id' => 'woolist_cancelled_list_id',
|
|
'type' => 'number',
|
|
'default' => '',
|
|
'custom_attributes' => [ 'min' => '1' ],
|
|
'desc_tip' => true,
|
|
],
|
|
[
|
|
'type' => 'sectionend',
|
|
'id' => 'woolist_section_cancelled',
|
|
],
|
|
|
|
// ── Section 4: Account Signup ────────────────────────────────────
|
|
[
|
|
'title' => __( 'Account Signup', 'woolist-phplist' ),
|
|
'type' => 'title',
|
|
'id' => 'woolist_section_signup',
|
|
],
|
|
[
|
|
'title' => __( 'Enable sync', 'woolist-phplist' ),
|
|
'desc' => __( 'Subscribe new WordPress users to phpList when they register.', 'woolist-phplist' ),
|
|
'id' => 'woolist_sync_signup',
|
|
'type' => 'checkbox',
|
|
'default' => 'no',
|
|
],
|
|
[
|
|
'title' => __( 'List ID', 'woolist-phplist' ),
|
|
'desc' => __( 'phpList list ID for new account signups.', 'woolist-phplist' ),
|
|
'id' => 'woolist_signup_list_id',
|
|
'type' => 'number',
|
|
'default' => '',
|
|
'custom_attributes' => [ 'min' => '1' ],
|
|
'desc_tip' => true,
|
|
],
|
|
[
|
|
'type' => 'sectionend',
|
|
'id' => 'woolist_section_signup',
|
|
],
|
|
|
|
// ── Section 5: Newsletter Shortcode ──────────────────────────────
|
|
[
|
|
'title' => __( 'Newsletter Shortcode', 'woolist-phplist' ),
|
|
'type' => 'title',
|
|
'desc' => __( 'Settings for the [woolist_newsletter] shortcode.', 'woolist-phplist' ),
|
|
'id' => 'woolist_section_newsletter',
|
|
],
|
|
[
|
|
'title' => __( 'Enable sync', 'woolist-phplist' ),
|
|
'desc' => __( 'Enable the [woolist_newsletter] shortcode.', 'woolist-phplist' ),
|
|
'id' => 'woolist_sync_newsletter',
|
|
'type' => 'checkbox',
|
|
'default' => 'no',
|
|
],
|
|
[
|
|
'title' => __( 'List ID', 'woolist-phplist' ),
|
|
'desc' => __( 'phpList list ID for newsletter shortcode subscribers.', 'woolist-phplist' ),
|
|
'id' => 'woolist_newsletter_list_id',
|
|
'type' => 'number',
|
|
'default' => '',
|
|
'custom_attributes' => [ 'min' => '1' ],
|
|
'desc_tip' => true,
|
|
],
|
|
[
|
|
'title' => __( 'Enable incentive coupon', 'woolist-phplist' ),
|
|
'desc' => __( 'Send a WooCommerce coupon after newsletter signup.', 'woolist-phplist' ),
|
|
'id' => 'woolist_newsletter_enable_coupon',
|
|
'type' => 'checkbox',
|
|
'default' => 'no',
|
|
],
|
|
[
|
|
'title' => __( 'Coupon mode', 'woolist-phplist' ),
|
|
'desc' => __( 'Choose how the coupon is provided.', 'woolist-phplist' ),
|
|
'id' => 'woolist_coupon_mode',
|
|
'type' => 'select',
|
|
'default' => 'fixed',
|
|
'options' => [
|
|
'fixed' => __( 'Fixed code', 'woolist-phplist' ),
|
|
'autogenerate' => __( 'Auto-generate unique code', 'woolist-phplist' ),
|
|
],
|
|
'desc_tip' => true,
|
|
],
|
|
[
|
|
'title' => __( 'Fixed coupon code', 'woolist-phplist' ),
|
|
'desc' => __( 'The coupon code to show when mode is set to "Fixed code".', 'woolist-phplist' ),
|
|
'id' => 'woolist_coupon_fixed_code',
|
|
'type' => 'text',
|
|
'default' => '',
|
|
'desc_tip' => true,
|
|
],
|
|
[
|
|
'title' => __( 'Discount (%)', 'woolist-phplist' ),
|
|
'desc' => __( 'Percentage discount for auto-generated coupons.', 'woolist-phplist' ),
|
|
'id' => 'woolist_coupon_discount_pct',
|
|
'type' => 'number',
|
|
'default' => '10',
|
|
'custom_attributes' => [ 'min' => '1', 'max' => '100' ],
|
|
'desc_tip' => true,
|
|
],
|
|
[
|
|
'title' => __( 'Expiry (days)', 'woolist-phplist' ),
|
|
'desc' => __( 'Days until auto-generated coupon expires. Use 0 for no expiry.', 'woolist-phplist' ),
|
|
'id' => 'woolist_coupon_expiry_days',
|
|
'type' => 'number',
|
|
'default' => '30',
|
|
'custom_attributes' => [ 'min' => '0' ],
|
|
'desc_tip' => true,
|
|
],
|
|
[
|
|
'title' => __( 'Thank-you message', 'woolist-phplist' ),
|
|
'desc' => __( 'Message shown after signup. Use {coupon} as a placeholder for the coupon code.', 'woolist-phplist' ),
|
|
'id' => 'woolist_newsletter_thankyou',
|
|
'type' => 'textarea',
|
|
'default' => __( 'Thank you for subscribing! Use coupon {coupon} for 10% off your first order.', 'woolist-phplist' ),
|
|
'css' => 'width:100%;height:80px;',
|
|
'desc_tip' => true,
|
|
],
|
|
[
|
|
'type' => 'sectionend',
|
|
'id' => 'woolist_section_newsletter',
|
|
],
|
|
|
|
// ── Section 6: Logging ───────────────────────────────────────────
|
|
[
|
|
'title' => __( 'Logging', 'woolist-phplist' ),
|
|
'type' => 'title',
|
|
'desc' => __( 'Control what gets recorded in the activity log shown below.', 'woolist-phplist' ),
|
|
'id' => 'woolist_section_logging',
|
|
],
|
|
[
|
|
'title' => __( 'Enable debug logging', 'woolist-phplist' ),
|
|
'desc' => __( 'Log full API request URLs (password redacted) and raw responses. Disable on production once everything works.', 'woolist-phplist' ),
|
|
'id' => 'woolist_enable_debug_log',
|
|
'type' => 'checkbox',
|
|
'default' => 'no',
|
|
],
|
|
[
|
|
'type' => 'sectionend',
|
|
'id' => 'woolist_section_logging',
|
|
],
|
|
];
|
|
}
|
|
}
|