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

322 lines 9.6 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\Databases\Migrations\FormLogs;
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('ff_scheduled_actions')
41 ->select('action')
42 ->groupBy('action')
43 ->get();
44
45 $formattedComponents = [];
46 foreach ($components as $component) {
47 $formattedComponents[] = $component->action;
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 FormLogs::migrate();
83 }
84
85 return wpFluent()->table('fluentform_logs')
86 ->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('fluentform_entry_logs', $logs, $entry_id);
99 } else {
100 $logs = wpFluent()->table('ff_scheduled_actions')
101 ->select([
102 'id',
103 'action',
104 'status',
105 'note',
106 'created_at'
107 ])
108 ->where('origin_id', $entry_id)
109 ->orderBy('id', 'DESC')
110 ->get();
111
112 $logs = apply_filters('fluentform_entry_api_logs', $logs, $entry_id);
113 }
114
115
116 wp_send_json_success([
117 'logs' => $logs
118 ], 200);
119 }
120
121 public function getAllLogs()
122 {
123 $limit = intval($_REQUEST['per_page']);
124 $pageNumber = intval($_REQUEST['page_number']);
125
126 $skip = ($pageNumber - 1) * $limit;
127
128 global $wpdb;
129 $logsQuery = wpFluent()->table('fluentform_logs')
130 ->select([
131 'fluentform_logs.*'
132 ])
133 ->select(wpFluent()->raw( $wpdb->prefix.'fluentform_forms.title as form_title' ))
134 ->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_logs.parent_source_id')
135 ->orderBy('fluentform_logs.id', 'DESC')
136 ->whereIn('fluentform_logs.source_type', ['submission_item', 'form_item']);
137
138
139 if($parentSourceId = ArrayHelper::get($_REQUEST, 'parent_source_id')) {
140 $logsQuery = $logsQuery->where('fluentform_logs.parent_source_id', intval($parentSourceId));
141 }
142
143 if($status = ArrayHelper::get($_REQUEST, 'status')) {
144 $logsQuery = $logsQuery->where('fluentform_logs.status', $status);
145 }
146
147 if($component = ArrayHelper::get($_REQUEST, 'component')) {
148 $logsQuery = $logsQuery->where('fluentform_logs.component', $component);
149 }
150
151
152 $logsQueryMain = $logsQuery;
153
154 $logs = $logsQuery->offset($skip)
155 ->limit($limit)
156 ->get();
157
158 $logs = apply_filters('fluentform_all_logs', $logs);
159
160 $total = $logsQueryMain->count();
161
162 wp_send_json_success([
163 'logs' => $logs,
164 'total' => $total
165 ], 200);
166
167 }
168
169
170 public function getApiLogs()
171 {
172 $limit = intval($_REQUEST['per_page']);
173 $pageNumber = intval($_REQUEST['page_number']);
174
175 $skip = ($pageNumber - 1) * $limit;
176 global $wpdb;
177 $logsQuery = wpFluent()->table('ff_scheduled_actions')
178 ->select([
179 'ff_scheduled_actions.id',
180 'ff_scheduled_actions.action',
181 'ff_scheduled_actions.form_id',
182 'ff_scheduled_actions.origin_id',
183 'ff_scheduled_actions.status',
184 'ff_scheduled_actions.note',
185 'ff_scheduled_actions.created_at',
186 ])
187 ->select(wpFluent()->raw( $wpdb->prefix.'fluentform_forms.title as form_title' ))
188 ->join('fluentform_forms', 'fluentform_forms.id', '=', 'ff_scheduled_actions.form_id')
189 ->orderBy('ff_scheduled_actions.id', 'DESC');
190
191
192 if($formId = ArrayHelper::get($_REQUEST, 'form_id')) {
193 $logsQuery = $logsQuery->where('ff_scheduled_actions.form_id', intval($formId));
194 }
195
196 if($status = ArrayHelper::get($_REQUEST, 'status')) {
197 $logsQuery = $logsQuery->where('ff_scheduled_actions.status', $status);
198 }
199
200 if($component = ArrayHelper::get($_REQUEST, 'component')) {
201 $logsQuery = $logsQuery->where('ff_scheduled_actions.action', $component);
202 }
203
204 $logsQueryMain = $logsQuery;
205
206 $logs = $logsQuery->offset($skip)
207 ->limit($limit)
208 ->get();
209
210 $logs = apply_filters('fluentform_api_all_logs', $logs);
211
212 foreach ($logs as $log) {
213 $log->submission_url = admin_url('admin.php?page=fluent_forms&route=entries&form_id='.$log->form_id.'#/entries/'.$log->origin_id);
214 }
215
216 $total = $logsQueryMain->count();
217
218 wp_send_json_success([
219 'logs' => $logs,
220 'total' => $total
221 ], 200);
222
223 }
224
225 public function deleteLogsByIds($ids = [])
226 {
227 if(!$ids) {
228 $ids = wp_unslash($_REQUEST['log_ids']);
229 }
230
231 if(!$ids) {
232 wp_send_json_error([
233 'message' => 'No selections found'
234 ], 423);
235 }
236
237 wpFluent()->table('fluentform_logs')
238 ->whereIn('id', $ids)
239 ->delete();
240
241 wp_send_json_success([
242 'message' => __('Selected logs successfully deleted', 'fluentform')
243 ], 200);
244 }
245
246 public function deleteApiLogsByIds($ids = [])
247 {
248 if(!$ids) {
249 $ids = wp_unslash($_REQUEST['log_ids']);
250 }
251
252 if(!$ids) {
253 wp_send_json_error([
254 'message' => 'No selections found'
255 ], 423);
256 }
257
258 wpFluent()->table('ff_scheduled_actions')
259 ->whereIn('id', $ids)
260 ->delete();
261
262 wp_send_json_success([
263 'message' => __('Selected logs successfully deleted', 'fluentform')
264 ], 200);
265 }
266
267 public function retryApiAction()
268 {
269 $logId = $this->app->request->get('log_id');
270 $actionFeed = wpFluent()->table('ff_scheduled_actions')
271 ->find($logId);
272
273 if(!$actionFeed) {
274 wp_send_json_error([
275 'message' => 'API log does not exist'
276 ], 423);
277 }
278
279 if(!$actionFeed->status == 'success') {
280 wp_send_json_error([
281 'message' => 'API log already in success mode'
282 ], 423);
283 }
284
285 $form = wpFluent()->table('fluentform_forms')->find($actionFeed->form_id);
286
287 $feed = maybe_unserialize($actionFeed->data);
288 $feed['scheduled_action_id'] = $actionFeed->id;
289
290 $submission = wpFluent()->table('fluentform_submissions')->find($actionFeed->origin_id);
291 $entry = $this->getEntry($submission, $form);
292 $formData = json_decode($submission->response, true);
293
294 wpFluent()->table($this->table)
295 ->where('id', $actionFeed->id)
296 ->update([
297 'status' => 'manual_retry',
298 'retry_count' => $actionFeed->retry_count + 1,
299 'updated_at' => current_time('mysql')
300 ]);
301
302 do_action($actionFeed->action, $feed, $formData, $entry, $form);
303
304 /*
305 * Hopefully it's done
306 */
307 $actionFeed = wpFluent()->table('ff_scheduled_actions')
308 ->find($logId);
309
310 wp_send_json_success([
311 'message' => 'Retry completed',
312 'feed' => $actionFeed
313 ], 200);
314 }
315
316 private function getEntry($submission, $form)
317 {
318 $formInputs = FormFieldsParser::getEntryInputs($form, ['admin_label', 'raw']);
319 return FormDataParser::parseFormEntry($submission, $form, $formInputs);
320 }
321 }
322