PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.20
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.20
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.3.20, at app/Services/Email/EmailNotifications.php

610 lines 29.8 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/invoice_reminder_due (@todo uncomment when invoice feature is deployed)
62 * - fluent_cart/invoice_reminder_overdue (used for payment reminders)
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 'order_refunded_admin' => [
178 'event' => 'order_refunded',
179 'group' => 'order',
180 'group_label' => __('Order Actions', 'fluent-cart'),
181 'title' => __('Send mail to admin after a refund.', 'fluent-cart'),
182 'description' => __('This email will be sent to the admin after an order is refunded (partial / full).', 'fluent-cart'),
183 'recipient' => 'admin',
184 'smartcode_groups' => [],
185 'template_path' => 'order.refunded.admin',
186 'is_async' => false,
187 'settings' => [
188 'active' => 'no',
189 'subject' => __('Refund sent to {{order.customer.full_name}}', 'fluent-cart'),
190 'is_default_body' => 'yes',
191 'email_body' => '',
192 'attach_pdf_template' => '',
193 ]
194 ],
195 'order_refunded_customer' => [
196 'event' => 'order_refunded',
197 'group' => 'order',
198 'group_label' => __('Order Actions', 'fluent-cart'),
199 'title' => __('Send mail to customer after a refund.', 'fluent-cart'),
200 'description' => __('This email will be sent to the customer after an order is refunded (partial / full).', 'fluent-cart'),
201 'recipient' => 'customer',
202 'smartcode_groups' => [],
203 'template_path' => 'order.refunded.customer',
204 'is_async' => false,
205 'settings' => [
206 'active' => 'yes',
207 'subject' => __('Refund Confirmation from {{settings.store_name}}', 'fluent-cart'),
208 'is_default_body' => 'yes',
209 'email_body' => '',
210 'attach_pdf_template' => '',
211 ]
212 ],
213 'order_shipped_customer' => [
214 'event' => 'shipping_status_changed_to_shipped',
215 'group' => 'order',
216 'group_label' => __('Order Actions', 'fluent-cart'),
217 'title' => __('Send mail to customer when shipping status changed to shipped.', 'fluent-cart'),
218 'description' => __('This email will be sent to the customer after an order is marked as shipped', 'fluent-cart'),
219 'recipient' => 'customer',
220 'smartcode_groups' => [],
221 'template_path' => 'order.shipped.customer',
222 'is_async' => false,
223 'settings' => [
224 'active' => 'yes',
225 'subject' => __('Order has been shipped #{{order.invoice_no}} 📦', 'fluent-cart'),
226 'is_default_body' => 'yes',
227 'email_body' => '',
228 'attach_pdf_template' => '',
229 ]
230 ],
231 'order_delivered_customer' => [
232 'event' => 'shipping_status_changed_to_delivered',
233 'group' => 'order',
234 'group_label' => __('Order Actions', 'fluent-cart'),
235 'title' => __('Send mail to customer when shipping status changed to delivered.', 'fluent-cart'),
236 'description' => __('This email will be sent to the customer after an order is marked as delivered', 'fluent-cart'),
237 'recipient' => 'customer',
238 'smartcode_groups' => [],
239 'template_path' => 'order.delivered.customer',
240 'is_async' => false,
241 'settings' => [
242 'active' => 'yes',
243 'subject' => __('Order has been delivered #{{order.invoice_no}}', 'fluent-cart'),
244 'is_default_body' => 'yes',
245 'email_body' => '',
246 'attach_pdf_template' => '',
247 ]
248 ],
249 'order_placed_admin' => [
250 'event' => 'order_placed_offline',
251 'group' => 'order',
252 'group_label' => __('Order Actions', 'fluent-cart'),
253 'title' => __('Send mail to admin after New Order Placed (Offline Payment)', 'fluent-cart'),
254 'description' => __('This email will be sent to the admin when an order is placed using offline payment method.', 'fluent-cart'),
255 'recipient' => 'admin',
256 'smartcode_groups' => [],
257 'template_path' => 'order.placed.admin',
258 'is_async' => false,
259 'manage_toggle' => 'no',
260 'toggle_label' => __('Auto-enabled for offline payments', 'fluent-cart'),
261 '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'),
262 'settings' => [
263 'subject' => __('New Order on {{settings.store_name}} (Offline Payment)', 'fluent-cart'),
264 'is_default_body' => 'yes',
265 'email_body' => '',
266 'attach_pdf_template' => '',
267 ]
268 ],
269 'order_placed_customer' => [
270 'event' => 'order_placed_offline',
271 'group' => 'order',
272 'group_label' => __('Order Actions', 'fluent-cart'),
273 'title' => __('Order confirmation to customer (Offline Payment)', 'fluent-cart'),
274 'description' => __('This email will be sent to the customer when an order is placed using offline payment method.', 'fluent-cart'),
275 'recipient' => 'customer',
276 'smartcode_groups' => [],
277 'template_path' => 'order.placed.customer',
278 'is_async' => false,
279 'manage_toggle' => 'no',
280 'toggle_label' => __('Auto-enabled for offline payments', 'fluent-cart'),
281 'settings' => [
282 'subject' => __('Order Confirmation #{{order.invoice_no}} (Offline Payment)', 'fluent-cart'),
283 'is_default_body' => 'yes',
284 'email_body' => '',
285 'attach_pdf_template' => '',
286 ]
287 ],
288 // @todo uncomment when invoice feature is deployed
289 // 'invoice_reminder_due_customer' => [
290 // 'event' => 'invoice_reminder_due',
291 // 'title' => __('Invoice due reminder to customer', 'fluent-cart'),
292 // 'description' => __('This email will be sent before/at invoice due date when payment is pending.', 'fluent-cart'),
293 // 'recipient' => 'customer',
294 // 'smartcode_groups' => [],
295 // 'template_path' => 'order.reminder.due.customer',
296 // 'is_async' => false,
297 // 'settings' => [
298 // 'active' => 'yes',
299 // 'subject' => __('Payment Reminder #{{order.invoice_no}}', 'fluent-cart'),
300 // 'is_default_body' => 'yes',
301 // 'email_body' => '',
302 // ]
303 // ],
304 // 'invoice_reminder_due_admin' => [
305 // 'event' => 'invoice_reminder_due',
306 // 'title' => __('Invoice due reminder copy to admin', 'fluent-cart'),
307 // 'description' => __('This email will be sent to admin when a due reminder is sent to customer.', 'fluent-cart'),
308 // 'recipient' => 'admin',
309 // 'smartcode_groups' => [],
310 // 'template_path' => 'order.reminder.due.admin',
311 // 'pre_header' => 'An invoice due reminder was sent to a customer. Review the order details in this email or visit FluentCart Dashboard.',
312 // 'is_async' => false,
313 // 'settings' => [
314 // 'active' => 'no',
315 // 'subject' => __('Invoice Reminder Sent #{{order.invoice_no}}', 'fluent-cart'),
316 // 'is_default_body' => 'yes',
317 // 'email_body' => '',
318 // ]
319 // ],
320 'invoice_reminder_overdue_customer' => [
321 'event' => 'invoice_reminder_overdue',
322 'group' => 'scheduler',
323 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
324 'title' => __('Payment reminder to customer', 'fluent-cart'),
325 'description' => __('This email will be sent to remind customer about pending payment.', 'fluent-cart'),
326 'recipient' => 'customer',
327 'smartcode_groups' => [],
328 'template_path' => 'order.reminder.overdue.customer',
329 'is_async' => false,
330 'manage_toggle' => 'no',
331 'toggle_label' => __('On demand', 'fluent-cart'),
332 'settings' => [
333 'subject' => __('Payment Reminder - Order #{{order.id}}', 'fluent-cart'),
334 'is_default_body' => 'yes',
335 'email_body' => '',
336 ]
337 ],
338 'subscription_renewal_reminder_customer' => [
339 'event' => 'subscription_renewal_reminder',
340 'group' => 'scheduler',
341 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
342 'title' => __('Upcoming renewal reminder to customer', 'fluent-cart'),
343 'description' => __('This email will be sent before subscription auto-renewal date.', 'fluent-cart'),
344 'recipient' => 'customer',
345 'smartcode_groups' => [],
346 'template_path' => 'subscription.reminder.customer',
347 'is_async' => false,
348 'settings' => [
349 'active' => 'yes',
350 'subject' => __('Upcoming Renewal Reminder from {{settings.store_name}}', 'fluent-cart'),
351 'is_default_body' => 'yes',
352 'email_body' => '',
353 ]
354 ],
355 'subscription_renewal_reminder_admin' => [
356 'event' => 'subscription_renewal_reminder',
357 'group' => 'scheduler',
358 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
359 'title' => __('Upcoming renewal reminder copy to admin', 'fluent-cart'),
360 'description' => __('This email will be sent to admin when an upcoming renewal reminder is sent.', 'fluent-cart'),
361 'recipient' => 'admin',
362 'smartcode_groups' => [],
363 'template_path' => 'subscription.reminder.admin',
364 'pre_header' => __('A subscription renewal reminder was sent to a customer. Review subscription details from FluentCart Dashboard.', 'fluent-cart'),
365 'is_async' => false,
366 'settings' => [
367 'active' => 'no',
368 'subject' => __('Renewal Reminder Sent for {{order.customer.full_name}}', 'fluent-cart'),
369 'is_default_body' => 'yes',
370 'email_body' => '',
371 ]
372 ],
373 'subscription_trial_end_reminder_customer' => [
374 'event' => 'subscription_trial_end_reminder',
375 'group' => 'scheduler',
376 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
377 'title' => __('Trial ending soon reminder to customer', 'fluent-cart'),
378 'description' => __('This email will be sent before a trial period ends and converts to a paid subscription.', 'fluent-cart'),
379 'recipient' => 'customer',
380 'smartcode_groups' => [],
381 'template_path' => 'subscription.trial_end.customer',
382 'is_async' => false,
383 'settings' => [
384 'active' => 'yes',
385 'subject' => __('Your Trial is Ending Soon - {{settings.store_name}}', 'fluent-cart'),
386 'is_default_body' => 'yes',
387 'email_body' => '',
388 ]
389 ],
390 'subscription_trial_end_reminder_admin' => [
391 'event' => 'subscription_trial_end_reminder',
392 'group' => 'scheduler',
393 'group_label' => __('Scheduler / Reminder Actions', 'fluent-cart'),
394 'title' => __('Trial ending soon reminder copy to admin', 'fluent-cart'),
395 'description' => __('This email will be sent to admin when a trial ending reminder is sent to a customer.', 'fluent-cart'),
396 'recipient' => 'admin',
397 'smartcode_groups' => [],
398 'template_path' => 'subscription.trial_end.admin',
399 'pre_header' => __('A trial ending soon reminder was sent to a customer. Review subscription details from FluentCart Dashboard.', 'fluent-cart'),
400 'is_async' => false,
401 'settings' => [
402 'active' => 'no',
403 'subject' => __('Trial Ending Soon - {{order.customer.full_name}}', 'fluent-cart'),
404 'is_default_body' => 'yes',
405 'email_body' => '',
406 ]
407 ],
408 ];
409
410 }
411
412 public static function getNotificationsOfEvent($event, $viewData): array
413 {
414 $notifications = static::getNotifications();
415 $notifications = array_filter($notifications, function ($notification) use ($event) {
416 return $notification['event'] === $event;
417 });
418
419 if (empty($notifications)) {
420 return [];
421 }
422
423 $emails = [];
424 $mailingSettings = static::getSettings();
425
426 foreach ($notifications as $key => $notification) {
427
428 $settings = Arr::get($notification, 'settings');
429
430 // Skip active check for on-demand notifications (offline payments, payment reminders)
431 if (in_array($notification['event'], ['order_placed_offline', 'invoice_reminder_overdue']) || Arr::get($settings, 'active') === 'yes') {
432 // Continue with normal flow
433 } else {
434 continue;
435 }
436 $recipient = Arr::get($notification, 'recipient');
437
438 if (!in_array($recipient, ['admin', 'customer', 'user', 'subscriber'])) {
439 continue;
440 }
441
442 if ($recipient === 'admin') {
443 $toEmail = Arr::get($mailingSettings, 'admin_email', '');
444 } else {
445 $toEmail = self::EMAIL_RECIPIENT_MAP[$recipient];
446 }
447
448 if (empty($toEmail)) {
449 continue;
450 }
451
452 $emails[$key] = static::formatNotification($notification, $viewData);
453 }
454
455 return $emails;
456 }
457
458 public static function formatNotification($notification, $viewData): array
459 {
460 $settings = Arr::get($notification, 'settings');
461 $mailingSettings = static::getSettings();
462
463
464 $recipient = Arr::get($notification, 'recipient');
465 if ($recipient === 'admin') {
466 $toEmail = Arr::get($mailingSettings, 'admin_email', '');
467 } else {
468 $toEmail = self::EMAIL_RECIPIENT_MAP[$recipient];
469 }
470
471
472 $isDefaultEmailBody = Arr::get($settings, 'is_default_body', 'yes') === 'yes' || empty(Arr::get($settings, 'email_body'));
473
474 $emailBody = $isDefaultEmailBody ?
475 TemplateService::getTemplateByPathName(Arr::get($notification, 'template_path'), $viewData) :
476 Arr::get($settings, 'email_body');
477
478 return [
479 'to' => $toEmail,
480 'body' => $emailBody,
481 'pre_header' => Arr::get($notification, 'pre_header', ''),
482 'is_async' => $notification['is_async'],
483 'subject' => Arr::get($settings, 'subject'),
484 'is_custom' => !$isDefaultEmailBody,
485 'settings' => $settings,
486 ];
487 }
488
489 public static function getNotification($name)
490 {
491 $notifications = static::getNotifications();
492 return Arr::get($notifications, $name);
493 }
494
495 //returns only the notification settings
496 public static function getNotificationConfig($notificationName = null)
497 {
498 $configs = self::getNotifications();
499 if ($notificationName) {
500 return Arr::get($configs, $notificationName . '.settings', []);
501 }
502 return Arr::pluck($configs, 'settings', 'name');
503 }
504
505 public static function updateNotification($name, $data)
506 {
507 $updateableKeys = [
508 'active',
509 'subject',
510 'email_body',
511 'is_default_body',
512 'attach_pdf_template'
513 ];
514 $allConfig = static::getSettings();
515 $config = static::getNotificationConfig($name);
516
517 foreach ($updateableKeys as $key) {
518 $defaultValue = Arr::get($config, $key, '');
519 $config[$key] = Arr::get($data, $key, $defaultValue);
520 }
521
522 // When switching back to default body, clear the custom email body unless draft mode is enabled
523 if (Arr::get($config, 'is_default_body') === 'yes' && !apply_filters('fluent_cart/keep_email_body_draft', false, ['notification_name' => $name])) {
524 $config['email_body'] = '';
525 }
526
527 Arr::set($allConfig, 'notification_config.' . $name, $config);
528
529 $isUpdate = Meta::query()->updateOrCreate(
530 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
531 ['meta_key' => static::META_KEY, 'object_type' => 'email_notification'],
532 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
533 ['meta_value' => $allConfig]
534 );
535
536 if ($isUpdate) {
537 static::updateCache();
538 }
539
540 return static::getNotification($name);
541 }
542
543 public static function defaultSettings(): array
544 {
545 return [
546 'from_name' => '',
547 'from_email' => '',
548 'reply_to_name' => '',
549 'reply_to_email' => '',
550 'email_footer' => '',
551 'show_email_footer' => 'yes',
552 'admin_email' => '{{wp.admin_email}}',
553 ];
554 }
555
556 //returns all the settings
557 public static function getSettings($key = null)
558 {
559
560 $defaultSettings = static::defaultSettings();
561 $cachedSettings = static::cachedSettings();
562 $settings = wp_parse_args($cachedSettings, $defaultSettings);
563
564 $settings['notification_config'] = wp_parse_args(
565 Arr::get($cachedSettings, 'notification_config', []),
566 Arr::get($defaultSettings, 'notification_config', [])
567 );
568
569 if (!empty($key)) {
570 return Arr::get($settings, $key);
571 }
572
573 return $settings;
574 }
575
576 public static function cachedSettings()
577 {
578 return Cache::get(static::META_KEY, function () {
579 $config = Meta::query()->where('object_type', 'email_notification')
580 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
581 ->where('meta_key', static::META_KEY)->first();
582 return $config ? $config->meta_value : [];
583 });
584 }
585
586 public static function updateSettings($data)
587 {
588 $allConfig = static::getSettings();
589
590 foreach ($data as $key => $value) {
591 Arr::set($allConfig, $key, $value);
592 }
593
594 $config = Meta::query()->updateOrCreate(
595 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
596 ['meta_key' => static::META_KEY, 'object_type' => 'email_notification'],
597 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
598 ['meta_value' => $allConfig]
599 );
600
601 static::updateCache();
602 return $config;
603 }
604
605 private static function updateCache(): void
606 {
607 Cache::forget(static::META_KEY);
608 }
609 }
610