Files
WooNooW/includes/Api/NotificationsController.php
dwindown b6c2b077ee feat: Complete Social Icons & Settings Expansion! 🎨
## Implemented (Tasks 1-6):

### 1. All Social Platforms Added 
**Platforms:**
- Facebook, X (Twitter), Instagram
- LinkedIn, YouTube
- Discord, Spotify, Telegram
- WhatsApp, Threads, Website

**Frontend:** Updated select dropdown with all platforms
**Backend:** Added to allowed_platforms whitelist

### 2. PNG Icons Instead of Emoji 
- Use local PNG files from `/assets/icons/`
- Format: `mage--{platform}-{color}.png`
- Applied to email rendering and preview
- Much more accurate than emoji

### 3. Icon Color Option (Black/White) 
- New setting: `social_icon_color`
- Select dropdown: White Icons / Black Icons
- White for dark backgrounds
- Black for light backgrounds
- Applied to all social icons

### 4. Body Background Color Setting 
- New setting: `body_bg_color`
- Color picker + hex input
- Default: #f8f8f8
- Applied to email body background
- Applied to preview

### 5. Editor Mode Styling 📝
**Note:** Editor mode intentionally shows structure/content
Preview mode shows final styled result with all customizations
This is standard email builder UX pattern

### 6. Hero Preview Text Color Fixed 
- Applied `hero_text_color` directly to h3 and p
- Now correctly shows selected color
- Both heading and paragraph use custom color

## Technical Changes:

**Frontend:**
- Added body_bg_color and social_icon_color to interface
- Updated all social platform icons (Lucide)
- PNG icon URLs in preview
- Hero preview color fix

**Backend:**
- Added body_bg_color and social_icon_color to defaults
- Sanitization for new fields
- Updated allowed_platforms array
- PNG icon URL generation with color param

**Email Rendering:**
- Use PNG icons with color selection
- Apply body_bg_color
- get_social_icon_url() updated for PNG files

## Files Modified:
- `routes/Settings/Notifications/EmailCustomization.tsx`
- `routes/Settings/Notifications/EditTemplate.tsx`
- `includes/Api/NotificationsController.php`
- `includes/Core/Notifications/EmailRenderer.php`

Task 7 (default email content) pending - separate commit.
2025-11-13 14:50:55 +07:00

950 lines
29 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;
use WooNooW\Core\Notifications\TemplateProvider;
use WooNooW\Core\Notifications\PushNotificationHandler;
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 /woonoow/v1/notifications/templates
register_rest_route($this->namespace, '/' . $this->rest_base . '/templates', [
[
'methods' => 'GET',
'callback' => [$this, 'get_templates'],
'permission_callback' => [$this, 'check_permission'],
],
]);
// GET /woonoow/v1/notifications/templates/:eventId/:channelId
register_rest_route($this->namespace, '/' . $this->rest_base . '/templates/(?P<eventId>[a-zA-Z0-9_-]+)/(?P<channelId>[a-zA-Z0-9_-]+)', [
[
'methods' => 'GET',
'callback' => [$this, 'get_template'],
'permission_callback' => [$this, 'check_permission'],
],
]);
// POST /woonoow/v1/notifications/templates
register_rest_route($this->namespace, '/' . $this->rest_base . '/templates', [
[
'methods' => 'POST',
'callback' => [$this, 'save_template'],
'permission_callback' => [$this, 'check_permission'],
],
]);
// DELETE /woonoow/v1/notifications/templates/:eventId/:channelId
register_rest_route($this->namespace, '/' . $this->rest_base . '/templates/(?P<eventId>[a-zA-Z0-9_-]+)/(?P<channelId>[a-zA-Z0-9_-]+)', [
[
'methods' => 'DELETE',
'callback' => [$this, 'delete_template'],
'permission_callback' => [$this, 'check_permission'],
],
]);
// GET /woonoow/v1/notifications/push/vapid-key
register_rest_route($this->namespace, '/' . $this->rest_base . '/push/vapid-key', [
[
'methods' => 'GET',
'callback' => [$this, 'get_vapid_key'],
'permission_callback' => '__return_true',
],
]);
// POST /woonoow/v1/notifications/push/subscribe
register_rest_route($this->namespace, '/' . $this->rest_base . '/push/subscribe', [
[
'methods' => 'POST',
'callback' => [$this, 'push_subscribe'],
'permission_callback' => '__return_true',
],
]);
// POST /woonoow/v1/notifications/push/unsubscribe
register_rest_route($this->namespace, '/' . $this->rest_base . '/push/unsubscribe', [
[
'methods' => 'POST',
'callback' => [$this, 'push_unsubscribe'],
'permission_callback' => '__return_true',
],
]);
// GET /woonoow/v1/notifications/email-settings
register_rest_route($this->namespace, '/' . $this->rest_base . '/email-settings', [
[
'methods' => 'GET',
'callback' => [$this, 'get_email_settings'],
'permission_callback' => [$this, 'check_permission'],
],
]);
// POST /woonoow/v1/notifications/email-settings
register_rest_route($this->namespace, '/' . $this->rest_base . '/email-settings', [
[
'methods' => 'POST',
'callback' => [$this, 'save_email_settings'],
'permission_callback' => [$this, 'check_permission'],
],
]);
// DELETE /woonoow/v1/notifications/email-settings
register_rest_route($this->namespace, '/' . $this->rest_base . '/email-settings', [
[
'methods' => 'DELETE',
'callback' => [$this, 'reset_email_settings'],
'permission_callback' => [$this, 'check_permission'],
],
]);
// GET /woonoow/v1/notifications/push/settings
register_rest_route($this->namespace, '/' . $this->rest_base . '/push/settings', [
[
'methods' => 'GET',
'callback' => [$this, 'get_push_settings'],
'permission_callback' => [$this, 'check_permission'],
],
]);
// POST /woonoow/v1/notifications/push/settings
register_rest_route($this->namespace, '/' . $this->rest_base . '/push/settings', [
[
'methods' => 'POST',
'callback' => [$this, 'update_push_settings'],
'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'],
],
]);
// GET /woonoow/v1/notifications/staff/events
register_rest_route($this->namespace, '/' . $this->rest_base . '/staff/events', [
[
'methods' => 'GET',
'callback' => [$this, 'get_staff_events'],
'permission_callback' => [$this, 'check_permission'],
],
]);
// GET /woonoow/v1/notifications/customer/events
register_rest_route($this->namespace, '/' . $this->rest_base . '/customer/events', [
[
'methods' => 'GET',
'callback' => [$this, 'get_customer_events'],
'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) {
// 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' => (bool) $email_enabled,
'builtin' => true,
],
[
'id' => 'push',
'label' => __('Push Notifications', 'woonoow'),
'icon' => 'bell',
'enabled' => (bool) $push_enabled,
'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']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, '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']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, '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']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, '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']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, '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']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, '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']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, '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']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, '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']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, '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']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, 'recipient' => 'customer']],
],
],
];
// Allow addons to add custom events
$events = apply_filters('woonoow_notification_events', $events);
return new WP_REST_Response($events, 200);
}
/**
* Get staff notification events (admin/staff recipient)
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response
*/
public function get_staff_events(WP_REST_Request $request) {
$all_events = $this->get_all_events();
// Filter events where recipient_type is 'staff'
$staff_events = [];
foreach ($all_events as $category => $events) {
$filtered = array_filter($events, function($event) {
return ($event['recipient_type'] ?? 'staff') === 'staff';
});
if (!empty($filtered)) {
$staff_events[$category] = array_values($filtered);
}
}
return new WP_REST_Response($staff_events, 200);
}
/**
* Get customer notification events (customer recipient)
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response
*/
public function get_customer_events(WP_REST_Request $request) {
$all_events = $this->get_all_events();
// Filter events where recipient_type is 'customer'
$customer_events = [];
foreach ($all_events as $category => $events) {
$filtered = array_filter($events, function($event) {
return ($event['recipient_type'] ?? 'staff') === 'customer';
});
if (!empty($filtered)) {
$customer_events[$category] = array_values($filtered);
}
}
return new WP_REST_Response($customer_events, 200);
}
/**
* Get all events (internal helper)
*
* @return array
*/
private function get_all_events() {
// Get saved settings
$settings = get_option('woonoow_notification_settings', []);
// Define all events
$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,
'recipient_type' => 'staff',
'channels' => $settings['order_placed']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, '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,
'recipient_type' => 'customer',
'channels' => $settings['order_processing']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, '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,
'recipient_type' => 'customer',
'channels' => $settings['order_completed']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, 'recipient' => 'customer']],
],
[
'id' => 'order_cancelled',
'label' => __('Order Cancelled', 'woonoow'),
'description' => __('When order is cancelled', 'woonoow'),
'category' => 'orders',
'wc_email' => 'cancelled_order',
'enabled' => true,
'recipient_type' => 'staff',
'channels' => $settings['order_cancelled']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, 'recipient' => 'admin']],
],
[
'id' => 'order_refunded',
'label' => __('Order Refunded', 'woonoow'),
'description' => __('When order is refunded', 'woonoow'),
'category' => 'orders',
'wc_email' => 'customer_refunded_order',
'enabled' => true,
'recipient_type' => 'customer',
'channels' => $settings['order_refunded']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, '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,
'recipient_type' => 'staff',
'channels' => $settings['low_stock']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, '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,
'recipient_type' => 'staff',
'channels' => $settings['out_of_stock']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, '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,
'recipient_type' => 'customer',
'channels' => $settings['new_customer']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, '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,
'recipient_type' => 'customer',
'channels' => $settings['customer_note']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, 'recipient' => 'customer']],
],
],
];
// Allow addons to add custom events
return apply_filters('woonoow_notification_events', $events);
}
/**
* Update event settings
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response|WP_Error
*/
public function update_event(WP_REST_Request $request) {
$params = $request->get_json_params();
$event_id = isset($params['eventId']) ? $params['eventId'] : null;
$channel_id = isset($params['channelId']) ? $params['channelId'] : null;
$enabled = isset($params['enabled']) ? $params['enabled'] : null;
$recipient = isset($params['recipient']) ? $params['recipient'] : null;
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] = ['channels' => []];
}
if (!isset($settings[$event_id]['channels'])) {
$settings[$event_id]['channels'] = [];
}
$settings[$event_id]['channels'][$channel_id] = [
'enabled' => (bool) $enabled,
'recipient' => $recipient ?? 'admin',
];
// Save settings
update_option('woonoow_notification_settings', $settings, false);
// 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');
}
/**
* Get all templates
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response
*/
public function get_templates(WP_REST_Request $request) {
$templates = TemplateProvider::get_templates();
return new WP_REST_Response($templates, 200);
}
/**
* Get single template
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response|WP_Error
*/
public function get_template(WP_REST_Request $request) {
$event_id = $request->get_param('eventId');
$channel_id = $request->get_param('channelId');
$template = TemplateProvider::get_template($event_id, $channel_id);
if (!$template) {
return new WP_Error(
'template_not_found',
__('Template not found', 'woonoow'),
['status' => 404]
);
}
// Add event and channel labels for UI
// Get events from controller method
$events_response = $this->get_events(new WP_REST_Request());
$events_data = $events_response->get_data();
// Get channels from controller method
$channels_response = $this->get_channels(new WP_REST_Request());
$channels_data = $channels_response->get_data();
// Find event label
foreach ($events_data as $category => $event_list) {
foreach ($event_list as $event) {
if ($event['id'] === $event_id) {
$template['event_label'] = $event['label'];
break 2;
}
}
}
// Find channel label
foreach ($channels_data as $channel) {
if ($channel['id'] === $channel_id) {
$template['channel_label'] = $channel['label'];
break;
}
}
return new WP_REST_Response($template, 200);
}
/**
* Save template
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response|WP_Error
*/
public function save_template(WP_REST_Request $request) {
$event_id = $request->get_param('eventId');
$channel_id = $request->get_param('channelId');
$subject = $request->get_param('subject');
$body = $request->get_param('body');
if (empty($event_id) || empty($channel_id)) {
return new WP_Error(
'invalid_params',
__('Event ID and Channel ID are required', 'woonoow'),
['status' => 400]
);
}
$success = TemplateProvider::save_template($event_id, $channel_id, [
'subject' => $subject,
'body' => $body,
]);
if (!$success) {
return new WP_Error(
'save_failed',
__('Failed to save template', 'woonoow'),
['status' => 500]
);
}
return new WP_REST_Response([
'success' => true,
'message' => __('Template saved successfully', 'woonoow'),
], 200);
}
/**
* Delete template (revert to default)
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response
*/
public function delete_template(WP_REST_Request $request) {
$event_id = $request->get_param('eventId');
$channel_id = $request->get_param('channelId');
TemplateProvider::delete_template($event_id, $channel_id);
return new WP_REST_Response([
'success' => true,
'message' => __('Template reverted to default', 'woonoow'),
], 200);
}
/**
* Get VAPID public key for push notifications
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response
*/
public function get_vapid_key(WP_REST_Request $request) {
$public_key = PushNotificationHandler::get_public_key();
return new WP_REST_Response([
'publicKey' => $public_key,
], 200);
}
/**
* Subscribe to push notifications
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response|WP_Error
*/
public function push_subscribe(WP_REST_Request $request) {
$subscription = $request->get_param('subscription');
$user_id = get_current_user_id();
if (empty($subscription)) {
return new WP_Error(
'invalid_subscription',
__('Subscription data is required', 'woonoow'),
['status' => 400]
);
}
$success = PushNotificationHandler::subscribe($subscription, $user_id);
if (!$success) {
return new WP_Error(
'subscribe_failed',
__('Failed to subscribe to push notifications', 'woonoow'),
['status' => 500]
);
}
return new WP_REST_Response([
'success' => true,
'message' => __('Subscribed to push notifications', 'woonoow'),
], 200);
}
/**
* Unsubscribe from push notifications
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response
*/
public function push_unsubscribe(WP_REST_Request $request) {
$subscription_id = $request->get_param('subscriptionId');
PushNotificationHandler::unsubscribe($subscription_id);
return new WP_REST_Response([
'success' => true,
'message' => __('Unsubscribed from push notifications', 'woonoow'),
], 200);
}
/**
* Get push notification settings
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response
*/
public function get_push_settings(WP_REST_Request $request) {
$settings = PushNotificationHandler::get_settings();
return new WP_REST_Response($settings, 200);
}
/**
* Update push notification settings
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response
*/
public function update_push_settings(WP_REST_Request $request) {
$settings = $request->get_json_params();
if (empty($settings)) {
return new WP_Error(
'invalid_settings',
__('Settings data is required', 'woonoow'),
['status' => 400]
);
}
$success = PushNotificationHandler::update_settings($settings);
if (!$success) {
return new WP_Error(
'update_failed',
__('Failed to update push notification settings', 'woonoow'),
['status' => 500]
);
}
return new WP_REST_Response([
'success' => true,
'message' => __('Push notification settings updated', 'woonoow'),
'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) {
$params = $request->get_json_params();
$channel_id = isset($params['channelId']) ? $params['channelId'] : null;
$enabled = isset($params['enabled']) ? $params['enabled'] : null;
if (empty($channel_id)) {
return new WP_Error(
'invalid_channel',
__('Channel ID is required', 'woonoow'),
['status' => 400]
);
}
if ($enabled === null) {
return new WP_Error(
'invalid_enabled',
__('Enabled parameter is required', 'woonoow'),
['status' => 400]
);
}
// Only allow toggling built-in channels
$option_key = '';
if ($channel_id === 'email') {
$option_key = 'woonoow_email_notifications_enabled';
} elseif ($channel_id === 'push') {
$option_key = 'woonoow_push_notifications_enabled';
} else {
return new WP_Error(
'invalid_channel',
__('Invalid channel ID', 'woonoow'),
['status' => 400]
);
}
// 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(
__('%s notifications %s', 'woonoow'),
$channel_id === 'email' ? __('Email', 'woonoow') : __('Push', 'woonoow'),
$enabled ? __('enabled', 'woonoow') : __('disabled', 'woonoow')
),
'channelId' => $channel_id,
'enabled' => (bool) $verified,
], 200);
}
/**
* Get email customization settings
*/
public function get_email_settings(WP_REST_Request $request) {
$defaults = [
'primary_color' => '#7f54b3',
'secondary_color' => '#7f54b3',
'hero_gradient_start' => '#667eea',
'hero_gradient_end' => '#764ba2',
'hero_text_color' => '#ffffff',
'button_text_color' => '#ffffff',
'body_bg_color' => '#f8f8f8',
'social_icon_color' => 'white',
'logo_url' => '',
'header_text' => '',
'footer_text' => '',
'social_links' => [],
];
$settings = get_option('woonoow_email_settings', $defaults);
// Ensure social_links is an array
if (!isset($settings['social_links']) || !is_array($settings['social_links'])) {
$settings['social_links'] = [];
}
// Merge with defaults to ensure all fields exist
$settings = array_merge($defaults, $settings);
return new WP_REST_Response($settings, 200);
}
/**
* Save email customization settings
*/
public function save_email_settings(WP_REST_Request $request) {
$data = $request->get_json_params();
$settings = [
'primary_color' => sanitize_hex_color($data['primary_color'] ?? '#7f54b3'),
'secondary_color' => sanitize_hex_color($data['secondary_color'] ?? '#7f54b3'),
'hero_gradient_start' => sanitize_hex_color($data['hero_gradient_start'] ?? '#667eea'),
'hero_gradient_end' => sanitize_hex_color($data['hero_gradient_end'] ?? '#764ba2'),
'hero_text_color' => sanitize_hex_color($data['hero_text_color'] ?? '#ffffff'),
'button_text_color' => sanitize_hex_color($data['button_text_color'] ?? '#ffffff'),
'body_bg_color' => sanitize_hex_color($data['body_bg_color'] ?? '#f8f8f8'),
'social_icon_color' => in_array($data['social_icon_color'] ?? 'white', ['white', 'black']) ? $data['social_icon_color'] : 'white',
'logo_url' => esc_url_raw($data['logo_url'] ?? ''),
'header_text' => sanitize_text_field($data['header_text'] ?? ''),
'footer_text' => sanitize_text_field($data['footer_text'] ?? ''),
'social_links' => $this->sanitize_social_links($data['social_links'] ?? []),
];
update_option('woonoow_email_settings', $settings);
return new WP_REST_Response([
'success' => true,
'message' => __('Email settings saved successfully', 'woonoow'),
'settings' => $settings,
], 200);
}
/**
* Reset email customization settings to defaults
*/
public function reset_email_settings(WP_REST_Request $request) {
delete_option('woonoow_email_settings');
return new WP_REST_Response([
'success' => true,
'message' => __('Email settings reset to defaults', 'woonoow'),
], 200);
}
/**
* Sanitize social links array
*/
private function sanitize_social_links($links) {
if (!is_array($links)) {
return [];
}
$sanitized = [];
$allowed_platforms = ['facebook', 'x', 'instagram', 'linkedin', 'youtube', 'discord', 'spotify', 'telegram', 'whatsapp', 'threads', 'website'];
foreach ($links as $link) {
if (!is_array($link) || !isset($link['platform']) || !isset($link['url'])) {
continue;
}
$platform = sanitize_text_field($link['platform']);
$url = esc_url_raw($link['url']);
if (in_array($platform, $allowed_platforms) && !empty($url)) {
$sanitized[] = [
'platform' => $platform,
'url' => $url,
];
}
}
return $sanitized;
}
}