PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 5.2.9
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v5.2.9
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 5.2.9, at app/Modules/Logger/DataLogger.php

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