From 7b8e61a59f5ce431858d77aedb39448efd8ac7ca Mon Sep 17 00:00:00 2001 From: asus Date: Wed, 14 Feb 2024 02:49:14 +0100 Subject: [PATCH] added ajax call inside class plugin --- plugins/fipfcard_plugin/fipfcard_plugin.php | 68 +++------- .../fipfcard_plugin/php/menu/example_menu.php | 8 +- .../php/utils/plugin_tools.php | 120 +++++++++++------- 3 files changed, 97 insertions(+), 99 deletions(-) diff --git a/plugins/fipfcard_plugin/fipfcard_plugin.php b/plugins/fipfcard_plugin/fipfcard_plugin.php index 7c66fac..f048e73 100644 --- a/plugins/fipfcard_plugin/fipfcard_plugin.php +++ b/plugins/fipfcard_plugin/fipfcard_plugin.php @@ -55,63 +55,37 @@ plugin shortcode */ function fipfcard_main_shortcode() { -$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_tools = new PLGNTOOLS(); -// fipfcard_add_files_to_front( array( -// "example_style.css", -// "example_init.js", -// "example_script2.js", -// "example_script3.js", -// )); - -$fipfcard_tools->add_files_to_front( array( - "example_style.css", - "example_init.js", - "example_script2.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_2 = "I am two"; -// fipfcard_add_var_to_front( compact( -// "myvar_1", -// "myvar_2", -// )); -$fipfcard_tools->add_var_to_front( compact( - "myvar_1", - "myvar_2", -)); + $fipfcard_tools->add_var_to_front( compact( + "myvar_1", + "myvar_2", + )); $names = ["hugo", "camille"]; $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( - array( - "example_index.html", - "example_index2.html", - ), - compact( - "names", - "ages", - ) -); + $html_front = $fipfcard_tools->create_html( + array( + "example_index.html", + "example_index2.html", + ), + compact( + "names", + "ages", + ) + ); return $html_front; } diff --git a/plugins/fipfcard_plugin/php/menu/example_menu.php b/plugins/fipfcard_plugin/php/menu/example_menu.php index 52af7b6..37ba2dd 100644 --- a/plugins/fipfcard_plugin/php/menu/example_menu.php +++ b/plugins/fipfcard_plugin/php/menu/example_menu.php @@ -1,15 +1,13 @@ add_files_to_front( array( "menu/example_menu.js", )); - echo fipfcard_create_html("menu/example_menu.html"); + echo $fipfcard_tools->create_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 82e65c7..aef932f 100644 --- a/plugins/fipfcard_plugin/php/utils/plugin_tools.php +++ b/plugins/fipfcard_plugin/php/utils/plugin_tools.php @@ -12,14 +12,21 @@ class PLGNTOOLS // static properties to hold the plugin dir path and url public static $root_path; public static $root_url; + private static $_ajax_already_there; 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) { - if (isset(self::$root_path)) - return; - if (isset(self::$root_url)) - return; + if (isset( self::$root_path )) + return ; + if (isset( self::$root_url )) + return ; self::$root_path = $path; self::$root_url = $url; } @@ -31,43 +38,64 @@ class PLGNTOOLS 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(); + ?> + + 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); -// -// } + /** + * function that add the ajax script to front + * no real needs to check if already included : + * - $handle is uniq so it will not be re-enqueued + * the first enqueued version would be kept + * - if we used add_var_to_front() (which use wp_add_inline_script()) + * the vars would be added twice + * leading to js syntaxe error (redeclaraiton of 'let' or 'const') + * - but we use wp_localize_script() so the object will be overwritten + * 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 ; + + $ajax_script = $this->get_ajax_script(); + wp_add_inline_script($this->_first_script, $ajax_script, 'before'); + } @@ -104,8 +132,6 @@ class PLGNTOOLS $file->path = $this->get_path().$dir_path.$file_name; $file->version = date("ymd-Gis", filemtime($file->path)); - error_log(json_encode($file)); - return $file; } @@ -123,16 +149,13 @@ class PLGNTOOLS //wp_enqueue_script(, /url/to/file, [depends on], version, defer? ); //wp_enqueue_style( , /url/to/file, [depends on], version, media ); -// if ($add_ajax === true) -// fipfcard_add_ajax_post(); - $previous_css_basename = ''; $previous_js_basename = ''; foreach ($files_arr as $file_name) { $file = $this->init_file($file_name); if ($file->ext === "js") { - if (is_null($this->$_first_script)) - $this->$_first_script = $file->handle; + if (is_null($this->_first_script)) + $this->_first_script = $file->handle; wp_enqueue_script( $file->handle, $file->url, $previous_js_basename, $file->version, true); $previous_js_basename = $file->basename; } @@ -141,6 +164,9 @@ class PLGNTOOLS $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) { if (is_null($handle)) { - $handle = $this->$_first_script; + $handle = $this->_first_script; } extract($vars);