ajax script is automatically added when a script is added

This commit is contained in:
asus
2024-02-11 18:17:05 +01:00
parent 87a0f7fc0b
commit 322e440422
6 changed files with 94 additions and 48 deletions

View File

@@ -1,76 +1,122 @@
<?php
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@param an array of files
function that add the ajax script to front
*/
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 );
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(<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
@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)

View File

@@ -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";
?>