feat: Add template editor and push notifications infrastructure
## ✅ Template Editor + Push Notifications ### Backend (PHP) **1. TemplateProvider** (`includes/Core/Notifications/TemplateProvider.php`) - Manages notification templates in wp_options - Default templates for all events x channels - Variable system (order, product, customer, store) - Template CRUD operations - Variable replacement engine **2. PushNotificationHandler** (`includes/Core/Notifications/PushNotificationHandler.php`) - VAPID keys generation and storage - Push subscription management - Queue system for push notifications - User-specific subscriptions - Service worker integration ready **3. NotificationsController** - Extended with: - GET /notifications/templates - List all templates - GET /notifications/templates/:eventId/:channelId - Get template - POST /notifications/templates - Save template - DELETE /notifications/templates/:eventId/:channelId - Reset to default - GET /notifications/push/vapid-key - Get VAPID public key - POST /notifications/push/subscribe - Subscribe to push - POST /notifications/push/unsubscribe - Unsubscribe **4. Push channel added to built-in channels** ### Frontend (React) **1. TemplateEditor Component** (`TemplateEditor.tsx`) - Modal dialog for editing templates - Subject + Body text editors - Variable insertion with dropdown - Click-to-insert variables - Live preview - Save and reset to default - Per event + channel customization **2. Templates Page** - Completely rewritten: - Lists all events x channels - Shows "Custom" badge for customized templates - Edit button opens template editor - Fetches templates from API - Variable reference guide - Organized by channel ### Key Features ✅ **Simple Text Editor** (not HTML builder) - Subject line - Body text with variables - Variable picker - Preview mode ✅ **Variable System** - Order variables: {order_number}, {order_total}, etc. - Customer variables: {customer_name}, {customer_email}, etc. - Product variables: {product_name}, {stock_quantity}, etc. - Store variables: {store_name}, {store_url}, etc. - Click to insert at cursor position ✅ **Push Notifications Ready** - VAPID key generation - Subscription management - Queue system - PWA integration ready - Built-in channel (alongside email) ✅ **Template Management** - Default templates for all events - Per-event, per-channel customization - Reset to default functionality - Custom badge indicator ### Default Templates Included **Email:** - Order Placed, Processing, Completed, Cancelled, Refunded - Low Stock, Out of Stock - New Customer, Customer Note **Push:** - Order Placed, Processing, Completed - Low Stock Alert ### Next Steps 1. ✅ Service worker for push notifications 2. ✅ Push subscription UI in Channels page 3. ✅ Test push notifications 4. ✅ Addon integration examples --- **Ready for testing!** 🚀
This commit is contained in:
@@ -12,6 +12,8 @@ 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 {
|
||||
|
||||
@@ -55,6 +57,69 @@ class NotificationsController {
|
||||
'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',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,6 +137,13 @@ class NotificationsController {
|
||||
'enabled' => true,
|
||||
'builtin' => true,
|
||||
],
|
||||
[
|
||||
'id' => 'push',
|
||||
'label' => __('Push Notifications', 'woonoow'),
|
||||
'icon' => 'bell',
|
||||
'enabled' => true,
|
||||
'builtin' => true,
|
||||
],
|
||||
];
|
||||
|
||||
// Allow addons to register their channels
|
||||
@@ -240,4 +312,160 @@ class NotificationsController {
|
||||
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]
|
||||
);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user