litle bit of cleaning in plugin class
This commit is contained in:
@@ -49,162 +49,7 @@ class PLGNTLS_class
|
|||||||
return(self::$root_url);
|
return(self::$root_url);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* js function that creates an ajax post action
|
|
||||||
*/
|
|
||||||
public function get_ajax_script() {
|
|
||||||
if (is_null($this->_first_script))
|
|
||||||
return ;
|
|
||||||
if (self::$_ajax_already_there)
|
|
||||||
return ;
|
|
||||||
self::$_ajax_already_there = true;
|
|
||||||
|
|
||||||
// return ''
|
|
||||||
// . 'function (ajax_data, action) {'
|
|
||||||
// . ' const _ajax_nonce = "' . wp_create_nonce( 'wp-pageviews-nonce' ) . '";'
|
|
||||||
// . ' const _ajax_url = "' . admin_url( 'admin-ajax.php' ) . '";'
|
|
||||||
// . ' const data = new FormData();'
|
|
||||||
// . ' data.append("action", action);'
|
|
||||||
// . ' data.append("_ajax_nonce", _ajax_nonce);'
|
|
||||||
// . ' data.append("data", ajax_data);'
|
|
||||||
// . ' return fetch(_ajax_url, {'
|
|
||||||
// . ' method: "POST",'
|
|
||||||
// . ' credentials: "same-origin",'
|
|
||||||
// . ' body: data'
|
|
||||||
// . ' });'
|
|
||||||
// . '}';
|
|
||||||
|
|
||||||
ob_start();
|
|
||||||
?>
|
|
||||||
function (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();
|
|
||||||
data.append("action", action);
|
|
||||||
data.append("_ajax_nonce", _ajax_nonce);
|
|
||||||
data.append("data", ajax_data);
|
|
||||||
|
|
||||||
return fetch(_ajax_url, {
|
|
||||||
method: "POST",
|
|
||||||
credentials: "same-origin",
|
|
||||||
body: data
|
|
||||||
});
|
|
||||||
}
|
|
||||||
<?php
|
|
||||||
return ob_get_clean();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* function that add the ajax script to front
|
|
||||||
* no real needs to check if already included :
|
|
||||||
* - $handle is uniq so it will not be re-enqueued
|
|
||||||
* the first enqueued version would be kept
|
|
||||||
* - if we used add_var_to_front() (which use wp_add_inline_script())
|
|
||||||
* the vars would be added twice
|
|
||||||
* leading to js syntaxe error (redeclaraiton of 'let' or 'const')
|
|
||||||
* - but we use wp_localize_script() so the object will be overwritten
|
|
||||||
* it's not a real pbm
|
|
||||||
* (what is more efficient, check for double or overwritte object ?)
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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 (! in_array($file->ext, array("js", "css", "html")))
|
|
||||||
return null;
|
|
||||||
|
|
||||||
$file->basename = pathinfo($file_name, PATHINFO_FILENAME);
|
|
||||||
$file->handle = str_replace(".", "_", $file_name);
|
|
||||||
|
|
||||||
$file->url = $this->get_url().$file_name;
|
|
||||||
$file->path = $this->get_path().$file_name;
|
|
||||||
$file->version = date("ymd-Gis", filemtime($file->path));
|
|
||||||
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
private function add_files_to_front($files_arr) {
|
|
||||||
//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) {
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
|
|
||||||
private function add_vars_to_front($vars_arr, $ajax = null) {
|
|
||||||
if (is_null($this->_first_script))
|
|
||||||
return ;
|
|
||||||
if (is_null($vars_arr) && is_null($ajax))
|
|
||||||
return ;
|
|
||||||
$handle = $this->_first_script;
|
|
||||||
$object_name = $this->_object_data;
|
|
||||||
|
|
||||||
if (is_null($vars_arr))
|
|
||||||
$obj = "let $object_name = {};";
|
|
||||||
else {
|
|
||||||
$vars_json = json_encode($vars_arr);
|
|
||||||
$obj = "let $object_name = $vars_json;";
|
|
||||||
}
|
|
||||||
if (! is_null($ajax))
|
|
||||||
$obj .= "$object_name.ajax = $ajax";
|
|
||||||
wp_add_inline_script($handle, $obj, 'before');
|
|
||||||
}
|
|
||||||
public function add_to_front($files_arr = null, $vars = null) {
|
public function add_to_front($files_arr = null, $vars = null) {
|
||||||
$files = array();
|
$files = array();
|
||||||
foreach($files_arr as $file_name) {
|
foreach($files_arr as $file_name) {
|
||||||
@@ -217,6 +62,31 @@ class PLGNTLS_class
|
|||||||
return $this->create_html($files, $vars);
|
return $this->create_html($files, $vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
* PRIVATES FUNCTIONS
|
||||||
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param two arguments :
|
* @param two arguments :
|
||||||
* 1. html files to include in front
|
* 1. html files to include in front
|
||||||
@@ -253,6 +123,139 @@ class PLGNTLS_class
|
|||||||
|
|
||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
private function add_vars_to_front($vars_arr, $ajax = null) {
|
||||||
|
if (is_null($this->_first_script))
|
||||||
|
return ;
|
||||||
|
if (is_null($vars_arr) && is_null($ajax))
|
||||||
|
return ;
|
||||||
|
$handle = $this->_first_script;
|
||||||
|
$object_name = $this->_object_data;
|
||||||
|
|
||||||
|
if (is_null($vars_arr))
|
||||||
|
$obj = "let $object_name = {};";
|
||||||
|
else {
|
||||||
|
$vars_json = json_encode($vars_arr);
|
||||||
|
$obj = "let $object_name = $vars_json;";
|
||||||
|
}
|
||||||
|
if (! is_null($ajax))
|
||||||
|
$obj .= "$object_name.ajax = $ajax";
|
||||||
|
wp_add_inline_script($handle, $obj, 'before');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
private function add_files_to_front($files_arr) {
|
||||||
|
//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) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 (! in_array($file->ext, array("js", "css", "html")))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
$file->basename = pathinfo($file_name, PATHINFO_FILENAME);
|
||||||
|
$file->handle = str_replace(".", "_", $file_name);
|
||||||
|
|
||||||
|
$file->url = $this->get_url().$file_name;
|
||||||
|
$file->path = $this->get_path().$file_name;
|
||||||
|
$file->version = date("ymd-Gis", filemtime($file->path));
|
||||||
|
|
||||||
|
return $file;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* function that add the ajax script to front
|
||||||
|
* no real needs to check if already included :
|
||||||
|
* - $handle is uniq so it will not be re-enqueued
|
||||||
|
* the first enqueued version would be kept
|
||||||
|
* - if we used add_var_to_front() (which use wp_add_inline_script())
|
||||||
|
* the vars would be added twice
|
||||||
|
* leading to js syntaxe error (redeclaraiton of 'let' or 'const')
|
||||||
|
* - but we use wp_localize_script() so the object will be overwritten
|
||||||
|
* it's not a real pbm
|
||||||
|
* (what is more efficient, check for double or overwritte object ?)
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* js function that creates an ajax post action
|
||||||
|
*/
|
||||||
|
private function get_ajax_script() {
|
||||||
|
if (is_null($this->_first_script))
|
||||||
|
return ;
|
||||||
|
if (self::$_ajax_already_there)
|
||||||
|
return ;
|
||||||
|
self::$_ajax_already_there = true;
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
?>
|
||||||
|
function (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();
|
||||||
|
data.append("action", action);
|
||||||
|
data.append("_ajax_nonce", _ajax_nonce);
|
||||||
|
data.append("data", ajax_data);
|
||||||
|
|
||||||
|
return fetch(_ajax_url, {
|
||||||
|
method: "POST",
|
||||||
|
credentials: "same-origin",
|
||||||
|
body: data
|
||||||
|
});
|
||||||
|
}
|
||||||
|
<?php
|
||||||
|
return ob_get_clean();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user