PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.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 1.3.19 All 49 releases
fluent-cart / app / Services / Email / EmailNotifications.php

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

793 lines 40.9 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
63 * - fluent_cart/subscription_renewal_reminder
64 * - fluent_cart/subscription_trial_end_reminder
65 */
66
67 return [
68 'order_paid_admin' => [
69 'event' => 'order_paid_done',
70 'group' => 'order',
71 'group_label' => __('Order Actions', 'fluent-cart'),
72 'title' => __('Send mail to admin after New Order Paid', 'fluent-cart'),
73 'description' => __('This email will be sent to the admin after an order is placed.', 'fluent-cart'),
74 'recipient' => 'admin',
75 'smartcode_groups' => [],
76 'template_path' => 'order.paid.admin',
77 'is_async' => false,
78 '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'),
79 'settings' => [
80 'active' => 'yes',
81 'subject' => __('New Sales On {{settings.store_name}}', 'fluent-cart'),
82 'is_default_body' => 'yes',
83 'email_body' => '',
84 'attach_pdf_template' => '',
85 ]
86 ],
87 'order_paid_customer' => [
88 'event' => 'order_paid',
89 'group' => 'order',
90 'group_label' => __('Order Actions', 'fluent-cart'),
91 'title' => __('Purchase receipt to customer', 'fluent-cart'),
92 'description' => __('This email will be sent to the customer after an order is placed.', 'fluent-cart'),
93 'recipient' => 'customer',
94 'smartcode_groups' => [],
95 'template_path' => 'order.paid.customer',
96 'is_async' => false,
97 'settings' => [
98 'active' => 'yes',
99 'subject' => __('Purchase Receipt #{{order.invoice_no}}', 'fluent-cart'),
100 'is_default_body' => 'yes',
101 'email_body' => '',
102 'attach_pdf_template' => '',
103 ]
104 ],
105 'subscription_renewal_customer' => [
106 'event' => 'subscription_renewed',
107 'group' => 'subscription',
108 'group_label' => __('Subscription Actions', 'fluent-cart'),
109 'title' => __('Send mail to customer after a subscription renewed', 'fluent-cart'),
110 'description' => __('This email will be sent to the customer after a renewal payment made', 'fluent-cart'),
111 'recipient' => 'customer',
112 'smartcode_groups' => [],
113 'template_path' => 'subscription.renewal.customer',
114 'is_async' => false,
115 'settings' => [
116 'active' => 'yes',
117 'subject' => __('Renewal Confirmation on {{settings.store_name}}', 'fluent-cart'),
118 'is_default_body' => 'yes',
119 'email_body' => '',
120 'attach_pdf_template' => '',
121 ]
122 ],
123 'subscription_renewal_admin' => [
124 'event' => 'subscription_renewed',
125 'group' => 'subscription',
126 'group_label' => __('Subscription Actions', 'fluent-cart'),
127 'title' => __('Send mail to admin after a subscription renewed', 'fluent-cart'),
128 'description' => __('This email will be sent to the admin after a renewal payment made', 'fluent-cart'),
129 'recipient' => 'admin',
130 'smartcode_groups' => [],
131 'template_path' => 'subscription.renewal.admin',
132 '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'),
133 'is_async' => false,
134 'settings' => [
135 'active' => 'yes',
136 'subject' => __('New Renewal On {{settings.store_name}}', 'fluent-cart'),
137 'is_default_body' => 'yes',
138 'email_body' => '',
139 'attach_pdf_template' => '',
140 ]
141 ],
142 'subscription_canceled_customer' => [
143 'event' => 'subscription_canceled',
144 'group' => 'subscription',
145 'group_label' => __('Subscription Actions', 'fluent-cart'),
146 'title' => __('Send mail to customer when a subscription is canceled', 'fluent-cart'),
147 'description' => __('This email will be sent to the customer when their subscription is canceled.', 'fluent-cart'),
148 'recipient' => 'customer',
149 'smartcode_groups' => [],
150 'template_path' => 'subscription.canceled.customer',
151 'is_async' => false,
152 'settings' => [
153 'active' => 'yes',
154 'subject' => __('Subscription Canceled on {{settings.store_name}}', 'fluent-cart'),
155 'is_default_body' => 'yes',
156 'email_body' => '',
157 ]
158 ],
159 'subscription_canceled_admin' => [
160 'event' => 'subscription_canceled',
161 'group' => 'subscription',
162 'group_label' => __('Subscription Actions', 'fluent-cart'),
163 'title' => __('Send mail to admin when a subscription is canceled', 'fluent-cart'),
164 'description' => __('This email will be sent to the admin when a subscription is canceled.', 'fluent-cart'),
165 'recipient' => 'admin',
166 'smartcode_groups' => [],
167 'template_path' => 'subscription.canceled.admin',
168 'pre_header' => __('A subscription has been canceled. Review the details in this email or visit FluentCart Dashboard to manage the subscription.', 'fluent-cart'),
169 'is_async' => false,
170 'settings' => [
171 'active' => 'yes',
172 'subject' => __('Subscription Canceled - {{order.customer.full_name}}', 'fluent-cart'),
173 'is_default_body' => 'yes',
174 'email_body' => '',
175 ]
176 ],
177 'subscription_period_skipped_customer' => [
178 'event' => 'subscription_period_skipped',
179 'group' => 'subscription',
180 'group_label' => __('Subscription Actions', 'fluent-cart'),
181 'title' => __('Notify customer when a billing period is skipped', 'fluent-cart'),
182 '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'),
183 'recipient' => 'customer',
184 'smartcode_groups' => [],
185 'template_path' => 'subscription.period_skipped.customer',
186 'is_async' => false,
187 'settings' => [
188 'active' => 'no',
189 'subject' => __('Your next payment has been skipped on {{settings.store_name}}', 'fluent-cart'),
190 'is_default_body' => 'yes',
191 'email_body' => '',
192 ]
193 ],
194 'order_refunded_admin' => [
195 'event' => 'order_refunded',
196 'group' => 'order',
197 'group_label' => __('Order Actions', 'fluent-cart'),
198 'title' => __('Send mail to admin after a refund.', 'fluent-cart'),
199 'description' => __('This email will be sent to the admin after an order is refunded (partial / full).', 'fluent-cart'),
200 'recipient' => 'admin',
201 'smartcode_groups' => [],
202 'template_path' => 'order.refunded.admin',
203 'is_async' => false,
204 'settings' => [
205 'active' => 'no',
206 'subject' => __('Refund sent to {{order.customer.full_name}}', 'fluent-cart'),
207 'is_default_body' => 'yes',
208 'email_body' => '',
209 'attach_pdf_template' => '',
210 ]
211 ],
212 'order_refunded_customer' => [
213 'event' => 'order_refunded',
214 'group' => 'order',
215 'group_label' => __('Order Actions', 'fluent-cart'),
216 'title' => __('Send mail to customer after a refund.', 'fluent-cart'),
217 'description' => __('This email will be sent to the customer after an order is refunded (partial / full).', 'fluent-cart'),
218 'recipient' => 'customer',
219 'smartcode_groups' => [],
220 'template_path' => 'order.refunded.customer',
221 'is_async' => false,
222 'settings' => [
223 'active' => 'yes',
224 'subject' => __('Refund Confirmation from {{settings.store_name}}', 'fluent-cart'),
225 'is_default_body' => 'yes',
226 'email_body' => '',
227 'attach_pdf_template' => '',
228 ]
229 ],
230 'order_shipped_customer' => [
231 'event' => 'shipping_status_changed_to_shipped',
232 'group' => 'order',
233 'group_label' => __('Order Actions', 'fluent-cart'),
234 'title' => __('Send mail to customer when shipping status changed to shipped.', 'fluent-cart'),
235 'description' => __('This email will be sent to the customer after an order is marked as shipped', 'fluent-cart'),
236 'recipient' => 'customer',
237 'smartcode_groups' => [],
238 'template_path' => 'order.shipped.customer',
239 'is_async' => false,
240 'settings' => [
241 'active' => 'yes',
242 'subject' => __('Order has been shipped #{{order.invoice_no}} 📦', 'fluent-cart'),
243 'is_default_body' => 'yes',
244 'email_body' => '',
245 'attach_pdf_template' => '',
246 ]
247 ],
248 'order_delivered_customer' => [
249 'event' => 'shipping_status_changed_to_delivered',
250 'group' => 'order',
251 'group_label' => __('Order Actions', 'fluent-cart'),
252 'title' => __('Send mail to customer when shipping status changed to delivered.', 'fluent-cart'),
253 'description' => __('This email will be sent to the customer after an order is marked as delivered', 'fluent-cart'),
254 'recipient' => 'customer',
255 'smartcode_groups' => [],
256 'template_path' => 'order.delivered.customer',
257 'is_async' => false,
258 'settings' => [
259 'active' => 'yes',
260 'subject' => __('Order has been delivered #{{order.invoice_no}}', 'fluent-cart'),
261 'is_default_body' => 'yes',
262 'email_body' => '',
263 'attach_pdf_template' => '',
264 ]
265 ],
266 'order_placed_admin' => [
267 'event' => 'order_placed_offline',
268 'group' => 'order',
269 'group_label' => __('Order Actions', 'fluent-cart'),
270 'title' => __('Send mail to admin after New Order Placed (Offline Payment)', 'fluent-cart'),
271 'description' => __('This email will be sent to the admin when an order is placed using offline payment method.', 'fluent-cart'),
272 'recipient' => 'admin',
273 'smartcode_groups' => [],
274 'template_path' => 'order.placed.admin',
275 'is_async' => false,
276 'manage_toggle' => 'no',
277 'toggle_label' => __('Auto-enabled for offline payments', 'fluent-cart'),
278 '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'),
279 'settings' => [
280 'subject' => __('New Order on {{settings.store_name}} (Offline Payment)', 'fluent-cart'),
281 'is_default_body' => 'yes',
282 'email_body' => '',
283 'attach_pdf_template' => '',
284 ]
285 ],
286 'order_placed_customer' => [
287 'event' => 'order_placed_offline',
288 'group' => 'order',
289 'group_label' => __('Order Actions', 'fluent-cart'),
290 'title' => __('Order confirmation to customer (Offline Payment)', 'fluent-cart'),
291 'description' => __('This email will be sent to the customer when an order is placed using offline payment method.', 'fluent-cart'),
292 'recipient' => 'customer',
293 'smartcode_groups' => [],
294 'template_path' => 'order.placed.customer',
295 'is_async' => false,
296 'manage_toggle' => 'no',
297 'toggle_label' => __('Auto-enabled for offline payments', 'fluent-cart'),
298 'settings' => [
299 'subject' => __('Order Confirmation #{{order.invoice_no}} (Offline Payment)', 'fluent-cart'),
300 'is_default_body' => 'yes',
301 'email_body' => '',
302 'attach_pdf_template' => '',
303 ]
304 ],
305 'renewal_created_admin' => [
306 'event' => 'renewal_created',
307 'group' => 'manual_subscription',
308 'group_label' => __('Store-Managed Renewals', 'fluent-cart'),
309 'title' => __('Notify admin when a renewal order is created', 'fluent-cart'),
310 'description' => __('This email will be sent to the admin when a renewal order is generated for a store-managed subscription.', 'fluent-cart'),
311 'recipient' => 'admin',
312 'smartcode_groups' => [],
313 'template_path' => 'renewal.created.admin',
314 'pre_header' => 'A new renewal order has been created for a store-managed subscription. Review it in the FluentCart Dashboard.',
315 'is_async' => false,
316 'settings' => [
317 'active' => 'yes',
318 'subject' => __('New Renewal Order Created on {{settings.store_name}}', 'fluent-cart'),
319 'is_default_body' => 'yes',
320 'email_body' => '',
321 ]
322 ],
323 'renewal_created_customer' => [
324 'event' => 'renewal_created',
325 'group' => 'manual_subscription',
326 'group_label' => __('Store-Managed Renewals', 'fluent-cart'),
327 'title' => __('Send the renewal order to the customer when a renewal order is created', 'fluent-cart'),
328 'description' => __('This email will be sent to the customer when a renewal order is generated for a store-managed subscription.', 'fluent-cart'),
329 'recipient' => 'customer',
330 'smartcode_groups' => [],
331 'template_path' => 'renewal.created.customer',
332 'is_async' => false,
333 'settings' => [
334 'active' => 'yes',
335 'subject' => __('Your Subscription Renewal #{{order.invoice_no}}', 'fluent-cart'),
336 'is_default_body' => 'yes',
337 'email_body' => '',
338 ]
339 ],
340 'system_upcoming_charge_customer' => [
341 'event' => 'system_upcoming_charge',
342 'group' => 'system_subscription',
343 'group_label' => __('Store-Managed Renewals · Auto-Charge', 'fluent-cart'),
344 'title' => __('Notify customer before an automatic renewal charge', 'fluent-cart'),
345 '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'),
346 'recipient' => 'customer',
347 'smartcode_groups' => [],
348 'template_path' => 'subscription.upcoming_charge.customer',
349 'is_async' => false,
350 'settings' => [
351 'active' => 'yes',
352 'subject' => __('Upcoming automatic payment for your subscription', 'fluent-cart'),
353 'is_default_body' => 'yes',
354 'email_body' => '',
355 ]
356 ],
357 'system_charge_failed_customer' => [
358 'event' => 'system_charge_failed',
359 'group' => 'system_subscription',
360 'group_label' => __('Store-Managed Renewals · Auto-Charge', 'fluent-cart'),
361 'title' => __('Notify customer when an automatic renewal charge fails', 'fluent-cart'),
362 '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'),
363 'recipient' => 'customer',
364 'smartcode_groups' => [],
365 'template_path' => 'subscription.charge_failed.customer',
366 'is_async' => false,
367 'settings' => [
368 'active' => 'yes',
369 'subject' => __('Automatic Payment Failed for Renewal #{{order.invoice_no}}', 'fluent-cart'),
370 'is_default_body' => 'yes',
371 'email_body' => '',
372 ]
373 ],
374 'system_charge_failed_admin' => [
375 'event' => 'system_charge_failed',
376 'group' => 'system_subscription',
377 'group_label' => __('Store-Managed Renewals · Auto-Charge', 'fluent-cart'),
378 'title' => __('Notify admin when an automatic renewal charge fails', 'fluent-cart'),
379 'description' => __('This email will be sent to the admin when a saved payment method could not be charged for a renewal order.', 'fluent-cart'),
380 'recipient' => 'admin',
381 'smartcode_groups' => [],
382 'template_path' => 'subscription.charge_failed.admin',
383 'pre_header' => 'An automatic subscription renewal charge failed. Review the renewal order in the FluentCart Dashboard.',
384 'is_async' => false,
385 'settings' => [
386 'active' => 'no',
387 'subject' => __('Automatic Charge Failed for Renewal #{{order.invoice_no}}', 'fluent-cart'),
388 'is_default_body' => 'yes',
389 'email_body' => '',
390 ]
391 ],
392 'renewal_reminder_due_admin' => [
393 'event' => 'renewal_reminder_due',
394 'group' => 'manual_subscription',
395 'group_label' => __('Store-Managed Renewals', 'fluent-cart'),
396 'title' => __('Renewal due reminder copy to admin', 'fluent-cart'),
397 'description' => __('This email will be sent to admin when a due reminder is sent to customer.', 'fluent-cart'),
398 'recipient' => 'admin',
399 'smartcode_groups' => [],
400 'template_path' => 'order.reminder.due.admin',
401 'pre_header' => 'A renewal due reminder was sent to a customer. Review the order details in this email or visit FluentCart Dashboard.',
402 'is_async' => false,
403 'settings' => [
404 'active' => 'no',
405 'subject' => __('Renewal Reminder Sent #{{order.invoice_no}}', 'fluent-cart'),
406 'is_default_body' => 'yes',
407 'email_body' => '',
408 ]
409 ],
410 'renewal_reminder_due_customer' => [
411 'event' => 'renewal_reminder_due',
412 'group' => 'manual_subscription',
413 'group_label' => __('Store-Managed Renewals', 'fluent-cart'),
414 'title' => __('Renewal due reminder to customer', 'fluent-cart'),
415 'description' => __('This email will be sent before/at renewal due date when payment is pending.', 'fluent-cart'),
416 'recipient' => 'customer',
417 'smartcode_groups' => [],
418 'template_path' => 'order.reminder.due.customer',
419 'is_async' => false,
420 'settings' => [
421 'active' => 'yes',
422 'subject' => __('Payment Reminder #{{order.invoice_no}}', 'fluent-cart'),
423 'is_default_body' => 'yes',
424 'email_body' => '',
425 ]
426 ],
427 'renewal_reminder_overdue_customer' => [
428 'event' => 'renewal_reminder_overdue',
429 'group' => 'scheduler',
430 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
431 'title' => __('Payment reminder to customer', 'fluent-cart'),
432 'description' => __('This email will be sent to remind customer about pending payment.', 'fluent-cart'),
433 'recipient' => 'customer',
434 'smartcode_groups' => [],
435 'template_path' => 'order.reminder.overdue.customer',
436 'is_async' => false,
437 'manage_toggle' => 'no',
438 'toggle_label' => __('On demand', 'fluent-cart'),
439 'settings' => [
440 'subject' => __('Payment Reminder - Order #{{order.id}}', 'fluent-cart'),
441 'is_default_body' => 'yes',
442 'email_body' => '',
443 ]
444 ],
445 'subscription_past_due_admin' => [
446 'event' => 'subscription_past_due',
447 'group' => 'subscription',
448 'group_label' => __('Subscription Actions', 'fluent-cart'),
449 'title' => __('Notify admin when a subscription is past due', 'fluent-cart'),
450 'description' => __('This email will be sent to the admin when a subscription is marked as past due.', 'fluent-cart'),
451 'recipient' => 'admin',
452 'smartcode_groups' => [],
453 'template_path' => 'subscription.past_due.admin',
454 'pre_header' => 'A subscription payment is past due. Review the subscription details in this email or visit FluentCart Dashboard.',
455 'is_async' => false,
456 'settings' => [
457 'active' => 'no',
458 'subject' => __('Subscription Past Due #{{order.invoice_no}}', 'fluent-cart'),
459 'is_default_body' => 'yes',
460 'email_body' => '',
461 ]
462 ],
463 'subscription_past_due_customer' => [
464 'event' => 'subscription_past_due',
465 'group' => 'subscription',
466 'group_label' => __('Subscription Actions', 'fluent-cart'),
467 'title' => __('Notify customer when subscription is past due', 'fluent-cart'),
468 'description' => __('This email will be sent to the customer when their subscription is marked as past due.', 'fluent-cart'),
469 'recipient' => 'customer',
470 'smartcode_groups' => [],
471 'template_path' => 'subscription.past_due.customer',
472 'is_async' => false,
473 'settings' => [
474 'active' => 'yes',
475 'subject' => __('Your Subscription is Past Due on {{settings.store_name}}', 'fluent-cart'),
476 'is_default_body' => 'yes',
477 'email_body' => '',
478 ]
479 ],
480 'subscription_renewal_reminder_admin' => [
481 'event' => 'subscription_renewal_reminder',
482 'group' => 'scheduler',
483 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
484 'title' => __('Upcoming renewal reminder copy to admin', 'fluent-cart'),
485 'description' => __('This email will be sent to admin when an upcoming renewal reminder is sent.', 'fluent-cart'),
486 'recipient' => 'admin',
487 'smartcode_groups' => [],
488 'template_path' => 'subscription.reminder.admin',
489 'pre_header' => __('A subscription renewal reminder was sent to a customer. Review subscription details from FluentCart Dashboard.', 'fluent-cart'),
490 'is_async' => false,
491 'settings' => [
492 'active' => 'no',
493 'subject' => __('Renewal Reminder Sent for {{order.customer.full_name}}', 'fluent-cart'),
494 'is_default_body' => 'yes',
495 'email_body' => '',
496 ]
497 ],
498 'subscription_renewal_reminder_customer' => [
499 'event' => 'subscription_renewal_reminder',
500 'group' => 'scheduler',
501 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
502 'title' => __('Upcoming renewal reminder to customer', 'fluent-cart'),
503 'description' => __('This email will be sent before subscription auto-renewal date.', 'fluent-cart'),
504 'recipient' => 'customer',
505 'smartcode_groups' => [],
506 'template_path' => 'subscription.reminder.customer',
507 'is_async' => false,
508 'settings' => [
509 'active' => 'yes',
510 'subject' => __('Upcoming Renewal Reminder from {{settings.store_name}}', 'fluent-cart'),
511 'is_default_body' => 'yes',
512 'email_body' => '',
513 ]
514 ],
515 'subscription_trial_end_reminder_admin' => [
516 'event' => 'subscription_trial_end_reminder',
517 'group' => 'scheduler',
518 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
519 'title' => __('Trial ending soon reminder copy to admin', 'fluent-cart'),
520 'description' => __('This email will be sent to admin when a trial ending reminder is sent to a customer.', 'fluent-cart'),
521 'recipient' => 'admin',
522 'smartcode_groups' => [],
523 'template_path' => 'subscription.trial_end.admin',
524 'pre_header' => __('A trial ending soon reminder was sent to a customer. Review subscription details from FluentCart Dashboard.', 'fluent-cart'),
525 'is_async' => false,
526 'settings' => [
527 'active' => 'no',
528 'subject' => __('Trial Ending Soon - {{order.customer.full_name}}', 'fluent-cart'),
529 'is_default_body' => 'yes',
530 'email_body' => '',
531 ]
532 ],
533 'subscription_trial_end_reminder_customer' => [
534 'event' => 'subscription_trial_end_reminder',
535 'group' => 'scheduler',
536 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
537 'title' => __('Trial ending soon reminder to customer', 'fluent-cart'),
538 'description' => __('This email will be sent before a trial period ends and converts to a paid subscription.', 'fluent-cart'),
539 'recipient' => 'customer',
540 'smartcode_groups' => [],
541 'template_path' => 'subscription.trial_end.customer',
542 'is_async' => false,
543 'settings' => [
544 'active' => 'yes',
545 'subject' => __('Your Trial is Ending Soon - {{settings.store_name}}', 'fluent-cart'),
546 'is_default_body' => 'yes',
547 'email_body' => '',
548 ]
549 ],
550 ];
551
552 }
553
554 public static function getNotificationsOfEvent($event, $viewData): array
555 {
556 $notifications = static::getNotifications();
557 $notifications = array_filter($notifications, function ($notification) use ($event) {
558 return $notification['event'] === $event;
559 });
560
561 if (empty($notifications)) {
562 return [];
563 }
564
565 $emails = [];
566 $mailingSettings = static::getSettings();
567
568 foreach ($notifications as $key => $notification) {
569
570 $settings = Arr::get($notification, 'settings');
571
572 // Skip active check for on-demand notifications (offline payments, payment reminders)
573 if (in_array($notification['event'], ['order_placed_offline', 'renewal_reminder_overdue']) || Arr::get($settings, 'active') === 'yes') {
574 // Continue with normal flow
575 } else {
576 continue;
577 }
578 $recipient = Arr::get($notification, 'recipient');
579
580 // A notification that declares its own `to` carries its recipient
581 // with it, so the whitelist — which only describes the recipients
582 // core knows how to resolve — does not apply to it.
583 if (!Arr::get($notification, 'to') && !in_array($recipient, ['admin', 'customer', 'user', 'subscriber'])) {
584 continue;
585 }
586
587 $toEmail = static::resolveRecipientTemplate($notification, $mailingSettings);
588
589 if (empty($toEmail)) {
590 continue;
591 }
592
593 $emails[$key] = static::formatNotification($notification, $viewData);
594 }
595
596 return $emails;
597 }
598
599 public static function formatNotification($notification, $viewData): array
600 {
601 $settings = Arr::get($notification, 'settings');
602 $mailingSettings = static::getSettings();
603
604 $recipient = Arr::get($notification, 'recipient');
605 $toEmail = static::resolveRecipientTemplate($notification, $mailingSettings);
606
607
608 $isDefaultEmailBody = Arr::get($settings, 'is_default_body', 'yes') === 'yes' || empty(Arr::get($settings, 'email_body'));
609
610 $emailBody = $isDefaultEmailBody ?
611 TemplateService::getTemplateByPathName(Arr::get($notification, 'template_path'), $viewData) :
612 Arr::get($settings, 'email_body');
613
614 return [
615 'to' => $toEmail,
616 'body' => $emailBody,
617 'pre_header' => Arr::get($notification, 'pre_header', ''),
618 'is_async' => $notification['is_async'],
619 'subject' => Arr::get($settings, 'subject'),
620 'is_custom' => !$isDefaultEmailBody,
621 'settings' => $settings,
622 // Carried through so send-time consumers can tell one notification
623 // from another, and admin-bound mail from customer-bound — see the
624 // admin recipient filter in parseEmailContent().
625 'name' => Arr::get($notification, 'name', ''),
626 'event' => Arr::get($notification, 'event', ''),
627 'recipient' => $recipient,
628 ];
629 }
630
631 /**
632 * Resolve the recipient template for a notification.
633 *
634 * A notification may declare its own `to` — a smartcode template, or a
635 * literal address — which wins outright. Add-ons need this: they register
636 * notifications that are not order-bound, and the built-in customer
637 * recipient resolves to {{order.customer.email}}, which has nothing to read
638 * from without an order.
639 *
640 * Falling back to the recipient map keeps every core notification, and any
641 * add-on that does not declare `to`, on exactly the path it used before.
642 *
643 * The returned value is a template — parseEmailContent() runs it through
644 * the shortcode parser before use. A declared `to` is returned as given:
645 * wp_mail() accepts an array of recipients as well as a comma-separated
646 * string, so an array is passed through rather than flattened.
647 *
648 * @param array $notification
649 * @param array|null $mailingSettings resolved settings, passed in to avoid a repeat lookup in loops
650 * @return string|array
651 */
652 public static function resolveRecipientTemplate($notification, $mailingSettings = null)
653 {
654 $declaredTo = Arr::get($notification, 'to', '');
655 if (!empty($declaredTo)) {
656 return $declaredTo;
657 }
658
659 if ($mailingSettings === null) {
660 $mailingSettings = static::getSettings();
661 }
662
663 $recipient = Arr::get($notification, 'recipient');
664
665 if ($recipient === 'admin') {
666 return (string)Arr::get($mailingSettings, 'admin_email', '');
667 }
668
669 return (string)Arr::get(self::EMAIL_RECIPIENT_MAP, $recipient, '');
670 }
671
672 public static function getNotification($name)
673 {
674 $notifications = static::getNotifications();
675 return Arr::get($notifications, $name);
676 }
677
678 //returns only the notification settings
679 public static function getNotificationConfig($notificationName = null)
680 {
681 $configs = self::getNotifications();
682 if ($notificationName) {
683 return Arr::get($configs, $notificationName . '.settings', []);
684 }
685 return Arr::pluck($configs, 'settings', 'name');
686 }
687
688 public static function updateNotification($name, $data)
689 {
690 $updateableKeys = [
691 'active',
692 'subject',
693 'email_body',
694 'is_default_body',
695 'attach_pdf_template',
696 ];
697 $allConfig = static::getSettings();
698 $config = static::getNotificationConfig($name);
699
700 foreach ($updateableKeys as $key) {
701 $defaultValue = Arr::get($config, $key, '');
702 $config[$key] = Arr::get($data, $key, $defaultValue);
703 }
704
705 // When switching back to default body, clear the custom email body unless draft mode is enabled
706 if (Arr::get($config, 'is_default_body') === 'yes' && !apply_filters('fluent_cart/keep_email_body_draft', false, ['notification_name' => $name])) {
707 $config['email_body'] = '';
708 }
709
710 Arr::set($allConfig, 'notification_config.' . $name, $config);
711
712 $isUpdate = Meta::query()->updateOrCreate(
713 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
714 ['meta_key' => static::META_KEY, 'object_type' => 'email_notification'],
715 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
716 ['meta_value' => $allConfig]
717 );
718
719 if ($isUpdate) {
720 static::updateCache();
721 }
722
723 return static::getNotification($name);
724 }
725
726 public static function defaultSettings(): array
727 {
728 return [
729 'from_name' => '',
730 'from_email' => '',
731 'reply_to_name' => '',
732 'reply_to_email' => '',
733 'email_footer' => '',
734 'show_email_footer' => 'yes',
735 'admin_email' => '{{wp.admin_email}}',
736 ];
737 }
738
739 //returns all the settings
740 public static function getSettings($key = null)
741 {
742
743 $defaultSettings = static::defaultSettings();
744 $cachedSettings = static::cachedSettings();
745 $settings = wp_parse_args($cachedSettings, $defaultSettings);
746
747 $settings['notification_config'] = wp_parse_args(
748 Arr::get($cachedSettings, 'notification_config', []),
749 Arr::get($defaultSettings, 'notification_config', [])
750 );
751
752 if (!empty($key)) {
753 return Arr::get($settings, $key);
754 }
755
756 return $settings;
757 }
758
759 public static function cachedSettings()
760 {
761 return Cache::get(static::META_KEY, function () {
762 $config = Meta::query()->where('object_type', 'email_notification')
763 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
764 ->where('meta_key', static::META_KEY)->first();
765 return $config ? $config->meta_value : [];
766 });
767 }
768
769 public static function updateSettings($data)
770 {
771 $allConfig = static::getSettings();
772
773 foreach ($data as $key => $value) {
774 Arr::set($allConfig, $key, $value);
775 }
776
777 $config = Meta::query()->updateOrCreate(
778 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
779 ['meta_key' => static::META_KEY, 'object_type' => 'email_notification'],
780 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
781 ['meta_value' => $allConfig]
782 );
783
784 static::updateCache();
785 return $config;
786 }
787
788 private static function updateCache(): void
789 {
790 Cache::forget(static::META_KEY);
791 }
792 }
793