## ✅ Notification System Implementation Following NOTIFICATION_STRATEGY.md, built on top of WooCommerce email infrastructure. ### Backend (PHP) **1. NotificationManager** (`includes/Core/Notifications/NotificationManager.php`) - Central manager for notification system - Registers email channel (built-in) - Registers default notification events (orders, products, customers) - Provides hooks for addon channels - Maps to WooCommerce email IDs **2. NotificationSettingsProvider** (`includes/Core/Notifications/NotificationSettingsProvider.php`) - Manages settings in wp_options - Per-event channel configuration - Per-channel recipient settings (admin/customer/both) - Default settings with email enabled **3. NotificationsController** (`includes/Api/NotificationsController.php`) - REST API endpoints: - GET /notifications/channels - List available channels - GET /notifications/events - List notification events (grouped by category) - GET /notifications/settings - Get all settings - POST /notifications/settings - Update settings ### Frontend (React) **Updated Notifications.tsx:** - Shows available notification channels (email + addons) - Channel cards with built-in/addon badges - Event configuration by category (orders, products, customers) - Toggle channels per event with button UI - Link to WooCommerce advanced email settings - Responsive and modern UI ### Key Features ✅ **Built on WooCommerce Emails** - Email channel uses existing WC email system - No reinventing the wheel - Maps events to WC email IDs ✅ **Extensible Architecture** - Addons can register channels via hooks - `woonoow_notification_channels` filter - `woonoow_notification_send_{channel}` action - Per-event channel selection ✅ **User-Friendly UI** - Clear channel status (Active/Inactive) - Per-event channel toggles - Category grouping (orders, products, customers) - Addon discovery hints ✅ **Settings Storage** - Stored in wp_options (woonoow_notification_settings) - Per-event configuration - Per-channel settings - Default: email enabled for all events ### Addon Integration Example ```php // Addon registers WhatsApp channel add_action("woonoow_register_notification_channels", function() { NotificationManager::register_channel("whatsapp", [ "label" => "WhatsApp", "icon" => "message-circle", "addon" => "woonoow-whatsapp", ]); }); // Addon handles sending add_action("woonoow_notification_send_whatsapp", function($event_id, $data) { // Send WhatsApp message }, 10, 2); ``` ### Files Created - NotificationManager.php - NotificationSettingsProvider.php - NotificationsController.php ### Files Modified - Routes.php - Register NotificationsController - Bootstrap.php - Initialize NotificationManager - Notifications.tsx - New UI with channels and events --- **Ready for addon development!** 🚀 Next: Build Telegram addon as proof of concept
183 lines
4.2 KiB
PHP
183 lines
4.2 KiB
PHP
<?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';
|
|
}
|
|
}
|