| 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 |
]; |
| 640 |
|
| 641 |
} |
| 642 |
|
| 643 |
public static function getNotificationsOfEvent($event, $viewData): array |
| 644 |
{ |
| 645 |
$notifications = static::getNotifications(); |
| 646 |
$notifications = array_filter($notifications, function ($notification) use ($event) { |
| 647 |
return $notification['event'] === $event; |
| 648 |
}); |
| 649 |
|
| 650 |
if (empty($notifications)) { |
| 651 |
return []; |
| 652 |
} |
| 653 |
|
| 654 |
$emails = []; |
| 655 |
$mailingSettings = static::getSettings(); |
| 656 |
|
| 657 |
foreach ($notifications as $key => $notification) { |
| 658 |
|
| 659 |
$settings = Arr::get($notification, 'settings'); |
| 660 |
|
| 661 |
// Skip active check for on-demand notifications (offline payments, payment reminders) |
| 662 |
if (in_array($notification['event'], ['order_placed_offline', 'renewal_reminder_overdue']) || Arr::get($settings, 'active') === 'yes') { |
| 663 |
// Continue with normal flow |
| 664 |
} else { |
| 665 |
continue; |
| 666 |
} |
| 667 |
$recipient = Arr::get($notification, 'recipient'); |
| 668 |
|
| 669 |
// A notification that declares its own `to` carries its recipient |
| 670 |
// with it, so the whitelist — which only describes the recipients |
| 671 |
// core knows how to resolve — does not apply to it. |
| 672 |
if (!Arr::get($notification, 'to') && !in_array($recipient, ['admin', 'customer', 'user', 'subscriber'])) { |
| 673 |
continue; |
| 674 |
} |
| 675 |
|
| 676 |
$toEmail = static::resolveRecipientTemplate($notification, $mailingSettings); |
| 677 |
|
| 678 |
if (empty($toEmail)) { |
| 679 |
continue; |
| 680 |
} |
| 681 |
|
| 682 |
$emails[$key] = static::formatNotification($notification, $viewData); |
| 683 |
} |
| 684 |
|
| 685 |
return $emails; |
| 686 |
} |
| 687 |
|
| 688 |
public static function formatNotification($notification, $viewData): array |
| 689 |
{ |
| 690 |
$settings = Arr::get($notification, 'settings'); |
| 691 |
$mailingSettings = static::getSettings(); |
| 692 |
|
| 693 |
$recipient = Arr::get($notification, 'recipient'); |
| 694 |
$toEmail = static::resolveRecipientTemplate($notification, $mailingSettings); |
| 695 |
|
| 696 |
|
| 697 |
$isDefaultEmailBody = Arr::get($settings, 'is_default_body', 'yes') === 'yes' || empty(Arr::get($settings, 'email_body')); |
| 698 |
|
| 699 |
$emailBody = $isDefaultEmailBody ? |
| 700 |
TemplateService::getTemplateByPathName(Arr::get($notification, 'template_path'), $viewData) : |
| 701 |
Arr::get($settings, 'email_body'); |
| 702 |
|
| 703 |
return [ |
| 704 |
'to' => $toEmail, |
| 705 |
'body' => $emailBody, |
| 706 |
'pre_header' => Arr::get($notification, 'pre_header', ''), |
| 707 |
'is_async' => $notification['is_async'], |
| 708 |
'subject' => Arr::get($settings, 'subject'), |
| 709 |
'is_custom' => !$isDefaultEmailBody, |
| 710 |
'settings' => $settings, |
| 711 |
// Carried through so send-time consumers can tell one notification |
| 712 |
// from another, and admin-bound mail from customer-bound — see the |
| 713 |
// admin recipient filter in parseEmailContent(). |
| 714 |
'name' => Arr::get($notification, 'name', ''), |
| 715 |
'event' => Arr::get($notification, 'event', ''), |
| 716 |
'recipient' => $recipient, |
| 717 |
]; |
| 718 |
} |
| 719 |
|
| 720 |
/** |
| 721 |
* Resolve the recipient template for a notification. |
| 722 |
* |
| 723 |
* A notification may declare its own `to` — a smartcode template, or a |
| 724 |
* literal address — which wins outright. Add-ons need this: they register |
| 725 |
* notifications that are not order-bound, and the built-in customer |
| 726 |
* recipient resolves to {{order.customer.email}}, which has nothing to read |
| 727 |
* from without an order. |
| 728 |
* |
| 729 |
* Falling back to the recipient map keeps every core notification, and any |
| 730 |
* add-on that does not declare `to`, on exactly the path it used before. |
| 731 |
* |
| 732 |
* The returned value is a template — parseEmailContent() runs it through |
| 733 |
* the shortcode parser before use. A declared `to` is returned as given: |
| 734 |
* wp_mail() accepts an array of recipients as well as a comma-separated |
| 735 |
* string, so an array is passed through rather than flattened. |
| 736 |
* |
| 737 |
* @param array $notification |
| 738 |
* @param array|null $mailingSettings resolved settings, passed in to avoid a repeat lookup in loops |
| 739 |
* @return string|array |
| 740 |
*/ |
| 741 |
public static function resolveRecipientTemplate($notification, $mailingSettings = null) |
| 742 |
{ |
| 743 |
$declaredTo = Arr::get($notification, 'to', ''); |
| 744 |
if (!empty($declaredTo)) { |
| 745 |
return $declaredTo; |
| 746 |
} |
| 747 |
|
| 748 |
if ($mailingSettings === null) { |
| 749 |
$mailingSettings = static::getSettings(); |
| 750 |
} |
| 751 |
|
| 752 |
$recipient = Arr::get($notification, 'recipient'); |
| 753 |
|
| 754 |
if ($recipient === 'admin') { |
| 755 |
return (string)Arr::get($mailingSettings, 'admin_email', ''); |
| 756 |
} |
| 757 |
|
| 758 |
return (string)Arr::get(self::EMAIL_RECIPIENT_MAP, $recipient, ''); |
| 759 |
} |
| 760 |
|
| 761 |
public static function getNotification($name) |
| 762 |
{ |
| 763 |
$notifications = static::getNotifications(); |
| 764 |
return Arr::get($notifications, $name); |
| 765 |
} |
| 766 |
|
| 767 |
//returns only the notification settings |
| 768 |
public static function getNotificationConfig($notificationName = null) |
| 769 |
{ |
| 770 |
$configs = self::getNotifications(); |
| 771 |
if ($notificationName) { |
| 772 |
return Arr::get($configs, $notificationName . '.settings', []); |
| 773 |
} |
| 774 |
return Arr::pluck($configs, 'settings', 'name'); |
| 775 |
} |
| 776 |
|
| 777 |
public static function updateNotification($name, $data) |
| 778 |
{ |
| 779 |
$updateableKeys = [ |
| 780 |
'active', |
| 781 |
'subject', |
| 782 |
'email_body', |
| 783 |
'is_default_body', |
| 784 |
'attach_pdf_template', |
| 785 |
]; |
| 786 |
$allConfig = static::getSettings(); |
| 787 |
$config = static::getNotificationConfig($name); |
| 788 |
|
| 789 |
foreach ($updateableKeys as $key) { |
| 790 |
$defaultValue = Arr::get($config, $key, ''); |
| 791 |
$config[$key] = Arr::get($data, $key, $defaultValue); |
| 792 |
} |
| 793 |
|
| 794 |
// When switching back to default body, clear the custom email body unless draft mode is enabled |
| 795 |
if (Arr::get($config, 'is_default_body') === 'yes' && !apply_filters('fluent_cart/keep_email_body_draft', false, ['notification_name' => $name])) { |
| 796 |
$config['email_body'] = ''; |
| 797 |
} |
| 798 |
|
| 799 |
Arr::set($allConfig, 'notification_config.' . $name, $config); |
| 800 |
|
| 801 |
$isUpdate = Meta::query()->updateOrCreate( |
| 802 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 803 |
['meta_key' => static::META_KEY, 'object_type' => 'email_notification'], |
| 804 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 805 |
['meta_value' => $allConfig] |
| 806 |
); |
| 807 |
|
| 808 |
if ($isUpdate) { |
| 809 |
static::updateCache(); |
| 810 |
} |
| 811 |
|
| 812 |
return static::getNotification($name); |
| 813 |
} |
| 814 |
|
| 815 |
public static function defaultSettings(): array |
| 816 |
{ |
| 817 |
return [ |
| 818 |
'from_name' => '', |
| 819 |
'from_email' => '', |
| 820 |
'reply_to_name' => '', |
| 821 |
'reply_to_email' => '', |
| 822 |
'email_footer' => '', |
| 823 |
'show_email_footer' => 'yes', |
| 824 |
'admin_email' => '{{wp.admin_email}}', |
| 825 |
]; |
| 826 |
} |
| 827 |
|
| 828 |
//returns all the settings |
| 829 |
public static function getSettings($key = null) |
| 830 |
{ |
| 831 |
|
| 832 |
$defaultSettings = static::defaultSettings(); |
| 833 |
$cachedSettings = static::cachedSettings(); |
| 834 |
$settings = wp_parse_args($cachedSettings, $defaultSettings); |
| 835 |
|
| 836 |
$settings['notification_config'] = wp_parse_args( |
| 837 |
Arr::get($cachedSettings, 'notification_config', []), |
| 838 |
Arr::get($defaultSettings, 'notification_config', []) |
| 839 |
); |
| 840 |
|
| 841 |
if (!empty($key)) { |
| 842 |
return Arr::get($settings, $key); |
| 843 |
} |
| 844 |
|
| 845 |
return $settings; |
| 846 |
} |
| 847 |
|
| 848 |
public static function cachedSettings() |
| 849 |
{ |
| 850 |
return Cache::get(static::META_KEY, function () { |
| 851 |
$config = Meta::query()->where('object_type', 'email_notification') |
| 852 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 853 |
->where('meta_key', static::META_KEY)->first(); |
| 854 |
return $config ? $config->meta_value : []; |
| 855 |
}); |
| 856 |
} |
| 857 |
|
| 858 |
public static function updateSettings($data) |
| 859 |
{ |
| 860 |
$allConfig = static::getSettings(); |
| 861 |
|
| 862 |
foreach ($data as $key => $value) { |
| 863 |
Arr::set($allConfig, $key, $value); |
| 864 |
} |
| 865 |
|
| 866 |
$config = Meta::query()->updateOrCreate( |
| 867 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 868 |
['meta_key' => static::META_KEY, 'object_type' => 'email_notification'], |
| 869 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 870 |
['meta_value' => $allConfig] |
| 871 |
); |
| 872 |
|
| 873 |
static::updateCache(); |
| 874 |
return $config; |
| 875 |
} |
| 876 |
|
| 877 |
private static function updateCache(): void |
| 878 |
{ |
| 879 |
Cache::forget(static::META_KEY); |
| 880 |
} |
| 881 |
} |
| 882 |
|