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
|
inclusions
|
||||||
*/
|
*/
|
||||||
include_once(FIPFCARD_PLUGIN_DIR . '/php/utils/globals.php');
|
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');
|
include_once(FIPFCARD_PLUGIN_DIR . '/php/menu/example_menu.php');
|
||||||
|
|
||||||
@@ -74,12 +71,12 @@ function fipfcard_main_shortcode() {
|
|||||||
|
|
||||||
return $fipfcard_tools->add_to_front(
|
return $fipfcard_tools->add_to_front(
|
||||||
array(
|
array(
|
||||||
"example_style.css",
|
"css/example_style.css",
|
||||||
"example_init.js",
|
"js/example_init.js",
|
||||||
"example_script2.js",
|
"js/example_script2.js",
|
||||||
"example_script3.js",
|
"js/example_script3.js",
|
||||||
"example_index.html",
|
"html/example_index.html",
|
||||||
"example_index2.html",
|
"html/example_index2.html",
|
||||||
),
|
),
|
||||||
compact(
|
compact(
|
||||||
"myvar_1",
|
"myvar_1",
|
||||||
|
|||||||
@@ -6,5 +6,15 @@ title.innerHTML = "--- coucou ;) " + PLGNTLS_data.myvar_1;
|
|||||||
|
|
||||||
const ajax_button_1 = document.querySelector("#test_ajax_1");
|
const ajax_button_1 = document.querySelector("#test_ajax_1");
|
||||||
ajax_button_1.addEventListener('click', () => {
|
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;
|
const inputValue = inputElement.value;
|
||||||
console.log("inputValue:");
|
console.log("inputValue:");
|
||||||
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((response) => response.json())
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
console.log("dataaa: ");
|
console.log("dataaa: ");
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
function fipfcard_plugin_content() {
|
function fipfcard_plugin_content() {
|
||||||
$fipfcard_tools = new PLGNTLS_class();
|
$fipfcard = new PLGNTLS_class();
|
||||||
|
|
||||||
echo $fipfcard_tools->add_to_front( array(
|
echo $fipfcard->add_to_front( array(
|
||||||
"menu/example_menu.js",
|
"js/menu/example_menu.js",
|
||||||
"menu/example_menu.html",
|
"html/menu/example_menu.html",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,24 +53,43 @@ class PLGNTLS_class
|
|||||||
* js function that creates an ajax post action
|
* js function that creates an ajax post action
|
||||||
*/
|
*/
|
||||||
public function get_ajax_script() {
|
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();
|
ob_start();
|
||||||
?>
|
?>
|
||||||
<script type="text/javascript">
|
function (ajax_data, action) {
|
||||||
function PLGNTLS_ajax(ajax_data, action) {
|
|
||||||
const _ajax_nonce = "<?php echo wp_create_nonce( 'wp-pageviews-nonce' ); ?>";
|
const _ajax_nonce = "<?php echo wp_create_nonce( 'wp-pageviews-nonce' ); ?>";
|
||||||
const _ajax_url = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
|
const _ajax_url = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
|
||||||
const data = new FormData();
|
const data = new FormData();
|
||||||
data.append('action', action);
|
data.append("action", action);
|
||||||
data.append('_ajax_nonce', _ajax_nonce);
|
data.append("_ajax_nonce", _ajax_nonce);
|
||||||
data.append('data', ajax_data);
|
data.append("data", ajax_data);
|
||||||
|
|
||||||
return fetch(_ajax_url, {
|
return fetch(_ajax_url, {
|
||||||
method: 'POST',
|
method: "POST",
|
||||||
credentials: 'same-origin',
|
credentials: "same-origin",
|
||||||
body: data
|
body: data
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
</script>
|
|
||||||
<?php
|
<?php
|
||||||
return ob_get_clean();
|
return ob_get_clean();
|
||||||
}
|
}
|
||||||
@@ -88,16 +107,6 @@ class PLGNTLS_class
|
|||||||
* 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_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 = (object)[];
|
||||||
|
|
||||||
$file->ext = pathinfo($file_name, PATHINFO_EXTENSION);
|
$file->ext = pathinfo($file_name, PATHINFO_EXTENSION);
|
||||||
if ($file->ext === "js")
|
if (! in_array($file->ext, array("js", "css", "html")))
|
||||||
$dir_path = 'js/';
|
|
||||||
else if ($file->ext === "css")
|
|
||||||
$dir_path = 'css/';
|
|
||||||
else if ($file->ext === "html")
|
|
||||||
$dir_path = 'html/';
|
|
||||||
else
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
$file->basename = pathinfo($file_name, PATHINFO_FILENAME);
|
$file->basename = pathinfo($file_name, PATHINFO_FILENAME);
|
||||||
$file->handle = str_replace(".", "_", $file_name);
|
$file->handle = str_replace(".", "_", $file_name);
|
||||||
|
|
||||||
$file->url = $this->get_url().$dir_path.$file_name;
|
$file->url = $this->get_url().$file_name;
|
||||||
$file->path = $this->get_path().$dir_path.$file_name;
|
$file->path = $this->get_path().$file_name;
|
||||||
$file->version = date("ymd-Gis", filemtime($file->path));
|
$file->version = date("ymd-Gis", filemtime($file->path));
|
||||||
|
|
||||||
return $file;
|
return $file;
|
||||||
@@ -149,7 +152,7 @@ class PLGNTLS_class
|
|||||||
* - to add ajax script and variables
|
* - to add ajax script and variables
|
||||||
* - default to true
|
* - 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_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 );
|
||||||
|
|
||||||
@@ -167,9 +170,6 @@ class PLGNTLS_class
|
|||||||
$previous_css_basename = $file->basename;
|
$previous_css_basename = $file->basename;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($add_ajax === true)
|
|
||||||
$this->add_ajax_script();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -187,12 +187,23 @@ class PLGNTLS_class
|
|||||||
* init.js -> init_js
|
* 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))
|
if (is_null($this->_first_script))
|
||||||
return ;
|
return ;
|
||||||
|
if (is_null($vars_arr) && is_null($ajax))
|
||||||
|
return ;
|
||||||
$handle = $this->_first_script;
|
$handle = $this->_first_script;
|
||||||
$object_name = $this->_object_data;
|
$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) {
|
public function add_to_front($files_arr = null, $vars = null) {
|
||||||
$files = array();
|
$files = array();
|
||||||
@@ -201,8 +212,8 @@ class PLGNTLS_class
|
|||||||
}
|
}
|
||||||
if (!is_null($files_arr))
|
if (!is_null($files_arr))
|
||||||
$this->add_files_to_front($files);
|
$this->add_files_to_front($files);
|
||||||
if (!is_null($vars))
|
$ajax = $this->get_ajax_script();
|
||||||
$this->add_vars_to_front($vars);
|
$this->add_vars_to_front($vars, $ajax);
|
||||||
return $this->create_html($files, $vars);
|
return $this->create_html($files, $vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,8 +246,6 @@ class PLGNTLS_class
|
|||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
foreach($files as $file) {
|
foreach($files as $file) {
|
||||||
error_log("file in create_html_tmp");
|
|
||||||
error_log(json_encode($file));
|
|
||||||
if ($file->ext === 'html')
|
if ($file->ext === 'html')
|
||||||
include($file->path);
|
include($file->path);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user