- added a default css file to include to have a first handle

- cahnged get_path/url to root_path/url
- changed path setup -> now automatic from inside plugin
This commit is contained in:
asus
2024-03-10 22:37:38 +01:00
parent 9ee35b23bc
commit 1021d9bf71
7 changed files with 146 additions and 127 deletions

View File

@@ -30,9 +30,9 @@ if (!defined('ABSPATH')) {
* array( // files added in order by types
* 'path/to/style1.css', //
* 'path/to/script1.js', //
* 'path/to/style2.css', // -> depends on style1.css
* 'style1_css' => 'path/to/style2.css', // -> depends on style1.css
* 'http://my_url1.com', //
* 'path/to/script2.js', // -> depends on script1.js
* 'script1_js' => 'path/to/script2.js', // -> depends on script1.js
* 'path/to/file1.html', // | will be returned
* 'path/to/file2.html', // -> | as expanded html
* 'path/to/file3.html', // | in the order included
@@ -105,6 +105,10 @@ class PLGNTLS_class
private static $_DEBUG_INFOS = false;
private static $_plugin_dir_path;
private static $_plugin_name;
private static $_file_dir_path;
private static $_file_name;
private static $_root_path;
private static $_root_url;
@@ -115,9 +119,11 @@ class PLGNTLS_class
private $_css_dependencies;
private $_scripts_attributes;
/**
*/
/*
*/
public function __construct() {
if (!isset(self::$_plugin_name, self::$_file_name, self::$_file_dir_path, self::$_plugin_dir_path))
self::set_root_dir();
$this->_prefix = "PLGNTLS";
$this->_first_script = null;
$this->_first_style = null;
@@ -126,22 +132,42 @@ class PLGNTLS_class
$this->_scripts_attributes = array();
}
/**
* can be used before class is instanciated :
* 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 ))
/*
* ex:
* /home/www-data/cipf_plugin/php/test/test2/test3/test.php
* _plugin_dir_path /home/www-data
* _plugin_name cipf_plugin
* _file_dir_path php/test/test2/test3
* _file_name test.php
* _root_path /home/www-data/cipf_plugin/
*
* /home/www-data/cipf_plugin/test.php
* _plugin_dir_path /home/www-data
* _plugin_name cipf_plugin
* _file_dir_path ''
* _file_name test.php
* _root_path /home/www-data/cipf_plugin/
*
*/
private static function set_root_dir() {
if (isset(self::$_plugin_name, self::$_file_name, self::$_file_dir_path, self::$_plugin_dir_path))
return ;
if (isset( self::$_root_url ))
return ;
self::$_root_path = $path;
self::$_root_url = $url;
$exploded_plugin_path = explode('/', plugin_basename( __FILE__ ));
self::$_plugin_name = array_shift($exploded_plugin_path);
self::$_file_name = array_pop($exploded_plugin_path);
self::$_file_dir_path = implode('/', $exploded_plugin_path);
self::$_plugin_dir_path = str_replace('/'.plugin_basename(__DIR__).'/', '', plugin_dir_path(__FILE__));
self::$_root_path = self::$_plugin_dir_path.'/'.self::$_plugin_name.'/';
self::$_root_url = plugins_url(self::$_plugin_name.'/');
}
public static function get_path() {
public static function root_path() {
if (!isset(self::$_plugin_name, self::$_file_name, self::$_file_dir_path, self::$_plugin_dir_path))
self::set_root_dir();
return(self::$_root_path);
}
public static function get_url() {
public static function root_url() {
if (!isset(self::$_plugin_name, self::$_file_name, self::$_file_dir_path, self::$_plugin_dir_path))
self::set_root_dir();
return(self::$_root_url);
}
@@ -162,8 +188,10 @@ class PLGNTLS_class
public function add_to_front($srcs_arr = null, $vars = null) {
if (!is_array($vars))
$vars = array();
if (!is_null($srcs_arr))
if (!is_null($srcs_arr)) {
$this->add_fetch($srcs_arr, $vars);
$this->add_default_css($srcs_arr, $vars);
}
$srcs = array();
foreach($srcs_arr as $src_key => $src_value) {
@@ -199,18 +227,18 @@ class PLGNTLS_class
/**
* for fetch, we add the file script that contains the fetch function
* it is an es6 module with the export keyword
* that way, it can also be used by modules scripts
* to achieve that, we wouldn't need to include it here though
* but we need that the fetch function is also available for inline scripts
* so we could attach a copy of the script file without the export keyword
* but instead, inside the file, we add the fetch function to the global scope
*/
/*
* for fetch, we add the file script that contains the fetch function
* it is an es6 module with the export keyword
* that way, it can also be used by modules scripts
* to achieve that, we wouldn't need to include it here though
* but we need that the fetch function is also available for inline scripts
* so we could attach a copy of the script file without the export keyword
* but instead, inside the file, we add the fetch function to the global scope
*/
private function add_fetch(&$srcs_arr, &$vars) {
// add fetch script at beginning of scripts list
array_unshift($srcs_arr, array("utils/plgntls_fetch.js", 'type'=>'module'));
array_unshift($srcs_arr, array(self::$_file_dir_path."/plgntls_fetch.js", 'type'=>'module'));
// for the custom endpoints in rest api to work
// they need to have a nonce named 'wp_rest'
// see : https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
@@ -220,34 +248,39 @@ class PLGNTLS_class
$vars = array_merge($vars, $fetch_url);
}
private function add_default_css(&$srcs_arr, &$vars) {
// add default css file at beginning of style list
array_unshift($srcs_arr, self::$_file_dir_path."/plgntls_default.css");
}
/**
* @param two arguments :
* 1. html files to include in front
* - can be a string of 1 filename
* - or an array of strings of filenames
* ( https://stackoverflow.com/q/4747876/9497573 )
* - it's probably better to only add 1 file, and let it include other files
* 2. list of variables to make available to this files
* - in the form of key => val
* - recommanded to do it with compact()
* ex: create_html( "file.html", compact("var1","var2",) );
* 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
*/
/*
* @param two arguments :
* 1. html files to include in front
* - can be a string of 1 filename
* - or an array of strings of filenames
* ( https://stackoverflow.com/q/4747876/9497573 )
* - it's probably better to only add 1 file, and let it include other files
* 2. list of variables to make available to this files
* - in the form of key => val
* - recommanded to do it with compact()
* ex: create_html( "file.html", compact("var1","var2",) );
* 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))
return null;
$plgn_dir = $this->get_path();
$plgn_dir = $this->root_path();
if (!is_null($vars))
extract($vars);
@@ -261,18 +294,18 @@ class PLGNTLS_class
return $html;
}
/**
* pass variables to js front as global variables
* @param array : list of key => value
* with the key being name of the variable, like this :
* 'my_var' => 'value',
* simpler way to do it is to use compact when calling the function :
* add_var_to_front(compact("var1", "var2", "var3"));
* @param string (optionnal) : name of first embended script that need these variables
* (it will be available to this script and all followings)
* this name is the filename + "_" + extension :
* init.js -> init_js
*/
/*
* pass variables to js front as global variables
* @param array : list of key => value
* with the key being name of the variable, like this :
* 'my_var' => 'value',
* simpler way to do it is to use compact when calling the function :
* add_var_to_front(compact("var1", "var2", "var3"));
* @param string (optionnal) : name of first embended script that need these variables
* (it will be available to this script and all followings)
* this name is the filename + "_" + extension :
* init.js -> init_js
*/
private function add_vars_to_front($vars_arr)
{
if (is_null($this->_first_script))
@@ -323,16 +356,16 @@ class PLGNTLS_class
add_filter( 'wp_script_attributes', fn($attr)=>$this->add_attributes_to_script($attr), 10, 1 );
/*
* uncomment to print all enqueued files, can be usefull
*/
/*
global $wp_scripts;
error_log("wp_scripts->queue:");
error_log(json_encode($wp_scripts->queue));
global $wp_styles;
error_log("wp_styles->queue:");
error_log(json_encode($wp_styles->queue));
*/
* uncomment to print all enqueued files, can be usefull
*/
/*
global $wp_scripts;
error_log("wp_scripts->queue:");
error_log(json_encode($wp_scripts->queue));
*/
global $wp_styles;
error_log("wp_styles->queue:");
error_log(json_encode($wp_styles->queue));
}
private function add_script($script, $previous_js_basename) {
if (is_null($this->_first_script))
@@ -352,22 +385,12 @@ class PLGNTLS_class
$handle = $src->depends;
if ($handle === null || empty($handle))
$handle = $this->_first_script;
if ($handle === null || empty($handle)) {
global $wp_scripts;
$script_queue = $wp_scripts->queue;
$handle = array_pop($script_queue);
}
wp_add_inline_script($handle, $src->src, 'before');
}
private function add_inline_style($src) {
$handle = $src->depends;
if ($handle === null || empty($handle))
$handle = $this->_first_style;
if ($handle === null || empty($handle)) {
global $wp_styles;
$style_queue = $wp_styles->queue;
$handle = array_pop($style_queue);
}
wp_add_inline_style($handle, $src->src);
}
@@ -452,7 +475,6 @@ class PLGNTLS_class
];
// 7. depends
$src->depends = '';
if (is_string($key))
$src->depends = $key;
@@ -495,8 +517,7 @@ class PLGNTLS_class
// basename handle
// https://www.url.com/route -> www.url.com/route -> www_url_com_route
// path/to/script.js -> script.js -> script_js
if ($src->ext === "url")
{
if ($src->ext === "url") {
$url = parse_url($src->src);
$src->basename = $url['host'] . $url['path'];
}
@@ -513,8 +534,8 @@ class PLGNTLS_class
$src->version = null;
}
else {
$src->url = $this->get_url().$src->src;
$src->path = $this->get_path().$src->src;
$src->url = $this->root_url().$src->src;
$src->path = $this->root_path().$src->src;
$src->version = date("ymd-Gis", filemtime($src->path));
}

View File

@@ -0,0 +1,4 @@
/*
* this css file will be automatically added to front by PLGNTLS_class
* no need to write any style here, but you can if you want
*/