- struggled to make ajax works, but now is ok

- starting to make serverside works
This commit is contained in:
asus
2024-02-23 19:36:03 +01:00
parent 7cfa2e6351
commit b7685cbbc1
9 changed files with 428 additions and 37 deletions

View File

@@ -3,9 +3,12 @@
* - PLGNTLS_data.ajax_nonce and PLGNTLS_data.ajax_url
* are passed from the class PLGNTLS_class
*/
/*
// OLD VERSION
console.log("PLGNTLS_data");
console.log(PLGNTLS_data);
function PLGNTLS_ajax(data_obj, action) {
function PLGNTLS_ajax(action, data_obj) {
const data = new FormData();
data.append("action", action);
data.append("_ajax_nonce", PLGNTLS_data.ajax_nonce);
@@ -15,6 +18,101 @@ function PLGNTLS_ajax(data_obj, action) {
return fetch(PLGNTLS_data.ajax_url, {
method: "POST",
credentials: "same-origin",
body: data
body: data,
});
}
*/
// https://stackoverflow.com/q/52657814/9497573
function is_plain_object(obj)
{
if (!obj)
return false;
if (obj.constructor !== Object)
return false;
if (Object.getPrototypeOf(obj) !== Object.prototype)
return false;
return true;
}
function PLGNTLS_fetch(src = null, options = null)
{
if (src === null)
return null;
if (options === null)
{
const data = new FormData();
data.append('action', src);
data.append('_ajax_nonce', PLGNTLS_data.ajax_nonce);
options = {
method: 'POST',
credentials: 'same-origin',
body: data,
};
}
else if (is_plain_object(options))
{
// check for method, default POST
if (!Object.hasOwn(options, 'method'))
options.method = 'POST';
// check for credentials, default same-origin
if (!Object.hasOwn(options, 'credentials'))
options.credentials = 'same-origin';
// check for body, default contains action and nonce
if (!Object.hasOwn(options, 'body'))
options.body = new FormData();
// add action and nonce in options.body
if (options.body instanceof FormData)
{
// dont check if action and nonce already exist, need new anyway
options.body.set('action', src);
options.body.set('_ajax_nonce', PLGNTLS_data.ajax_nonce);
}
else
{
// should think a better strategy : https://stackoverflow.com/q/20295080/9497573
// const data = {};
// if (is_plain_object(options.body))
// {
// for (const key in options.body)
// data[key] = options.body[key];
// }
// else
// data.data = options.body;
// data.action = src;
// data._ajax_nonce = PLGNTLS_data.ajax_nonce;
// options.body = JSON.stringify(data);
// cannot work if fetch use :
// headers: {"Content-Type": "application/json"},
const data = new FormData( );
data.append( 'action', src );
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

@@ -54,10 +54,13 @@ class PLGNTLS_class
public function add_to_front($scripts_arr = null, $vars = null) {
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();
$vars = array_merge($vars, $nonce);
$vars = array_merge($vars, $url);
}
@@ -68,6 +71,9 @@ class PLGNTLS_class
}
if (!is_null($scripts_arr))
$this->add_scripts_to_front($scripts);
console_log("vars:");
console_log($vars);
if (!is_null($vars))
$this->add_vars_to_front($vars);
return $this->create_html($scripts, $vars);
@@ -248,8 +254,6 @@ class PLGNTLS_class
else
$script->basename = pathinfo($script_name, PATHINFO_BASENAME);
$script->handle = "PLGNTLS_" . str_replace(".", "_", $script->basename);
console_log("script->handle:");
console_log($script->handle);
if ($script->ext === "url") {
$script->url = $script_name;