feat: Implement notification system with 3 subpages (Events, Channels, Templates)

##  Correct Implementation Following NOTIFICATION_STRATEGY.md

### Frontend (React) - 3 Subpages

**1. Main Notifications Page** (`Notifications.tsx`)
- Tab navigation for 3 sections
- Events | Channels | Templates

**2. Events Subpage** (`Notifications/Events.tsx`)
- Configure which channels per event
- Grouped by category (Orders, Products, Customers)
- Toggle channels (Email, WhatsApp, Telegram, etc.)
- Show recipient (Admin/Customer/Both)
- Switch UI for enable/disable per channel

**3. Channels Subpage** (`Notifications/Channels.tsx`)
- List available channels
- Built-in channels (Email)
- Addon channels (WhatsApp, Telegram, SMS, Push)
- Channel status (Active/Inactive)
- Configure button for each channel
- Addon discovery cards

**4. Templates Subpage** (`Notifications/Templates.tsx`)
- Email templates (link to WooCommerce)
- Addon channel templates
- Template variables reference
- Preview and edit buttons
- Variable documentation ({order_number}, {customer_name}, etc.)

### Backend (PHP) - Bridge to WooCommerce

**NotificationsController** (`includes/Api/NotificationsController.php`)
- Bridges to WooCommerce email system
- Does NOT reinvent notification system
- Provides addon integration hooks

**REST API Endpoints:**
```
GET  /notifications/channels  - List channels (email + addons)
GET  /notifications/events     - List events (maps to WC emails)
POST /notifications/events/update - Update event channel settings
```

**Key Features:**
 Leverages WooCommerce emails (not reinventing)
 Stores settings in wp_options
 Provides hooks for addons:
   - `woonoow_notification_channels` filter
   - `woonoow_notification_events` filter
   - `woonoow_notification_event_updated` action

### Addon Integration

**Example: WhatsApp Addon**
```php
// Register channel
add_filter("woonoow_notification_channels", function($channels) {
    $channels[] = [
        "id" => "whatsapp",
        "label" => "WhatsApp",
        "icon" => "message-circle",
        "enabled" => true,
        "addon" => "woonoow-whatsapp",
    ];
    return $channels;
});

// React to event updates
add_action("woonoow_notification_event_updated", function($event_id, $channel_id, $enabled, $recipient) {
    if ($channel_id === "whatsapp" && $enabled) {
        // Setup WhatsApp notification for this event
    }
}, 10, 4);

// Hook into WooCommerce email triggers
add_action("woocommerce_order_status_processing", function($order_id) {
    // Send WhatsApp notification
}, 10, 1);
```

### Architecture

**NOT a new notification system** 
- Uses WooCommerce email infrastructure
- Maps events to WC email IDs
- Addons hook into WC triggers

**IS an extensible framework** 
- Unified UI for all channels
- Per-event channel configuration
- Template management
- Addon discovery

### Files Created
- `Notifications.tsx` - Main page with tabs
- `Notifications/Events.tsx` - Events configuration
- `Notifications/Channels.tsx` - Channel management
- `Notifications/Templates.tsx` - Template editor
- `NotificationsController.php` - REST API bridge

### Files Modified
- `Routes.php` - Register NotificationsController

---

**Ready for addon development!** 🚀
Next: Build Telegram addon as proof of concept
This commit is contained in:
dwindown
2025-11-11 12:31:13 +07:00
parent 01fc3eb36d
commit ffdc7aae5f
8 changed files with 910 additions and 752 deletions

View File

@@ -19,7 +19,6 @@ use WooNooW\Core\Mail\MailQueue;
use WooNooW\Core\Mail\WooEmailOverride;
use WooNooW\Core\DataStores\OrderStore;
use WooNooW\Core\MediaUpload;
use WooNooW\Core\Notifications\NotificationManager;
use WooNooW\Branding;
class Bootstrap {
@@ -31,7 +30,6 @@ class Bootstrap {
StandaloneAdmin::init();
Branding::init();
MediaUpload::init();
NotificationManager::init();
// Addon system (order matters: Registry → Routes → Navigation)
AddonRegistry::init();

View File

@@ -1,230 +0,0 @@
<?php
/**
* Notification Manager
*
* Central manager for notification system. Works with WooCommerce emails
* and provides hooks for addon channels (WhatsApp, Telegram, etc.)
*
* @package WooNooW\Core\Notifications
*/
namespace WooNooW\Core\Notifications;
class NotificationManager {
/**
* Registered notification channels
*
* @var array
*/
private static $channels = [];
/**
* Notification events registry
*
* @var array
*/
private static $events = [];
/**
* Initialize notification system
*/
public static function init() {
// Register built-in email channel
self::register_channel('email', [
'id' => 'email',
'label' => __('Email', 'woonoow'),
'icon' => 'mail',
'builtin' => true,
'enabled' => true,
]);
// Register default notification events
self::register_default_events();
// Allow addons to register their channels
add_action('woonoow_register_notification_channels', [__CLASS__, 'allow_addon_registration']);
}
/**
* Register a notification channel
*
* @param string $id Channel ID
* @param array $args Channel arguments
*/
public static function register_channel($id, $args = []) {
$defaults = [
'id' => $id,
'label' => ucfirst($id),
'icon' => 'bell',
'builtin' => false,
'enabled' => false,
'addon' => '',
];
self::$channels[$id] = wp_parse_args($args, $defaults);
}
/**
* Get all registered channels
*
* @return array
*/
public static function get_channels() {
return apply_filters('woonoow_notification_channels', self::$channels);
}
/**
* Register default notification events
*/
private static function register_default_events() {
// Order events
self::register_event('order_placed', [
'label' => __('Order Placed', 'woonoow'),
'description' => __('When a new order is placed', 'woonoow'),
'category' => 'orders',
'wc_email' => 'new_order', // Maps to WC_Email_New_Order
]);
self::register_event('order_processing', [
'label' => __('Order Processing', 'woonoow'),
'description' => __('When order status changes to processing', 'woonoow'),
'category' => 'orders',
'wc_email' => 'customer_processing_order',
]);
self::register_event('order_completed', [
'label' => __('Order Completed', 'woonoow'),
'description' => __('When order is marked as completed', 'woonoow'),
'category' => 'orders',
'wc_email' => 'customer_completed_order',
]);
self::register_event('order_cancelled', [
'label' => __('Order Cancelled', 'woonoow'),
'description' => __('When order is cancelled', 'woonoow'),
'category' => 'orders',
'wc_email' => 'cancelled_order',
]);
self::register_event('order_refunded', [
'label' => __('Order Refunded', 'woonoow'),
'description' => __('When order is refunded', 'woonoow'),
'category' => 'orders',
'wc_email' => 'customer_refunded_order',
]);
// Product events
self::register_event('low_stock', [
'label' => __('Low Stock Alert', 'woonoow'),
'description' => __('When product stock is low', 'woonoow'),
'category' => 'products',
'wc_email' => 'low_stock',
]);
self::register_event('out_of_stock', [
'label' => __('Out of Stock Alert', 'woonoow'),
'description' => __('When product is out of stock', 'woonoow'),
'category' => 'products',
'wc_email' => 'no_stock',
]);
// Customer events
self::register_event('new_customer', [
'label' => __('New Customer', 'woonoow'),
'description' => __('When a new customer registers', 'woonoow'),
'category' => 'customers',
'wc_email' => 'customer_new_account',
]);
self::register_event('customer_note', [
'label' => __('Customer Note Added', 'woonoow'),
'description' => __('When a note is added to customer order', 'woonoow'),
'category' => 'customers',
'wc_email' => 'customer_note',
]);
}
/**
* Register a notification event
*
* @param string $id Event ID
* @param array $args Event arguments
*/
public static function register_event($id, $args = []) {
$defaults = [
'id' => $id,
'label' => ucfirst(str_replace('_', ' ', $id)),
'description' => '',
'category' => 'general',
'wc_email' => '',
'channels' => [],
];
self::$events[$id] = wp_parse_args($args, $defaults);
}
/**
* Get all registered events
*
* @return array
*/
public static function get_events() {
return apply_filters('woonoow_notification_events', self::$events);
}
/**
* Get events by category
*
* @param string $category Category name
* @return array
*/
public static function get_events_by_category($category) {
$events = self::get_events();
return array_filter($events, function($event) use ($category) {
return $event['category'] === $category;
});
}
/**
* Send notification
*
* @param string $event_id Event ID
* @param array $data Notification data
* @param array $channels Channels to use (default: from settings)
*/
public static function send($event_id, $data = [], $channels = null) {
// Get event configuration
$event = self::$events[$event_id] ?? null;
if (!$event) {
return;
}
// Get channels from settings if not specified
if ($channels === null) {
$settings = NotificationSettingsProvider::get_event_settings($event_id);
$channels = $settings['channels'] ?? ['email'];
}
// Send via each enabled channel
foreach ($channels as $channel_id) {
// Email is handled by WooCommerce, skip it
if ($channel_id === 'email') {
continue;
}
// Fire action for addon channels
do_action("woonoow_notification_send_{$channel_id}", $event_id, $data);
}
}
/**
* Allow addons to register channels
*/
public static function allow_addon_registration() {
// Addons hook into this to register their channels
// Example: add_action('woonoow_register_notification_channels', function() {
// NotificationManager::register_channel('whatsapp', [...]);
// });
}
}

View File

@@ -1,182 +0,0 @@
<?php
/**
* Notification Settings Provider
*
* Manages notification settings stored in wp_options.
* Works with WooCommerce email settings.
*
* @package WooNooW\Core\Notifications
*/
namespace WooNooW\Core\Notifications;
class NotificationSettingsProvider {
/**
* Option key for notification settings
*/
const OPTION_KEY = 'woonoow_notification_settings';
/**
* Get all notification settings
*
* @return array
*/
public static function get_settings() {
$defaults = self::get_default_settings();
$saved = get_option(self::OPTION_KEY, []);
return wp_parse_args($saved, $defaults);
}
/**
* Get default notification settings
*
* @return array
*/
private static function get_default_settings() {
return [
'events' => self::get_default_event_settings(),
'channels' => self::get_default_channel_settings(),
];
}
/**
* Get default event settings
*
* @return array
*/
private static function get_default_event_settings() {
$events = NotificationManager::get_events();
$settings = [];
foreach ($events as $event_id => $event) {
$settings[$event_id] = [
'enabled' => true,
'channels' => ['email'], // Email enabled by default
'recipients' => [
'email' => 'admin', // admin, customer, or both
],
];
}
return $settings;
}
/**
* Get default channel settings
*
* @return array
*/
private static function get_default_channel_settings() {
return [
'email' => [
'enabled' => true,
// Email settings are managed by WooCommerce
// We just track if it's enabled in our system
],
];
}
/**
* Get settings for a specific event
*
* @param string $event_id Event ID
* @return array
*/
public static function get_event_settings($event_id) {
$settings = self::get_settings();
return $settings['events'][$event_id] ?? [];
}
/**
* Get settings for a specific channel
*
* @param string $channel_id Channel ID
* @return array
*/
public static function get_channel_settings($channel_id) {
$settings = self::get_settings();
return $settings['channels'][$channel_id] ?? [];
}
/**
* Update notification settings
*
* @param array $new_settings New settings
* @return bool
*/
public static function update_settings($new_settings) {
$current = self::get_settings();
$updated = wp_parse_args($new_settings, $current);
return update_option(self::OPTION_KEY, $updated);
}
/**
* Update event settings
*
* @param string $event_id Event ID
* @param array $event_settings Event settings
* @return bool
*/
public static function update_event_settings($event_id, $event_settings) {
$settings = self::get_settings();
$settings['events'][$event_id] = $event_settings;
return self::update_settings($settings);
}
/**
* Update channel settings
*
* @param string $channel_id Channel ID
* @param array $channel_settings Channel settings
* @return bool
*/
public static function update_channel_settings($channel_id, $channel_settings) {
$settings = self::get_settings();
$settings['channels'][$channel_id] = $channel_settings;
return self::update_settings($settings);
}
/**
* Check if event is enabled
*
* @param string $event_id Event ID
* @return bool
*/
public static function is_event_enabled($event_id) {
$event_settings = self::get_event_settings($event_id);
return $event_settings['enabled'] ?? true;
}
/**
* Check if channel is enabled for event
*
* @param string $event_id Event ID
* @param string $channel_id Channel ID
* @return bool
*/
public static function is_channel_enabled_for_event($event_id, $channel_id) {
$event_settings = self::get_event_settings($event_id);
$channels = $event_settings['channels'] ?? [];
return in_array($channel_id, $channels, true);
}
/**
* Get recipient type for event channel
*
* @param string $event_id Event ID
* @param string $channel_id Channel ID
* @return string admin|customer|both
*/
public static function get_recipient_type($event_id, $channel_id) {
$event_settings = self::get_event_settings($event_id);
$recipients = $event_settings['recipients'] ?? [];
return $recipients[$channel_id] ?? 'admin';
}
}