ajax script is automatically added when a script is added
This commit is contained in:
@@ -3,8 +3,6 @@ const title = document.querySelector(".first_el_to_change");
|
|||||||
title.innerHTML = "--- coucou ;) " + myvar_1;
|
title.innerHTML = "--- coucou ;) " + myvar_1;
|
||||||
|
|
||||||
const ajax_button_1 = document.querySelector("#test_ajax_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_button_1.addEventListener('click', () => {
|
||||||
ajax_post(ajax_button_1, 'get_data');
|
ajax_post(ajax_button_1, 'get_data');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
const title3 = document.querySelector(".third_el_to_change");
|
const title3 = document.querySelector(".third_el_to_change");
|
||||||
title3.innerHTML = "--- bye bye, " + myvar_2;
|
title3.innerHTML = "--- bye bye, " + myvar_2;
|
||||||
console.log(myvar_2);
|
|
||||||
|
|||||||
@@ -1,76 +1,122 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
@param an array of files
|
function that add the ajax script to front
|
||||||
*/
|
*/
|
||||||
function add_files_to_front($files_arr) {
|
function add_ajax_post() {
|
||||||
//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;
|
global $first_script;
|
||||||
|
global $ajax_file;
|
||||||
|
|
||||||
// always adding ajax file first
|
$file = init_file($ajax_file);
|
||||||
$files_arr = ["ajax.js", ...$files_arr];
|
$first_script = $file->handle;
|
||||||
|
wp_enqueue_script( $file->handle, $file->url, $previous_js_basename, $file->version, true);
|
||||||
$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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$ajax_url = admin_url( 'admin-ajax.php' );
|
$ajax_url = admin_url( 'admin-ajax.php' );
|
||||||
$nonce = wp_create_nonce( 'wp-pageviews-nonce' );
|
$nonce = wp_create_nonce( 'wp-pageviews-nonce' );
|
||||||
add_var_to_front(
|
add_var_to_front(
|
||||||
compact(
|
compact(
|
||||||
"ajax_url",
|
"ajax_url",
|
||||||
"nonce",
|
"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(<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;
|
||||||
|
|
||||||
|
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
|
pass variables to js front as global variables
|
||||||
@param two params :
|
@param array : list of key => value
|
||||||
1. an array of key => value
|
|
||||||
with the key being name of the variable, like this :
|
with the key being name of the variable, like this :
|
||||||
'my_var' => 'value',
|
'my_var' => 'value',
|
||||||
simpler way to do it is to use compact when calling the function :
|
simpler way to do it is to use compact when calling the function :
|
||||||
add_var_to_front(compact("var1", "var2", "var3"));
|
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)
|
(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) {
|
function add_var_to_front($vars, $handle = null) {
|
||||||
if (is_null($handle)) {
|
if (is_null($handle)) {
|
||||||
global $first_script;
|
global $first_script;
|
||||||
$handle = $first_script;
|
$handle = $first_script;
|
||||||
}
|
}
|
||||||
$handle = pathinfo($handle, PATHINFO_FILENAME);
|
|
||||||
|
|
||||||
extract($vars);
|
extract($vars);
|
||||||
foreach ($vars as $key => $var)
|
foreach ($vars as $key => $var)
|
||||||
|
|||||||
@@ -11,9 +11,12 @@ const CONSOLE_OFF = true;
|
|||||||
*/
|
*/
|
||||||
const CONSOLE_OFF = false;
|
const CONSOLE_OFF = false;
|
||||||
|
|
||||||
/* switch console_log
|
/* a variable that will contain the name of the first script enqueued
|
||||||
const CONSOLE_OFF = true;
|
|
||||||
*/
|
*/
|
||||||
$first_script = null;
|
$first_script = null;
|
||||||
|
|
||||||
|
/* path to ajax.js file, from root of js dir
|
||||||
|
*/
|
||||||
|
$ajax_file = "utils/ajax.js";
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -71,8 +71,8 @@ function main_shortcode() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
global $wp_scripts;
|
global $wp_scripts;
|
||||||
console_log("wp_scripts: ");
|
console_log("wp_scripts->queue: ");
|
||||||
console_log($wp_scripts);
|
console_log($wp_scripts->queue);
|
||||||
|
|
||||||
return $html_front;
|
return $html_front;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user