changed add_var_to_front back to using add_inline instead as localize, because need to not overwrite the variables

This commit is contained in:
asus
2024-02-11 17:07:27 +01:00
parent de6fd7a8a7
commit 87a0f7fc0b
9 changed files with 58 additions and 21 deletions

View File

@@ -7,6 +7,7 @@
function add_files_to_front($files_arr) {
//wp_enqueue_script(<give_it_a_name>, /url/to/file, [depends on], version, defer? );
//wp_enqueue_style( <give_it_a_name>, /url/to/file, [depends on], version, media );
global $first_script;
// always adding ajax file first
$files_arr = ["ajax.js", ...$files_arr];
@@ -28,6 +29,8 @@ function add_files_to_front($files_arr) {
$file_version = date("ymd-Gis", filemtime($file_path));
if ($file_ext === "js") {
if (is_null($first_script))
$first_script = $file_basename;
wp_enqueue_script( $file_basename, $file_url, $previous_js_basename, $file_version, true);
$previous_js_basename = $file_basename;
}
@@ -52,24 +55,41 @@ function add_files_to_front($files_arr) {
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
pass variables to front as global variables, accessible in js as an object
it also adds the ajax url
pass variables to js front as global variables
@param two params :
1. an array of key => value
with the key being name of the variable, like this :
'my_var' => 'value',
simpler way to do it is to use compact when calling the function :
add_var_to_front(compact("var1", "var2", "var3"));
2. name of first embended script (if we want to have the variables
availables to all js : it will be available to the script and all following)
- default value is "ajax", assuming the first script is "ajax.js"
since it's automatically added first by `add_files_to_front`
2. (optionnal) name of first embended script that need these variables
(it will be available to this script and all followings)
*/
function add_var_to_front($vars, $handle = "ajax") {
function add_var_to_front($vars, $handle = null) {
if (is_null($handle)) {
global $first_script;
$handle = $first_script;
}
$handle = pathinfo($handle, PATHINFO_FILENAME);
$object_name = "php_data";
wp_localize_script($handle, $object_name, $vars);
extract($vars);
foreach ($vars as $key => $var)
{
$js_var = 'const ' . $key . ' = ';
$js_var .= json_encode($var);
$js_var .= ';';
wp_add_inline_script($handle, $js_var, 'before');
}
//
// the other way with localize has multiple incidences :
// - it creates an object from wich you can access the variables
// - so if you call it again wiht the same name, it will overwrite the previous
// {
// $handle = pathinfo($handle, PATHINFO_FILENAME);
// $object_name = "php_data";
//
// wp_localize_script($handle, $object_name, $vars);
// }
}
?>

View File

@@ -24,8 +24,6 @@ https://stackoverflow.com/a/4402045/9497573
function create_html($files, $vars = null) {
$files = (array)$files;
$html_dir = PLUGIN_DIR.'html/';
console_log("vars: ");
console_log($vars);
if (!is_null($vars))
extract($vars);

View File

@@ -11,4 +11,9 @@ const CONSOLE_OFF = true;
*/
const CONSOLE_OFF = false;
/* switch console_log
const CONSOLE_OFF = true;
*/
$first_script = null;
?>