improved plugin class by changing for only one method to do all
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user