improved plugin class by changing for only one method to do all
This commit is contained in:
@@ -33,7 +33,7 @@ define( 'FIPFCARD_PLUGIN_DIR', plugin_dir_path(__FILE__) );
|
|||||||
define( 'FIPFCARD_PLUGIN_URL', plugin_dir_url(__FILE__) );
|
define( 'FIPFCARD_PLUGIN_URL', plugin_dir_url(__FILE__) );
|
||||||
|
|
||||||
include_once( plugin_dir_path(__FILE__) . '/php/utils/plugin_tools.php');
|
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() {
|
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_1 = "I am one";
|
||||||
$myvar_2 = "I am two";
|
$myvar_2 = "I am two";
|
||||||
|
|
||||||
$fipfcard_tools->add_var_to_front( compact(
|
|
||||||
"myvar_1",
|
|
||||||
"myvar_2",
|
|
||||||
));
|
|
||||||
|
|
||||||
|
|
||||||
$names = ["hugo", "camille"];
|
$names = ["hugo", "camille"];
|
||||||
$ages = ["13", "34", "56"];
|
$ages = ["13", "34", "56"];
|
||||||
|
|
||||||
@@ -84,12 +71,19 @@ function fipfcard_main_shortcode() {
|
|||||||
$getUserMeta = get_metadata( 'user', get_current_user_id() );
|
$getUserMeta = get_metadata( 'user', get_current_user_id() );
|
||||||
$user_meta = $getUserMeta;
|
$user_meta = $getUserMeta;
|
||||||
|
|
||||||
$html_front = $fipfcard_tools->create_html(
|
|
||||||
|
return $fipfcard_tools->add_to_front(
|
||||||
array(
|
array(
|
||||||
|
"example_style.css",
|
||||||
|
"example_init.js",
|
||||||
|
"example_script2.js",
|
||||||
|
"example_script3.js",
|
||||||
"example_index.html",
|
"example_index.html",
|
||||||
"example_index2.html",
|
"example_index2.html",
|
||||||
),
|
),
|
||||||
compact(
|
compact(
|
||||||
|
"myvar_1",
|
||||||
|
"myvar_2",
|
||||||
"post_meta",
|
"post_meta",
|
||||||
"user_meta",
|
"user_meta",
|
||||||
"names",
|
"names",
|
||||||
@@ -97,7 +91,6 @@ function fipfcard_main_shortcode() {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
return $html_front;
|
|
||||||
}
|
}
|
||||||
add_shortcode('fipfcard_plugin', 'fipfcard_main_shortcode');
|
add_shortcode('fipfcard_plugin', 'fipfcard_main_shortcode');
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<p>list of post meta :</p>
|
<p>list of post meta :</p>
|
||||||
<?php
|
<?php
|
||||||
foreach($post_meta as $meta_key => $meta_value) {
|
foreach($post_meta as $meta_key => $meta_value) {
|
||||||
include($html_dir."templates/print_meta.html");
|
include($plgn_dir."html/templates/print_meta.html");
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
</ol>
|
</ol>
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
<p>list of user meta :</p>
|
<p>list of user meta :</p>
|
||||||
<?php
|
<?php
|
||||||
foreach($user_meta as $meta_key => $meta_value) {
|
foreach($user_meta as $meta_key => $meta_value) {
|
||||||
include($html_dir."templates/print_meta.html");
|
include($plgn_dir."html/templates/print_meta.html");
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
</ol>
|
</ol>
|
||||||
@@ -21,6 +21,6 @@
|
|||||||
<button id='test_ajax_1' name="ajax_button_1" value="2024">2024</button>
|
<button id='test_ajax_1' name="ajax_button_1" value="2024">2024</button>
|
||||||
<?php
|
<?php
|
||||||
foreach($names as $name) {
|
foreach($names as $name) {
|
||||||
include($html_dir."templates/example_presentation.html");
|
include($plgn_dir."html/templates/example_presentation.html");
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
|
console.log("PLGNTLS_data:");
|
||||||
|
console.log(PLGNTLS_data);
|
||||||
|
|
||||||
const title = document.querySelector(".first_el_to_change");
|
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");
|
const ajax_button_1 = document.querySelector("#test_ajax_1");
|
||||||
ajax_button_1.addEventListener('click', () => {
|
ajax_button_1.addEventListener('click', () => {
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
|
|
||||||
const title3 = document.querySelector(".third_el_to_change");
|
const title3 = document.querySelector(".third_el_to_change");
|
||||||
title3.innerHTML = "--- bye bye, " + myvar_2;
|
title3.innerHTML = "--- bye bye, " + PLGNTLS_data.myvar_2;
|
||||||
|
|||||||
@@ -5,5 +5,14 @@ sendButton.addEventListener('click', () => {
|
|||||||
const inputValue = inputElement.value;
|
const inputValue = inputElement.value;
|
||||||
console.log("inputValue:");
|
console.log("inputValue:");
|
||||||
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
function fipfcard_plugin_content() {
|
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.js",
|
||||||
|
"menu/example_menu.html",
|
||||||
));
|
));
|
||||||
|
|
||||||
echo $fipfcard_tools->create_html("menu/example_menu.html");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -4,24 +4,36 @@
|
|||||||
* include those two lines at the top of the main plugin file
|
* include those two lines at the top of the main plugin file
|
||||||
*
|
*
|
||||||
* include_once( plugin_dir_path(__FILE__) . '/php/utils/plugin_tools.php');
|
* 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
|
private static $root_path;
|
||||||
public static $root_path;
|
private static $root_url;
|
||||||
public static $root_url;
|
|
||||||
private static $_ajax_already_there;
|
private static $_ajax_already_there;
|
||||||
|
|
||||||
private $_first_script;
|
private $_first_script;
|
||||||
|
private $_object_data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* init _ajax_already_there
|
||||||
|
* _first_script
|
||||||
|
* _object_data
|
||||||
|
*/
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
if (isset( self::$_ajax_already_there ))
|
if (! isset( self::$_ajax_already_there ))
|
||||||
return ;
|
|
||||||
self::$_ajax_already_there = false;
|
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) {
|
public static function set_root_dir($path, $url) {
|
||||||
if (isset( self::$root_path ))
|
if (isset( self::$root_path ))
|
||||||
return ;
|
return ;
|
||||||
@@ -30,7 +42,6 @@ class PLGNTOOLS
|
|||||||
self::$root_path = $path;
|
self::$root_path = $path;
|
||||||
self::$root_url = $url;
|
self::$root_url = $url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_path() {
|
public function get_path() {
|
||||||
return(self::$root_path);
|
return(self::$root_path);
|
||||||
}
|
}
|
||||||
@@ -39,14 +50,13 @@ class PLGNTOOLS
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* js function that create an ajax post action
|
* js function that creates an ajax post action
|
||||||
* it can be "overloaded" with a callback_response and _error
|
|
||||||
*/
|
*/
|
||||||
public function get_ajax_script() {
|
public function get_ajax_script() {
|
||||||
ob_start();
|
ob_start();
|
||||||
?>
|
?>
|
||||||
<script type="text/javascript">
|
<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_nonce = "<?php echo wp_create_nonce( 'wp-pageviews-nonce' ); ?>";
|
||||||
const _ajax_url = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
|
const _ajax_url = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
|
||||||
const data = new FormData();
|
const data = new FormData();
|
||||||
@@ -54,19 +64,10 @@ class PLGNTOOLS
|
|||||||
data.append('_ajax_nonce', _ajax_nonce);
|
data.append('_ajax_nonce', _ajax_nonce);
|
||||||
data.append('data', ajax_data);
|
data.append('data', ajax_data);
|
||||||
|
|
||||||
fetch(_ajax_url, {
|
return fetch(_ajax_url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
credentials: 'same-origin',
|
credentials: 'same-origin',
|
||||||
body: data
|
body: data
|
||||||
})
|
|
||||||
.then((response) => response.json())
|
|
||||||
.then((data) => {
|
|
||||||
console.log("data: ");
|
|
||||||
console.log(data);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.log("error: ");
|
|
||||||
console.log(error);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
@@ -92,6 +93,7 @@ class PLGNTOOLS
|
|||||||
return ;
|
return ;
|
||||||
if (self::$_ajax_already_there)
|
if (self::$_ajax_already_there)
|
||||||
return ;
|
return ;
|
||||||
|
self::$_ajax_already_there = true;
|
||||||
|
|
||||||
$ajax_script = $this->get_ajax_script();
|
$ajax_script = $this->get_ajax_script();
|
||||||
wp_add_inline_script($this->_first_script, $ajax_script, 'before');
|
wp_add_inline_script($this->_first_script, $ajax_script, 'before');
|
||||||
@@ -122,6 +124,8 @@ class PLGNTOOLS
|
|||||||
$dir_path = 'js/';
|
$dir_path = 'js/';
|
||||||
else if ($file->ext === "css")
|
else if ($file->ext === "css")
|
||||||
$dir_path = 'css/';
|
$dir_path = 'css/';
|
||||||
|
else if ($file->ext === "html")
|
||||||
|
$dir_path = 'html/';
|
||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
@@ -145,14 +149,13 @@ class PLGNTOOLS
|
|||||||
* - to add ajax script and variables
|
* - to add ajax script and variables
|
||||||
* - default to true
|
* - 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_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 );
|
//wp_enqueue_style( <give_it_a_name>, /url/to/file, [depends on], version, media );
|
||||||
|
|
||||||
$previous_css_basename = '';
|
$previous_css_basename = '';
|
||||||
$previous_js_basename = '';
|
$previous_js_basename = '';
|
||||||
foreach ($files_arr as $file_name) {
|
foreach ($files_arr as $file) {
|
||||||
$file = $this->init_file($file_name);
|
|
||||||
if ($file->ext === "js") {
|
if ($file->ext === "js") {
|
||||||
if (is_null($this->_first_script))
|
if (is_null($this->_first_script))
|
||||||
$this->_first_script = $file->handle;
|
$this->_first_script = $file->handle;
|
||||||
@@ -183,27 +186,24 @@ class PLGNTOOLS
|
|||||||
* this name is the filename + "_" + extension :
|
* this name is the filename + "_" + extension :
|
||||||
* init.js -> init_js
|
* init.js -> init_js
|
||||||
*/
|
*/
|
||||||
public function add_var_to_front($vars, $handle = null) {
|
|
||||||
if (is_null($handle)) {
|
private function add_vars_to_front($var_array) {
|
||||||
|
if (is_null($this->_first_script))
|
||||||
|
return ;
|
||||||
$handle = $this->_first_script;
|
$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) {
|
||||||
extract($vars);
|
$files = array();
|
||||||
foreach ($vars as $key => $var)
|
foreach($files_arr as $file_name) {
|
||||||
{
|
$files[] = $this->init_file($file_name);
|
||||||
$js_var = 'let '.$key.' = '.json_encode($var).';';
|
|
||||||
wp_add_inline_script($handle, $js_var, 'before');
|
|
||||||
}
|
}
|
||||||
|
if (!is_null($files_arr))
|
||||||
// the other way with localize has multiple incidences :
|
$this->add_files_to_front($files);
|
||||||
// - it creates an object from wich you can access the variables
|
if (!is_null($vars))
|
||||||
// - so if you call it again wiht the same name, it will overwrite the previous
|
$this->add_vars_to_front($vars);
|
||||||
// {
|
return $this->create_html($files, $vars);
|
||||||
// $handle = pathinfo($handle, PATHINFO_FILENAME);
|
|
||||||
// $object_name = "php_data";
|
|
||||||
//
|
|
||||||
// wp_localize_script($handle, $object_name, $vars);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -226,21 +226,24 @@ class PLGNTOOLS
|
|||||||
*
|
*
|
||||||
* https://stackoverflow.com/a/4402045/9497573
|
* https://stackoverflow.com/a/4402045/9497573
|
||||||
*/
|
*/
|
||||||
function create_html($files, $vars = null) {
|
private function create_html($files = null, $vars = null) {
|
||||||
$files = (array)$files;
|
if (is_null($files))
|
||||||
$html_dir = $this->get_path().'html/';
|
return null;
|
||||||
|
$plgn_dir = $this->get_path();
|
||||||
if (!is_null($vars))
|
if (!is_null($vars))
|
||||||
extract($vars);
|
extract($vars);
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
foreach($files as $file) {
|
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();
|
$html = ob_get_clean();
|
||||||
|
|
||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user