wip handle puting files and var on front

This commit is contained in:
asus
2024-02-08 12:11:16 +01:00
parent ae5fa7191a
commit da5bb42a62
6 changed files with 72 additions and 34 deletions

View File

@@ -2,54 +2,39 @@
function add_css_to_front($style_files_arr) {
//wp_enqueue_style( <give_it_a_name>, /url/to/file, [depends on], version, media );
$dir_path = 'styles/';
$file_ext = '.css';
$previous_file = '';
foreach ($style_files_arr as $file) {
$file_url = plugin_dir_url(__DIR__).'styles/'.$file.'.css';
$file_path = plugin_dir_path(__DIR__).'styles/'.$file.'.css';
$file_url = plugin_dir_url(__DIR__).$dir_path.$file.$file_ext;
$file_path = plugin_dir_path(__DIR__).$dir_path.$file.$file_ext;
$file_version = date("ymd-Gis", filemtime($file_path));
wp_enqueue_style( $file, $file_url, $previous_file, $file_version, '');
$previous_file = $file;
}
}
function add_script_to_front($script_files_arr) {
function add_scripts_to_front($script_files_arr) {
//wp_enqueue_script(<give_it_a_name>, /url/to/file, [depends on], version, defer? );
$dir_path = 'scripts/';
$file_ext = '.js';
$previous_file = '';
foreach ($script_files_arr as $file) {
$file_url = plugin_dir_url(__DIR__).'scripts/'.$file.'.js';
$file_path = plugin_dir_path(__DIR__).'scripts/'.$file.'.js';
$file_url = plugin_dir_url(__DIR__).$dir_path.$file.$file_ext;
$file_path = plugin_dir_path(__DIR__).$dir_path.$file.$file_ext;
$file_version = date("ymd-Gis", filemtime($file_path));
console_log("file_url: " . $file_url);
wp_enqueue_script( $file, $file_url, $previous_file, $file_version, true);
$previous_file = $file;
}
}
# https://stackoverflow.com/a/61442261/9497573
function get_varname(&$var) {
$tmp = $var; // store the variable value
$var = '_$_%&33xc$%^*7awdlawidawd__D:DWD2398q_r4'; // give the variable a new unique value
$name = array_search($var, $GLOBALS); // search $GLOBALS for that unique value and return the key(variable)
$var = $tmp; // restore the variable old value
console_log("name : " . $name);
return $name;
}
function add_var_to_front($var_array) {
# foreach ($var_array as $key => $var)
# {
# $js_var = 'const ' . $key . ' = ';
# $js_var .= json_encode($var);
# $js_var .= ';';
# console_log("in php, js_var: " . $js_var);
# wp_add_inline_script('myscript', $js_var, 'before');
# }
foreach ($var_array as $var)
foreach ($var_array as $key => $var)
{
$js_var = 'const ' . get_varname($var) . ' = ';
$js_var = 'const ' . $key . ' = ';
$js_var .= json_encode($var);
$js_var .= ';';
console_log("in php, js_var: " . $js_var);

View File

@@ -0,0 +1,29 @@
<?php
function template($file) {
$template_dir_path = plugin_dir_path(__DIR__).'html/templates/';
include($template_dir_path.$file);
}
# using ob_start() and ob_get_clean()
# allows to have php expansion inside the html loaded
# in opposition to the methode file_get_contents()
#
# https://stackoverflow.com/a/4402045/9497573
function create_html($files) {
$html_dir = plugin_dir_path(__DIR__).'html/';
$template_dir = plugin_dir_path(__DIR__).'html/templates/';
$names = ["hugo", "camille"];
ob_start();
foreach($files as $file) {
include($html_dir.$file);
}
$html = ob_get_clean();
return $html;
}
?>