Skip to content

Commit 607a457

Browse files
authored
Merge pull request #81 from Bit-Apps-Pro/post-request-lts
Post request refactor
2 parents 371dccb + f4e2640 commit 607a457

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

frontend-dev/src/Utils/bitsFetch.jsx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
export default async function bitsFetch(data, action, queryParam = null, method = 'POST', signal) {
55
const uri = new URL(typeof btcbi === 'undefined' ? bitFromsFront?.ajaxURL : btcbi.ajaxURL)
6-
uri.searchParams.append('action', `btcbi_${action}`)
7-
uri.searchParams.append('_ajax_nonce', typeof btcbi === 'undefined' ? '' : btcbi.nonce)
6+
7+
if (method.toLowerCase() === 'get') {
8+
uri.searchParams.append('action', `btcbi_${action}`)
9+
uri.searchParams.append('_ajax_nonce', typeof btcbi === 'undefined' ? '' : btcbi.nonce)
10+
}
811
// append query params in url
912
if (queryParam) {
1013
for (const key in queryParam) {
@@ -21,11 +24,25 @@ export default async function bitsFetch(data, action, queryParam = null, method
2124
}
2225

2326
if (method.toLowerCase() === 'post') {
24-
options.body = data instanceof FormData ? data : JSON.stringify(data)
27+
/**
28+
* @type FormData
29+
*/
30+
let formData
31+
if (!(data instanceof FormData)) {
32+
formData = new FormData()
33+
formData.set('data', JSON.stringify(data))
34+
} else {
35+
formData = data
36+
}
37+
38+
formData.set('action', `btcbi_${action}`)
39+
formData.set('_ajax_nonce', typeof btcbi === 'undefined' ? '' : btcbi.nonce)
40+
41+
options.body = formData
2542
}
2643
const response = await fetch(uri, options)
27-
.then(res => res.text())
28-
.then(res => {
44+
.then((res) => res.text())
45+
.then((res) => {
2946
try {
3047
return JSON.parse(res)
3148
} catch (error) {

includes/Core/Util/Route.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,14 @@ public static function action()
5959
) {
6060
$invokeable = static::$_invokeable[$action][$requestMethod];
6161
unset($_POST['_ajax_nonce'], $_POST['action'], $_GET['_ajax_nonce'], $_GET['action']);
62+
6263
if (method_exists($invokeable[0], $invokeable[1])) {
6364
if ($requestMethod == 'POST') {
6465
if (isset($_SERVER['CONTENT_TYPE']) && strpos(sanitize_text_field($_SERVER['CONTENT_TYPE']), 'form-data') === false && strpos(sanitize_text_field($_SERVER['CONTENT_TYPE']), 'x-www-form-urlencoded') === false) {
6566
$inputJSON = file_get_contents('php://input');
6667
$data = \is_string($inputJSON) ? json_decode($inputJSON) : $inputJSON;
68+
} elseif ($_POST['data']) {
69+
$data = \is_string($_POST['data']) ? json_decode(wp_unslash($_POST['data'])) : $_POST['data'];
6770
} else {
6871
$data = (object) $_POST;
6972
}
@@ -73,6 +76,7 @@ public static function action()
7376

7477
$reflectionMethod = new ReflectionMethod($invokeable[0], $invokeable[1]);
7578
$response = $reflectionMethod->invoke($reflectionMethod->isStatic() ? null : new $invokeable[0](), $data);
79+
7680
if (is_wp_error($response)) {
7781
wp_send_json_error($response);
7882
} else {

0 commit comments

Comments
 (0)