- wip trying to patch jquery url validator

- resolve conflict in addition of multiple vars and ajax function
This commit is contained in:
asus
2024-02-18 01:21:08 +01:00
parent 444cb1dbc0
commit daaf6a8a42
6 changed files with 112 additions and 49 deletions

View File

@@ -11,23 +11,24 @@
class PLGNTLS_class
{
private static $root_path;
private static $root_url;
private static $_root_path;
private static $_root_url;
private static $_ajax_already_there;
private $_first_script;
private $_object_data;
private $_prefix;
private $_js_dependencies;
private $_css_dependencies;
/**
* init _ajax_already_there
* _first_script
* _object_data
*/
public function __construct() {
if (! isset( self::$_ajax_already_there ))
self::$_ajax_already_there = false;
$this->_object_data = "PLGNTLS_data";
$this->_prefix = "PLGNTLS";
$this->_first_script = null;
$this->_js_dependencies = array();
$this->_css_dependencies = array();
}
/**
@@ -35,25 +36,25 @@ class PLGNTLS_class
* PLGNTLS_class::set_root_dir( plugin_dir_path(__FILE__), plugin_dir_url(__FILE__) );
*/
public static function set_root_dir($path, $url) {
if (isset( self::$root_path ))
if (isset( self::$_root_path ))
return ;
if (isset( self::$root_url ))
if (isset( self::$_root_url ))
return ;
self::$root_path = $path;
self::$root_url = $url;
self::$_root_path = $path;
self::$_root_url = $url;
}
public function get_path() {
return(self::$root_path);
return(self::$_root_path);
}
public function get_url() {
return(self::$root_url);
return(self::$_root_url);
}
public function add_to_front($files_arr = null, $vars = null) {
$files = array();
foreach($files_arr as $file_name) {
$files[] = $this->init_file($file_name);
foreach($files_arr as $key => $file_name) {
$files[] = $this->init_file($key, $file_name);
}
if (!is_null($files_arr))
$this->add_files_to_front($files);
@@ -136,23 +137,24 @@ class PLGNTLS_class
* this name is the filename + "_" + extension :
* init.js -> init_js
*/
private function add_vars_to_front($vars_arr, $ajax = null) {
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;
$object_name = $this->_prefix . "_data";
if (is_null($vars_arr))
$obj = "let $object_name = {};";
else {
$front = "";
if (!is_null($vars_arr))
{
$vars_json = json_encode($vars_arr);
$obj = "let $object_name = $vars_json;";
$front .= "let $object_name = $vars_json;";
}
if (! is_null($ajax))
$obj .= "$object_name.ajax = $ajax";
wp_add_inline_script($handle, $obj, 'before');
$front .= "$ajax";
wp_add_inline_script($handle, $front, 'before');
}
/**
@@ -173,15 +175,47 @@ class PLGNTLS_class
if ($file->ext === "js") {
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;
$depends_on = $this->check_dependencies($file, $previous_js_basename);
if ($depends_on !== null)
wp_enqueue_script( $file->handle, $file->url, $depends_on, $file->version, true);
$previous_js_basename = $file->handle;
}
else if ($file->ext === "css") {
wp_enqueue_style( $file->handle, $file->url, $previous_css_basename, $file->version, '');
$previous_css_basename = $file->basename;
$depends_on = $this->check_dependencies($file, $previous_css_basename);
if ($depends_on !== null)
wp_enqueue_style( $file->handle, $file->url, $depends_on, $file->version, '');
$previous_css_basename = $file->handle;
}
}
}
private function check_dependencies(&$file, $previous_basename)
{
if ($file->ext === "js")
{
global $wp_scripts;
$already_enqueued = array_search($file->handle, $wp_scripts->queue);
if ($already_enqueued !== false)
{
error_log("double !");
return null;
}
}
else if ($file->ext === "css")
{
global $wp_styles;
$already_enqueued = array_search($file->handle, $wp_styles->queue);
if ($already_enqueued !== false)
return null;
}
$depends_on = array();
if (isset($file->depends) && $file->depends !== '')
$depends_on[] = $file->depends;
if (isset($previous_basename) && $previous_basename !== '')
$depends_on[] = $previous_basename;
return $depends_on;
}
/**
* @param string : name of the file, with its path from its extension directory
@@ -196,21 +230,28 @@ class PLGNTLS_class
* - url : url to file in wordpress
* - path : path to file in server
* - version : used to avoid browser caching
* - depends : string if depends on a handle, or ''
*/
private function init_file($file_name) {
private function init_file($key, $file_name) {
$file = (object)[];
$file->ext = pathinfo($file_name, PATHINFO_EXTENSION);
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->basename = pathinfo($file_name, PATHINFO_BASENAME);
$file->handle = "PLGNTLS_" . str_replace(".", "_", $file->basename);
error_log("in init file->handle");
error_log(json_encode($file->handle));
$file->url = $this->get_url().$file_name;
$file->path = $this->get_path().$file_name;
$file->version = date("ymd-Gis", filemtime($file->path));
$file->depends = '';
if (is_string($key))
$file->depends = $key;
return $file;
}
@@ -239,7 +280,7 @@ class PLGNTLS_class
ob_start();
?>
function (ajax_data, action) {
function <?php echo $this->_prefix ?>_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();