PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.13
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.13
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
bit-form / includes / GlobalHelper.php

GlobalHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.21.13, at includes/GlobalHelper.php

121 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm;
4
5 use BitCode\BitForm\Core\Util\Log;
6
7 if (!\defined('ABSPATH')) {
8 exit;
9 }
10
11 class GlobalHelper
12 {
13 /**
14 * Get all forms.
15 *
16 * @return array
17 */
18 public static function getForms()
19 {
20 global $wpdb;
21
22 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
23 $allForms = $wpdb->get_results("SELECT forms.id,forms.entries as fm_entries,forms.form_name,forms.status,forms.views,forms.created_at,COUNT(entries.id) as entries FROM `{$wpdb->prefix}bitforms_form` as forms LEFT JOIN `{$wpdb->prefix}bitforms_form_entries` as entries ON forms.id = entries.form_id GROUP BY forms.id");
24
25 if (is_wp_error($allForms)) {
26 return $allForms;
27 }
28 $forms = array_reduce($allForms, function ($carry, $form) {
29 $carry[$form->id] = $form->form_name . ' (' . $form->id . ')';
30 return $carry;
31 }, []);
32
33 if (!empty($forms)) {
34 $forms[0] = __('Select a Bitform', 'bit-form');
35 }
36 return $forms;
37 }
38
39 /**
40 * Request data formatting.
41 *
42 * Receives raw data from ajax request and formats it into an object.
43 * @param mixed $data
44 * @return object
45 */
46 public static function formatRequestData(): object
47 {
48 $data = $_POST['data'] ?? null;
49 if (null === $data) {
50 throw new \InvalidArgumentException('The "data" parameter is required.');
51 }
52
53 if (is_array($data)) {
54 return (object) $data;
55 }
56 // Unslash input
57 $userData = wp_unslash($data);
58 $result = json_decode($userData);
59 if(JSON_ERROR_NONE !== json_last_error()) {
60 throw new \InvalidArgumentException('Invalid JSON provided in data.');
61 }
62 if(gettype($result) !== 'object') {
63 $emptyObject = new \stdClass();
64 Log::debug_log([
65 'message'=>'Invalid JSON provided in data. Returning empty object.',
66 'data'=>$userData,
67 'result'=>$result,
68 'type'=>gettype($result)
69 ]);
70 return $emptyObject;
71 }
72 return $result;
73
74 // // Normalize to array
75 // if (is_string($userData)) {
76 // $decoded = json_decode($userData, true);
77 // if (JSON_ERROR_NONE !== json_last_error()) {
78 // throw new \InvalidArgumentException('Invalid JSON provided in data.');
79 // }
80 // $userData = $decoded ?? [];
81 // } elseif (is_object($userData)) {
82 // $userData = (array) $userData;
83 // }
84
85 // if (!is_array($userData)) {
86 // // You may want to throw here instead of returning empty object
87 // return new \stdClass();
88 // }
89
90 // // Recursive sanitizer — always returns objects
91 // $sanitizeRecursive = function ($value) use (&$sanitizeRecursive) {
92 // if (is_array($value)) {
93 // $clean = new \stdClass();
94 // foreach ($value as $k => $v) {
95 // $clean->$k = $sanitizeRecursive($v);
96 // }
97 // return $clean;
98 // } elseif (is_object($value)) {
99 // $clean = new \stdClass();
100 // foreach ($value as $k => $v) {
101 // $clean->$k = $sanitizeRecursive($v);
102 // }
103 // return $clean;
104 // } else {
105 // return sanitize_text_field($value);
106 // }
107 // };
108
109 // return $sanitizeRecursive($userData); // already an object
110 }
111
112 public static function requirePostMethod(): void
113 {
114 if (!isset($_SERVER['REQUEST_METHOD']) || 'POST' !== sanitize_text_field(wp_unslash($_SERVER['REQUEST_METHOD']))) {
115 Log::debug_log('Invalid request method. POST required.');
116 wp_send_json_error(__('Invalid request method. POST required.', 'bit-form'), 405);
117 return;
118 }
119 }
120 }
121