wip renaming plugins elements
This commit is contained in:
15
plugins/fipfcard_plugin/php/menu/example_menu.php
Normal file
15
plugins/fipfcard_plugin/php/menu/example_menu.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
function wp_model_plugin_content() {
|
||||
global $first_script;
|
||||
console_log("in menu : first_script:");
|
||||
console_log($first_script);
|
||||
|
||||
add_files_to_front( array(
|
||||
"menu/example_menu.js",
|
||||
));
|
||||
|
||||
echo create_html("menu/example_menu.html");
|
||||
}
|
||||
|
||||
?>
|
||||
156
plugins/fipfcard_plugin/php/utils/add_to_front.php
Normal file
156
plugins/fipfcard_plugin/php/utils/add_to_front.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
function that add the ajax script to front
|
||||
no real needs to check if already included :
|
||||
- $handle is uniq so it will not be re-enqueued
|
||||
the first enqueued version would be kept
|
||||
- if we used add_var_to_front() (which use wp_add_inline_script())
|
||||
the vars would be added twice
|
||||
leading to js syntaxe error (redeclaraiton of 'let' or 'const')
|
||||
- but we use wp_localize_script() so the object will be overwritten
|
||||
it's not a real pbm
|
||||
(what is more efficient, check for double or overwritte object ?)
|
||||
*/
|
||||
function add_ajax_post() {
|
||||
global $first_script;
|
||||
global $ajax_file;
|
||||
|
||||
$file = init_file($ajax_file);
|
||||
|
||||
// // check if ajax script was already enqueued
|
||||
// global $wp_scripts;
|
||||
// $already_enqueued = array_search($file->handle, $wp_scripts->queue);
|
||||
// if ($already_enqueued !== false)
|
||||
// return ;
|
||||
|
||||
$first_script = $file->handle;
|
||||
wp_enqueue_script( $file->handle, $file->url, $previous_js_basename, $file->version, true);
|
||||
|
||||
$_url = admin_url( 'admin-ajax.php' );
|
||||
$_nonce = wp_create_nonce( 'wp-pageviews-nonce' );
|
||||
$vars = compact("_url","_nonce",);
|
||||
// add_var_to_front($vars);
|
||||
$object_name = "wp_ajax";
|
||||
wp_localize_script($file->handle, $object_name, $vars);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
@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 = FIPFCARD_PLUGIN_URL.$dir_path.$file_name;
|
||||
$file->path = FIPFCARD_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 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"));
|
||||
@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;
|
||||
}
|
||||
|
||||
extract($vars);
|
||||
foreach ($vars as $key => $var)
|
||||
{
|
||||
$js_var = 'let '.$key.' = '.json_encode($var).';';
|
||||
wp_add_inline_script($handle, $js_var, 'before');
|
||||
}
|
||||
|
||||
// the other way with localize has multiple incidences :
|
||||
// - it creates an object from wich you can access the variables
|
||||
// - so if you call it again wiht the same name, it will overwrite the previous
|
||||
// {
|
||||
// $handle = pathinfo($handle, PATHINFO_FILENAME);
|
||||
// $object_name = "php_data";
|
||||
//
|
||||
// wp_localize_script($handle, $object_name, $vars);
|
||||
// }
|
||||
}
|
||||
|
||||
?>
|
||||
16
plugins/fipfcard_plugin/php/utils/console_log.php
Normal file
16
plugins/fipfcard_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;
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
39
plugins/fipfcard_plugin/php/utils/create_html.php
Normal file
39
plugins/fipfcard_plugin/php/utils/create_html.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?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 = null) {
|
||||
$files = (array)$files;
|
||||
$html_dir = FIPFCARD_PLUGIN_DIR.'html/';
|
||||
if (!is_null($vars))
|
||||
extract($vars);
|
||||
|
||||
ob_start();
|
||||
foreach($files as $file) {
|
||||
include($html_dir.$file);
|
||||
}
|
||||
$html = ob_get_clean();
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
?>
|
||||
22
plugins/fipfcard_plugin/php/utils/globals.php
Normal file
22
plugins/fipfcard_plugin/php/utils/globals.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?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;
|
||||
|
||||
/* 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";
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user