dont add ajax script multiple times

This commit is contained in:
asus
2024-02-11 19:46:40 +01:00
parent 322e440422
commit e2bd001a8a
3 changed files with 16 additions and 8 deletions

View File

@@ -1,6 +1,10 @@
<?php <?php
function wp_model_plugin_content() { function wp_model_plugin_content() {
global $first_script;
console_log("in menu : first_script:");
console_log($first_script);
add_files_to_front( array( add_files_to_front( array(
"menu/menu.js", "menu/menu.js",
)); ));

View File

@@ -5,12 +5,21 @@
/* /*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function that add the ajax script to front function that add the ajax script to front
no need to check if already included, because $handle is uniq
so it will not be recincluded, the first enqueued version will be kept
*/ */
function add_ajax_post() { function add_ajax_post() {
global $first_script; global $first_script;
global $ajax_file; global $ajax_file;
$file = init_file($ajax_file); $file = init_file($ajax_file);
// check if ajax script was already enqueued
global $wp_scripts;
$already_enqueued = array_search($file->handle, $wp_scripts->queue);
if ($already_enqueued !== false)
return ;
$first_script = $file->handle; $first_script = $file->handle;
wp_enqueue_script( $file->handle, $file->url, $previous_js_basename, $file->version, true); wp_enqueue_script( $file->handle, $file->url, $previous_js_basename, $file->version, true);
$ajax_url = admin_url( 'admin-ajax.php' ); $ajax_url = admin_url( 'admin-ajax.php' );
@@ -25,6 +34,7 @@ function add_ajax_post() {
/* /*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@param string : name of the file, with its path from its extension directory @param string : name of the file, with its path from its extension directory
@@ -121,12 +131,10 @@ function add_var_to_front($vars, $handle = null) {
extract($vars); extract($vars);
foreach ($vars as $key => $var) foreach ($vars as $key => $var)
{ {
$js_var = 'const ' . $key . ' = '; $js_var = 'let '.$key.' = '.json_encode($var).';';
$js_var .= json_encode($var);
$js_var .= ';';
wp_add_inline_script($handle, $js_var, 'before'); wp_add_inline_script($handle, $js_var, 'before');
} }
//
// the other way with localize has multiple incidences : // the other way with localize has multiple incidences :
// - it creates an object from wich you can access the variables // - 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 // - so if you call it again wiht the same name, it will overwrite the previous

View File

@@ -70,10 +70,6 @@ function main_shortcode() {
) )
); );
global $wp_scripts;
console_log("wp_scripts->queue: ");
console_log($wp_scripts->queue);
return $html_front; return $html_front;
} }
add_shortcode('wp_model_plugin', 'main_shortcode'); add_shortcode('wp_model_plugin', 'main_shortcode');