diff --git a/plugins/fipfcard_plugin/fipfcard_plugin.php b/plugins/fipfcard_plugin/fipfcard_plugin.php index 8ce9c66..e0c63f2 100644 --- a/plugins/fipfcard_plugin/fipfcard_plugin.php +++ b/plugins/fipfcard_plugin/fipfcard_plugin.php @@ -33,7 +33,7 @@ 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__) ); +PLGNTLS_class::set_root_dir( plugin_dir_path(__FILE__), plugin_dir_url(__FILE__) ); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * @@ -55,25 +55,12 @@ plugin shortcode */ function fipfcard_main_shortcode() { - $fipfcard_tools = new PLGNTOOLS(); + $fipfcard_tools = new PLGNTLS_class(); - $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_tools->add_var_to_front( compact( - "myvar_1", - "myvar_2", - )); - - $names = ["hugo", "camille"]; $ages = ["13", "34", "56"]; @@ -84,12 +71,19 @@ function fipfcard_main_shortcode() { $getUserMeta = get_metadata( 'user', get_current_user_id() ); $user_meta = $getUserMeta; - $html_front = $fipfcard_tools->create_html( + + return $fipfcard_tools->add_to_front( array( + "example_style.css", + "example_init.js", + "example_script2.js", + "example_script3.js", "example_index.html", "example_index2.html", ), compact( + "myvar_1", + "myvar_2", "post_meta", "user_meta", "names", @@ -97,7 +91,6 @@ function fipfcard_main_shortcode() { ) ); - return $html_front; } add_shortcode('fipfcard_plugin', 'fipfcard_main_shortcode'); diff --git a/plugins/fipfcard_plugin/html/example_index.html b/plugins/fipfcard_plugin/html/example_index.html index 4af6dda..8ac055b 100644 --- a/plugins/fipfcard_plugin/html/example_index.html +++ b/plugins/fipfcard_plugin/html/example_index.html @@ -3,7 +3,7 @@
list of post meta :
$meta_value) { - include($html_dir."templates/print_meta.html"); + include($plgn_dir."html/templates/print_meta.html"); } ?> @@ -11,7 +11,7 @@list of user meta :
$meta_value) { - include($html_dir."templates/print_meta.html"); + include($plgn_dir."html/templates/print_meta.html"); } ?> @@ -21,6 +21,6 @@ diff --git a/plugins/fipfcard_plugin/js/example_init.js b/plugins/fipfcard_plugin/js/example_init.js index f12f550..a84223a 100644 --- a/plugins/fipfcard_plugin/js/example_init.js +++ b/plugins/fipfcard_plugin/js/example_init.js @@ -1,6 +1,8 @@ +console.log("PLGNTLS_data:"); +console.log(PLGNTLS_data); const title = document.querySelector(".first_el_to_change"); -title.innerHTML = "--- coucou ;) " + myvar_1; +title.innerHTML = "--- coucou ;) " + PLGNTLS_data.myvar_1; const ajax_button_1 = document.querySelector("#test_ajax_1"); ajax_button_1.addEventListener('click', () => { diff --git a/plugins/fipfcard_plugin/js/example_script3.js b/plugins/fipfcard_plugin/js/example_script3.js index b1abd92..a463e1f 100644 --- a/plugins/fipfcard_plugin/js/example_script3.js +++ b/plugins/fipfcard_plugin/js/example_script3.js @@ -1,3 +1,3 @@ const title3 = document.querySelector(".third_el_to_change"); -title3.innerHTML = "--- bye bye, " + myvar_2; +title3.innerHTML = "--- bye bye, " + PLGNTLS_data.myvar_2; diff --git a/plugins/fipfcard_plugin/js/menu/example_menu.js b/plugins/fipfcard_plugin/js/menu/example_menu.js index afa2176..d02b6dc 100644 --- a/plugins/fipfcard_plugin/js/menu/example_menu.js +++ b/plugins/fipfcard_plugin/js/menu/example_menu.js @@ -5,5 +5,14 @@ sendButton.addEventListener('click', () => { const inputValue = inputElement.value; console.log("inputValue:"); console.log(inputValue); - ajax_post(inputValue, 'get_data'); + PLGNTLS_ajax(inputValue, 'get_data') + .then((response) => response.json()) + .then((data) => { + console.log("dataaa: "); + console.log(data); + }) + .catch((error) => { + console.log("error: "); + console.log(error); + }); }); diff --git a/plugins/fipfcard_plugin/php/menu/example_menu.php b/plugins/fipfcard_plugin/php/menu/example_menu.php index 37ba2dd..101ff02 100644 --- a/plugins/fipfcard_plugin/php/menu/example_menu.php +++ b/plugins/fipfcard_plugin/php/menu/example_menu.php @@ -1,13 +1,12 @@ add_files_to_front( array( + echo $fipfcard_tools->add_to_front( array( "menu/example_menu.js", + "menu/example_menu.html", )); - - echo $fipfcard_tools->create_html("menu/example_menu.html"); } ?> diff --git a/plugins/fipfcard_plugin/php/utils/plugin_tools.php b/plugins/fipfcard_plugin/php/utils/plugin_tools.php index aef932f..1a9d821 100644 --- a/plugins/fipfcard_plugin/php/utils/plugin_tools.php +++ b/plugins/fipfcard_plugin/php/utils/plugin_tools.php @@ -4,24 +4,36 @@ * include those two lines at the top of the main plugin file * * include_once( plugin_dir_path(__FILE__) . '/php/utils/plugin_tools.php'); - * PLGNTOOLS::set_root_dir( plugin_dir_path(__FILE__), plugin_dir_url(__FILE__) ); + * PLGNTLS_class::set_root_dir( plugin_dir_path(__FILE__), plugin_dir_url(__FILE__) ); + * + * PLGNTLS means PLUGIN TOOLS */ -class PLGNTOOLS +class PLGNTLS_class { - // static properties to hold the plugin dir path and url - public static $root_path; - public static $root_url; + private static $root_path; + private static $root_url; + private static $_ajax_already_there; - private $_first_script; + private $_object_data; + /** + * init _ajax_already_there + * _first_script + * _object_data + */ public function __construct() { - if (isset( self::$_ajax_already_there )) - return ; - self::$_ajax_already_there = false; + if (! isset( self::$_ajax_already_there )) + self::$_ajax_already_there = false; + $this->_object_data = "PLGNTLS_data"; + $this->_first_script = null; } + /** + * can be used before class is instanciated : + * PLGNTLS_class::set_root_dir( plugin_dir_path(__FILE__), plugin_dir_url(__FILE__) ); + */ public static function set_root_dir($path, $url) { if (isset( self::$root_path )) return ; @@ -30,7 +42,6 @@ class PLGNTOOLS self::$root_path = $path; self::$root_url = $url; } - public function get_path() { return(self::$root_path); } @@ -39,14 +50,13 @@ class PLGNTOOLS } /** - * js function that create an ajax post action - * it can be "overloaded" with a callback_response and _error + * js function that creates an ajax post action */ public function get_ajax_script() { ob_start(); ?> @@ -92,6 +93,7 @@ class PLGNTOOLS return ; if (self::$_ajax_already_there) return ; + self::$_ajax_already_there = true; $ajax_script = $this->get_ajax_script(); wp_add_inline_script($this->_first_script, $ajax_script, 'before'); @@ -122,6 +124,8 @@ class PLGNTOOLS $dir_path = 'js/'; else if ($file->ext === "css") $dir_path = 'css/'; + else if ($file->ext === "html") + $dir_path = 'html/'; else return null; @@ -145,14 +149,13 @@ class PLGNTOOLS * - to add ajax script and variables * - default to true */ - public function add_files_to_front($files_arr, $add_ajax = true) { + private function add_files_to_front($files_arr, $add_ajax = true) { //wp_enqueue_script(