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

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