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

@@ -15,6 +15,7 @@ Author URI:
include_once(dirname(__FILE__) . '/utils/console_log.php');
include_once(dirname(__FILE__) . '/utils/add_to_front.php');
include_once(dirname(__FILE__) . '/utils/create_html.php');
@@ -31,12 +32,13 @@ function main_shortcode() {
));
# send scripts files by name, without extension .js
add_script_to_front( array(
add_scripts_to_front( array(
"myscript",
"myscript2",
"myscript3",
));
$myvar_1 = "I am one";
$myvar_2 = "I am two";
# maybe a future version of php will allow to
@@ -44,18 +46,32 @@ function main_shortcode() {
# https://wiki.php.net/rfc/nameof
# but for now i have to declare both the variable and its name
# (more : https://stackoverflow.com/q/255312/9497573)
# add_var_to_front( array(
# "myvar_1" => $myvar_1,
# "myvar_2" => $myvar_2,
# ));
add_var_to_front( array(
$myvar_1,
$myvar_2,
"myvar_1" => $myvar_1,
"myvar_2" => $myvar_2,
));
return create_html( array(
"index.html",
"index2.html",
));
}
add_shortcode('fipf_wp_plugin', 'main_shortcode');
?>
/* * * * * * * * * * * * * * * * * * * * * * * *
* menu plugin
*/
function plugin_menu() {
add_menu_page(
'FIPF wp plugin', // webpage title
'FIPF', // menu title
'manage_options', // capability
'fipf-wp-plugin', // menu_slug
'fipf_wp_plugin_content' // callback function to display page content
);
}
add_action('admin_menu', 'plugin_menu');
?>

View File

@@ -0,0 +1,6 @@
<p>i am a new p</p>
<?php
foreach($names as $name) {
include($template_dir."presentation.html");
}
?>

View File

@@ -0,0 +1 @@
<p>aaaaaand this is the end</p>

View File

@@ -0,0 +1 @@
<p>hello <?php echo esc_html($name); ?></p>

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;
}
?>