- phpList REST API wrapper with subscriber get-or-create + list assignment - WooCommerce Settings tab (5 sections: connection, orders, signup, newsletter) - Test Connection button via admin-post action - Hooks for order completed/cancelled and user_register events - [woolist_newsletter] shortcode with jQuery AJAX, fixed & auto-generated coupons - Responsive front-end form styles and JS with loading/success/error states Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
105 lines
2.4 KiB
PHP
105 lines
2.4 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 {
|
|
if ( get_option( 'woolist_sync_completed' ) !== 'yes' ) {
|
|
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.' );
|
|
return;
|
|
}
|
|
|
|
$order = wc_get_order( $order_id );
|
|
if ( ! $order ) {
|
|
return;
|
|
}
|
|
|
|
$email = $order->get_billing_email();
|
|
if ( ! is_email( $email ) ) {
|
|
return;
|
|
}
|
|
|
|
$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 {
|
|
if ( get_option( 'woolist_sync_cancelled' ) !== 'yes' ) {
|
|
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.' );
|
|
return;
|
|
}
|
|
|
|
$order = wc_get_order( $order_id );
|
|
if ( ! $order ) {
|
|
return;
|
|
}
|
|
|
|
$email = $order->get_billing_email();
|
|
if ( ! is_email( $email ) ) {
|
|
return;
|
|
}
|
|
|
|
$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 {
|
|
if ( get_option( 'woolist_sync_signup' ) !== 'yes' ) {
|
|
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.' );
|
|
return;
|
|
}
|
|
|
|
$user = get_userdata( $user_id );
|
|
if ( ! $user || ! is_email( $user->user_email ) ) {
|
|
return;
|
|
}
|
|
|
|
$this->api->subscribe_email_to_list( $user->user_email, $list_id );
|
|
}
|
|
}
|