added ajax call inside class plugin

This commit is contained in:
asus
2024-02-14 02:49:14 +01:00
parent 259cb8aa00
commit 7b8e61a59f
3 changed files with 97 additions and 99 deletions

View File

@@ -1,15 +1,13 @@
<?php
function fipfcard_plugin_content() {
global $fipfcard_first_script;
error_log("in menu : first_script:");
error_log($fipfcard_first_script);
$fipfcard_tools = new PLGNTOOLS();
fipfcard_add_files_to_front( array(
$fipfcard_tools->add_files_to_front( array(
"menu/example_menu.js",
));
echo fipfcard_create_html("menu/example_menu.html");
echo $fipfcard_tools->create_html("menu/example_menu.html");
}
?>

View File

@@ -12,14 +12,21 @@ class PLGNTOOLS
// static properties to hold the plugin dir path and url
public static $root_path;
public static $root_url;
private static $_ajax_already_there;
private $_first_script;
public function __construct() {
if (isset( self::$_ajax_already_there ))
return ;
self::$_ajax_already_there = false;
}
public static function set_root_dir($path, $url) {
if (isset(self::$root_path))
return;
if (isset(self::$root_url))
return;
if (isset( self::$root_path ))
return ;
if (isset( self::$root_url ))
return ;
self::$root_path = $path;
self::$root_url = $url;
}
@@ -31,43 +38,64 @@ class PLGNTOOLS
return(self::$root_url);
}
/**
* js function that create an ajax post action
* it can be "overloaded" with a callback_response and _error
*/
public function get_ajax_script() {
ob_start();
?>
<script type="text/javascript">
function ajax_post(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);
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>
<?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 ?)
// */
// public function add_ajax_post() {
// global $fipfcard_first_script;
// global $fipfcard_ajax_file;
//
// $file = fipfcard_init_file($fipfcard_ajax_file);
//
// // // check if ajax script was already enqueued
// // global $wp_scripts;
// // $already_enqueued = array_search($file->handle, $wp_scripts->queue);
// // if ($already_enqueued !== false)
// // return ;
//
// $fipfcard_first_script = $file->handle;
// wp_enqueue_script( $file->handle, $file->url, '', $file->version, true);
//
// $_url = admin_url( 'admin-ajax.php' );
// $_nonce = wp_create_nonce( 'wp-pageviews-nonce' );
// $vars = compact("_url","_nonce",);
// // add_var_to_front($vars);
// $object_name = "fipfcard_ajax";
// wp_localize_script($file->handle, $object_name, $vars);
//
// }
/**
* 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 ?)
*/
public function add_ajax_script() {
if (is_null($this->_first_script))
return ;
if (self::$_ajax_already_there)
return ;
$ajax_script = $this->get_ajax_script();
wp_add_inline_script($this->_first_script, $ajax_script, 'before');
}
@@ -104,8 +132,6 @@ class PLGNTOOLS
$file->path = $this->get_path().$dir_path.$file_name;
$file->version = date("ymd-Gis", filemtime($file->path));
error_log(json_encode($file));
return $file;
}
@@ -123,16 +149,13 @@ class PLGNTOOLS
//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 );
// if ($add_ajax === true)
// fipfcard_add_ajax_post();
$previous_css_basename = '';
$previous_js_basename = '';
foreach ($files_arr as $file_name) {
$file = $this->init_file($file_name);
if ($file->ext === "js") {
if (is_null($this->$_first_script))
$this->$_first_script = $file->handle;
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;
}
@@ -141,6 +164,9 @@ class PLGNTOOLS
$previous_css_basename = $file->basename;
}
}
if ($add_ajax === true)
$this->add_ajax_script();
}
@@ -159,7 +185,7 @@ class PLGNTOOLS
*/
public function add_var_to_front($vars, $handle = null) {
if (is_null($handle)) {
$handle = $this->$_first_script;
$handle = $this->_first_script;
}
extract($vars);