PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.7.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.7.0
1.7.0 1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 All 50 releases
fluent-cart / app / Services / Email / EmailNotifications.php

EmailNotifications.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.7.0, at app/Services/Email/EmailNotifications.php

995 lines 52.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Services\Email;
4
5 use FluentCart\App\Models\Meta;
6 use FluentCart\App\Services\Cache;
7 use FluentCart\App\Services\TemplateService;
8 use FluentCart\Framework\Support\Arr;
9
10 class EmailNotifications
11 {
12
13 const EMAIL_RECIPIENT_MAP = [
14 'customer' => '{{order.customer.email}}',
15 'user' => '{{user.user_email}}',
16 'subscriber' => '{{order.customer.email}}'
17 ];
18
19 const META_KEY = 'email_notifications_config';
20
21 public static function getNotifications(): array
22 {
23 $settings = static::getDefaultNotifications();
24 $settings = apply_filters('fluent_cart/email_notifications', $settings);
25 $config = Arr::get(static::cachedSettings(), 'notification_config', []);
26
27 foreach ($settings as $key => &$setting) {
28 $setting['name'] = $key;
29
30 if (empty($setting['group_label'])) {
31 $setting['group_label'] = __('Other Actions', 'fluent-cart');
32 }
33
34 $keyConfig = Arr::get($config, $key, []);
35 if (!$keyConfig) {
36 continue;
37 }
38
39 $setting['settings'] = wp_parse_args($keyConfig, $setting['settings']);
40
41 // Backward compat: migrate old attach_pdf to attach_pdf_template
42 if (empty($setting['settings']['attach_pdf_template']) && !empty($setting['settings']['attach_pdf']) && $setting['settings']['attach_pdf'] === 'yes') {
43 $setting['settings']['attach_pdf_template'] = 'order_receipt';
44 }
45 unset($setting['settings']['attach_pdf']);
46 }
47
48 return $settings;
49 }
50
51 public static function getDefaultNotifications(): array
52 {
53
54 /**
55 * Order Placed (Offline Payment) -> fluent_cart/order_placed_offline (customer + admin)
56 * Order Paid -> fluent_cart/order_paid (customer + admin)
57 * Order Renewal -> fluent_cart/subscription_renewed (customer + admin)
58 * Order Refunded -> fluent_cart/order_fully_refunded (customer + admin)
59 * Order Shipping Status Changed => fluent_cart/shipping_status_changed (customer + admin)
60 * Reminder Events:
61 * - fluent_cart/renewal_reminder_due
62 * - fluent_cart/renewal_reminder_overdue (manual/on-demand payment reminder)
63 * - fluent_cart/renewal_overdue_first (scheduled, first overdue stage)
64 * - fluent_cart/renewal_overdue_followup (scheduled, intermediate overdue stages)
65 * - fluent_cart/renewal_overdue_final (scheduled, last overdue stage)
66 * - fluent_cart/subscription_renewal_reminder
67 * - fluent_cart/subscription_trial_end_reminder
68 */
69
70 return [
71 'order_paid_admin' => [
72 'event' => 'order_paid_done',
73 'group' => 'order',
74 'group_label' => __('Order Actions', 'fluent-cart'),
75 'title' => __('Send mail to admin after New Order Paid', 'fluent-cart'),
76 'description' => __('This email will be sent to the admin after an order is placed.', 'fluent-cart'),
77 'recipient' => 'admin',
78 'smartcode_groups' => [],
79 'template_path' => 'order.paid.admin',
80 'is_async' => false,
81 'pre_header' => __('You got a new order on your shop. Congratulations! Checkout all the details in this email. You can also go to FluentCart Dashboard to view the order details and manage it. Thank you for using FluentCart.', 'fluent-cart'),
82 'settings' => [
83 'active' => 'yes',
84 'subject' => __('New Sales On {{settings.store_name}}', 'fluent-cart'),
85 'is_default_body' => 'yes',
86 'email_body' => '',
87 'attach_pdf_template' => '',
88 ]
89 ],
90 'order_paid_customer' => [
91 'event' => 'order_paid',
92 'group' => 'order',
93 'group_label' => __('Order Actions', 'fluent-cart'),
94 'title' => __('Purchase receipt to customer', 'fluent-cart'),
95 'description' => __('This email will be sent to the customer after an order is placed.', 'fluent-cart'),
96 'recipient' => 'customer',
97 'smartcode_groups' => [],
98 'template_path' => 'order.paid.customer',
99 'is_async' => false,
100 'settings' => [
101 'active' => 'yes',
102 'subject' => __('Purchase Receipt #{{order.invoice_no}}', 'fluent-cart'),
103 'is_default_body' => 'yes',
104 'email_body' => '',
105 'attach_pdf_template' => '',
106 ]
107 ],
108 'subscription_renewal_customer' => [
109 'event' => 'subscription_renewed',
110 'group' => 'subscription',
111 'group_label' => __('Subscription Actions', 'fluent-cart'),
112 'title' => __('Send mail to customer after a subscription renewed', 'fluent-cart'),
113 'description' => __('This email will be sent to the customer after a renewal payment made', 'fluent-cart'),
114 'recipient' => 'customer',
115 'smartcode_groups' => [],
116 'template_path' => 'subscription.renewal.customer',
117 'is_async' => false,
118 'settings' => [
119 'active' => 'yes',
120 'subject' => __('Renewal Confirmation on {{settings.store_name}}', 'fluent-cart'),
121 'is_default_body' => 'yes',
122 'email_body' => '',
123 'attach_pdf_template' => '',
124 ]
125 ],
126 'subscription_renewal_admin' => [
127 'event' => 'subscription_renewed',
128 'group' => 'subscription',
129 'group_label' => __('Subscription Actions', 'fluent-cart'),
130 'title' => __('Send mail to admin after a subscription renewed', 'fluent-cart'),
131 'description' => __('This email will be sent to the admin after a renewal payment made', 'fluent-cart'),
132 'recipient' => 'admin',
133 'smartcode_groups' => [],
134 'template_path' => 'subscription.renewal.admin',
135 'pre_header' => __('You got a new Renewal on your shop. Congratulations! Checkout all the details in this email. You can also go to FluentCart Dashboard to view the order details and manage it. Thank you for using FluentCart.', 'fluent-cart'),
136 'is_async' => false,
137 'settings' => [
138 'active' => 'yes',
139 'subject' => __('New Renewal On {{settings.store_name}}', 'fluent-cart'),
140 'is_default_body' => 'yes',
141 'email_body' => '',
142 'attach_pdf_template' => '',
143 ]
144 ],
145 'subscription_renewal_failed_customer' => [
146 'event' => 'subscription_renewal_failed',
147 'group' => 'subscription',
148 'group_label' => __('Subscription Actions', 'fluent-cart'),
149 'title' => __('Notify customer when an automatic renewal charge fails', 'fluent-cart'),
150 'description' => __('This email will be sent to the customer when their saved payment method could not be charged for a subscription renewal.', 'fluent-cart'),
151 'recipient' => 'customer',
152 'smartcode_groups' => [],
153 'template_path' => 'subscription.renewal_failed.customer',
154 'is_async' => false,
155 'settings' => [
156 'active' => 'yes',
157 'subject' => __('Automatic Payment Failed for Renewal #{{order.invoice_no}}', 'fluent-cart'),
158 'is_default_body' => 'yes',
159 'email_body' => '',
160 ]
161 ],
162 'subscription_renewal_failed_admin' => [
163 'event' => 'subscription_renewal_failed',
164 'group' => 'subscription',
165 'group_label' => __('Subscription Actions', 'fluent-cart'),
166 'title' => __('Notify admin when an automatic renewal charge fails', 'fluent-cart'),
167 'description' => __('This email will be sent to the admin when a saved payment method could not be charged for a subscription renewal.', 'fluent-cart'),
168 'recipient' => 'admin',
169 'smartcode_groups' => [],
170 'template_path' => 'subscription.renewal_failed.admin',
171 'pre_header' => __('An automatic subscription renewal charge failed. Review the order in the FluentCart Dashboard.', 'fluent-cart'),
172 'is_async' => false,
173 'settings' => [
174 'active' => 'no',
175 'subject' => __('Automatic Charge Failed for Renewal #{{order.invoice_no}}', 'fluent-cart'),
176 'is_default_body' => 'yes',
177 'email_body' => '',
178 ]
179 ],
180 'subscription_canceled_customer' => [
181 'event' => 'subscription_canceled',
182 'group' => 'subscription',
183 'group_label' => __('Subscription Actions', 'fluent-cart'),
184 'title' => __('Send mail to customer when a subscription is canceled', 'fluent-cart'),
185 'description' => __('This email will be sent to the customer when their subscription is canceled.', 'fluent-cart'),
186 'recipient' => 'customer',
187 'smartcode_groups' => [],
188 'template_path' => 'subscription.canceled.customer',
189 'is_async' => false,
190 'settings' => [
191 'active' => 'yes',
192 'subject' => __('Subscription Canceled on {{settings.store_name}}', 'fluent-cart'),
193 'is_default_body' => 'yes',
194 'email_body' => '',
195 ]
196 ],
197 'subscription_canceled_admin' => [
198 'event' => 'subscription_canceled',
199 'group' => 'subscription',
200 'group_label' => __('Subscription Actions', 'fluent-cart'),
201 'title' => __('Send mail to admin when a subscription is canceled', 'fluent-cart'),
202 'description' => __('This email will be sent to the admin when a subscription is canceled.', 'fluent-cart'),
203 'recipient' => 'admin',
204 'smartcode_groups' => [],
205 'template_path' => 'subscription.canceled.admin',
206 'pre_header' => __('A subscription has been canceled. Review the details in this email or visit FluentCart Dashboard to manage the subscription.', 'fluent-cart'),
207 'is_async' => false,
208 'settings' => [
209 'active' => 'yes',
210 'subject' => __('Subscription Canceled - {{order.customer.full_name}}', 'fluent-cart'),
211 'is_default_body' => 'yes',
212 'email_body' => '',
213 ]
214 ],
215 'subscription_period_skipped_customer' => [
216 'event' => 'subscription_period_skipped',
217 'group' => 'subscription',
218 'group_label' => __('Subscription Actions', 'fluent-cart'),
219 'title' => __('Notify customer when a billing period is skipped', 'fluent-cart'),
220 'description' => __('This email will be sent to the customer when an admin skips their next billing period. The internal reason and actor are never included. Off by default.', 'fluent-cart'),
221 'recipient' => 'customer',
222 'smartcode_groups' => [],
223 'template_path' => 'subscription.period_skipped.customer',
224 'is_async' => false,
225 'settings' => [
226 'active' => 'no',
227 'subject' => __('Your next payment has been skipped on {{settings.store_name}}', 'fluent-cart'),
228 'is_default_body' => 'yes',
229 'email_body' => '',
230 ]
231 ],
232 'order_refunded_admin' => [
233 'event' => 'order_refunded',
234 'group' => 'order',
235 'group_label' => __('Order Actions', 'fluent-cart'),
236 'title' => __('Send mail to admin after a refund.', 'fluent-cart'),
237 'description' => __('This email will be sent to the admin after an order is refunded (partial / full).', 'fluent-cart'),
238 'recipient' => 'admin',
239 'smartcode_groups' => [],
240 'template_path' => 'order.refunded.admin',
241 'is_async' => false,
242 'settings' => [
243 'active' => 'no',
244 'subject' => __('Refund sent to {{order.customer.full_name}}', 'fluent-cart'),
245 'is_default_body' => 'yes',
246 'email_body' => '',
247 'attach_pdf_template' => '',
248 ]
249 ],
250 'order_refunded_customer' => [
251 'event' => 'order_refunded',
252 'group' => 'order',
253 'group_label' => __('Order Actions', 'fluent-cart'),
254 'title' => __('Send mail to customer after a refund.', 'fluent-cart'),
255 'description' => __('This email will be sent to the customer after an order is refunded (partial / full).', 'fluent-cart'),
256 'recipient' => 'customer',
257 'smartcode_groups' => [],
258 'template_path' => 'order.refunded.customer',
259 'is_async' => false,
260 'settings' => [
261 'active' => 'yes',
262 'subject' => __('Refund Confirmation from {{settings.store_name}}', 'fluent-cart'),
263 'is_default_body' => 'yes',
264 'email_body' => '',
265 'attach_pdf_template' => '',
266 ]
267 ],
268 'order_shipped_customer' => [
269 'event' => 'shipping_status_changed_to_shipped',
270 'group' => 'order',
271 'group_label' => __('Order Actions', 'fluent-cart'),
272 'title' => __('Send mail to customer when shipping status changed to shipped.', 'fluent-cart'),
273 'description' => __('This email will be sent to the customer after an order is marked as shipped', 'fluent-cart'),
274 'recipient' => 'customer',
275 'smartcode_groups' => [],
276 'template_path' => 'order.shipped.customer',
277 'is_async' => false,
278 'settings' => [
279 'active' => 'yes',
280 'subject' => __('Order has been shipped #{{order.invoice_no}} 📦', 'fluent-cart'),
281 'is_default_body' => 'yes',
282 'email_body' => '',
283 'attach_pdf_template' => '',
284 ]
285 ],
286 'order_delivered_customer' => [
287 'event' => 'shipping_status_changed_to_delivered',
288 'group' => 'order',
289 'group_label' => __('Order Actions', 'fluent-cart'),
290 'title' => __('Send mail to customer when shipping status changed to delivered.', 'fluent-cart'),
291 'description' => __('This email will be sent to the customer after an order is marked as delivered', 'fluent-cart'),
292 'recipient' => 'customer',
293 'smartcode_groups' => [],
294 'template_path' => 'order.delivered.customer',
295 'is_async' => false,
296 'settings' => [
297 'active' => 'yes',
298 'subject' => __('Order has been delivered #{{order.invoice_no}}', 'fluent-cart'),
299 'is_default_body' => 'yes',
300 'email_body' => '',
301 'attach_pdf_template' => '',
302 ]
303 ],
304 'order_placed_admin' => [
305 'event' => 'order_placed_offline',
306 'group' => 'order',
307 'group_label' => __('Order Actions', 'fluent-cart'),
308 'title' => __('Send mail to admin after New Order Placed (Offline Payment)', 'fluent-cart'),
309 'description' => __('This email will be sent to the admin when an order is placed using offline payment method.', 'fluent-cart'),
310 'recipient' => 'admin',
311 'smartcode_groups' => [],
312 'template_path' => 'order.placed.admin',
313 'is_async' => false,
314 'manage_toggle' => 'no',
315 'toggle_label' => __('Auto-enabled for offline payments', 'fluent-cart'),
316 'pre_header' => __('You have a new order on your shop placed with offline payment. Please review the order details in this email. You can also go to FluentCart Dashboard to view the order details and manage it. Thank you for using FluentCart.', 'fluent-cart'),
317 'settings' => [
318 'subject' => __('New Order on {{settings.store_name}} (Offline Payment)', 'fluent-cart'),
319 'is_default_body' => 'yes',
320 'email_body' => '',
321 'attach_pdf_template' => '',
322 ]
323 ],
324 'order_placed_customer' => [
325 'event' => 'order_placed_offline',
326 'group' => 'order',
327 'group_label' => __('Order Actions', 'fluent-cart'),
328 'title' => __('Order confirmation to customer (Offline Payment)', 'fluent-cart'),
329 'description' => __('This email will be sent to the customer when an order is placed using offline payment method.', 'fluent-cart'),
330 'recipient' => 'customer',
331 'smartcode_groups' => [],
332 'template_path' => 'order.placed.customer',
333 'is_async' => false,
334 'manage_toggle' => 'no',
335 'toggle_label' => __('Auto-enabled for offline payments', 'fluent-cart'),
336 'settings' => [
337 'subject' => __('Order Confirmation #{{order.invoice_no}} (Offline Payment)', 'fluent-cart'),
338 'is_default_body' => 'yes',
339 'email_body' => '',
340 'attach_pdf_template' => '',
341 ]
342 ],
343 'renewal_created_admin' => [
344 'event' => 'renewal_created',
345 'group' => 'manual_subscription',
346 'group_label' => __('Store-Managed Renewals', 'fluent-cart'),
347 'title' => __('Notify admin when a renewal order is created', 'fluent-cart'),
348 'description' => __('This email will be sent to the admin when a renewal order is generated for a store-managed subscription.', 'fluent-cart'),
349 'recipient' => 'admin',
350 'smartcode_groups' => [],
351 'template_path' => 'renewal.created.admin',
352 'pre_header' => 'A new renewal order has been created for a store-managed subscription. Review it in the FluentCart Dashboard.',
353 'is_async' => false,
354 'settings' => [
355 'active' => 'yes',
356 'subject' => __('New Renewal Order Created on {{settings.store_name}}', 'fluent-cart'),
357 'is_default_body' => 'yes',
358 'email_body' => '',
359 ]
360 ],
361 'renewal_created_customer' => [
362 'event' => 'renewal_created',
363 'group' => 'manual_subscription',
364 'group_label' => __('Store-Managed Renewals', 'fluent-cart'),
365 'title' => __('Send the renewal order to the customer when a renewal order is created', 'fluent-cart'),
366 'description' => __('This email will be sent to the customer when a renewal order is generated for a store-managed subscription.', 'fluent-cart'),
367 'recipient' => 'customer',
368 'smartcode_groups' => [],
369 'template_path' => 'renewal.created.customer',
370 'is_async' => false,
371 'settings' => [
372 'active' => 'yes',
373 'subject' => __('Your Subscription Renewal #{{order.invoice_no}}', 'fluent-cart'),
374 'is_default_body' => 'yes',
375 'email_body' => '',
376 ]
377 ],
378 'system_upcoming_charge_customer' => [
379 'event' => 'system_upcoming_charge',
380 'group' => 'system_subscription',
381 'group_label' => __('Store-Managed Renewals · Auto-Charge', 'fluent-cart'),
382 'title' => __('Notify customer before an automatic renewal charge', 'fluent-cart'),
383 'description' => __('This email tells the customer the amount and date of the upcoming automatic charge to their saved payment method, when the renewal order is created ahead of the due date.', 'fluent-cart'),
384 'recipient' => 'customer',
385 'smartcode_groups' => [],
386 'template_path' => 'subscription.upcoming_charge.customer',
387 'is_async' => false,
388 'settings' => [
389 'active' => 'yes',
390 'subject' => __('Upcoming automatic payment for your subscription', 'fluent-cart'),
391 'is_default_body' => 'yes',
392 'email_body' => '',
393 ]
394 ],
395 'system_charge_failed_customer' => [
396 'event' => 'system_charge_failed',
397 'group' => 'system_subscription',
398 'group_label' => __('Store-Managed Renewals · Auto-Charge', 'fluent-cart'),
399 'title' => __('Notify customer when an automatic renewal charge fails', 'fluent-cart'),
400 'description' => __('This email will be sent to the customer when the saved payment method could not be charged for a renewal order. It includes the failure reason and a Pay Now link.', 'fluent-cart'),
401 'recipient' => 'customer',
402 'smartcode_groups' => [],
403 'template_path' => 'subscription.charge_failed.customer',
404 'is_async' => false,
405 'settings' => [
406 'active' => 'yes',
407 'subject' => __('Automatic Payment Failed for Renewal #{{order.invoice_no}}', 'fluent-cart'),
408 'is_default_body' => 'yes',
409 'email_body' => '',
410 ]
411 ],
412 'system_charge_failed_admin' => [
413 'event' => 'system_charge_failed',
414 'group' => 'system_subscription',
415 'group_label' => __('Store-Managed Renewals · Auto-Charge', 'fluent-cart'),
416 'title' => __('Notify admin when an automatic renewal charge fails', 'fluent-cart'),
417 'description' => __('This email will be sent to the admin when a saved payment method could not be charged for a renewal order.', 'fluent-cart'),
418 'recipient' => 'admin',
419 'smartcode_groups' => [],
420 'template_path' => 'subscription.charge_failed.admin',
421 'pre_header' => 'An automatic subscription renewal charge failed. Review the renewal order in the FluentCart Dashboard.',
422 'is_async' => false,
423 'settings' => [
424 'active' => 'no',
425 'subject' => __('Automatic Charge Failed for Renewal #{{order.invoice_no}}', 'fluent-cart'),
426 'is_default_body' => 'yes',
427 'email_body' => '',
428 ]
429 ],
430 'renewal_reminder_due_admin' => [
431 'event' => 'renewal_reminder_due',
432 'group' => 'manual_subscription',
433 'group_label' => __('Store-Managed Renewals', 'fluent-cart'),
434 'title' => __('Renewal due reminder copy to admin', 'fluent-cart'),
435 'description' => __('This email will be sent to admin when a due reminder is sent to customer.', 'fluent-cart'),
436 'recipient' => 'admin',
437 'smartcode_groups' => [],
438 'template_path' => 'order.reminder.due.admin',
439 'pre_header' => 'A renewal due reminder was sent to a customer. Review the order details in this email or visit FluentCart Dashboard.',
440 'is_async' => false,
441 'settings' => [
442 'active' => 'no',
443 'subject' => __('Renewal Reminder Sent #{{order.invoice_no}}', 'fluent-cart'),
444 'is_default_body' => 'yes',
445 'email_body' => '',
446 ]
447 ],
448 'renewal_reminder_due_customer' => [
449 'event' => 'renewal_reminder_due',
450 'group' => 'manual_subscription',
451 'group_label' => __('Store-Managed Renewals', 'fluent-cart'),
452 'title' => __('Renewal due reminder to customer', 'fluent-cart'),
453 'description' => __('This email will be sent before/at renewal due date when payment is pending.', 'fluent-cart'),
454 'recipient' => 'customer',
455 'smartcode_groups' => [],
456 'template_path' => 'order.reminder.due.customer',
457 'is_async' => false,
458 'settings' => [
459 'active' => 'yes',
460 'subject' => __('Payment Reminder #{{order.invoice_no}}', 'fluent-cart'),
461 'is_default_body' => 'yes',
462 'email_body' => '',
463 ]
464 ],
465 'renewal_reminder_overdue_customer' => [
466 'event' => 'renewal_reminder_overdue',
467 'group' => 'scheduler',
468 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
469 'title' => __('Payment reminder to customer', 'fluent-cart'),
470 'description' => __('This email will be sent to remind customer about pending payment.', 'fluent-cart'),
471 'recipient' => 'customer',
472 'smartcode_groups' => [],
473 'template_path' => 'order.reminder.overdue.customer',
474 'is_async' => false,
475 'manage_toggle' => 'no',
476 'toggle_label' => __('On demand', 'fluent-cart'),
477 'settings' => [
478 'subject' => __('Payment Reminder - Order #{{order.id}}', 'fluent-cart'),
479 'is_default_body' => 'yes',
480 'email_body' => '',
481 ]
482 ],
483 'subscription_past_due_admin' => [
484 'event' => 'subscription_past_due',
485 'group' => 'subscription',
486 'group_label' => __('Subscription Actions', 'fluent-cart'),
487 'title' => __('Notify admin when a subscription is past due', 'fluent-cart'),
488 'description' => __('This email will be sent to the admin when a subscription is marked as past due.', 'fluent-cart'),
489 'recipient' => 'admin',
490 'smartcode_groups' => [],
491 'template_path' => 'subscription.past_due.admin',
492 'pre_header' => 'A subscription payment is past due. Review the subscription details in this email or visit FluentCart Dashboard.',
493 'is_async' => false,
494 'settings' => [
495 'active' => 'no',
496 'subject' => __('Subscription Past Due #{{order.invoice_no}}', 'fluent-cart'),
497 'is_default_body' => 'yes',
498 'email_body' => '',
499 ]
500 ],
501 'subscription_past_due_customer' => [
502 'event' => 'subscription_past_due',
503 'group' => 'subscription',
504 'group_label' => __('Subscription Actions', 'fluent-cart'),
505 'title' => __('Notify customer when subscription is past due', 'fluent-cart'),
506 'description' => __('This email will be sent to the customer when their subscription is marked as past due.', 'fluent-cart'),
507 'recipient' => 'customer',
508 'smartcode_groups' => [],
509 'template_path' => 'subscription.past_due.customer',
510 'is_async' => false,
511 'settings' => [
512 'active' => 'yes',
513 'subject' => __('Your Subscription is Past Due on {{settings.store_name}}', 'fluent-cart'),
514 'is_default_body' => 'yes',
515 'email_body' => '',
516 ]
517 ],
518 'subscription_renewal_reminder_admin' => [
519 'event' => 'subscription_renewal_reminder',
520 'group' => 'scheduler',
521 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
522 'title' => __('Upcoming renewal reminder copy to admin', 'fluent-cart'),
523 'description' => __('This email will be sent to admin when an upcoming renewal reminder is sent.', 'fluent-cart'),
524 'recipient' => 'admin',
525 'smartcode_groups' => [],
526 'template_path' => 'subscription.reminder.admin',
527 'pre_header' => __('A subscription renewal reminder was sent to a customer. Review subscription details from FluentCart Dashboard.', 'fluent-cart'),
528 'is_async' => false,
529 'settings' => [
530 'active' => 'no',
531 'subject' => __('Renewal Reminder Sent for {{order.customer.full_name}}', 'fluent-cart'),
532 'is_default_body' => 'yes',
533 'email_body' => '',
534 ]
535 ],
536 'subscription_renewal_reminder_customer' => [
537 'event' => 'subscription_renewal_reminder',
538 'group' => 'scheduler',
539 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
540 'title' => __('Upcoming renewal reminder to customer', 'fluent-cart'),
541 'description' => __('This email will be sent before subscription auto-renewal date.', 'fluent-cart'),
542 'recipient' => 'customer',
543 'smartcode_groups' => [],
544 'template_path' => 'subscription.reminder.customer',
545 'is_async' => false,
546 'settings' => [
547 'active' => 'yes',
548 'subject' => __('Upcoming Renewal Reminder from {{settings.store_name}}', 'fluent-cart'),
549 'is_default_body' => 'yes',
550 'email_body' => '',
551 ]
552 ],
553 'renewal_overdue_first_customer' => [
554 'event' => 'renewal_overdue_first',
555 'group' => 'scheduler',
556 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
557 'title' => __('First overdue renewal reminder to customer', 'fluent-cart'),
558 'description' => __('This email will be sent at the first overdue reminder day after a renewal due date while the payment is still pending.', 'fluent-cart'),
559 'recipient' => 'customer',
560 'smartcode_groups' => [],
561 'template_path' => 'order.reminder.renewal_overdue.first.customer',
562 'is_async' => false,
563 'settings' => [
564 'active' => 'yes',
565 'subject' => __('Payment Reminder for Renewal #{{order.order_ref}}', 'fluent-cart'),
566 'is_default_body' => 'yes',
567 'email_body' => '',
568 ]
569 ],
570 'renewal_overdue_followup_customer' => [
571 'event' => 'renewal_overdue_followup',
572 'group' => 'scheduler',
573 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
574 'title' => __('Follow-up overdue renewal reminder to customer', 'fluent-cart'),
575 'description' => __('This email will be sent at the intermediate overdue reminder days while the renewal payment remains pending.', 'fluent-cart'),
576 'recipient' => 'customer',
577 'smartcode_groups' => [],
578 'template_path' => 'order.reminder.renewal_overdue.followup.customer',
579 'is_async' => false,
580 'settings' => [
581 'active' => 'yes',
582 'subject' => __('Payment Overdue for Renewal #{{order.order_ref}}', 'fluent-cart'),
583 'is_default_body' => 'yes',
584 'email_body' => '',
585 ]
586 ],
587 'renewal_overdue_final_customer' => [
588 'event' => 'renewal_overdue_final',
589 'group' => 'scheduler',
590 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
591 'title' => __('Final overdue renewal notice to customer', 'fluent-cart'),
592 'description' => __('This email will be sent at the last configured overdue reminder day while the renewal payment is still pending.', 'fluent-cart'),
593 'recipient' => 'customer',
594 'smartcode_groups' => [],
595 'template_path' => 'order.reminder.renewal_overdue.final.customer',
596 'is_async' => false,
597 'settings' => [
598 'active' => 'yes',
599 'subject' => __('Final Notice: Payment Overdue for Renewal #{{order.order_ref}}', 'fluent-cart'),
600 'is_default_body' => 'yes',
601 'email_body' => '',
602 ]
603 ],
604 'subscription_trial_end_reminder_admin' => [
605 'event' => 'subscription_trial_end_reminder',
606 'group' => 'scheduler',
607 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
608 'title' => __('Trial ending soon reminder copy to admin', 'fluent-cart'),
609 'description' => __('This email will be sent to admin when a trial ending reminder is sent to a customer.', 'fluent-cart'),
610 'recipient' => 'admin',
611 'smartcode_groups' => [],
612 'template_path' => 'subscription.trial_end.admin',
613 'pre_header' => __('A trial ending soon reminder was sent to a customer. Review subscription details from FluentCart Dashboard.', 'fluent-cart'),
614 'is_async' => false,
615 'settings' => [
616 'active' => 'no',
617 'subject' => __('Trial Ending Soon - {{order.customer.full_name}}', 'fluent-cart'),
618 'is_default_body' => 'yes',
619 'email_body' => '',
620 ]
621 ],
622 'subscription_trial_end_reminder_customer' => [
623 'event' => 'subscription_trial_end_reminder',
624 'group' => 'scheduler',
625 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
626 'title' => __('Trial ending soon reminder to customer', 'fluent-cart'),
627 'description' => __('This email will be sent before a trial period ends and converts to a paid subscription.', 'fluent-cart'),
628 'recipient' => 'customer',
629 'smartcode_groups' => [],
630 'template_path' => 'subscription.trial_end.customer',
631 'is_async' => false,
632 'settings' => [
633 'active' => 'yes',
634 'subject' => __('Your Trial is Ending Soon - {{settings.store_name}}', 'fluent-cart'),
635 'is_default_body' => 'yes',
636 'email_body' => '',
637 ]
638 ],
639 'review_submitted_admin' => [
640 'event' => 'review_created',
641 'group' => 'review',
642 'group_label' => __('Review Actions', 'fluent-cart'),
643 'title' => __('Send mail to admin when a new review is submitted', 'fluent-cart'),
644 'description' => __('Email admin when a customer submits a new review.', 'fluent-cart'),
645 'recipient' => 'admin',
646 'smartcode_groups' => [],
647 'template_path' => 'review.submitted.admin',
648 'is_async' => false,
649 'pre_header' => __('A new product review has been submitted.', 'fluent-cart'),
650 'settings' => [
651 'active' => 'yes',
652 'subject' => __('New Review Submitted', 'fluent-cart'),
653 'is_default_body' => 'yes',
654 'email_body' => '',
655 ]
656 ],
657 'review_approved_customer' => [
658 // The settled hook, not the transition: fired from a queued
659 // job once the review has been re-read and is still approved,
660 // the way order_paid_done is to order_paid. A bulk approve is
661 // fifty of these, and none of them is sent inside that request.
662 'event' => 'review_approved_done',
663 'group' => 'review',
664 'group_label' => __('Review Actions', 'fluent-cart'),
665 'title' => __('Send mail to the reviewer when their review is approved', 'fluent-cart'),
666 'description' => __('Email the customer who wrote a review once it is approved and visible on the product page.', 'fluent-cart'),
667 'recipient' => 'customer',
668 // Declared, because the customer recipient resolves through an
669 // order and a review has none. The resolver behind this code is
670 // the one every review notification uses.
671 'to' => '{{review.recipient_email}}',
672 'smartcode_groups' => [],
673 'template_path' => 'review.approved.customer',
674 'is_async' => false,
675 'pre_header' => __('Your review is now live.', 'fluent-cart'),
676 'settings' => [
677 'active' => 'yes',
678 'subject' => __('Your review of {{review.product_title}} is now live', 'fluent-cart'),
679 'is_default_body' => 'yes',
680 'email_body' => '',
681 ]
682 ],
683 'review_replied_customer' => [
684 // Fired from a queued job once the reply and the review it
685 // answers have both been re-read and are still published.
686 'event' => 'review_replied_done',
687 'group' => 'review',
688 'group_label' => __('Review Actions', 'fluent-cart'),
689 'title' => __('Send mail to the reviewer when the store replies to their review', 'fluent-cart'),
690 'description' => __('Email the customer who wrote a review when the store posts a reply to it.', 'fluent-cart'),
691 'recipient' => 'customer',
692 // The review's author, resolved the way every review
693 // notification resolves it — {{review.*}} is the review being
694 // answered, not the reply.
695 'to' => '{{review.recipient_email}}',
696 'smartcode_groups' => [],
697 'template_path' => 'review.replied.customer',
698 'is_async' => false,
699 'pre_header' => __('The store has replied to your review.', 'fluent-cart'),
700 'settings' => [
701 'active' => 'yes',
702 'subject' => __('{{settings.store_name}} replied to your review of {{review.product_title}}', 'fluent-cart'),
703 'is_default_body' => 'yes',
704 'email_body' => '',
705 ]
706 ],
707 ];
708
709 }
710
711 /**
712 * The names of the notifications that would send for an event — the same
713 * gate getNotificationsOfEvent() applies, without rendering a body.
714 *
715 * For a caller that only needs to know whether, and which, before it
716 * commits to something — a once-only lease, say — and will render at
717 * send time through mailByEmailName(). Rendering here as well would draw
718 * every template twice per send.
719 *
720 * Kept in step with getNotificationsOfEvent(): the four conditions below
721 * are that method's, in the same order.
722 *
723 * @param string $event
724 * @return string[] notification names, in registry order
725 */
726 public static function activeNotificationNamesOfEvent($event): array
727 {
728 $mailingSettings = static::getSettings();
729 $names = [];
730
731 foreach (static::getNotifications() as $key => $notification) {
732 if (Arr::get($notification, 'event') !== $event) {
733 continue;
734 }
735
736 $settings = Arr::get($notification, 'settings');
737 if (!in_array($notification['event'], ['order_placed_offline', 'renewal_reminder_overdue']) && Arr::get($settings, 'active') !== 'yes') {
738 continue;
739 }
740
741 $recipient = Arr::get($notification, 'recipient');
742 if (!Arr::get($notification, 'to') && !in_array($recipient, ['admin', 'customer', 'user', 'subscriber'])) {
743 continue;
744 }
745
746 if (empty(static::resolveRecipientTemplate($notification, $mailingSettings))) {
747 continue;
748 }
749
750 $names[] = (string) $key;
751 }
752
753 return $names;
754 }
755
756 public static function getNotificationsOfEvent($event, $viewData): array
757 {
758 $notifications = static::getNotifications();
759 $notifications = array_filter($notifications, function ($notification) use ($event) {
760 return $notification['event'] === $event;
761 });
762
763 if (empty($notifications)) {
764 return [];
765 }
766
767 $emails = [];
768 $mailingSettings = static::getSettings();
769
770 foreach ($notifications as $key => $notification) {
771
772 $settings = Arr::get($notification, 'settings');
773
774 // Skip active check for on-demand notifications (offline payments, payment reminders)
775 if (in_array($notification['event'], ['order_placed_offline', 'renewal_reminder_overdue']) || Arr::get($settings, 'active') === 'yes') {
776 // Continue with normal flow
777 } else {
778 continue;
779 }
780 $recipient = Arr::get($notification, 'recipient');
781
782 // A notification that declares its own `to` carries its recipient
783 // with it, so the whitelist — which only describes the recipients
784 // core knows how to resolve — does not apply to it.
785 if (!Arr::get($notification, 'to') && !in_array($recipient, ['admin', 'customer', 'user', 'subscriber'])) {
786 continue;
787 }
788
789 $toEmail = static::resolveRecipientTemplate($notification, $mailingSettings);
790
791 if (empty($toEmail)) {
792 continue;
793 }
794
795 $emails[$key] = static::formatNotification($notification, $viewData);
796 }
797
798 return $emails;
799 }
800
801 public static function formatNotification($notification, $viewData): array
802 {
803 $settings = Arr::get($notification, 'settings');
804 $mailingSettings = static::getSettings();
805
806 $recipient = Arr::get($notification, 'recipient');
807 $toEmail = static::resolveRecipientTemplate($notification, $mailingSettings);
808
809
810 $isDefaultEmailBody = Arr::get($settings, 'is_default_body', 'yes') === 'yes' || empty(Arr::get($settings, 'email_body'));
811
812 $emailBody = $isDefaultEmailBody ?
813 TemplateService::getTemplateByPathName(Arr::get($notification, 'template_path'), $viewData) :
814 Arr::get($settings, 'email_body');
815
816 return [
817 'to' => $toEmail,
818 'body' => $emailBody,
819 'pre_header' => Arr::get($notification, 'pre_header', ''),
820 'is_async' => $notification['is_async'],
821 'subject' => Arr::get($settings, 'subject'),
822 'is_custom' => !$isDefaultEmailBody,
823 'settings' => $settings,
824 // Carried through so send-time consumers can tell one notification
825 // from another, and admin-bound mail from customer-bound — see the
826 // admin recipient filter in parseEmailContent().
827 'name' => Arr::get($notification, 'name', ''),
828 'event' => Arr::get($notification, 'event', ''),
829 'recipient' => $recipient,
830 ];
831 }
832
833 /**
834 * Resolve the recipient template for a notification.
835 *
836 * A notification may declare its own `to` — a smartcode template, or a
837 * literal address — which wins outright. Add-ons need this: they register
838 * notifications that are not order-bound, and the built-in customer
839 * recipient resolves to {{order.customer.email}}, which has nothing to read
840 * from without an order.
841 *
842 * Falling back to the recipient map keeps every core notification, and any
843 * add-on that does not declare `to`, on exactly the path it used before.
844 *
845 * The returned value is a template — parseEmailContent() runs it through
846 * the shortcode parser before use. A declared `to` is returned as given:
847 * wp_mail() accepts an array of recipients as well as a comma-separated
848 * string, so an array is passed through rather than flattened.
849 *
850 * @param array $notification
851 * @param array|null $mailingSettings resolved settings, passed in to avoid a repeat lookup in loops
852 * @return string|array
853 */
854 public static function resolveRecipientTemplate($notification, $mailingSettings = null)
855 {
856 $declaredTo = Arr::get($notification, 'to', '');
857 if (!empty($declaredTo)) {
858 return $declaredTo;
859 }
860
861 if ($mailingSettings === null) {
862 $mailingSettings = static::getSettings();
863 }
864
865 $recipient = Arr::get($notification, 'recipient');
866
867 if ($recipient === 'admin') {
868 return (string)Arr::get($mailingSettings, 'admin_email', '');
869 }
870
871 return (string)Arr::get(self::EMAIL_RECIPIENT_MAP, $recipient, '');
872 }
873
874 public static function getNotification($name)
875 {
876 $notifications = static::getNotifications();
877 return Arr::get($notifications, $name);
878 }
879
880 //returns only the notification settings
881 public static function getNotificationConfig($notificationName = null)
882 {
883 $configs = self::getNotifications();
884 if ($notificationName) {
885 return Arr::get($configs, $notificationName . '.settings', []);
886 }
887 return Arr::pluck($configs, 'settings', 'name');
888 }
889
890 public static function updateNotification($name, $data)
891 {
892 $updateableKeys = [
893 'active',
894 'subject',
895 'email_body',
896 'is_default_body',
897 'attach_pdf_template',
898 ];
899 $allConfig = static::getSettings();
900 $config = static::getNotificationConfig($name);
901
902 foreach ($updateableKeys as $key) {
903 $defaultValue = Arr::get($config, $key, '');
904 $config[$key] = Arr::get($data, $key, $defaultValue);
905 }
906
907 // When switching back to default body, clear the custom email body unless draft mode is enabled
908 if (Arr::get($config, 'is_default_body') === 'yes' && !apply_filters('fluent_cart/keep_email_body_draft', false, ['notification_name' => $name])) {
909 $config['email_body'] = '';
910 }
911
912 Arr::set($allConfig, 'notification_config.' . $name, $config);
913
914 $isUpdate = Meta::query()->updateOrCreate(
915 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
916 ['meta_key' => static::META_KEY, 'object_type' => 'email_notification'],
917 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
918 ['meta_value' => $allConfig]
919 );
920
921 if ($isUpdate) {
922 static::updateCache();
923 }
924
925 return static::getNotification($name);
926 }
927
928 public static function defaultSettings(): array
929 {
930 return [
931 'from_name' => '',
932 'from_email' => '',
933 'reply_to_name' => '',
934 'reply_to_email' => '',
935 'email_footer' => '',
936 'show_email_footer' => 'yes',
937 'admin_email' => '{{wp.admin_email}}',
938 ];
939 }
940
941 //returns all the settings
942 public static function getSettings($key = null)
943 {
944
945 $defaultSettings = static::defaultSettings();
946 $cachedSettings = static::cachedSettings();
947 $settings = wp_parse_args($cachedSettings, $defaultSettings);
948
949 $settings['notification_config'] = wp_parse_args(
950 Arr::get($cachedSettings, 'notification_config', []),
951 Arr::get($defaultSettings, 'notification_config', [])
952 );
953
954 if (!empty($key)) {
955 return Arr::get($settings, $key);
956 }
957
958 return $settings;
959 }
960
961 public static function cachedSettings()
962 {
963 return Cache::get(static::META_KEY, function () {
964 $config = Meta::query()->where('object_type', 'email_notification')
965 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
966 ->where('meta_key', static::META_KEY)->first();
967 return $config ? $config->meta_value : [];
968 });
969 }
970
971 public static function updateSettings($data)
972 {
973 $allConfig = static::getSettings();
974
975 foreach ($data as $key => $value) {
976 Arr::set($allConfig, $key, $value);
977 }
978
979 $config = Meta::query()->updateOrCreate(
980 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
981 ['meta_key' => static::META_KEY, 'object_type' => 'email_notification'],
982 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
983 ['meta_value' => $allConfig]
984 );
985
986 static::updateCache();
987 return $config;
988 }
989
990 private static function updateCache(): void
991 {
992 Cache::forget(static::META_KEY);
993 }
994 }
995