From de6fd7a8a71dce9fac6e0dd3128cc844f7611f8b Mon Sep 17 00:00:00 2001 From: asus Date: Sat, 10 Feb 2024 19:19:47 +0100 Subject: [PATCH] wip make ajax automatically added --- plugins/wp_model_plugin/js/ajax.js | 7 ++++-- plugins/wp_model_plugin/js/menu/menu.js | 22 +------------------ plugins/wp_model_plugin/php/menu/menu.php | 10 --------- .../php/utils/add_to_front.php | 18 +++++++++++++-- .../wp_model_plugin/php/utils/create_html.php | 2 ++ plugins/wp_model_plugin/plugin_hooks.php | 9 +++++--- 6 files changed, 30 insertions(+), 38 deletions(-) diff --git a/plugins/wp_model_plugin/js/ajax.js b/plugins/wp_model_plugin/js/ajax.js index e53fe4c..a7942e9 100644 --- a/plugins/wp_model_plugin/js/ajax.js +++ b/plugins/wp_model_plugin/js/ajax.js @@ -1,8 +1,11 @@ -function ajax_post(data, action, callback_response, callback_error) { +/* + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +*/ +function ajax_post(mydata, action, callback_response, callback_error) { const data = new FormData(); data.append('action', action); data.append('_ajax_nonce', php_data.nonce); - data.append('data', data); + data.append('data', mydata); fetch(php_data.ajax_url, { method: 'POST', diff --git a/plugins/wp_model_plugin/js/menu/menu.js b/plugins/wp_model_plugin/js/menu/menu.js index f2ceb62..88b3bb9 100644 --- a/plugins/wp_model_plugin/js/menu/menu.js +++ b/plugins/wp_model_plugin/js/menu/menu.js @@ -3,26 +3,6 @@ const sendButton = document.getElementById('mybutton'); sendButton.addEventListener('click', () => { const inputValue = inputElement.value; - const myurl = php_data.ajax_url; - console.log(myurl); - const data = new FormData(); - data.append('action', 'get_data',); - data.append('_ajax_nonce', php_data.nonce); - data.append('data', inputValue); - - fetch(myurl, { - 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); - }); + ajax_post(inputValue, 'get_data'); }); diff --git a/plugins/wp_model_plugin/php/menu/menu.php b/plugins/wp_model_plugin/php/menu/menu.php index f29c8bb..fa806b1 100644 --- a/plugins/wp_model_plugin/php/menu/menu.php +++ b/plugins/wp_model_plugin/php/menu/menu.php @@ -5,16 +5,6 @@ function wp_model_plugin_content() { "menu/menu.js", )); - $ajax_url = admin_url( 'admin-ajax.php' ); - $nonce = wp_create_nonce( 'wp-pageviews-nonce' ); - add_var_to_front( - compact( - "ajax_url", - "nonce", - ), - "menu" - ); - echo create_html("menu/menu.html"); } diff --git a/plugins/wp_model_plugin/php/utils/add_to_front.php b/plugins/wp_model_plugin/php/utils/add_to_front.php index 93149ba..ec0f600 100644 --- a/plugins/wp_model_plugin/php/utils/add_to_front.php +++ b/plugins/wp_model_plugin/php/utils/add_to_front.php @@ -8,6 +8,9 @@ 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 ); + // always adding ajax file first + $files_arr = ["ajax.js", ...$files_arr]; + $previous_css_basename = ''; $previous_js_basename = ''; foreach ($files_arr as $file) { @@ -33,6 +36,16 @@ function add_files_to_front($files_arr) { $previous_css_basename = $file_basename; } } + + $ajax_url = admin_url( 'admin-ajax.php' ); + $nonce = wp_create_nonce( 'wp-pageviews-nonce' ); + add_var_to_front( + compact( + "ajax_url", + "nonce", + ), + "ajax" + ); } @@ -49,9 +62,10 @@ it also adds the ajax url add_var_to_front(compact("var1", "var2", "var3")); 2. name of first embended script (if we want to have the variables availables to all js : it will be available to the script and all following) - - default value is "init", assuming the first script is called "init.js" + - default value is "ajax", assuming the first script is "ajax.js" + since it's automatically added first by `add_files_to_front` */ -function add_var_to_front($vars, $handle = "init") { +function add_var_to_front($vars, $handle = "ajax") { $handle = pathinfo($handle, PATHINFO_FILENAME); $object_name = "php_data"; diff --git a/plugins/wp_model_plugin/php/utils/create_html.php b/plugins/wp_model_plugin/php/utils/create_html.php index 235ddef..bdef3c0 100644 --- a/plugins/wp_model_plugin/php/utils/create_html.php +++ b/plugins/wp_model_plugin/php/utils/create_html.php @@ -24,6 +24,8 @@ https://stackoverflow.com/a/4402045/9497573 function create_html($files, $vars = null) { $files = (array)$files; $html_dir = PLUGIN_DIR.'html/'; + console_log("vars: "); + console_log($vars); if (!is_null($vars)) extract($vars); diff --git a/plugins/wp_model_plugin/plugin_hooks.php b/plugins/wp_model_plugin/plugin_hooks.php index 38a1812..020aec1 100644 --- a/plugins/wp_model_plugin/plugin_hooks.php +++ b/plugins/wp_model_plugin/plugin_hooks.php @@ -106,9 +106,12 @@ ajax use $_POST['property_name'] */ function my_ajax_handler() { - error_log("test"); - error_log($_POST['data']); - wp_send_json_success( 'It works' ); + wp_send_json_success( array( + 'It works', + "data_received" => $_POST['data'], + ), + 200 + ); } add_action( 'wp_ajax_get_data', 'my_ajax_handler' );