now ajax is in PLGMTLS object as a method
This commit is contained in:
@@ -40,9 +40,6 @@ PLGNTLS_class::set_root_dir( plugin_dir_path(__FILE__), plugin_dir_url(__FILE__)
|
||||
inclusions
|
||||
*/
|
||||
include_once(FIPFCARD_PLUGIN_DIR . '/php/utils/globals.php');
|
||||
include_once(FIPFCARD_PLUGIN_DIR . '/php/utils/console_log.php');
|
||||
include_once(FIPFCARD_PLUGIN_DIR . '/php/utils/add_to_front.php');
|
||||
include_once(FIPFCARD_PLUGIN_DIR . '/php/utils/create_html.php');
|
||||
|
||||
include_once(FIPFCARD_PLUGIN_DIR . '/php/menu/example_menu.php');
|
||||
|
||||
@@ -74,12 +71,12 @@ function fipfcard_main_shortcode() {
|
||||
|
||||
return $fipfcard_tools->add_to_front(
|
||||
array(
|
||||
"example_style.css",
|
||||
"example_init.js",
|
||||
"example_script2.js",
|
||||
"example_script3.js",
|
||||
"example_index.html",
|
||||
"example_index2.html",
|
||||
"css/example_style.css",
|
||||
"js/example_init.js",
|
||||
"js/example_script2.js",
|
||||
"js/example_script3.js",
|
||||
"html/example_index.html",
|
||||
"html/example_index2.html",
|
||||
),
|
||||
compact(
|
||||
"myvar_1",
|
||||
|
||||
@@ -6,5 +6,15 @@ title.innerHTML = "--- coucou ;) " + PLGNTLS_data.myvar_1;
|
||||
|
||||
const ajax_button_1 = document.querySelector("#test_ajax_1");
|
||||
ajax_button_1.addEventListener('click', () => {
|
||||
ajax_post(ajax_button_1, 'get_data');
|
||||
PLGNTLS_data.ajax(ajax_button_1, 'get_data')
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
console.log("datou: ");
|
||||
console.log(data);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("error: ");
|
||||
console.log(error);
|
||||
})
|
||||
;
|
||||
});
|
||||
|
||||
@@ -5,7 +5,8 @@ sendButton.addEventListener('click', () => {
|
||||
const inputValue = inputElement.value;
|
||||
console.log("inputValue:");
|
||||
console.log(inputValue);
|
||||
PLGNTLS_ajax(inputValue, 'get_data')
|
||||
console.log(PLGNTLS_data);
|
||||
PLGNTLS_data.ajax(inputValue, 'get_data')
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
console.log("dataaa: ");
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
<?php
|
||||
|
||||
function fipfcard_plugin_content() {
|
||||
$fipfcard_tools = new PLGNTLS_class();
|
||||
$fipfcard = new PLGNTLS_class();
|
||||
|
||||
echo $fipfcard_tools->add_to_front( array(
|
||||
"menu/example_menu.js",
|
||||
"menu/example_menu.html",
|
||||
echo $fipfcard->add_to_front( array(
|
||||
"js/menu/example_menu.js",
|
||||
"html/menu/example_menu.html",
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user