diff --git a/plugins/wp_model_plugin/js/init.js b/plugins/wp_model_plugin/js/init.js index 39a5ca9..f12f550 100644 --- a/plugins/wp_model_plugin/js/init.js +++ b/plugins/wp_model_plugin/js/init.js @@ -3,8 +3,6 @@ const title = document.querySelector(".first_el_to_change"); title.innerHTML = "--- coucou ;) " + myvar_1; const ajax_button_1 = document.querySelector("#test_ajax_1"); -console.log("ajax_button_1: "); -console.log(ajax_button_1); ajax_button_1.addEventListener('click', () => { ajax_post(ajax_button_1, 'get_data'); }); diff --git a/plugins/wp_model_plugin/js/myscript3.js b/plugins/wp_model_plugin/js/myscript3.js index 7bee422..b1abd92 100644 --- a/plugins/wp_model_plugin/js/myscript3.js +++ b/plugins/wp_model_plugin/js/myscript3.js @@ -1,4 +1,3 @@ const title3 = document.querySelector(".third_el_to_change"); title3.innerHTML = "--- bye bye, " + myvar_2; -console.log(myvar_2); diff --git a/plugins/wp_model_plugin/js/ajax.js b/plugins/wp_model_plugin/js/utils/ajax.js similarity index 100% rename from plugins/wp_model_plugin/js/ajax.js rename to plugins/wp_model_plugin/js/utils/ajax.js diff --git a/plugins/wp_model_plugin/php/utils/add_to_front.php b/plugins/wp_model_plugin/php/utils/add_to_front.php index 5b3f84b..28f86dd 100644 --- a/plugins/wp_model_plugin/php/utils/add_to_front.php +++ b/plugins/wp_model_plugin/php/utils/add_to_front.php @@ -1,76 +1,122 @@ , /url/to/file, [depends on], version, defer? ); - //wp_enqueue_style( , /url/to/file, [depends on], version, media ); +function add_ajax_post() { global $first_script; + global $ajax_file; - // always adding ajax file first - $files_arr = ["ajax.js", ...$files_arr]; - - $previous_css_basename = ''; - $previous_js_basename = ''; - foreach ($files_arr as $file) { - $file_ext = pathinfo($file, PATHINFO_EXTENSION); - $file_basename = pathinfo($file, PATHINFO_FILENAME); - if ($file_ext === "js") - $dir_path = 'js/'; - else if ($file_ext === "css") - $dir_path = 'css/'; - else - continue; - - $file_url = PLUGIN_URL.$dir_path.$file; - $file_path = PLUGIN_DIR.$dir_path.$file; - $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; - } - else if ($file_ext === "css") { - wp_enqueue_style( $file, $file_url, $previous_css_basename, $file_version, ''); - $previous_css_basename = $file_basename; - } - } - + $file = init_file($ajax_file); + $first_script = $file->handle; + wp_enqueue_script( $file->handle, $file->url, $previous_js_basename, $file->version, true); $ajax_url = admin_url( 'admin-ajax.php' ); $nonce = wp_create_nonce( 'wp-pageviews-nonce' ); add_var_to_front( compact( "ajax_url", "nonce", - ), - "ajax" + ) ); } +/* + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +@param string : name of the file, with its path from its extension directory + - from js/ root for .js files + - from css/ root for .css files +@return object / null : + - null if file is not js or css + - or an object with all the necessary infos : + - ext : name.js -> "js" + - basename : name.js -> "name" + - handle : name.js -> "name_js" + - url : url to file in wordpress + - path : path to file in server + - version : used to avoid browser caching +*/ +function init_file($file_name) { + $file = (object)[]; + + $file->ext = pathinfo($file_name, PATHINFO_EXTENSION); + if ($file->ext === "js") + $dir_path = 'js/'; + else if ($file->ext === "css") + $dir_path = 'css/'; + else + return null; + + $file->basename = pathinfo($file_name, PATHINFO_FILENAME); + $file->handle = str_replace(".", "_", $file_name); + + $file->url = PLUGIN_URL.$dir_path.$file_name; + $file->path = PLUGIN_DIR.$dir_path.$file_name; + $file->version = date("ymd-Gis", filemtime($file->path)); + + return $file; +} + + + +/* + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +@param array : list of files : + - with their path from root of their type of file (ex: from js/ to .js files) + - and with their extension +@param boolean + - to add ajax script and variables + - default to true +*/ +function add_files_to_front($files_arr, $add_ajax = true) { + //wp_enqueue_script(, /url/to/file, [depends on], version, defer? ); + //wp_enqueue_style( , /url/to/file, [depends on], version, media ); + global $first_script; + + if ($add_ajax === true) + add_ajax_post($file); + + $previous_css_basename = ''; + $previous_js_basename = ''; + foreach ($files_arr as $file_name) { + $file = init_file($file_name); + if ($file->ext === "js") { + if (is_null($first_script)) + $first_script = $file->handle; + wp_enqueue_script( $file->handle, $file->url, $previous_js_basename, $file->version, true); + $previous_js_basename = $file->basename; + } + else if ($file->ext === "css") { + wp_enqueue_style( $file->handle, $file->url, $previous_css_basename, $file->version, ''); + $previous_css_basename = $file->basename; + } + } +} + + + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * pass variables to js front as global variables -@param two params : -1. an array of key => value +@param array : list 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. (optionnal) name of first embended script that need these variables +@param string (optionnal) : name of first embended script that need these variables (it will be available to this script and all followings) + this name is the filename + "_" + extension : + init.js -> init_js */ function add_var_to_front($vars, $handle = null) { if (is_null($handle)) { global $first_script; $handle = $first_script; } - $handle = pathinfo($handle, PATHINFO_FILENAME); extract($vars); foreach ($vars as $key => $var) diff --git a/plugins/wp_model_plugin/php/utils/globals.php b/plugins/wp_model_plugin/php/utils/globals.php index c6cdcb8..1d38805 100644 --- a/plugins/wp_model_plugin/php/utils/globals.php +++ b/plugins/wp_model_plugin/php/utils/globals.php @@ -11,9 +11,12 @@ const CONSOLE_OFF = true; */ const CONSOLE_OFF = false; -/* switch console_log -const CONSOLE_OFF = true; +/* a variable that will contain the name of the first script enqueued */ $first_script = null; +/* path to ajax.js file, from root of js dir +*/ +$ajax_file = "utils/ajax.js"; + ?> diff --git a/plugins/wp_model_plugin/plugin_hooks.php b/plugins/wp_model_plugin/plugin_hooks.php index d2ec1bc..b787f1b 100644 --- a/plugins/wp_model_plugin/plugin_hooks.php +++ b/plugins/wp_model_plugin/plugin_hooks.php @@ -71,8 +71,8 @@ function main_shortcode() { ); global $wp_scripts; - console_log("wp_scripts: "); - console_log($wp_scripts); + console_log("wp_scripts->queue: "); + console_log($wp_scripts->queue); return $html_front; }