diff --git a/plugins/cipf_plugin/cipf_plugin.php b/plugins/cipf_plugin/cipf_plugin.php index 6a58c0d..642b3a0 100644 --- a/plugins/cipf_plugin/cipf_plugin.php +++ b/plugins/cipf_plugin/cipf_plugin.php @@ -17,7 +17,7 @@ if (!defined('ABSPATH')) { -include_once( plugin_dir_path(__FILE__) . '/utils/plgntls_class.php'); +include_once( plugin_dir_path(__FILE__) . '/plgntls_class.php'); diff --git a/plugins/cipf_plugin/utils/plgntls_class.php b/plugins/cipf_plugin/plgntls_class.php similarity index 74% rename from plugins/cipf_plugin/utils/plgntls_class.php rename to plugins/cipf_plugin/plgntls_class.php index 637ca4e..ee696ec 100644 --- a/plugins/cipf_plugin/utils/plgntls_class.php +++ b/plugins/cipf_plugin/plgntls_class.php @@ -188,6 +188,7 @@ vous allez être redirigés vers votre espace', private static $_root_path; private static $_root_url; private static $_instance_count = 0; + private static $_adding_count = 0; private $_first_script; private $_first_style; @@ -210,6 +211,98 @@ vous allez être redirigés vers votre espace', $this->_scripts_attributes = array(); } + + + + + /* + * for debug purposes + * + */ + public static function debug_infos($level = null) { + if (self::$_DEBUG_INFOS === 0) { + return; + } + else if (self::$_DEBUG_INFOS === 1 && $level === 2) { + return; + } + $trace = debug_backtrace(); + $function = $trace[1]['function']; + $file = $trace[0]['file']; + $line = $trace[0]['line']; + error_log("-debug: function '".$function."' (in ".$file.", line ".$line .')'); + } + + + public static function add_to_front($srcs_arr = array(), $vars = array()) { + $instance = new self(); + return $instance->_add_to_front($srcs_arr, $vars); + } + + + + + + + + + + + + +/* +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* PRIVATES FUNCTIONS +* +* 1. set paths and urls +* 2. add to front +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +*/ + + + /* + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * 1 * + * * * * * * * + *)( set paths and urls * + * * * * * * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ + + /* * ex: * /home/www-data/cipf_plugin/php/test/test2/test3/test.php @@ -264,55 +357,65 @@ vous allez être redirigés vers votre espace', + + + + + + + /* - * for debug purposes - * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * 2 * + * * * * * * * + *)( add to front * + * * * * * * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - public static function debug_infos($level = null) { - if (self::$_DEBUG_INFOS === 0) { - return; - } - else if (self::$_DEBUG_INFOS === 1 && $level === 2) { - return; - } - $trace = debug_backtrace(); - $function = $trace[1]['function']; - $file = $trace[0]['file']; - $line = $trace[0]['line']; - error_log("-debug: function '".$function."' (in ".$file.", line ".$line .')'); - } - - - public static function add_to_front($srcs_arr = array(), $vars = array()) { - $instance = new self(); - return $instance->_add_to_front($srcs_arr, $vars); - } - - - - - - - - - - - - -/** - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * PRIVATES FUNCTIONS - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - */ private function _add_to_front($srcs_arr, $vars) { /* - * event if the function is called with no srcs + * even if the function is called with no srcs * add fetch, because it can be used just for that */ - $this->_add_fetch($srcs_arr, $vars); + if (self::$_adding_count === 0) { + $this->_add_fetch($srcs_arr, $vars); + } $srcs = array(); foreach($srcs_arr as $src_key => $src_value) { @@ -320,16 +423,19 @@ vous allez être redirigés vers votre espace', if ($init !== null) $srcs[] = $init; } - if (!is_null($srcs_arr)) + if (!is_null($srcs_arr)) { $this->_add_srcs_to_front($srcs); + } - if (!is_null($vars)) + if (!is_null($vars)) { $this->_add_vars_to_front($vars); - return $this->_create_html($srcs, $vars); + } + $add_html = $this->_create_html($srcs, $vars); + self::$_adding_count += 1; + return $add_html; } - /* * for fetch, we add the script that contains the fetch function * it's an inline script, but is made globally available by adding it to window @@ -365,10 +471,6 @@ vous allez être redirigés vers votre espace', } - - - - /* * @param two arguments : * 1. html files to include in front @@ -383,11 +485,6 @@ vous allez être redirigés vers votre espace', * ex: _create_html( array("file1.html", "file2.html"), array("var1"=>"value") ); * @return a string of html code * - * using ob_start() and ob_get_clean() - * allows to have php expansion inside the html loaded - * in opposition to the methode file_get_contents() - * - * https://stackoverflow.com/a/4402045/9497573 */ private function _create_html($files = null, $vars = null) { if (is_null($files)) @@ -396,6 +493,10 @@ vous allez être redirigés vers votre espace', if (!is_null($vars)) extract($vars); + // using ob_start() and ob_get_clean() + // allows to have php expansion inside the html loaded + // in opposition to the method file_get_contents() + // https://stackoverflow.com/a/4402045/9497573 ob_start(); foreach($files as $file) { if ($file->ext === 'html') @@ -419,20 +520,30 @@ vous allez être redirigés vers votre espace', * init.js -> init_js */ private function _add_vars_to_front($vars_arr) { - if (is_null($this->_first_script)) - return ; if (is_null($vars_arr)) return ; - $handle = $this->_first_script; - $object_name = $this->_prefix . "_data"; + $object_name = $this->_prefix . "_data"; $vars_json = json_encode($vars_arr); - $front = ""; - if (self::$_instance_count === 0) { - $front .= "let "; + // note : we need to use 'var' instead of 'let' or 'const', + // because their scope is restricted to the if statement + $front = " + if (typeof $object_name === 'undefined') { + var $object_name = $vars_json; + } + else { + Object.assign($object_name, $vars_json); + } + "; + + $handle = $this->_check_inline_handles('js'); + if (!is_null($handle)) { + wp_add_inline_script($handle, $front, 'before'); + } + else { + // in last ressort, but bad + echo ''; } - $front .= "$object_name = $vars_json;"; - wp_add_inline_script($handle, $front, 'before'); } /* @@ -470,9 +581,7 @@ vous allez être redirigés vers votre espace', // https://wordpress.stackexchange.com/questions/66843/attach-a-private-function-at-a-hook add_filter( 'wp_script_attributes', fn($attr)=>$this->_add_attributes_to_script($attr), 10, 1 ); - /* - * uncomment to print all enqueued files, can be usefull - */ + //uncomment to print all enqueued files, can be usefull /* global $wp_scripts; error_log("wp_scripts->queue:"); @@ -501,7 +610,7 @@ vous allez être redirigés vers votre espace', } } private function _add_inline_script($src) { - $handle = $this->_check_inline_handles($src); + $handle = $this->_check_inline_handles('js', $src); if (!is_null($handle)) { wp_add_inline_script($handle, $src->src, 'before'); } @@ -513,7 +622,7 @@ vous allez être redirigés vers votre espace', } } private function _add_inline_style($src) { - $handle = $this->_check_inline_handles($src); + $handle = $this->_check_inline_handles('css', $src); if (!is_null($handle)) { wp_add_inline_style($handle, $src->src); } @@ -569,43 +678,45 @@ vous allez être redirigés vers votre espace', return $depends_on; } - private function _check_inline_handles(&$src) { + private function _check_inline_handles($type = null, $src = null) { /* * first, try to simply get the explicit dependency * */ - $handle = $src->depends; + $handle = null;; + if (!is_null($src)){ + $handle = $src->depends; + } if (!empty($handle) && !is_null($handle)) { return $handle; } - /* * second, try to get the first enqueued src of this call to 'add_to_front()' * and make it the dependency + * default to js * */ - if ($src->inline === "js") { + if ($type === "js" || is_null($type)) { $handle = $this->_first_script; } - else if ($src->inline === "css") { + else if ($type === "css") { $handle = $this->_first_style; } if (!empty($handle) && !is_null($handle)) { return $handle; } - /* * third, try to get the last enqueued src of the page * https://www.php.net/manual/en/function.array-key-last.php * */ - if ($src->inline === "js") { + if ($type === "js" || is_null($type)) { global $wp_scripts; $array = $wp_scripts->queue; } - else if ($src->inline === "css") { + else if ($type === "css") { global $wp_styles; $array = $wp_styles->queue; } @@ -734,6 +845,13 @@ vous allez être redirigés vers votre espace', } + + + /* + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + *)( end + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ }