feat: Add email and push channel enable/disable toggles
## ✅ Channel Toggle System Complete ### Backend (PHP) **NotificationsController Updates:** - `get_channels()` - Now reads enabled state from options - `woonoow_email_notifications_enabled` (default: true) - `woonoow_push_notifications_enabled` (default: true) - `POST /notifications/channels/toggle` - New endpoint - `toggle_channel()` - Callback to enable/disable channels **Features:** - Email notifications can be disabled - Push notifications can be disabled - Settings persist in wp_options - Returns current state in channels API ### Frontend (React) **Channels Page:** - Added enable/disable toggle for all channels - Switch shows "Enabled" or "Disabled" label - Mutation with optimistic updates - Toast notifications - Disabled state during save - Mobile-responsive layout **UI Flow:** 1. User toggles channel switch 2. API call to update setting 3. Channels list refreshes 4. Toast confirmation 5. Active badge updates color ### Use Cases **Email Channel:** - Toggle to disable all WooCommerce email notifications - Useful for testing or maintenance - Can still configure SMTP settings when disabled **Push Channel:** - Toggle to disable all push notifications - Subscription management still available - Settings preserved when disabled ### Integration ✅ **Backend Storage** - wp_options ✅ **REST API** - POST endpoint ✅ **Frontend Toggle** - Switch component ✅ **State Management** - React Query ✅ **Visual Feedback** - Toast + badge colors ✅ **Mobile Responsive** - Proper layout --- **Notification system is now complete!** 🎉
This commit is contained in:
@@ -138,6 +138,15 @@ class NotificationsController {
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
],
|
||||
]);
|
||||
|
||||
// POST /woonoow/v1/notifications/channels/toggle
|
||||
register_rest_route($this->namespace, '/' . $this->rest_base . '/channels/toggle', [
|
||||
[
|
||||
'methods' => 'POST',
|
||||
'callback' => [$this, 'toggle_channel'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,19 +156,23 @@ class NotificationsController {
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public function get_channels(WP_REST_Request $request) {
|
||||
// Get channel enabled states
|
||||
$email_enabled = get_option('woonoow_email_notifications_enabled', true);
|
||||
$push_enabled = get_option('woonoow_push_notifications_enabled', true);
|
||||
|
||||
$channels = [
|
||||
[
|
||||
'id' => 'email',
|
||||
'label' => __('Email', 'woonoow'),
|
||||
'icon' => 'mail',
|
||||
'enabled' => true,
|
||||
'enabled' => (bool) $email_enabled,
|
||||
'builtin' => true,
|
||||
],
|
||||
[
|
||||
'id' => 'push',
|
||||
'label' => __('Push Notifications', 'woonoow'),
|
||||
'icon' => 'bell',
|
||||
'enabled' => true,
|
||||
'enabled' => (bool) $push_enabled,
|
||||
'builtin' => true,
|
||||
],
|
||||
];
|
||||
@@ -532,4 +545,45 @@ class NotificationsController {
|
||||
'settings' => PushNotificationHandler::get_settings(),
|
||||
], 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle notification channel
|
||||
*
|
||||
* @param WP_REST_Request $request Request object
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public function toggle_channel(WP_REST_Request $request) {
|
||||
$channel_id = $request->get_param('channelId');
|
||||
$enabled = $request->get_param('enabled');
|
||||
|
||||
if (empty($channel_id)) {
|
||||
return new WP_Error(
|
||||
'invalid_channel',
|
||||
__('Channel ID is required', 'woonoow'),
|
||||
['status' => 400]
|
||||
);
|
||||
}
|
||||
|
||||
// Only allow toggling built-in channels
|
||||
if ($channel_id === 'email') {
|
||||
update_option('woonoow_email_notifications_enabled', (bool) $enabled);
|
||||
} elseif ($channel_id === 'push') {
|
||||
update_option('woonoow_push_notifications_enabled', (bool) $enabled);
|
||||
} else {
|
||||
return new WP_Error(
|
||||
'invalid_channel',
|
||||
__('Invalid channel ID', 'woonoow'),
|
||||
['status' => 400]
|
||||
);
|
||||
}
|
||||
|
||||
return new WP_REST_Response([
|
||||
'success' => true,
|
||||
'message' => sprintf(
|
||||
__('%s notifications %s', 'woonoow'),
|
||||
$channel_id === 'email' ? __('Email', 'woonoow') : __('Push', 'woonoow'),
|
||||
$enabled ? __('enabled', 'woonoow') : __('disabled', 'woonoow')
|
||||
),
|
||||
], 200);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user