now ajax is in PLGMTLS object as a method

This commit is contained in:
asus
2024-02-15 02:47:11 +01:00
parent aaca16525b
commit 6c893049dc
5 changed files with 76 additions and 59 deletions

View File

@@ -53,24 +53,43 @@ class PLGNTLS_class
* 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();
?>
<script type="text/javascript">
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();
data.append('action', action);
data.append('_ajax_nonce', _ajax_nonce);
data.append('data', ajax_data);
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
});
};
</script>
return fetch(_ajax_url, {
method: "POST",
credentials: "same-origin",
body: data
});
}
<?php
return ob_get_clean();
}
@@ -88,16 +107,6 @@ class PLGNTLS_class
* it's not a real pbm
* (what is more efficient, check for double or overwritte object ?)
*/
public function add_ajax_script() {
if (is_null($this->_first_script))
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');
}
@@ -120,20 +129,14 @@ class PLGNTLS_class
$file = (object)[];
$file->ext = pathinfo($file_name, PATHINFO_EXTENSION);
if ($file->ext === "js")
$dir_path = 'js/';
else if ($file->ext === "css")
$dir_path = 'css/';
else if ($file->ext === "html")
$dir_path = 'html/';
else
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().$dir_path.$file_name;
$file->path = $this->get_path().$dir_path.$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;
@@ -149,7 +152,7 @@ class PLGNTLS_class
* - to add ajax script and variables
* - default to true
*/
private function add_files_to_front($files_arr, $add_ajax = 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 );
@@ -167,9 +170,6 @@ class PLGNTLS_class
$previous_css_basename = $file->basename;
}
}
if ($add_ajax === true)
$this->add_ajax_script();
}
@@ -187,12 +187,23 @@ class PLGNTLS_class
* init.js -> init_js
*/
private function add_vars_to_front($var_array) {
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;
wp_localize_script($handle, $object_name, $var_array);
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) {
$files = array();
@@ -201,8 +212,8 @@ class PLGNTLS_class
}
if (!is_null($files_arr))
$this->add_files_to_front($files);
if (!is_null($vars))
$this->add_vars_to_front($vars);
$ajax = $this->get_ajax_script();
$this->add_vars_to_front($vars, $ajax);
return $this->create_html($files, $vars);
}
@@ -235,8 +246,6 @@ class PLGNTLS_class
ob_start();
foreach($files as $file) {
error_log("file in create_html_tmp");
error_log(json_encode($file));
if ($file->ext === 'html')
include($file->path);
}