diff --git a/plugins/fipfcard_plugin/fipfcard_plugin.php b/plugins/fipfcard_plugin/fipfcard_plugin.php index 63a32e0..7c66fac 100644 --- a/plugins/fipfcard_plugin/fipfcard_plugin.php +++ b/plugins/fipfcard_plugin/fipfcard_plugin.php @@ -8,6 +8,20 @@ Version: 1.1.0 Author URI: */ +/* +5.5 . 5.48 +59 . 58.99 +50.6 . 50.58 +38.5 . 38.53 +99.8 . 99.84 +25.2 . 25.17 +131.6 . 135 +63.7 . 63.68 +85 . 85.05 +50.2 . 50.17 +41.4 . 41.43 +18.8 . 18.82 +*/ @@ -18,8 +32,8 @@ plugin dir root define( 'FIPFCARD_PLUGIN_DIR', plugin_dir_path(__FILE__) ); define( 'FIPFCARD_PLUGIN_URL', plugin_dir_url(__FILE__) ); - - +include_once( plugin_dir_path(__FILE__) . '/php/utils/plugin_tools.php'); +PLGNTOOLS::set_root_dir( plugin_dir_path(__FILE__), plugin_dir_url(__FILE__) ); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * @@ -41,34 +55,63 @@ plugin shortcode */ function fipfcard_main_shortcode() { - fipfcard_add_files_to_front( array( - "example_style.css", - "example_init.js", - "example_script2.js", - "example_script3.js", - )); +$fipfcard_tools = new PLGNTOOLS(); +$test_path_plgntools = $fipfcard_tools->get_path(); +$test_url_plgntools = $fipfcard_tools->get_url(); +error_log("-------------"); +error_log($test_path_plgntools); +error_log($test_url_plgntools); +// fipfcard_add_files_to_front( array( +// "example_style.css", +// "example_init.js", +// "example_script2.js", +// "example_script3.js", +// )); + +$fipfcard_tools->add_files_to_front( array( + "example_style.css", + "example_init.js", + "example_script2.js", + "example_script3.js", +)); $myvar_1 = "I am one"; $myvar_2 = "I am two"; - fipfcard_add_var_to_front( compact( - "myvar_1", - "myvar_2", - )); +// fipfcard_add_var_to_front( compact( +// "myvar_1", +// "myvar_2", +// )); + +$fipfcard_tools->add_var_to_front( compact( + "myvar_1", + "myvar_2", +)); $names = ["hugo", "camille"]; $ages = ["13", "34", "56"]; - $html_front = fipfcard_create_html( - array( - "example_index.html", - "example_index2.html", - ), - compact( - "names", - "ages", - ) - ); +// $html_front = fipfcard_create_html( +// array( +// "example_index.html", +// "example_index2.html", +// ), +// compact( +// "names", +// "ages", +// ) +// ); + +$html_front = $fipfcard_tools->create_html( + array( + "example_index.html", + "example_index2.html", + ), + compact( + "names", + "ages", + ) +); return $html_front; } diff --git a/plugins/fipfcard_plugin/js/utils/ajax.js b/plugins/fipfcard_plugin/js/utils/ajax.js index c457db1..30d4ecd 100644 --- a/plugins/fipfcard_plugin/js/utils/ajax.js +++ b/plugins/fipfcard_plugin/js/utils/ajax.js @@ -6,10 +6,10 @@ it can be "overloaded" with a callback_response and _error function ajax_post(ajax_data, action, callback_response, callback_error) { const data = new FormData(); data.append('action', action); - data.append('_ajax_nonce', wp_ajax._nonce); + data.append('_ajax_nonce', fipfcard_ajax._nonce); data.append('data', ajax_data); - fetch(wp_ajax._url, { + fetch(fipfcard_ajax._url, { method: 'POST', credentials: 'same-origin', body: data diff --git a/plugins/fipfcard_plugin/php/utils/add_to_front.php b/plugins/fipfcard_plugin/php/utils/add_to_front.php index 562be07..61c6a13 100644 --- a/plugins/fipfcard_plugin/php/utils/add_to_front.php +++ b/plugins/fipfcard_plugin/php/utils/add_to_front.php @@ -34,7 +34,7 @@ function fipfcard_add_ajax_post() { $_nonce = wp_create_nonce( 'wp-pageviews-nonce' ); $vars = compact("_url","_nonce",); // add_var_to_front($vars); - $object_name = "wp_ajax"; + $object_name = "fipfcard_ajax"; wp_localize_script($file->handle, $object_name, $vars); } diff --git a/plugins/fipfcard_plugin/php/utils/plugin_tools.php b/plugins/fipfcard_plugin/php/utils/plugin_tools.php new file mode 100644 index 0000000..82e65c7 --- /dev/null +++ b/plugins/fipfcard_plugin/php/utils/plugin_tools.php @@ -0,0 +1,221 @@ +handle, $wp_scripts->queue); +// // if ($already_enqueued !== false) +// // return ; +// +// $fipfcard_first_script = $file->handle; +// wp_enqueue_script( $file->handle, $file->url, '', $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 = "fipfcard_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 + */ + private 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 = $this->get_url().$dir_path.$file_name; + $file->path = $this->get_path().$dir_path.$file_name; + $file->version = date("ymd-Gis", filemtime($file->path)); + + error_log(json_encode($file)); + + 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 + */ + public function add_files_to_front($files_arr, $add_ajax = true) { + //wp_enqueue_script(, /url/to/file, [depends on], version, defer? ); + //wp_enqueue_style( , /url/to/file, [depends on], version, media ); + +// if ($add_ajax === true) +// fipfcard_add_ajax_post(); + + $previous_css_basename = ''; + $previous_js_basename = ''; + foreach ($files_arr as $file_name) { + $file = $this->init_file($file_name); + if ($file->ext === "js") { + if (is_null($this->$_first_script)) + $this->$_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 + */ + public function add_var_to_front($vars, $handle = null) { + if (is_null($handle)) { + $handle = $this->$_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); + // } + } + + /** + * @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 = $this->get_path().'html/'; + if (!is_null($vars)) + extract($vars); + + ob_start(); + foreach($files as $file) { + include($html_dir.$file); + } + $html = ob_get_clean(); + + return $html; + } + +} + + +?> diff --git a/private b/private index bda9f70..a3f485f 160000 --- a/private +++ b/private @@ -1 +1 @@ -Subproject commit bda9f7020cebf44af4767e8e9ef64155da548994 +Subproject commit a3f485f995fb1233b38253834d253d8938298830 diff --git a/wordpress_docker b/wordpress_docker index c65e1fd..c809edb 160000 --- a/wordpress_docker +++ b/wordpress_docker @@ -1 +1 @@ -Subproject commit c65e1fd03995c05b4b1e6c4f467cd591b2c25cbe +Subproject commit c809edb4f83b5a2389c449b005a7b39b568fb915