Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions frontend-dev/src/Utils/bitsFetch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

export default async function bitsFetch(data, action, queryParam = null, method = 'POST', signal) {
const uri = new URL(typeof btcbi === 'undefined' ? bitFromsFront?.ajaxURL : btcbi.ajaxURL)
uri.searchParams.append('action', `btcbi_${action}`)
uri.searchParams.append('_ajax_nonce', typeof btcbi === 'undefined' ? '' : btcbi.nonce)

if (method.toLowerCase() === 'get') {
uri.searchParams.append('action', `btcbi_${action}`)
uri.searchParams.append('_ajax_nonce', typeof btcbi === 'undefined' ? '' : btcbi.nonce)
}
// append query params in url
if (queryParam) {
for (const key in queryParam) {
Expand All @@ -21,11 +24,25 @@ export default async function bitsFetch(data, action, queryParam = null, method
}

if (method.toLowerCase() === 'post') {
options.body = data instanceof FormData ? data : JSON.stringify(data)
/**
* @type FormData
*/
let formData
if (!(data instanceof FormData)) {
formData = new FormData()
formData.set('data', JSON.stringify(data))
} else {
formData = data
}

formData.set('action', `btcbi_${action}`)
formData.set('_ajax_nonce', typeof btcbi === 'undefined' ? '' : btcbi.nonce)

options.body = formData
}
const response = await fetch(uri, options)
.then(res => res.text())
.then(res => {
.then((res) => res.text())
.then((res) => {
try {
return JSON.parse(res)
} catch (error) {
Expand Down
4 changes: 4 additions & 0 deletions includes/Core/Util/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ public static function action()
) {
$invokeable = static::$_invokeable[$action][$requestMethod];
unset($_POST['_ajax_nonce'], $_POST['action'], $_GET['_ajax_nonce'], $_GET['action']);

if (method_exists($invokeable[0], $invokeable[1])) {
if ($requestMethod == 'POST') {
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) {
$inputJSON = file_get_contents('php://input');
$data = \is_string($inputJSON) ? json_decode($inputJSON) : $inputJSON;
} elseif ($_POST['data']) {
$data = \is_string($_POST['data']) ? json_decode(wp_unslash($_POST['data'])) : $_POST['data'];
} else {
$data = (object) $_POST;
}
Expand All @@ -73,6 +76,7 @@ public static function action()

$reflectionMethod = new ReflectionMethod($invokeable[0], $invokeable[1]);
$response = $reflectionMethod->invoke($reflectionMethod->isStatic() ? null : new $invokeable[0](), $data);

if (is_wp_error($response)) {
wp_send_json_error($response);
} else {
Expand Down