improved plugin class by changing for only one method to do all

This commit is contained in:
asus
2024-02-15 00:40:07 +01:00
parent 8d0122f63e
commit aaca16525b
7 changed files with 84 additions and 78 deletions

View File

@@ -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');

View File

@@ -3,7 +3,7 @@
<p>list of post meta :</p>
<?php
foreach($post_meta as $meta_key => $meta_value) {
include($html_dir."templates/print_meta.html");
include($plgn_dir."html/templates/print_meta.html");
}
?>
</ol>
@@ -11,7 +11,7 @@
<p>list of user meta :</p>
<?php
foreach($user_meta as $meta_key => $meta_value) {
include($html_dir."templates/print_meta.html");
include($plgn_dir."html/templates/print_meta.html");
}
?>
</ol>
@@ -21,6 +21,6 @@
<button id='test_ajax_1' name="ajax_button_1" value="2024">2024</button>
<?php
foreach($names as $name) {
include($html_dir."templates/example_presentation.html");
include($plgn_dir."html/templates/example_presentation.html");
}
?>

View File

@@ -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', () => {

View File

@@ -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;

View File

@@ -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);
});
});

View File

@@ -1,13 +1,12 @@
<?php
function fipfcard_plugin_content() {
$fipfcard_tools = new PLGNTOOLS();
$fipfcard_tools = new PLGNTLS_class();
$fipfcard_tools->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");
}
?>

View File

@@ -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();
?>
<script type="text/javascript">
function ajax_post(ajax_data, action) {
function PLGNTLS_ajax(ajax_data, action) {
const _ajax_nonce = "<?php echo wp_create_nonce( 'wp-pageviews-nonce' ); ?>";
const _ajax_url = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
const data = new FormData();
@@ -54,19 +64,10 @@ class PLGNTOOLS
data.append('_ajax_nonce', _ajax_nonce);
data.append('data', ajax_data);
fetch(_ajax_url, {
return fetch(_ajax_url, {
method: 'POST',
credentials: 'same-origin',
body: data
})
.then((response) => response.json())
.then((data) => {
console.log("data: ");
console.log(data);
})
.catch((error) => {
console.log("error: ");
console.log(error);
});
};
</script>
@@ -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(<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_name) {
$file = $this->init_file($file_name);
foreach ($files_arr as $file) {
if ($file->ext === "js") {
if (is_null($this->_first_script))
$this->_first_script = $file->handle;
@@ -183,27 +186,24 @@ class PLGNTOOLS
* 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);
// }
private function add_vars_to_front($var_array) {
if (is_null($this->_first_script))
return ;
$handle = $this->_first_script;
$object_name = $this->_object_data;
wp_localize_script($handle, $object_name, $var_array);
}
public function add_to_front($files_arr = null, $vars = null) {
$files = array();
foreach($files_arr as $file_name) {
$files[] = $this->init_file($file_name);
}
if (!is_null($files_arr))
$this->add_files_to_front($files);
if (!is_null($vars))
$this->add_vars_to_front($vars);
return $this->create_html($files, $vars);
}
/**
@@ -226,21 +226,24 @@ class PLGNTOOLS
*
* https://stackoverflow.com/a/4402045/9497573
*/
function create_html($files, $vars = null) {
$files = (array)$files;
$html_dir = $this->get_path().'html/';
private function create_html($files = null, $vars = null) {
if (is_null($files))
return null;
$plgn_dir = $this->get_path();
if (!is_null($vars))
extract($vars);
ob_start();
foreach($files as $file) {
include($html_dir.$file);
error_log("file in create_html_tmp");
error_log(json_encode($file));
if ($file->ext === 'html')
include($file->path);
}
$html = ob_get_clean();
return $html;
}
}