- added const dir path and url
- better file organisation
This commit is contained in:
58
plugins/wp_model_plugin/php/utils/add_to_front.php
Normal file
58
plugins/wp_model_plugin/php/utils/add_to_front.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
@param an array of files
|
||||
*/
|
||||
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 );
|
||||
|
||||
$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") {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
@param expect an array 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"));
|
||||
*/
|
||||
function add_var_to_front($var_array) {
|
||||
extract($var_array);
|
||||
|
||||
foreach ($var_array as $key => $var)
|
||||
{
|
||||
$js_var = 'const ' . $key . ' = ';
|
||||
$js_var .= json_encode($var);
|
||||
$js_var .= ';';
|
||||
wp_add_inline_script('myscript', $js_var, 'before');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
16
plugins/wp_model_plugin/php/utils/console_log.php
Normal file
16
plugins/wp_model_plugin/php/utils/console_log.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
https://stackify.com/how-to-log-to-console-in-php/
|
||||
*/
|
||||
function console_log($output) {
|
||||
if (CONSOLE_OFF)
|
||||
return;
|
||||
$json_output = json_encode($output, JSON_HEX_TAG);
|
||||
$js_code = '<script>console.log(' . $json_output . ');</script>';
|
||||
echo $js_code;
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
38
plugins/wp_model_plugin/php/utils/create_html.php
Normal file
38
plugins/wp_model_plugin/php/utils/create_html.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
@param two arguments :
|
||||
1. html files to include in front
|
||||
- can be a string of 1 filename
|
||||
- or an array of strings of filenames
|
||||
( https://stackoverflow.com/q/4747876/9497573 )
|
||||
- it's probably better to only add 1 file, and let it include other files
|
||||
2. list of variables to make available to this files
|
||||
- in the form of key => val
|
||||
- recommanded to do it with compact()
|
||||
ex: create_html( "file.html", compact("var1","var2",) );
|
||||
ex: create_html( array("file1.html", "file2.html"), array("var1"=>"value") );
|
||||
@return a string of html code
|
||||
|
||||
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, $vars) {
|
||||
$files = (array)$files;
|
||||
$html_dir = PLUGIN_DIR.'html/';
|
||||
extract($vars);
|
||||
|
||||
ob_start();
|
||||
foreach($files as $file) {
|
||||
include($html_dir.$file);
|
||||
}
|
||||
$html = ob_get_clean();
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
?>
|
||||
14
plugins/wp_model_plugin/php/utils/globals.php
Normal file
14
plugins/wp_model_plugin/php/utils/globals.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
globals variables
|
||||
const vs define : https://stackoverflow.com/questions/2447791/php-define-vs-const
|
||||
*/
|
||||
|
||||
/* switch console_log
|
||||
const CONSOLE_OFF = true;
|
||||
*/
|
||||
const CONSOLE_OFF = false;
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user