try wordpress rest api instead of ajax

This commit is contained in:
asus
2024-02-23 23:43:23 +01:00
parent 0f15b67e8b
commit 38260912cd
5 changed files with 91 additions and 53 deletions

View File

@@ -92,27 +92,19 @@ function PLGNTLS_fetch(src = null, options = null)
data.append( '_ajax_nonce', PLGNTLS_data.ajax_nonce );
if ( is_plain_object( options.body ) )
{
console.log( "1" );
for ( const key in options.body )
data.append( key, JSON.stringify(options.body[key]) );
}
// is string : https://stackoverflow.com/q/4059147/9497573
else if ( typeof options.body === 'string' || options.body instanceof String )
{
console.log( "2" );
data.append( 'data', options.body );
}
else
{
console.log( "3" );
data.append('data', JSON.stringify(options.body));
}
options.body = data;
}
}
else
throw new Error('options not plain object or formData');
console.log("options: ", options);
return fetch(PLGNTLS_data.ajax_url, options);
}

View File

@@ -52,18 +52,20 @@ class PLGNTLS_class
public function add_to_front($scripts_arr = null, $vars = null) {
if (!is_array($vars))
$vars = array();
if (!is_null($scripts_arr))
{
console_log("in is null scripts_arr");
// add ajax file at beginning of files list
array_unshift($scripts_arr, "utils/plugin_ajax.js");
$nonce = array("ajax_nonce" => wp_create_nonce('wp-pageviews-nonce'));
$url = array("ajax_url" => admin_url('admin-ajax.php'));
if (!is_array($vars))
$vars = array();
$ajax_url = array("ajax_url" => admin_url('admin-ajax.php'));
$vars = array_merge($vars, $nonce);
$vars = array_merge($vars, $url);
$vars = array_merge($vars, $ajax_url);
}
$fetch_url = array("fetch_url" => get_site_url() . "/wp-json");
$vars = array_merge($vars, $fetch_url);
$scripts = array();
foreach($scripts_arr as $key => $script_name) {