PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.14
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.14
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Modules / Logger / DataLogger.php

DataLogger.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.14, at app/Modules/Logger/DataLogger.php

440 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace FluentForm\App\Modules\Logger;
2
3 use FluentForm\App\Helpers\Helper;
4 use FluentForm\Database\Migrations\Logs;
5 use FluentForm\App\Modules\Form\FormDataParser;
6 use FluentForm\App\Modules\Form\FormFieldsParser;
7 use FluentForm\Framework\Foundation\Application;
8 use FluentForm\Framework\Helpers\ArrayHelper;
9
10 class DataLogger
11 {
12 public $app;
13
14 public function __construct(Application $app)
15 {
16 $this->app = $app;
17 }
18
19 public function getLogFilters()
20 {
21 // SECURITY (H-02): unregistered handler; guard it so it is safe if ever wired to an action.
22 \FluentForm\App\Modules\Acl\Acl::verify('fluentform_settings_manager');
23 $statuses = wpFluent()->table('fluentform_logs')
24 ->select('status')
25 ->groupBy('status')
26 ->get();
27 $formattedStatuses = [];
28
29 foreach ($statuses as $status) {
30 $formattedStatuses[] = $status->status;
31 }
32
33 $apis = wpFluent()->table('ff_scheduled_actions')
34 ->select('status')
35 ->groupBy('status')
36 ->get();
37
38 $apiStatuses = [];
39 foreach ($apis as $api) {
40 $apiStatuses[] = $api->status;
41 }
42
43 $components = wpFluent()->table('fluentform_logs')
44 ->select('component')
45 ->groupBy('component')
46 ->get();
47
48 $formattedComponents = [];
49 foreach ($components as $component) {
50 $formattedComponents[] = $component->component;
51 }
52
53 $forms = wpFluent()->table('fluentform_logs')
54 ->select('fluentform_logs.parent_source_id', 'fluentform_forms.title')
55 ->groupBy('fluentform_logs.parent_source_id')
56 ->orderBy('fluentform_logs.parent_source_id', 'DESC')
57 ->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_logs.parent_source_id')
58 ->get();
59
60 $formattedForms = [];
61
62 foreach ($forms as $form) {
63 $formattedForms[] = [
64 'form_id' => $form->parent_source_id,
65 'title' => $form->title,
66 ];
67
68 }
69
70 wp_send_json_success([
71 'available_statuses' => $formattedStatuses,
72 'available_components' => $formattedComponents,
73 'available_forms' => $formattedForms,
74 'api_statuses' => $apiStatuses,
75 ]);
76 }
77
78 public function log($data)
79 {
80 if (!$data) {
81 return;
82 }
83
84 if (isset($data['description'])) {
85 $data['description'] = wp_kses($data['description'], [
86 'br' => [],
87 'b' => [],
88 'strong' => [],
89 'i' => [],
90 'em' => [],
91 'code' => [],
92 'p' => [],
93 'a' => ['href' => [], 'title' => [], 'target' => [], 'rel' => []],
94 ]);
95 }
96
97 $data['created_at'] = current_time('mysql');
98
99 if (!get_option('fluentform_db_fluentform_logs_added')) {
100 Logs::migrate();
101 }
102
103 wpFluent()->table('fluentform_logs')->insert($data);
104 }
105
106 public function getLogsByEntry($entry_id, $log_type = 'logs', $sourceType = 'submission_item')
107 {
108 if ($log_type == 'logs') {
109 $logs = wpFluent()->table('fluentform_logs')
110 ->where('source_id', $entry_id)
111 ->where('source_type', $sourceType)
112 ->orderBy('id', 'DESC')
113 ->get();
114
115 $logs = apply_filters_deprecated(
116 'fluentform_entry_logs',
117 [
118 $logs,
119 $entry_id,
120 ],
121 FLUENTFORM_FRAMEWORK_UPGRADE,
122 'fluentform/submission_logs',
123 'Use fluentform/submission_logs instead of fluentform_entry_logs.'
124 );
125
126 $logs = apply_filters('fluentform/submission_logs', $logs, $entry_id);
127 } else {
128 $logs = wpFluent()->table('ff_scheduled_actions')
129 ->select([
130 'id',
131 'action',
132 'status',
133 'note',
134 'created_at',
135 ])
136 ->where('origin_id', $entry_id)
137 ->orderBy('id', 'DESC')
138 ->get();
139
140 $logs = apply_filters_deprecated(
141 'fluentform_entry_api_logs',
142 [
143 $logs,
144 $entry_id,
145 ],
146 FLUENTFORM_FRAMEWORK_UPGRADE,
147 'fluentform/submission_api_logs',
148 'Use fluentform/submission_api_logs instead of fluentform_entry_api_logs.'
149 );
150
151 $logs = apply_filters('fluentform/submission_api_logs', $logs, $entry_id);
152 }
153
154 wp_send_json_success([
155 'logs' => $logs,
156 ], 200);
157 }
158
159 public function getAllLogs()
160 {
161 // SECURITY (H-02): unregistered handler; guard it so it is safe if ever wired to an action.
162 \FluentForm\App\Modules\Acl\Acl::verify('fluentform_settings_manager');
163 $limit = intval($this->app->request->get('per_page'));
164 $pageNumber = intval($this->app->request->get('page_number'));
165
166 $skip = ($pageNumber - 1) * $limit;
167
168 global $wpdb;
169 $logsQuery = wpFluent()->table('fluentform_logs')
170 ->select([
171 'fluentform_logs.*',
172 wpFluent()->raw($wpdb->prefix . 'fluentform_forms.title as form_title'),
173 wpFluent()->raw($wpdb->prefix . 'fluentform_logs.parent_source_id as form_id'),
174 wpFluent()->raw($wpdb->prefix . 'fluentform_logs.source_id as entry_id'),
175 ])
176 ->leftJoin('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_logs.parent_source_id')
177 ->orderBy('fluentform_logs.id', 'DESC');
178 // ->whereIn('fluentform_logs.source_type', ['submission_item', 'form_item']);
179
180 if ($parentSourceId = $this->app->request->get('parent_source_id')) {
181 $logsQuery = $logsQuery->where('fluentform_logs.parent_source_id', intval($parentSourceId));
182 }
183
184 if ($status = $this->app->request->get('status')) {
185 $logsQuery = $logsQuery->where('fluentform_logs.status', sanitize_text_field($status));
186 }
187
188 if ($component = $this->app->request->get('component')) {
189 $logsQuery = $logsQuery->where('fluentform_logs.component', sanitize_text_field($component));
190 }
191
192 if ($formId = $this->app->request->get('form_id')) {
193 $logsQuery = $logsQuery->where('fluentform_forms.id', intval($formId));
194 }
195
196 $logsQueryMain = $logsQuery;
197
198 $logs = $logsQuery->offset($skip)
199 ->limit($limit)
200 ->get();
201
202 foreach ($logs as $log) {
203 if ('submission_item' == $log->source_type && $log->entry_id) {
204 $log->submission_url = admin_url('admin.php?page=fluent_forms&route=entries&form_id=' . $log->form_id . '#/entries/' . $log->entry_id);
205 }
206 }
207
208 $logs = apply_filters_deprecated(
209 'fluentform_all_logs',
210 [
211 $logs,
212 ],
213 FLUENTFORM_FRAMEWORK_UPGRADE,
214 'fluentform/all_logs',
215 'Use fluentform/all_logs instead of fluentform_all_logs.'
216 );
217
218 $logs = apply_filters('fluentform/all_logs', $logs);
219
220 $total = $logsQueryMain->count();
221
222 wp_send_json_success([
223 'logs' => $logs,
224 'total' => $total,
225 ], 200);
226 }
227
228
229 public function getApiLogs()
230 {
231 // SECURITY (H-02): unregistered handler; guard it so it is safe if ever wired to an action.
232 \FluentForm\App\Modules\Acl\Acl::verify('fluentform_settings_manager');
233 $limit = intval($this->app->request->get('per_page'));
234 $pageNumber = intval($this->app->request->get('page_number'));
235
236 $skip = ($pageNumber - 1) * $limit;
237 global $wpdb;
238 $logsQuery = wpFluent()->table('ff_scheduled_actions')
239 ->select([
240 'ff_scheduled_actions.id',
241 'ff_scheduled_actions.action',
242 'ff_scheduled_actions.form_id',
243 'ff_scheduled_actions.origin_id',
244 'ff_scheduled_actions.status',
245 'ff_scheduled_actions.note',
246 'ff_scheduled_actions.created_at',
247 wpFluent()->raw($wpdb->prefix . 'fluentform_forms.title as form_title'),
248 ])
249 ->join('fluentform_forms', 'fluentform_forms.id', '=', 'ff_scheduled_actions.form_id')
250 ->orderBy('ff_scheduled_actions.id', 'DESC');
251
252 if ($formId = $this->app->request->get('form_id')) {
253 $logsQuery = $logsQuery->where('ff_scheduled_actions.form_id', intval($formId));
254 }
255
256 if ($status = $this->app->request->get('status')) {
257 $logsQuery = $logsQuery->where('ff_scheduled_actions.status', $status);
258 }
259
260 if ($component = $this->app->request->get('component')) {
261 $logsQuery = $logsQuery->where('ff_scheduled_actions.action', $component);
262 }
263
264 $logsQueryMain = $logsQuery;
265
266 $logs = $logsQuery->offset($skip)
267 ->limit($limit)
268 ->get();
269
270 $logs = apply_filters_deprecated(
271 'fluentform_api_all_logs',
272 [
273 $logs,
274 ],
275 FLUENTFORM_FRAMEWORK_UPGRADE,
276 'fluentform/api_all_logs',
277 'Use fluentform/api_all_logs instead of fluentform_api_all_logs.'
278 );
279
280 $logs = apply_filters('fluentform/api_all_logs', $logs);
281
282 foreach ($logs as $log) {
283 $log->submission_url = admin_url('admin.php?page=fluent_forms&route=entries&form_id=' . $log->form_id . '#/entries/' . $log->origin_id);
284 }
285
286 $total = $logsQueryMain->count();
287
288 wp_send_json_success([
289 'logs' => $logs,
290 'total' => $total,
291 ], 200);
292 }
293
294 public function deleteLogsByIds($ids = [])
295 {
296 if (!$ids) {
297 $ids = wp_unslash($this->app->request->get('log_ids'));
298 }
299
300 if (!$ids) {
301 wp_send_json_error([
302 'message' => 'No selections found',
303 ], 423);
304 }
305
306 wpFluent()->table('fluentform_logs')
307 ->whereIn('id', $ids)
308 ->delete();
309
310 wp_send_json_success([
311 'message' => __('Selected log(s) successfully deleted', 'fluentform'),
312 ], 200);
313 }
314
315 public function deleteApiLogsByIds($ids = [])
316 {
317 if (!$ids) {
318 $ids = wp_unslash($this->app->request->get('log_ids'));
319 }
320
321 if (!$ids) {
322 wp_send_json_error([
323 'message' => 'No selections found',
324 ], 423);
325 }
326
327 wpFluent()->table('ff_scheduled_actions')
328 ->whereIn('id', $ids)
329 ->delete();
330
331 wp_send_json_success([
332 'message' => __('Selected log(s) successfully deleted', 'fluentform'),
333 ], 200);
334 }
335
336 public function retryApiAction()
337 {
338 // SECURITY (H-02): unregistered handler; guard it so it is safe if ever wired to an action.
339 \FluentForm\App\Modules\Acl\Acl::verify('fluentform_settings_manager');
340 $logId = $this->app->request->get('log_id');
341 $actionFeed = wpFluent()->table('ff_scheduled_actions')
342 ->find($logId);
343
344 if (!$actionFeed) {
345 wp_send_json_error([
346 'message' => 'API log does not exist',
347 ], 423);
348 }
349
350 if ($actionFeed->status == 'success') {
351 wp_send_json_error([
352 'message' => 'API log already in success mode',
353 ], 423);
354 }
355
356 $form = wpFluent()->table('fluentform_forms')->find($actionFeed->form_id);
357
358 $feed = Helper::safeUnserialize($actionFeed->data);
359 $feed['scheduled_action_id'] = $actionFeed->id;
360
361 $submission = wpFluent()->table('fluentform_submissions')->find($actionFeed->origin_id);
362 $entry = $this->getEntry($submission, $form);
363 $formData = json_decode($submission->response, true);
364
365 wpFluent()->table('ff_scheduled_actions')
366 ->where('id', $actionFeed->id)
367 ->update([
368 'status' => 'manual_retry',
369 'retry_count' => $actionFeed->retry_count + 1,
370 'updated_at' => current_time('mysql'),
371 ]);
372
373 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Dynamic hook name from action feed
374 do_action($actionFeed->action, $feed, $formData, $entry, $form);
375
376 /*
377 * Hopefully it's done
378 */
379 $actionFeed = wpFluent()->table('ff_scheduled_actions')
380 ->find($logId);
381
382 wp_send_json_success([
383 'message' => 'Retry completed',
384 'feed' => $actionFeed,
385 ], 200);
386 }
387
388 private function getEntry($submission, $form)
389 {
390 $formInputs = FormFieldsParser::getEntryInputs($form, ['admin_label', 'raw']);
391 return FormDataParser::parseFormEntry($submission, $form, $formInputs);
392 }
393
394 public function getApiLogFilters()
395 {
396 $apis = wpFluent()->table('ff_scheduled_actions')
397 ->select('status')
398 ->groupBy('status')
399 ->get();
400
401 $apiStatuses = [];
402 foreach ($apis as $api) {
403 $apiStatuses[] = $api->status;
404 }
405
406 $components = wpFluent()->table('ff_scheduled_actions')
407 ->select('action')
408 ->groupBy('action')
409 ->get();
410
411 $formattedComponents = [];
412 foreach ($components as $component) {
413 $formattedComponents[] = $component->action;
414 }
415
416 $forms = wpFluent()->table('ff_scheduled_actions')
417 ->select('ff_scheduled_actions.form_id', 'fluentform_forms.title')
418 ->groupBy('ff_scheduled_actions.form_id')
419 ->orderBy('ff_scheduled_actions.form_id', 'DESC')
420 ->join('fluentform_forms', 'fluentform_forms.id', '=', 'ff_scheduled_actions.form_id')
421 ->get();
422
423 $formattedForms = [];
424
425 foreach ($forms as $form) {
426 $formattedForms[] = [
427 'form_id' => $form->form_id,
428 'title' => $form->title,
429 ];
430
431 }
432
433 wp_send_json_success([
434 'available_components' => $formattedComponents,
435 'available_forms' => $formattedForms,
436 'api_statuses' => $apiStatuses,
437 ]);
438 }
439 }
440