- forget old ajax wp, now use fetch wp
- trying js files without imports
This commit is contained in:
@@ -14,7 +14,6 @@ class PLGNTLS_class
|
||||
private static $_root_path;
|
||||
private static $_root_url;
|
||||
|
||||
private static $_ajax_already_there;
|
||||
private $_first_script;
|
||||
private $_prefix;
|
||||
private $_js_dependencies;
|
||||
@@ -23,8 +22,6 @@ class PLGNTLS_class
|
||||
/**
|
||||
*/
|
||||
public function __construct() {
|
||||
if (! isset( self::$_ajax_already_there ))
|
||||
self::$_ajax_already_there = false;
|
||||
$this->_prefix = "PLGNTLS";
|
||||
$this->_first_script = null;
|
||||
$this->_js_dependencies = array();
|
||||
@@ -56,20 +53,16 @@ class PLGNTLS_class
|
||||
$vars = array();
|
||||
if (!is_null($scripts_arr))
|
||||
{
|
||||
// add ajax file at beginning of files list
|
||||
array_unshift($scripts_arr, "utils/plugin_ajax.js");
|
||||
$ajax_nonce = array("ajax_nonce" => wp_create_nonce('wp-pageviews-nonce'));
|
||||
// add fetch script at beginning of scripts list
|
||||
array_unshift($scripts_arr, "utils/plgntls_fetch.js");
|
||||
// for the custom endpoints in rest api to work
|
||||
// they need to have a nonce named 'wp_rest'
|
||||
// see : https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
|
||||
$rest_nonce = array("rest_nonce" => wp_create_nonce('wp_rest'));
|
||||
$ajax_url = array("ajax_url" => admin_url('admin-ajax.php'));
|
||||
$vars = array_merge($vars, $ajax_nonce);
|
||||
$vars = array_merge($vars, $rest_nonce);
|
||||
$vars = array_merge($vars, $ajax_url);
|
||||
$fetch_nonce = array("fetch_nonce" => wp_create_nonce('wp_rest'));
|
||||
$fetch_url = array("fetch_url" => get_site_url() . "/wp-json");
|
||||
$vars = array_merge($vars, $fetch_nonce);
|
||||
$vars = array_merge($vars, $fetch_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) {
|
||||
@@ -203,10 +196,10 @@ class PLGNTLS_class
|
||||
}
|
||||
/*
|
||||
* uncomment to print all enqueued scripts, can be usefull
|
||||
*/
|
||||
global $wp_scripts;
|
||||
error_log("wp_scripts->queue:");
|
||||
error_log(json_encode($wp_scripts->queue));
|
||||
*/
|
||||
}
|
||||
|
||||
private function check_dependencies(&$script, $previous_basename)
|
||||
12
plugins/fipfcard_plugin/utils/plgntls_fetch.js
Normal file
12
plugins/fipfcard_plugin/utils/plgntls_fetch.js
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
function PLGNTLS_fetch(url, options = {}) {
|
||||
console.log("inside PLGNTLS_fetch");
|
||||
url = PLGNTLS_data.fetch_url + url;
|
||||
|
||||
options.headers = options.headers || {};
|
||||
options.headers['X-WP-Nonce'] = PLGNTLS_data.fetch_nonce,
|
||||
console.log("options:", options);
|
||||
|
||||
return fetch(url, options);
|
||||
}
|
||||
|
||||
@@ -8,20 +8,25 @@
|
||||
// OLD VERSION
|
||||
console.log("PLGNTLS_data");
|
||||
console.log(PLGNTLS_data);
|
||||
*/
|
||||
function PLGNTLS_ajax(action, data_obj) {
|
||||
const data = new FormData();
|
||||
data.append("action", action);
|
||||
data.append("_ajax_nonce", PLGNTLS_data.ajax_nonce);
|
||||
for (const key in data_obj)
|
||||
data.append(key, data_obj[key]);
|
||||
// for (const key in data_obj)
|
||||
// {
|
||||
// data.append(key, data_obj[key]);
|
||||
// }
|
||||
// console.log("data:", data);
|
||||
data.append(data_obj);
|
||||
|
||||
return fetch(PLGNTLS_data.ajax_url, {
|
||||
method: "POST",
|
||||
credentials: "same-origin",
|
||||
body: data,
|
||||
hedears: {"Content-Type": "application/json"},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
}
|
||||
*/
|
||||
|
||||
// https://stackoverflow.com/q/52657814/9497573
|
||||
function is_plain_object(obj)
|
||||
@@ -35,6 +40,7 @@ function is_plain_object(obj)
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
function PLGNTLS_fetch(src = null, options = null)
|
||||
{
|
||||
if (src === null)
|
||||
@@ -108,3 +114,4 @@ function PLGNTLS_fetch(src = null, options = null)
|
||||
|
||||
return fetch(PLGNTLS_data.ajax_url, options);
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user