From a9ff8e2ceae7fc423863fca9a4c77615212284b9 Mon Sep 17 00:00:00 2001 From: dwindown Date: Tue, 11 Nov 2025 15:57:01 +0700 Subject: [PATCH] fix: Channel toggle and event defaults issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 🐛 Critical Fixes ### Issue 1: Toggle Refuses to Disable **Problem:** Channels always return `enabled: true` even after toggling off **Root Cause:** Response didn't include actual saved state **Fix:** Added verification and return actual state in response **Changes:** ```php // Update option update_option($option_key, (bool) $enabled, false); // Verify the update $verified = get_option($option_key); // Return verified state return [ 'channelId' => $channel_id, 'enabled' => (bool) $verified, ]; ``` ### Issue 2: Wrong Event Channel Defaults **Problem:** - Email showing as enabled by default in frontend - Push showing as disabled in frontend - Mismatch between frontend and backend **Root Cause:** 1. Wrong path: `$settings['event_id']` instead of `$settings['event_id']['channels']` 2. Defaults set to `true` instead of `false` **Fix:** ```php // Before 'channels' => $settings['order_placed'] ?? ['email' => ['enabled' => true, ...]] // After 'channels' => $settings['order_placed']['channels'] ?? [ 'email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, 'recipient' => 'admin'] ] ``` ### What Was Fixed 1. ✅ Channel toggle now saves correctly 2. ✅ Response includes verified state 3. ✅ Event channels default to `false` (disabled) 4. ✅ Both email and push included in defaults 5. ✅ Correct path to saved settings 6. ✅ Consistent behavior across all events ### Testing - [ ] Toggle email off → stays off - [ ] Toggle push off → stays off - [ ] Reload page → state persists - [ ] Events page shows correct defaults (all disabled) - [ ] Enable per-event channel → saves correctly --- **Toggles should now work properly!** ✅ --- includes/Api/NotificationsController.php | 31 +++++++++++++++--------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/includes/Api/NotificationsController.php b/includes/Api/NotificationsController.php index 2727410..0437129 100644 --- a/includes/Api/NotificationsController.php +++ b/includes/Api/NotificationsController.php @@ -203,7 +203,7 @@ class NotificationsController { 'category' => 'orders', 'wc_email' => 'new_order', 'enabled' => true, - 'channels' => $settings['order_placed'] ?? ['email' => ['enabled' => true, 'recipient' => 'admin']], + 'channels' => $settings['order_placed']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, 'recipient' => 'admin']], ], [ 'id' => 'order_processing', @@ -212,7 +212,7 @@ class NotificationsController { 'category' => 'orders', 'wc_email' => 'customer_processing_order', 'enabled' => true, - 'channels' => $settings['order_processing'] ?? ['email' => ['enabled' => true, 'recipient' => 'customer']], + 'channels' => $settings['order_processing']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, 'recipient' => 'customer']], ], [ 'id' => 'order_completed', @@ -221,7 +221,7 @@ class NotificationsController { 'category' => 'orders', 'wc_email' => 'customer_completed_order', 'enabled' => true, - 'channels' => $settings['order_completed'] ?? ['email' => ['enabled' => true, 'recipient' => 'customer']], + 'channels' => $settings['order_completed']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, 'recipient' => 'customer']], ], [ 'id' => 'order_cancelled', @@ -230,7 +230,7 @@ class NotificationsController { 'category' => 'orders', 'wc_email' => 'cancelled_order', 'enabled' => true, - 'channels' => $settings['order_cancelled'] ?? ['email' => ['enabled' => true, 'recipient' => 'admin']], + 'channels' => $settings['order_cancelled']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, 'recipient' => 'admin']], ], [ 'id' => 'order_refunded', @@ -239,7 +239,7 @@ class NotificationsController { 'category' => 'orders', 'wc_email' => 'customer_refunded_order', 'enabled' => true, - 'channels' => $settings['order_refunded'] ?? ['email' => ['enabled' => true, 'recipient' => 'customer']], + 'channels' => $settings['order_refunded']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, 'recipient' => 'customer']], ], ], 'products' => [ @@ -250,7 +250,7 @@ class NotificationsController { 'category' => 'products', 'wc_email' => 'low_stock', 'enabled' => true, - 'channels' => $settings['low_stock'] ?? ['email' => ['enabled' => true, 'recipient' => 'admin']], + 'channels' => $settings['low_stock']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, 'recipient' => 'admin']], ], [ 'id' => 'out_of_stock', @@ -259,7 +259,7 @@ class NotificationsController { 'category' => 'products', 'wc_email' => 'no_stock', 'enabled' => true, - 'channels' => $settings['out_of_stock'] ?? ['email' => ['enabled' => true, 'recipient' => 'admin']], + 'channels' => $settings['out_of_stock']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, 'recipient' => 'admin']], ], ], 'customers' => [ @@ -270,7 +270,7 @@ class NotificationsController { 'category' => 'customers', 'wc_email' => 'customer_new_account', 'enabled' => true, - 'channels' => $settings['new_customer'] ?? ['email' => ['enabled' => true, 'recipient' => 'customer']], + 'channels' => $settings['new_customer']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, 'recipient' => 'customer']], ], [ 'id' => 'customer_note', @@ -279,7 +279,7 @@ class NotificationsController { 'category' => 'customers', 'wc_email' => 'customer_note', 'enabled' => true, - 'channels' => $settings['customer_note'] ?? ['email' => ['enabled' => true, 'recipient' => 'customer']], + 'channels' => $settings['customer_note']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, 'recipient' => 'customer']], ], ], ]; @@ -574,10 +574,11 @@ class NotificationsController { } // Only allow toggling built-in channels + $option_key = ''; if ($channel_id === 'email') { - update_option('woonoow_email_notifications_enabled', (bool) $enabled); + $option_key = 'woonoow_email_notifications_enabled'; } elseif ($channel_id === 'push') { - update_option('woonoow_push_notifications_enabled', (bool) $enabled); + $option_key = 'woonoow_push_notifications_enabled'; } else { return new WP_Error( 'invalid_channel', @@ -586,6 +587,12 @@ class NotificationsController { ); } + // Update the option + update_option($option_key, (bool) $enabled, false); // false = don't autoload + + // Verify the update + $verified = get_option($option_key); + return new WP_REST_Response([ 'success' => true, 'message' => sprintf( @@ -593,6 +600,8 @@ class NotificationsController { $channel_id === 'email' ? __('Email', 'woonoow') : __('Push', 'woonoow'), $enabled ? __('enabled', 'woonoow') : __('disabled', 'woonoow') ), + 'channelId' => $channel_id, + 'enabled' => (bool) $verified, ], 200); } }