PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.16.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.16.2
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
← All changes | includes/Core/Database/FormEntryMetaModel.php +159 -230 3.1.22.16.2 View file →
@@ -5,12 +5,10 @@
5 5 */
6 6
7 7 namespace BitCode\BitForm\Core\Database;
8 8
9 -use BitCode\BitForm\Core\Util\FileHandler;
10 -
11 9 /**
12 - * Manages entry meta (per-field values) for each form submission.
10 + * Undocumented class
13 11 */
14 12
15 13 class FormEntryMetaModel extends Model
16 14 {
@@ -20,10 +18,10 @@
20 18 {
21 19 $values[] = $data['duplicateID'];
22 20 $values[] = $data['entryID'];
23 21 $sql = "INSERT INTO $this->table_name (bitforms_form_entry_id,meta_key,meta_value)"
24 - . ' SELECT %d as bitforms_form_entry_id,meta_key,meta_value'
25 - . " FROM `$this->table_name` WHERE bitforms_form_entry_id = %d";
22 + . ' SELECT %d as bitforms_form_entry_id,meta_key,meta_value'
23 + . " FROM `$this->table_name` WHERE bitforms_form_entry_id = %d";
26 24 return $this->execute($sql, $values)->getResult();
27 25 }
28 26
29 27 public function update(array $data, array $condition)
@@ -31,9 +29,8 @@
31 29 $entryID = $condition['bitforms_form_entry_id'];
32 30 if (empty($entryID)) {
33 31 return false;
34 32 }
35 - // Form entry meta lookup; meta_key/meta_value query required to map dynamic field keys per entry.
36 33 $formEntryMeta = $this->get(
37 34 [
38 35 'meta_key',
39 36 'meta_value',
@@ -49,11 +46,11 @@
49 46 $updatedData = [];
50 47 $oldEntriesKey = array_keys($oldEntries);
51 48 foreach ($data as $upKey => $upValue) {
52 49 $updatedData[$upKey] = is_string($upValue) ?
53 - $upValue :
54 - wp_json_encode($upValue);
55 - if (!in_array($upKey, $oldEntriesKey, true)) {
50 + $upValue :
51 + wp_json_encode($upValue);
52 + if (!\in_array($upKey, $oldEntriesKey)) {
56 53 $this->insert(
57 54 [
58 55 'bitforms_form_entry_id' => $entryID,
59 56 'meta_key' => $upKey,
@@ -75,9 +72,9 @@
75 72 $condition['meta_key'] = array_keys($data);
76 73 foreach ($data as $key => $value) {
77 74 $value = is_string($value) ? $value : wp_json_encode($value);
78 75 $case_part .= "
79 - WHEN '" . esc_sql($key) . "' THEN " . $this->getFieldFormat($value);
76 + WHEN '$key' THEN " . $this->getFieldFormat($value);
80 77 $all_values[] = $value;
81 78 }
82 79 $formattedCondition = $this->getFormatedCondition($condition);
83 80 if ($formattedCondition) {
@@ -119,17 +116,11 @@
119 116
120 117 $dateQuery = $dbHelper->dateQueryList();
121 118
122 119 $validCondtions = $this->validQueryCondition($conditions);
123 - $safeOperators = ['=', '!=', '>', '<', '>=', '<=', 'LIKE', 'NOT LIKE'];
124 120
125 121 foreach ($validCondtions as $condition) {
126 122 if (is_object($condition)) {
127 - $field = sanitize_key($condition->field);
128 - if (empty($field)) {
129 - continue;
130 - }
131 -
132 123 if (is_array($condition->val)) {
133 124 $value = $dbHelper->arrValueModifyByLogic($condition->logic, $condition->val);
134 125 } else {
135 126 $value = $dbHelper->strValueModifyByLogic($condition->logic, $condition->val);
@@ -135,23 +126,19 @@
135 126 $value = $dbHelper->strValueModifyByLogic($condition->logic, $condition->val);
136 127 }
137 128
138 129 $operator = $dbHelper->convertToSqlOperator($condition->logic);
139 - if (!in_array($operator, $safeOperators, true)) {
140 - continue;
141 - }
142 -
143 130 if (!is_array($condition->val) && isset($dateQuery[$condition->val])) {
144 - $sql .= $dbHelper->fieldQueryByDate($field, $operator, $value, $condition->logic);
131 + $sql .= $dbHelper->fieldQueryByDate($condition->field, $operator, $value, $condition->logic);
145 132 } else {
146 133 if (!is_int($value)) {
147 - $value = "'" . esc_sql($value) . "'";
134 + $value = "'" . $value . "'";
148 135 }
149 136
150 - $sql .= "`{$field}` $operator $value";
137 + $sql .= "`$condition->field` $operator $value";
151 138 }
152 - } elseif (in_array(strtoupper(trim((string) $condition)), ['AND', 'OR'], true)) {
153 - $sql .= ' ' . strtoupper(trim((string) $condition));
139 + } else {
140 + $sql .= ' ' . $condition;
154 141 }
155 142 }
156 143
157 144 return trim($sql);
@@ -168,14 +155,12 @@
168 155 $countResult = $this->execute($sql, $all_values)->getResult();
169 156 return $countResult[0]->count;
170 157 }
171 158
172 - public function selectedEntryMeta($formFields, $fieldCount, $filter = null)
159 + public function selectedEntryMeta($formFields, $fieldCount)
173 160 {
174 161 $all_values = [];
175 162 $formFieldsNames = [];
176 - $globalFilterString = '';
177 - $globalFilterValues = [];
178 163 $metaChecker = 0;
179 164 $selectedMeta = '`bitforms_form_entry_id` as entry_id,';
180 165 $selectedMeta .= "e.user_id as '__user_id',";
181 166 $selectedMeta .= "e.user_ip as '__user_ip',";
@@ -190,10 +175,9 @@
190 175 $selectedMeta .= ',';
191 176 }
192 177
193 178 foreach ($formFields as $fieldDetails) {
194 - $safeFieldKey = sanitize_key($fieldDetails['key']);
195 - $fieldFormat = $this->getFieldFormat($safeFieldKey);
179 + $fieldFormat = $this->getFieldFormat($fieldDetails['key']);
196 180 $selectedMeta .= "GROUP_CONCAT(
197 181 CASE
198 182 `meta_key`
199 183 WHEN '$fieldFormat' THEN `meta_value`
@@ -199,33 +183,32 @@
199 183 WHEN '$fieldFormat' THEN `meta_value`
200 184 END
201 185 ) AS '$fieldFormat'";
202 186 $metaChecker += 1;
203 - $all_values[] = $safeFieldKey;
204 - $all_values[] = $safeFieldKey;
205 - $formFieldsNames[] = $safeFieldKey;
187 + $all_values[] = $fieldDetails['key'];
188 + $all_values[] = $fieldDetails['key'];
189 + $formFieldsNames[] = $fieldDetails['key'];
206 190 if ($metaChecker < $fieldCount) {
207 191 $selectedMeta .= ',';
208 192 }
209 - if (!empty($filter['global'])) {
210 - $globalFilterString .= ' `' . $safeFieldKey . '` LIKE %s ';
211 - if ($metaChecker < $fieldCount) {
212 - $globalFilterString .= ' OR ';
213 - }
214 - $globalFilterValues[] = '%' . $this->app_db->esc_like($filter['global']) . '%';
215 - }
193 + //#unused code commented by me##
194 + // if ( !empty( $filter['global'] ) ) {
195 + // $globalFilterString .= " `" . $fieldDetails['key'] . "` LIKE '%%" . $this->getFieldFormat( $filter['global'] ) . "%%' ";
196 + // if ( $metaChecker < $fieldCount ) {
197 + // $globalFilterString .= " OR ";
198 + // }
199 + // $globalFilterValues[] = $filter['global'];
200 + // }
216 201 }
217 202
218 203 return [
219 - 'selected_meta' => $selectedMeta,
220 - 'form_fields_names' => $formFieldsNames,
221 - 'all_values' => $all_values,
222 - 'global_filter_string' => $globalFilterString,
223 - 'global_filter_values' => $globalFilterValues,
204 + 'selected_meta' => $selectedMeta,
205 + 'form_fields_names' => $formFieldsNames,
206 + 'all_values' => $all_values,
224 207 ];
225 208 }
226 209
227 - public function groupedCondition($condition, $all_values, $fieldConditions, $filter = null, $globalFilterString = '', $globalFilterValues = [])
210 + public function groupedCondition($condition, $all_values, $fieldConditions)
228 211 {
229 212 $isFldCondition = false;
230 213 $formattedCondition = $this->getFormatedCondition($condition);
231 214 if ($formattedCondition) {
@@ -234,23 +217,51 @@
234 217 $all_values = array_merge($all_values, $formattedCondition['values']);
235 218 } else {
236 219 $groupedCondition = null;
237 220 }
238 - if (!empty($filter['global']) && !empty($globalFilterString) && !empty($globalFilterValues)) {
239 - $isFldCondition = true;
240 - if ($groupedCondition && false !== strpos($groupedCondition, 'HAVING')) {
241 - $groupedCondition .= ' AND (' . $globalFilterString . ') ';
242 - } else {
243 - $groupedCondition .= ' HAVING (' . $globalFilterString . ') ';
244 - }
245 - $all_values = array_merge($all_values, $globalFilterValues);
246 - }
221 + //#unused code commented by me##
222 + //$isRecount = false;
247 223
224 + // if ( !empty( $filter['field'] ) ) {
225 + // $isRecount = true;
226 + // $filterFieldCount = count( $filter['field'] );
227 + // $filterFieldChecker = 0;
228 + // if ( $filterFieldCount > 0 ) {
229 + // $groupedCondition .= " HAVING ";
230 + // }
231 + // foreach ( $filter['field'] as $filterFieldKey => $filterFieldDetails ) {
232 + // $groupedCondition .= " `$filterFieldDetails->id` ='%%" . $this->getFieldFormat( $filterFieldDetails->value ) . "%%'";
233 + // $all_values[] = $filterFieldDetails->value;
234 + // if ( $filterFieldChecker < $filterFieldCount ) {
235 + // $groupedCondition .= " AND ";
236 + // }
237 + // }
238 + // }
239 +
240 + // if ( !empty( $filter['global'] ) && !empty( $globalFilterString ) && !empty( $globalFilterValues ) ) {
241 + // $isRecount = true;
242 + // if ( !empty( $filter['field'] ) ) {
243 + // $groupedCondition .= " AND (" . $globalFilterString . ") ";
244 + // } else {
245 + // $groupedCondition .= " HAVING $globalFilterString ";
246 + // }
247 + // $offset = 0;
248 + // $all_values = array_merge( $all_values, $globalFilterValues );
249 + // }
250 +
251 + // if ( !empty( $dateBetweenFilter ) && !empty( $dateBetweenFilter->start_date ) && !empty( $dateBetweenFilter->end_date ) ) {
252 + // if ( strpos( $groupedCondition, 'HAVING' ) !== false ) {
253 + // $groupedCondition .= " AND `__created_at` BETWEEN '" . $dateBetweenFilter->start_date . "' AND '" . $dateBetweenFilter->end_date . "' ";
254 + // } else {
255 + // $groupedCondition .= " HAVING `__created_at` BETWEEN '" . $dateBetweenFilter->start_date . "' AND '" . $dateBetweenFilter->end_date . "' ";
256 + // }
257 + // }
258 +
248 259 $sqlQryByFldCondtion = $this->sqlQryGenerateByFldCondition($fieldConditions);
249 260
250 261 if (!empty($sqlQryByFldCondtion)) {
251 262 $isFldCondition = true;
252 - if ($groupedCondition && false !== strpos($groupedCondition, 'HAVING')) {
263 + if (false !== strpos($groupedCondition, 'HAVING')) {
253 264 $groupedCondition .= ' AND (' . $sqlQryByFldCondtion . ') ';
254 265 } else {
255 266 $groupedCondition .= ' HAVING (' . $sqlQryByFldCondtion . ') ';
256 267 }
@@ -272,12 +283,12 @@
272 283 $orderCondition .= ' ORDER BY ';
273 284 }
274 285 $orderList = '';
275 286 foreach ($sortBy as $sortableFieldKey => $sortableFieldDetails) {
287 + // $orderCondition .=" ".$this->getFieldFormat($sortableFieldDetails->id);
276 288 $sortableFieldChecker += 1;
277 - if (in_array($sortableFieldDetails->id, $formFieldsNames, true)) {
278 - $safeId = sanitize_key($sortableFieldDetails->id);
279 - $orderList .= " `$safeId` ";
289 + if (in_array($sortableFieldDetails->id, $formFieldsNames)) {
290 + $orderList .= " `$sortableFieldDetails->id` ";
280 291 $orderFollow = $sortableFieldDetails->desc ? ' DESC ' : ' ASC ';
281 292 $orderList .= ' ' . $orderFollow;
282 293 if ($sortableFieldChecker < $sortableFieldCount) {
283 294 $orderList .= ', ';
@@ -312,18 +323,20 @@
312 323 public function getEntryMeta($formFields, $entries, $limit = null, $offset = null, $filter = null, $sortBy = null, $fieldConditions = null, $dateBetweenFilter = null)
313 324 {
314 325 $entry_table = $this->app_db->prefix . 'bitforms_form_entries';
315 326 $fieldCount = count($formFields);
316 - $getSelectedMetaFldValue = $this->selectedEntryMeta($formFields, $fieldCount, $filter);
327 + $getSelectedMetaFldValue = $this->selectedEntryMeta($formFields, $fieldCount);
328 +
317 329 $selectedMeta = $getSelectedMetaFldValue['selected_meta'];
318 330 $formFieldsNames = $getSelectedMetaFldValue['form_fields_names'];
319 331 $all_values = $getSelectedMetaFldValue['all_values'];
320 - $globalFilterString = $getSelectedMetaFldValue['global_filter_string'];
321 - $globalFilterValues = $getSelectedMetaFldValue['global_filter_values'];
322 332 $entryIDs = [];
323 333 $entryCount = count($entries);
334 + // $paginateEntry = empty($sortBy) && empty($filter['field']) && empty($filter['global']);
335 + // $entries = $paginateEntry ? array_slice($entries, $offset, $limit) : $entries;
336 + $paginateEntry = false;
324 337 foreach ($entries as $entryDetail) {
325 - $entryIDs[] = $entryDetail->id ?? $entryDetail;
338 + $entryIDs[] = $entryDetail->id;
326 339 }
327 340 if (empty($entryIDs)) {
328 341 return [
329 342 'count' => 0,
@@ -330,36 +343,34 @@
330 343 'entries' => [],
331 344 ];
332 345 }
333 346 $condition['bitforms_form_entry_id'] = $entryIDs;
334 - $group = $this->groupedCondition($condition, $all_values, $fieldConditions, $filter, $globalFilterString, $globalFilterValues);
347 + $group = $this->groupedCondition($condition, $all_values, $fieldConditions);
335 348 $groupedCondition = $group['groupedCondition'];
336 349 $all_values = $group['all_values'];
337 350 $isFldCondition = $group['isFldCondition'];
338 - $orderCondition = $this->orderCondition($formFieldsNames, (array) $sortBy);
351 + $orderCondition = $this->orderCondition($formFieldsNames, $sortBy);
339 352
340 353 $paginate = null;
341 - if (!is_null($limit)) {
342 - $limit = intval($limit);
354 + if (!\is_null($limit)) {
355 + $limit = \intval($limit);
343 356 $paginate .= " LIMIT $limit ";
344 357 }
345 - if (!is_null($offset)) {
346 - $offset = intval($offset);
347 - $paginate .= " OFFSET $offset ";
358 + if (!\is_null($offset)) {
359 + $offset = \intval($offset);
360 + $paginate .= " OFFSET $offset ";
348 361 }
349 -
350 362 $sql = "SELECT $selectedMeta FROM `$this->table_name` em";
351 363 $sql .= " INNER JOIN $entry_table e on e.id = em.bitforms_form_entry_id ";
352 364 if ($dateBetweenFilter) {
353 - $startDate = sanitize_text_field($dateBetweenFilter->start_date ?? '');
354 - $endDate = sanitize_text_field($dateBetweenFilter->end_date ?? '');
355 -
365 + $startDate = $dateBetweenFilter->start_date;
366 + $endDate = $dateBetweenFilter->end_date;
356 367 if ($startDate && $endDate) {
357 - $sql .= $this->app_db->prepare(' AND e.created_at BETWEEN %s AND %s', $startDate . ' 00:00:00', $endDate . ' 23:59:59');
368 + $sql .= " AND DATE(e.created_at) BETWEEN '$startDate' AND '$endDate' ";
358 369 } elseif ($startDate) {
359 - $sql .= $this->app_db->prepare(' AND e.created_at >= %s', $startDate . ' 00:00:00');
370 + $sql .= " AND DATE(e.created_at) >= '$startDate' ";
360 371 } elseif ($endDate) {
361 - $sql .= $this->app_db->prepare(' AND e.created_at <= %s', $endDate . ' 23:59:59');
372 + $sql .= " AND DATE(e.created_at) <= '$endDate' ";
362 373 }
363 374 }
364 375 $sql .= $groupedCondition . $orderCondition . $paginate;
365 376 $result = $this->execute($sql, $all_values)->getResult();
@@ -364,15 +375,18 @@
364 375 $sql .= $groupedCondition . $orderCondition . $paginate;
365 376 $result = $this->execute($sql, $all_values)->getResult();
366 377 if (is_wp_error($result)) {
367 378 return [
368 - 'count' => 0,
379 + 'count' => $paginateEntry ? $entryCount : 0,
369 380 'entries' => [],
370 381 'error' => $result->get_error_message()
371 382 ];
372 383 }
373 384 if ($isFldCondition) {
374 - $entryCount = $this->queryRecount($selectedMeta, $groupedCondition, $orderCondition, $all_values);
385 + $condition['bitforms_form_entry_id'] = $entryIDs;
386 + $group = $this->groupedCondition($condition, $all_values, $fieldConditions);
387 + $all_values = $group['all_values'];
388 + $entryCount = $this->queryRecount($selectedMeta, $group['groupedCondition'], $orderCondition, $all_values);
375 389 }
376 390 $resultedEntries = [
377 391 'count' => $entryCount,
378 392 'entries' => $result,
@@ -403,13 +417,13 @@
403 417 }
404 418 return $result;
405 419 }
406 420
407 - private static function csvInjectionPrevent($value)
421 + private function csvInjectionPrevent($value)
408 422 {
409 423 $formula = ['=', '-', '+', '@', "\t", "\r"];
410 424 $valueFilter = preg_replace('/[\]["]/i', '', $value);
411 - if (in_array(substr($value, 0, 1), $formula, true)) {
425 + if (\in_array(substr($value, 0, 1), $formula, true)) {
412 426 $valueFilter = "'" . trim($valueFilter);
413 427 }
414 428
415 429 return $valueFilter;
@@ -414,83 +428,37 @@
414 428
415 429 return $valueFilter;
416 430 }
417 431
418 - private static function unescapeString($str)
432 + public function getExportEntry($formFields, $entries, $formId, $fieldLabels, $limit = null, $sortBy = null, $sortByField = null)
419 433 {
420 - if (is_string($str) && '' !== $str) {
421 - $decoded = json_decode('"' . str_replace('"', '\\"', $str) . '"');
422 - return (null !== $decoded) ? $decoded : $str;
423 - }
424 - return $str;
425 - }
426 -
427 - private static function formatRepeaterValue($rawValue, $fieldMap)
428 - {
429 - if (empty($rawValue)) {
430 - return '';
431 - }
432 - $rows = [];
433 - preg_match_all('/\{([^}]+)\}/', $rawValue, $matches);
434 -
435 - foreach ($matches[1] as $row) {
436 - $pairs = explode(',', $row);
437 - $formattedPairs = [];
438 -
439 - foreach ($pairs as $pair) {
440 - if (false === strpos($pair, ':')) {
441 - continue;
442 - }
443 - [$childKey, $value] = explode(':', $pair, 2);
444 - $childKey = trim($childKey);
445 - $value = trim($value);
446 -
447 - // Get label from fieldMap or use key
448 - $label = $fieldMap[$childKey]['adminLbl'] ?? $childKey;
449 - $formattedPairs[] = "$label: " . self::unescapeString($value);
450 - }
451 -
452 - $rows[] = implode(', ', $formattedPairs);
453 - }
454 -
455 - return implode('; ', $rows);
456 - }
457 -
458 - public function getExportEntry($formFields, $entries, $formId, $fieldLabels, $limit = null, $sortBy = null, $sortByField = null, $offset = null, $entryConditions = null)
459 - {
460 434 $entry_table = $this->app_db->prefix . 'bitforms_form_entries';
461 435 $selectedEntryMeta = '`bitforms_form_entry_id` as entry_id,';
462 - $selectedEntryMeta .= 'e.user_id as `__user_id`,';
463 - $selectedEntryMeta .= 'e.status as `__entry_status`,';
464 - $selectedEntryMeta .= 'e.user_ip as `__user_ip`,';
465 - $selectedEntryMeta .= 'e.user_location as `__user_location`,';
466 - $selectedEntryMeta .= 'e.user_device as `__user_device`,';
467 - $selectedEntryMeta .= 'e.referer as `__referer`,';
468 - $selectedEntryMeta .= 'e.created_at as `__created_at`,';
469 - $selectedEntryMeta .= 'e.updated_at as `__updated_at`,';
436 + $selectedEntryMeta .= "e.user_id as '__user_id',";
437 + $selectedEntryMeta .= "e.status as '__entry_status',";
438 + $selectedEntryMeta .= "e.user_ip as '__user_ip',";
439 + $selectedEntryMeta .= "e.user_location as '__user_location',";
440 + $selectedEntryMeta .= "e.user_device as '__user_device',";
441 + $selectedEntryMeta .= "e.referer as '__referer',";
442 + $selectedEntryMeta .= "e.created_at as '__created_at',";
443 + $selectedEntryMeta .= "e.updated_at as '__updated_at',";
470 444 $metaChecker = 0;
471 445
472 - $entryInfo = [
473 - '__user_id',
474 - '__user_ip', /* '__user_location', */
475 - '__user_device',
476 - '__entry_status',
477 - '__referer',
478 - '__created_at',
479 - '__updated_at'
480 - ];
446 + $entryInfo = ['__user_id', '__user_ip', /* '__user_location', */'__user_device',
447 + '__referer', '__created_at', '__updated_at'];
481 448 $all_values = [];
482 449 if ([] === $formFields) {
483 - return [
450 + $data = [
484 451 'count' => 0,
485 452 'entries' => [],
486 453 ];
454 + wp_send_json_success($data, 200);
487 455 }
488 456 $fieldCount = count($formFields) - count(array_intersect($formFields, $entryInfo));
489 457 $formFieldsNames = [];
490 458 foreach ($formFields as $fldKey) {
491 459 $formFieldsNames[] = $fldKey;
492 - if (in_array($fldKey, $entryInfo, true)) {
460 + if (in_array($fldKey, $entryInfo)) {
493 461 continue;
494 462 }
495 463 $fieldFormat = $this->getFieldFormat($fldKey);
496 464 $selectedEntryMeta .= "GROUP_CONCAT(
@@ -516,66 +484,42 @@
516 484 'entries' => [],
517 485 ];
518 486 }
519 487 $condition['bitforms_form_entry_id'] = $entryIDs;
520 - $grpCon = $this->groupedCondition($condition, $all_values, $entryConditions);
521 - $groupedCondition = $grpCon['groupedCondition'];
522 - $all_values = $grpCon['all_values'];
523 -
488 + $formattedCondition = $this->getFormatedCondition($condition);
489 + $groupedCondition = null;
490 + if ($formattedCondition) {
491 + $groupedCondition = $formattedCondition['conditions'] . ' GROUP BY
492 + `bitforms_form_entry_id` ';
493 + $all_values = array_merge($all_values, $formattedCondition['values']);
494 + }
524 495 $order = 'DESC' === $sortBy ? 'DESC ' : 'ASC ';
525 - $validSortFields = array_column($fieldLabels, 'key');
526 - $orderField = (!is_null($sortByField) && in_array($sortByField, $validSortFields, true))
527 - ? '`' . sanitize_key($sortByField) . '`'
528 - : '`bitforms_form_entry_id`';
496 + $orderField = \is_null($sortByField) ? 'bitforms_form_entry_id' : "`$sortByField`";
529 497
530 498 $orderCondition = "ORDER BY $orderField $order ";
531 - $limitClause = '';
532 - if (!is_null($limit)) {
533 - $limitInt = intval($limit);
534 - $limitClause = " LIMIT $limitInt ";
535 - if (!is_null($offset)) {
536 - $offsetInt = intval($offset);
537 - $limitClause .= " OFFSET $offsetInt ";
538 - }
499 + if (!\is_null($limit)) {
500 + $limitInt = \intval($limit);
501 + $limit = " LIMIT $limitInt ";
539 502 }
540 -
541 - $this->app_db->query('SET SESSION group_concat_max_len = 10000');
542 503 $sql = "SELECT $selectedEntryMeta FROM `$this->table_name` em";
543 504 $sql .= " INNER JOIN $entry_table e on e.id = em.bitforms_form_entry_id ";
544 - $sql .= $groupedCondition . $orderCondition . $limitClause;
505 + $sql .= $groupedCondition . $orderCondition . $limit;
506 +
545 507 $result = $this->execute($sql, $all_values)->getResult();
546 - if (is_wp_error($result)) {
547 - return new \WP_Error('db_error', 'Internal server error');
548 - }
549 -
550 508 $allData = [];
551 - $entryStatus = [
552 - '0' => 'Read',
553 - '1' => 'Unread',
554 - '2' => 'Unconfirmed',
555 - '3' => 'Confirmed',
556 - '9' => 'Draft',
557 - ];
558 - $userIds = array_unique(array_filter(
559 - array_map(static fn ($row) => (int) $row->__user_id, (array) $result),
560 - static fn ($id) => $id > 0
561 - ));
509 + $entry_id = 'entry_id';
510 + $users = get_users(['fields' => ['ID', 'display_name']]);
562 511 $userNames = [];
563 - if (!empty($userIds)) {
564 - $users = get_users(['include' => $userIds, 'fields' => ['ID', 'display_name']]);
565 - foreach ($users as $user) {
566 - $userNames[$user->ID] = $user->display_name;
567 - }
512 + foreach ($users as $key => $value) {
513 + $userNames[$value->ID] = $value->display_name;
568 514 }
569 515 foreach ($result as $key => $value) {
570 516 foreach ($formFieldsNames as $formFieldName) {
571 - $allData[$key]['entry_id'] = preg_replace('/[\]["]/i', '', $value->entry_id);
517 + $allData[$key]['entry_id'] = preg_replace('/[\]["]/i', '', $value->$entry_id);
572 518 if ('__user_id' === $formFieldName && intval($value->$formFieldName) > 0) {
573 - $allData[$key][$formFieldName] = $userNames[$value->$formFieldName] ?? '';
519 + $allData[$key][$formFieldName] = $userNames[$value->$formFieldName];
574 520 } elseif ('__user_ip' === $formFieldName) {
575 - $allData[$key][$formFieldName] = long2ip((int) $value->$formFieldName);
576 - } elseif ('__entry_status' === $formFieldName) {
577 - $allData[$key][$formFieldName] = $entryStatus[$value->{$formFieldName}] ?? '';
521 + $allData[$key][$formFieldName] = long2ip((int)$value->$formFieldName);
578 522 } else {
579 523 $allData[$key][$formFieldName] = preg_replace('/[\]["]/i', '', $value->$formFieldName);
580 524 }
581 525 }
@@ -580,55 +524,40 @@
580 524 }
581 525 }
582 526 }
583 527
584 - $fieldMap = [];
585 - $repeaterFields = [];
586 - $fileFields = [];
587 - $downloadableFieldType = ['file-up', 'signature', 'advanced-file-up'];
588 -
589 - foreach ($fieldLabels as $field) {
590 - $key = $field['key'];
591 - $fieldMap[$key] = $field;
592 - if ('repeater' === $field['type']) {
593 - $repeaterFields[] = $key;
594 - } elseif (in_array($field['type'], $downloadableFieldType, true)) {
595 - $fileFields[] = $key;
596 - }
597 - }
598 -
599 - foreach ($allData as &$entry) {
600 - foreach ($entry as $key => &$value) {
601 - if (is_string($value)) {
602 - $value = self::csvInjectionPrevent(self::unescapeString($value));
603 - }
604 - if (in_array($key, $repeaterFields, true)) {
605 - $value = self::formatRepeaterValue($value, $fieldMap);
606 - }
607 - }
608 - unset($value);
609 - }
610 - unset($entry, $value);
611 -
612 - foreach ($allData as &$entry) {
613 - $entryId = $entry['entry_id'];
614 - $_upload_dir = FileHandler::getEntriesFileUploadDir($formId, $entryId);
615 - foreach ($fileFields as $fileKey) {
616 - if (empty($entry[$fileKey])) {
617 - continue;
618 - }
619 - $fileIds = explode(',', $entry[$fileKey]);
620 - $urls = [];
621 - foreach ($fileIds as $fileId) {
622 - $path = "bitforms/bitforms-file/?formID=$formId&entryID=$entryId&fileID=$fileId";
623 - if (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $fileId)) {
624 - $urls[] = site_url($path);
528 + if (is_wp_error($result)) {
529 + wp_send_json_error('Internal server error', 500);
530 + } else {
531 + $downloadableFieldType = ['file-up', 'signature', 'advanced-file-up'];
532 + foreach ($fieldLabels as $field) {
533 + foreach ($allData as $index => $entry) {
534 + if (array_key_exists($field['key'], $entry) && in_array($field['type'], $downloadableFieldType, true)) {
535 + $key = $field['key'];
536 + if (empty($entry[$key])) {
537 + continue;
538 + }
539 + $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formId . DIRECTORY_SEPARATOR . $entry['entry_id'];
540 + $imageArray = explode(',', $entry[$key]);
541 + if (is_array($imageArray)) {
542 + $fileData = [];
543 + foreach ($imageArray as $file) {
544 + $path = "bitforms/bitforms-file-$formId/?formID=$formId&entryID=" . $entry['entry_id'] . "&fileID=$file";
545 + if (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $file)) {
546 + $fileData[] = site_url($path, null);
547 + }
548 + }
549 + $allData[$index][$key] = implode(',', $fileData);
550 + } else {
551 + $uploadedFile = explode('_', $entry[$key]);
552 + $path = "bitforms/bitforms-file-$formId/?formID=$formId&entryID=" . $entry['entry_id'] . '&fileID=' . $uploadedFile[0];
553 + if (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $uploadedFile[0])) {
554 + $allData[$index][$key] = site_url($path, null);
555 + }
556 + }
625 557 }
626 558 }
627 - $entry[$fileKey] = implode(',', $urls);
628 559 }
560 + wp_send_json_success($allData, 200);
629 561 }
630 - unset($entry);
631 -
632 - return $allData;
633 562 }
634 563 }