## ✅ 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
244 lines
6.9 KiB
PHP
244 lines
6.9 KiB
PHP
<?php
|
|
/**
|
|
* Notifications REST API Controller
|
|
*
|
|
* Bridge to WooCommerce emails + addon channel framework.
|
|
*
|
|
* @package WooNooW\Api
|
|
*/
|
|
|
|
namespace WooNooW\Api;
|
|
|
|
use WP_REST_Request;
|
|
use WP_REST_Response;
|
|
use WP_Error;
|
|
|
|
class NotificationsController {
|
|
|
|
/**
|
|
* REST API namespace
|
|
*/
|
|
private $namespace = 'woonoow/v1';
|
|
|
|
/**
|
|
* REST API base
|
|
*/
|
|
private $rest_base = 'notifications';
|
|
|
|
/**
|
|
* Register routes
|
|
*/
|
|
public function register_routes() {
|
|
// GET /woonoow/v1/notifications/channels
|
|
register_rest_route($this->namespace, '/' . $this->rest_base . '/channels', [
|
|
[
|
|
'methods' => 'GET',
|
|
'callback' => [$this, 'get_channels'],
|
|
'permission_callback' => [$this, 'check_permission'],
|
|
],
|
|
]);
|
|
|
|
// GET /woonoow/v1/notifications/events
|
|
register_rest_route($this->namespace, '/' . $this->rest_base . '/events', [
|
|
[
|
|
'methods' => 'GET',
|
|
'callback' => [$this, 'get_events'],
|
|
'permission_callback' => [$this, 'check_permission'],
|
|
],
|
|
]);
|
|
|
|
// POST /woonoow/v1/notifications/events/update
|
|
register_rest_route($this->namespace, '/' . $this->rest_base . '/events/update', [
|
|
[
|
|
'methods' => 'POST',
|
|
'callback' => [$this, 'update_event'],
|
|
'permission_callback' => [$this, 'check_permission'],
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get available notification channels
|
|
*
|
|
* @param WP_REST_Request $request Request object
|
|
* @return WP_REST_Response
|
|
*/
|
|
public function get_channels(WP_REST_Request $request) {
|
|
$channels = [
|
|
[
|
|
'id' => 'email',
|
|
'label' => __('Email', 'woonoow'),
|
|
'icon' => 'mail',
|
|
'enabled' => true,
|
|
'builtin' => true,
|
|
],
|
|
];
|
|
|
|
// Allow addons to register their channels
|
|
$channels = apply_filters('woonoow_notification_channels', $channels);
|
|
|
|
return new WP_REST_Response($channels, 200);
|
|
}
|
|
|
|
/**
|
|
* Get notification events
|
|
*
|
|
* @param WP_REST_Request $request Request object
|
|
* @return WP_REST_Response
|
|
*/
|
|
public function get_events(WP_REST_Request $request) {
|
|
// Get saved settings
|
|
$settings = get_option('woonoow_notification_settings', []);
|
|
|
|
// 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']],
|
|
],
|
|
],
|
|
];
|
|
|
|
// Allow addons to add custom events
|
|
$events = apply_filters('woonoow_notification_events', $events);
|
|
|
|
return new WP_REST_Response($events, 200);
|
|
}
|
|
|
|
/**
|
|
* Update event settings
|
|
*
|
|
* @param WP_REST_Request $request Request object
|
|
* @return WP_REST_Response|WP_Error
|
|
*/
|
|
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($event_id) || empty($channel_id)) {
|
|
return new WP_Error(
|
|
'invalid_params',
|
|
__('Event ID and Channel ID are required', 'woonoow'),
|
|
['status' => 400]
|
|
);
|
|
}
|
|
|
|
// Get current settings
|
|
$settings = get_option('woonoow_notification_settings', []);
|
|
|
|
// 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' => __('Event settings updated successfully', 'woonoow'),
|
|
], 200);
|
|
}
|
|
|
|
/**
|
|
* Check if user has permission
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function check_permission() {
|
|
return current_user_can('manage_woocommerce') || current_user_can('manage_options');
|
|
}
|
|
}
|