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

@@ -55,63 +55,37 @@ plugin shortcode
*/ */
function fipfcard_main_shortcode() { function fipfcard_main_shortcode() {
$fipfcard_tools = new PLGNTOOLS(); $fipfcard_tools = new PLGNTOOLS();
$test_path_plgntools = $fipfcard_tools->get_path();
$test_url_plgntools = $fipfcard_tools->get_url();
error_log("-------------");
error_log($test_path_plgntools);
error_log($test_url_plgntools);
// fipfcard_add_files_to_front( array( $fipfcard_tools->add_files_to_front( array(
// "example_style.css", "example_style.css",
// "example_init.js", "example_init.js",
// "example_script2.js", "example_script2.js",
// "example_script3.js", "example_script3.js",
// )); ));
$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_add_var_to_front( compact(
// "myvar_1",
// "myvar_2",
// ));
$fipfcard_tools->add_var_to_front( compact( $fipfcard_tools->add_var_to_front( compact(
"myvar_1", "myvar_1",
"myvar_2", "myvar_2",
)); ));
$names = ["hugo", "camille"]; $names = ["hugo", "camille"];
$ages = ["13", "34", "56"]; $ages = ["13", "34", "56"];
// $html_front = fipfcard_create_html(
// array(
// "example_index.html",
// "example_index2.html",
// ),
// compact(
// "names",
// "ages",
// )
// );
$html_front = $fipfcard_tools->create_html( $html_front = $fipfcard_tools->create_html(
array( array(
"example_index.html", "example_index.html",
"example_index2.html", "example_index2.html",
), ),
compact( compact(
"names", "names",
"ages", "ages",
) )
); );
return $html_front; return $html_front;
} }

View File

@@ -1,15 +1,13 @@
<?php <?php
function fipfcard_plugin_content() { function fipfcard_plugin_content() {
global $fipfcard_first_script; $fipfcard_tools = new PLGNTOOLS();
error_log("in menu : first_script:");
error_log($fipfcard_first_script);
fipfcard_add_files_to_front( array( $fipfcard_tools->add_files_to_front( array(
"menu/example_menu.js", "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 // static properties to hold the plugin dir path and url
public static $root_path; public static $root_path;
public static $root_url; public static $root_url;
private static $_ajax_already_there;
private $_first_script; 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) { public static function set_root_dir($path, $url) {
if (isset(self::$root_path)) if (isset( self::$root_path ))
return; return ;
if (isset(self::$root_url)) if (isset( self::$root_url ))
return; return ;
self::$root_path = $path; self::$root_path = $path;
self::$root_url = $url; self::$root_url = $url;
} }
@@ -31,43 +38,64 @@ class PLGNTOOLS
return(self::$root_url); 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 * function that add the ajax script to front
// * no real needs to check if already included : * no real needs to check if already included :
// * - $handle is uniq so it will not be re-enqueued * - $handle is uniq so it will not be re-enqueued
// * the first enqueued version would be kept * the first enqueued version would be kept
// * - if we used add_var_to_front() (which use wp_add_inline_script()) * - if we used add_var_to_front() (which use wp_add_inline_script())
// * the vars would be added twice * the vars would be added twice
// * leading to js syntaxe error (redeclaraiton of 'let' or 'const') * leading to js syntaxe error (redeclaraiton of 'let' or 'const')
// * - but we use wp_localize_script() so the object will be overwritten * - but we use wp_localize_script() so the object will be overwritten
// * it's not a real pbm * it's not a real pbm
// * (what is more efficient, check for double or overwritte object ?) * (what is more efficient, check for double or overwritte object ?)
// */ */
// public function add_ajax_post() { public function add_ajax_script() {
// global $fipfcard_first_script; if (is_null($this->_first_script))
// global $fipfcard_ajax_file; return ;
// if (self::$_ajax_already_there)
// $file = fipfcard_init_file($fipfcard_ajax_file); return ;
//
// // // check if ajax script was already enqueued $ajax_script = $this->get_ajax_script();
// // global $wp_scripts; wp_add_inline_script($this->_first_script, $ajax_script, 'before');
// // $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);
//
// }
@@ -104,8 +132,6 @@ class PLGNTOOLS
$file->path = $this->get_path().$dir_path.$file_name; $file->path = $this->get_path().$dir_path.$file_name;
$file->version = date("ymd-Gis", filemtime($file->path)); $file->version = date("ymd-Gis", filemtime($file->path));
error_log(json_encode($file));
return $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_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 );
// if ($add_ajax === true)
// fipfcard_add_ajax_post();
$previous_css_basename = ''; $previous_css_basename = '';
$previous_js_basename = ''; $previous_js_basename = '';
foreach ($files_arr as $file_name) { foreach ($files_arr as $file_name) {
$file = $this->init_file($file_name); $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;
wp_enqueue_script( $file->handle, $file->url, $previous_js_basename, $file->version, true); wp_enqueue_script( $file->handle, $file->url, $previous_js_basename, $file->version, true);
$previous_js_basename = $file->basename; $previous_js_basename = $file->basename;
} }
@@ -141,6 +164,9 @@ class PLGNTOOLS
$previous_css_basename = $file->basename; $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) { public function add_var_to_front($vars, $handle = null) {
if (is_null($handle)) { if (is_null($handle)) {
$handle = $this->$_first_script; $handle = $this->_first_script;
} }
extract($vars); extract($vars);