PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.27
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.27
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 trunk All 48 releases
fluent-cart / app / Http / Controllers / EmailNotificationController.php

EmailNotificationController.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.27, at app/Http/Controllers/EmailNotificationController.php

268 lines 9.1 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\Http\Controllers;
4
5 use FluentCart\App\App;
6 use FluentCart\Api\StoreSettings;
7 use FluentCart\App\Helpers\EditorShortCodeHelper;
8 use FluentCart\App\Http\Requests\EmailNotificationRequest;
9 use FluentCart\App\Http\Requests\EmailSettingsRequest;
10 use FluentCart\App\Http\Requests\SchedulingSettingsRequest;
11 use FluentCart\App\Services\Email\EmailNotificationMailer;
12 use FluentCart\App\Services\Email\EmailNotifications;
13 use FluentCart\App\Services\PDF\ReceiptPdfTemplateService;
14 use FluentCart\App\Services\Reminders\ReminderService;
15 use FluentCart\App\Services\ShortCodeParser\ShortcodeTemplateBuilder;
16 use FluentCart\App\Services\Email\EmailPreviewService;
17 use FluentCart\App\Services\TemplateService;
18 use FluentCart\Framework\Http\Request\Request;
19 use FluentCart\Framework\Support\Arr;
20 use FluentCart\Framework\Support\Str;
21
22 class EmailNotificationController extends Controller
23 {
24 public function index(Request $request): \WP_REST_Response
25 {
26
27 $getNotifications = EmailNotifications::getNotifications();
28
29 if ($getNotifications) {
30 return $this->sendSuccess([
31 'data' => $getNotifications
32 ]);
33 }
34 return $this->sendError([
35 'data' => []
36 ]);
37 }
38
39 public function find($notification): \WP_REST_Response
40 {
41 $name = sanitize_text_field($notification);
42 $notification = EmailNotifications::getNotification($name);
43
44
45 if ($notification) {
46 $hasFluentPdf = defined('FLUENT_PDF');
47 $fontsReady = false;
48 $setupUrl = '';
49
50 if ($hasFluentPdf && class_exists('FluentPdf\Classes\Controller\FontDownloader')) {
51 $fontDownloader = new \FluentPdf\Classes\Controller\FontDownloader();
52 $fontsReady = empty($fontDownloader->getDownloadableFonts());
53 $setupUrl = admin_url('admin.php?page=fluent_pdf.php');
54 }
55
56 $pdfTemplates = $hasFluentPdf ? (new ReceiptPdfTemplateService())->getTemplateList() : [];
57
58 return $this->sendSuccess([
59 'data' => $notification,
60 'shortcodes' => EditorShortCodeHelper::getEmailNotificationShortcodes(),
61 'has_fluent_pdf' => $hasFluentPdf,
62 'fonts_ready' => $fontsReady,
63 'setup_url' => $setupUrl,
64 'pdf_templates' => $pdfTemplates,
65 ]);
66 }
67
68 return $this->sendError([
69 'message' => __('Notification Details not found', 'fluent-cart')
70 ]);
71 }
72
73 public function update(EmailNotificationRequest $request, $notification): \WP_REST_Response
74 {
75 $data = $request->getSafe($request->sanitize());
76
77 $settings = Arr::get($data, 'settings', []);
78
79 // Strip custom template data in free; pro filter restores it
80 $settingsWithoutTemplate = $settings;
81 unset($settingsWithoutTemplate['email_body']);
82 $settingsWithoutTemplate['is_default_body'] = 'yes';
83
84 $settings = apply_filters('fluent_cart/prepare_email_template_data', $settingsWithoutTemplate, $settings);
85
86 $updated = EmailNotifications::updateNotification($notification, $settings);
87 if ($updated) {
88 return $this->sendSuccess([
89 'message' => __('Notification updated successfully', 'fluent-cart')
90 ]);
91 } else {
92 return $this->sendError([
93 'message' => __('Failed to update notification', 'fluent-cart')
94 ]);
95 }
96
97 }
98
99 public function enableNotification(Request $request, $name): \WP_REST_Response
100 {
101 $enabledValue = sanitize_text_field(Arr::get($request->all(), 'active'));
102
103 $notification = EmailNotifications::updateNotification(
104 $name,
105 ['active' => $enabledValue]
106 );
107
108 if ($notification) {
109 return $this->sendSuccess([
110 'message' => __('Notification updated successfully', 'fluent-cart')
111 ]);
112 }
113 return $this->sendError([
114 'message' => __('Failed to update notification', 'fluent-cart')
115 ]);
116
117 }
118
119 public function getShortCodes(Request $request): \WP_REST_Response
120 {
121 return $this->sendSuccess([
122 'data' => [
123 'email_templates' => $this->getTemplateFiles(),
124 'shortcodes' => EditorShortCodeHelper::getEmailNotificationShortcodes(),
125 'buttons' => EditorShortCodeHelper::getButtons()
126 ],
127 ]);
128 }
129
130 public function previewDefaultTemplate(Request $request)
131 {
132 $template = sanitize_text_field($request->get('template'));
133 $previewService = new EmailPreviewService();
134 $data = $previewService->getPreviewData($template);
135
136 $body = TemplateService::getTemplateByPathName($template, $data);
137
138 // Wrap in the same outer email template used by actual emails
139 $header = (string) App::make('view')->make('emails.parts.order_header', $data);
140 $mailer = new EmailNotificationMailer();
141 $view = (string) App::make('view')->make('emails.general_template', [
142 'emailBody' => $body,
143 'preheader' => '',
144 'header' => $header,
145 'emailFooter' => $mailer->getEmailFooter(),
146 ]);
147
148 // Resolve shortcodes (e.g., {{ settings.store_brand }}, {{order.created_at}})
149 $view = ShortcodeTemplateBuilder::make($view, $data);
150
151 // Disable all links/buttons in the preview so they are not clickable
152 $view .= '<style>a, button, [role="button"] { pointer-events: none !important; cursor: default !important; }</style>';
153
154 return $this->sendSuccess([
155 'data' => [
156 'content' => $view
157 ],
158 ]);
159 }
160
161 public function getTemplateFiles()
162 {
163 $defaultFilePath = FLUENTCART_PLUGIN_PATH . '/app/Views/emails';
164 $filesArray = [];
165 $files = scandir($defaultFilePath);
166 foreach ($files as $file) {
167 $filePath = $defaultFilePath . '/' . $file;
168 if (is_file($filePath)) {
169 $file = Str::of($file)->replace('.php', '');
170 $filesArray[] = [
171 'path' => $file,
172 'label' => Str::of($file)->replace('fluent_cart', '')->headline()
173 ];
174 }
175 }
176 return $filesArray;
177 }
178
179 public function getSettings(): \WP_REST_Response
180 {
181 return $this->sendSuccess([
182 'data' => EmailNotifications::getSettings(),
183 'shortcodes' => EditorShortCodeHelper::getEmailSettingsShortcodes(),
184 ]);
185 }
186
187 public function saveSettings(EmailSettingsRequest $request): \WP_REST_Response
188 {
189 $data = $request->getSafe($request->sanitize());
190
191 if (!App::isProActive()) {
192 $data['show_email_footer'] = 'yes';
193 }
194
195 $updated = EmailNotifications::updateSettings($data);
196
197 if ($updated) {
198 return $this->sendSuccess([
199 'message' => __('Email settings saved successfully', 'fluent-cart')
200 ]);
201 } else {
202 return $this->sendError([
203 'message' => __('Failed to save email settings', 'fluent-cart')
204 ]);
205 }
206 }
207
208 public function getSchedulingSettings(StoreSettings $storeSettings): array
209 {
210 $tab = 'reminders';
211 $currentTabFields = Arr::get($storeSettings->fields(), 'setting_tabs.schema.' . $tab);
212
213 return [
214 'settings' => $storeSettings->get(),
215 'fields' => [
216 $tab => $currentTabFields
217 ]
218 ];
219 }
220
221 public function saveSchedulingSettings(SchedulingSettingsRequest $request, StoreSettings $storeSettings): \WP_REST_Response
222 {
223 try {
224 $sanitizedData = $request->getSafe($request->sanitize());
225
226 $data = array_merge(
227 $storeSettings->get(),
228 $sanitizedData
229 );
230
231 $updated = $storeSettings->save($data);
232
233 return $this->sendSuccess([
234 'data' => $updated,
235 'message' => __('Scheduling settings saved successfully', 'fluent-cart')
236 ]);
237 } catch (\Throwable $e) {
238 return $this->sendError([
239 'message' => __('Failed to save scheduling settings', 'fluent-cart')
240 ], 423);
241 }
242 }
243
244 public function sendManualReminder(Request $request): \WP_REST_Response
245 {
246 $event = sanitize_text_field(Arr::get($request->all(), 'event', ''));
247 $entityId = absint(Arr::get($request->all(), 'entity_id', 0));
248
249 if (empty($event) || empty($entityId)) {
250 return $this->sendError([
251 'message' => __('Event type and entity ID are required', 'fluent-cart')
252 ]);
253 }
254
255 $result = (new ReminderService())->sendManualReminder($event, $entityId);
256
257 if ($result['success']) {
258 return $this->sendSuccess([
259 'message' => $result['message']
260 ]);
261 }
262
263 return $this->sendError([
264 'message' => $result['message']
265 ]);
266 }
267 }
268