From 6c893049dcb9b0bb92191ccc55deb604e591fcf1 Mon Sep 17 00:00:00 2001 From: asus Date: Thu, 15 Feb 2024 02:47:11 +0100 Subject: [PATCH] now ajax is in PLGMTLS object as a method --- plugins/fipfcard_plugin/fipfcard_plugin.php | 15 ++- plugins/fipfcard_plugin/js/example_init.js | 12 ++- .../fipfcard_plugin/js/menu/example_menu.js | 3 +- .../fipfcard_plugin/php/menu/example_menu.php | 8 +- .../php/utils/plugin_tools.php | 97 ++++++++++--------- 5 files changed, 76 insertions(+), 59 deletions(-) diff --git a/plugins/fipfcard_plugin/fipfcard_plugin.php b/plugins/fipfcard_plugin/fipfcard_plugin.php index e0c63f2..29de2cf 100644 --- a/plugins/fipfcard_plugin/fipfcard_plugin.php +++ b/plugins/fipfcard_plugin/fipfcard_plugin.php @@ -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", diff --git a/plugins/fipfcard_plugin/js/example_init.js b/plugins/fipfcard_plugin/js/example_init.js index a84223a..b003818 100644 --- a/plugins/fipfcard_plugin/js/example_init.js +++ b/plugins/fipfcard_plugin/js/example_init.js @@ -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); + }) + ; }); diff --git a/plugins/fipfcard_plugin/js/menu/example_menu.js b/plugins/fipfcard_plugin/js/menu/example_menu.js index d02b6dc..6264194 100644 --- a/plugins/fipfcard_plugin/js/menu/example_menu.js +++ b/plugins/fipfcard_plugin/js/menu/example_menu.js @@ -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: "); diff --git a/plugins/fipfcard_plugin/php/menu/example_menu.php b/plugins/fipfcard_plugin/php/menu/example_menu.php index 101ff02..15a38c1 100644 --- a/plugins/fipfcard_plugin/php/menu/example_menu.php +++ b/plugins/fipfcard_plugin/php/menu/example_menu.php @@ -1,11 +1,11 @@ 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", )); } diff --git a/plugins/fipfcard_plugin/php/utils/plugin_tools.php b/plugins/fipfcard_plugin/php/utils/plugin_tools.php index 1a9d821..b695c46 100644 --- a/plugins/fipfcard_plugin/php/utils/plugin_tools.php +++ b/plugins/fipfcard_plugin/php/utils/plugin_tools.php @@ -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(); ?> - + return fetch(_ajax_url, { + method: "POST", + credentials: "same-origin", + body: data + }); + } _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(, /url/to/file, [depends on], version, defer? ); //wp_enqueue_style( , /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); }