| 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 |
$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"); |
| 23 |
|
| 24 |
if (is_wp_error($allForms)) { |
| 25 |
return $allForms; |
| 26 |
} |
| 27 |
$forms = array_reduce($allForms, function ($carry, $form) { |
| 28 |
$carry[$form->id] = $form->form_name . ' (' . $form->id . ')'; |
| 29 |
return $carry; |
| 30 |
}, []); |
| 31 |
|
| 32 |
if (!empty($forms)) { |
| 33 |
$forms[0] = __('Select a Bitform', 'bit-form'); |
| 34 |
} |
| 35 |
return $forms; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Request data formatting. |
| 40 |
* |
| 41 |
* Receives raw data from ajax request and formats it into an object. |
| 42 |
* @param mixed $data |
| 43 |
* @return object |
| 44 |
*/ |
| 45 |
public static function formatRequestData($data): object |
| 46 |
{ |
| 47 |
if (null === $data) { |
| 48 |
throw new \InvalidArgumentException('The "data" parameter is required.'); |
| 49 |
} |
| 50 |
|
| 51 |
if (is_array($data)) { |
| 52 |
return (object) $data; |
| 53 |
} |
| 54 |
// Unslash input |
| 55 |
$userData = wp_unslash($data); |
| 56 |
return json_decode($userData); |
| 57 |
|
| 58 |
// // Normalize to array |
| 59 |
// if (is_string($userData)) { |
| 60 |
// $decoded = json_decode($userData, true); |
| 61 |
// if (JSON_ERROR_NONE !== json_last_error()) { |
| 62 |
// throw new \InvalidArgumentException('Invalid JSON provided in data.'); |
| 63 |
// } |
| 64 |
// $userData = $decoded ?? []; |
| 65 |
// } elseif (is_object($userData)) { |
| 66 |
// $userData = (array) $userData; |
| 67 |
// } |
| 68 |
|
| 69 |
// if (!is_array($userData)) { |
| 70 |
// // You may want to throw here instead of returning empty object |
| 71 |
// return new \stdClass(); |
| 72 |
// } |
| 73 |
|
| 74 |
// // Recursive sanitizer — always returns objects |
| 75 |
// $sanitizeRecursive = function ($value) use (&$sanitizeRecursive) { |
| 76 |
// if (is_array($value)) { |
| 77 |
// $clean = new \stdClass(); |
| 78 |
// foreach ($value as $k => $v) { |
| 79 |
// $clean->$k = $sanitizeRecursive($v); |
| 80 |
// } |
| 81 |
// return $clean; |
| 82 |
// } elseif (is_object($value)) { |
| 83 |
// $clean = new \stdClass(); |
| 84 |
// foreach ($value as $k => $v) { |
| 85 |
// $clean->$k = $sanitizeRecursive($v); |
| 86 |
// } |
| 87 |
// return $clean; |
| 88 |
// } else { |
| 89 |
// return sanitize_text_field($value); |
| 90 |
// } |
| 91 |
// }; |
| 92 |
|
| 93 |
// return $sanitizeRecursive($userData); // already an object |
| 94 |
} |
| 95 |
|
| 96 |
public static function requirePostMethod(): void |
| 97 |
{ |
| 98 |
if ('POST' !== $_SERVER['REQUEST_METHOD']) { |
| 99 |
Log::debug_log('Invalid request method. POST required.'); |
| 100 |
wp_send_json_error(__('Invalid request method. POST required.', 'bit-form'), 405); |
| 101 |
return; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|