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:
@@ -2,7 +2,7 @@
|
||||
/**
|
||||
* Notifications REST API Controller
|
||||
*
|
||||
* Handles notification settings API endpoints.
|
||||
* Bridge to WooCommerce emails + addon channel framework.
|
||||
*
|
||||
* @package WooNooW\Api
|
||||
*/
|
||||
@@ -12,8 +12,6 @@ namespace WooNooW\Api;
|
||||
use WP_REST_Request;
|
||||
use WP_REST_Response;
|
||||
use WP_Error;
|
||||
use WooNooW\Core\Notifications\NotificationManager;
|
||||
use WooNooW\Core\Notifications\NotificationSettingsProvider;
|
||||
|
||||
class NotificationsController {
|
||||
|
||||
@@ -49,20 +47,11 @@ class NotificationsController {
|
||||
],
|
||||
]);
|
||||
|
||||
// GET /woonoow/v1/notifications/settings
|
||||
register_rest_route($this->namespace, '/' . $this->rest_base . '/settings', [
|
||||
[
|
||||
'methods' => 'GET',
|
||||
'callback' => [$this, 'get_settings'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
],
|
||||
]);
|
||||
|
||||
// POST /woonoow/v1/notifications/settings
|
||||
register_rest_route($this->namespace, '/' . $this->rest_base . '/settings', [
|
||||
// POST /woonoow/v1/notifications/events/update
|
||||
register_rest_route($this->namespace, '/' . $this->rest_base . '/events/update', [
|
||||
[
|
||||
'methods' => 'POST',
|
||||
'callback' => [$this, 'update_settings'],
|
||||
'callback' => [$this, 'update_event'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
],
|
||||
]);
|
||||
@@ -75,17 +64,20 @@ class NotificationsController {
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public function get_channels(WP_REST_Request $request) {
|
||||
$channels = NotificationManager::get_channels();
|
||||
$channels = [
|
||||
[
|
||||
'id' => 'email',
|
||||
'label' => __('Email', 'woonoow'),
|
||||
'icon' => 'mail',
|
||||
'enabled' => true,
|
||||
'builtin' => true,
|
||||
],
|
||||
];
|
||||
|
||||
// Add enabled status from settings
|
||||
$settings = NotificationSettingsProvider::get_settings();
|
||||
$channel_settings = $settings['channels'] ?? [];
|
||||
// Allow addons to register their channels
|
||||
$channels = apply_filters('woonoow_notification_channels', $channels);
|
||||
|
||||
foreach ($channels as $id => &$channel) {
|
||||
$channel['enabled'] = $channel_settings[$id]['enabled'] ?? $channel['builtin'];
|
||||
}
|
||||
|
||||
return new WP_REST_Response(array_values($channels), 200);
|
||||
return new WP_REST_Response($channels, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,78 +87,148 @@ class NotificationsController {
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public function get_events(WP_REST_Request $request) {
|
||||
$events = NotificationManager::get_events();
|
||||
$settings = NotificationSettingsProvider::get_settings();
|
||||
$event_settings = $settings['events'] ?? [];
|
||||
// Get saved settings
|
||||
$settings = get_option('woonoow_notification_settings', []);
|
||||
|
||||
// Merge event data with settings
|
||||
foreach ($events as $id => &$event) {
|
||||
$event_config = $event_settings[$id] ?? [];
|
||||
$event['enabled'] = $event_config['enabled'] ?? true;
|
||||
$event['channels'] = $event_config['channels'] ?? ['email'];
|
||||
$event['recipients'] = $event_config['recipients'] ?? ['email' => 'admin'];
|
||||
}
|
||||
|
||||
// Group by category
|
||||
$grouped = [
|
||||
'orders' => [],
|
||||
'products' => [],
|
||||
'customers' => [],
|
||||
// Define default events (maps to WooCommerce emails)
|
||||
$events = [
|
||||
'orders' => [
|
||||
[
|
||||
'id' => 'order_placed',
|
||||
'label' => __('Order Placed', 'woonoow'),
|
||||
'description' => __('When a new order is placed', 'woonoow'),
|
||||
'category' => 'orders',
|
||||
'wc_email' => 'new_order',
|
||||
'enabled' => true,
|
||||
'channels' => $settings['order_placed'] ?? ['email' => ['enabled' => true, 'recipient' => 'admin']],
|
||||
],
|
||||
[
|
||||
'id' => 'order_processing',
|
||||
'label' => __('Order Processing', 'woonoow'),
|
||||
'description' => __('When order status changes to processing', 'woonoow'),
|
||||
'category' => 'orders',
|
||||
'wc_email' => 'customer_processing_order',
|
||||
'enabled' => true,
|
||||
'channels' => $settings['order_processing'] ?? ['email' => ['enabled' => true, 'recipient' => 'customer']],
|
||||
],
|
||||
[
|
||||
'id' => 'order_completed',
|
||||
'label' => __('Order Completed', 'woonoow'),
|
||||
'description' => __('When order is marked as completed', 'woonoow'),
|
||||
'category' => 'orders',
|
||||
'wc_email' => 'customer_completed_order',
|
||||
'enabled' => true,
|
||||
'channels' => $settings['order_completed'] ?? ['email' => ['enabled' => true, 'recipient' => 'customer']],
|
||||
],
|
||||
[
|
||||
'id' => 'order_cancelled',
|
||||
'label' => __('Order Cancelled', 'woonoow'),
|
||||
'description' => __('When order is cancelled', 'woonoow'),
|
||||
'category' => 'orders',
|
||||
'wc_email' => 'cancelled_order',
|
||||
'enabled' => true,
|
||||
'channels' => $settings['order_cancelled'] ?? ['email' => ['enabled' => true, 'recipient' => 'admin']],
|
||||
],
|
||||
[
|
||||
'id' => 'order_refunded',
|
||||
'label' => __('Order Refunded', 'woonoow'),
|
||||
'description' => __('When order is refunded', 'woonoow'),
|
||||
'category' => 'orders',
|
||||
'wc_email' => 'customer_refunded_order',
|
||||
'enabled' => true,
|
||||
'channels' => $settings['order_refunded'] ?? ['email' => ['enabled' => true, 'recipient' => 'customer']],
|
||||
],
|
||||
],
|
||||
'products' => [
|
||||
[
|
||||
'id' => 'low_stock',
|
||||
'label' => __('Low Stock Alert', 'woonoow'),
|
||||
'description' => __('When product stock is low', 'woonoow'),
|
||||
'category' => 'products',
|
||||
'wc_email' => 'low_stock',
|
||||
'enabled' => true,
|
||||
'channels' => $settings['low_stock'] ?? ['email' => ['enabled' => true, 'recipient' => 'admin']],
|
||||
],
|
||||
[
|
||||
'id' => 'out_of_stock',
|
||||
'label' => __('Out of Stock Alert', 'woonoow'),
|
||||
'description' => __('When product is out of stock', 'woonoow'),
|
||||
'category' => 'products',
|
||||
'wc_email' => 'no_stock',
|
||||
'enabled' => true,
|
||||
'channels' => $settings['out_of_stock'] ?? ['email' => ['enabled' => true, 'recipient' => 'admin']],
|
||||
],
|
||||
],
|
||||
'customers' => [
|
||||
[
|
||||
'id' => 'new_customer',
|
||||
'label' => __('New Customer', 'woonoow'),
|
||||
'description' => __('When a new customer registers', 'woonoow'),
|
||||
'category' => 'customers',
|
||||
'wc_email' => 'customer_new_account',
|
||||
'enabled' => true,
|
||||
'channels' => $settings['new_customer'] ?? ['email' => ['enabled' => true, 'recipient' => 'customer']],
|
||||
],
|
||||
[
|
||||
'id' => 'customer_note',
|
||||
'label' => __('Customer Note Added', 'woonoow'),
|
||||
'description' => __('When a note is added to customer order', 'woonoow'),
|
||||
'category' => 'customers',
|
||||
'wc_email' => 'customer_note',
|
||||
'enabled' => true,
|
||||
'channels' => $settings['customer_note'] ?? ['email' => ['enabled' => true, 'recipient' => 'customer']],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($events as $event) {
|
||||
$category = $event['category'] ?? 'general';
|
||||
if (!isset($grouped[$category])) {
|
||||
$grouped[$category] = [];
|
||||
}
|
||||
$grouped[$category][] = $event;
|
||||
}
|
||||
// Allow addons to add custom events
|
||||
$events = apply_filters('woonoow_notification_events', $events);
|
||||
|
||||
return new WP_REST_Response($grouped, 200);
|
||||
return new WP_REST_Response($events, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get notification settings
|
||||
*
|
||||
* @param WP_REST_Request $request Request object
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public function get_settings(WP_REST_Request $request) {
|
||||
$settings = NotificationSettingsProvider::get_settings();
|
||||
return new WP_REST_Response($settings, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update notification settings
|
||||
* Update event settings
|
||||
*
|
||||
* @param WP_REST_Request $request Request object
|
||||
* @return WP_REST_Response|WP_Error
|
||||
*/
|
||||
public function update_settings(WP_REST_Request $request) {
|
||||
$new_settings = $request->get_json_params();
|
||||
public function update_event(WP_REST_Request $request) {
|
||||
$event_id = $request->get_param('eventId');
|
||||
$channel_id = $request->get_param('channelId');
|
||||
$enabled = $request->get_param('enabled');
|
||||
$recipient = $request->get_param('recipient');
|
||||
|
||||
if (empty($new_settings)) {
|
||||
if (empty($event_id) || empty($channel_id)) {
|
||||
return new WP_Error(
|
||||
'invalid_settings',
|
||||
__('Invalid settings data', 'woonoow'),
|
||||
'invalid_params',
|
||||
__('Event ID and Channel ID are required', 'woonoow'),
|
||||
['status' => 400]
|
||||
);
|
||||
}
|
||||
|
||||
$updated = NotificationSettingsProvider::update_settings($new_settings);
|
||||
// Get current settings
|
||||
$settings = get_option('woonoow_notification_settings', []);
|
||||
|
||||
if (!$updated) {
|
||||
return new WP_Error(
|
||||
'update_failed',
|
||||
__('Failed to update notification settings', 'woonoow'),
|
||||
['status' => 500]
|
||||
);
|
||||
// Update settings
|
||||
if (!isset($settings[$event_id])) {
|
||||
$settings[$event_id] = [];
|
||||
}
|
||||
|
||||
$settings[$event_id][$channel_id] = [
|
||||
'enabled' => $enabled,
|
||||
'recipient' => $recipient ?? 'admin',
|
||||
];
|
||||
|
||||
// Save settings
|
||||
update_option('woonoow_notification_settings', $settings);
|
||||
|
||||
// Fire action for addons to react
|
||||
do_action('woonoow_notification_event_updated', $event_id, $channel_id, $enabled, $recipient);
|
||||
|
||||
return new WP_REST_Response([
|
||||
'success' => true,
|
||||
'message' => __('Notification settings updated successfully', 'woonoow'),
|
||||
'settings' => NotificationSettingsProvider::get_settings(),
|
||||
'message' => __('Event settings updated successfully', 'woonoow'),
|
||||
], 200);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user