- added const dir path and url

- better file organisation
This commit is contained in:
asus
2024-02-09 15:27:38 +01:00
parent 0c0b406de0
commit 35e851cf11
9 changed files with 37 additions and 24 deletions

View File

@@ -14,14 +14,14 @@ function add_files_to_front($files_arr) {
$file_ext = pathinfo($file, PATHINFO_EXTENSION); $file_ext = pathinfo($file, PATHINFO_EXTENSION);
$file_basename = pathinfo($file, PATHINFO_FILENAME); $file_basename = pathinfo($file, PATHINFO_FILENAME);
if ($file_ext === "js") if ($file_ext === "js")
$dir_path = 'scripts/'; $dir_path = 'js/';
else if ($file_ext === "css") else if ($file_ext === "css")
$dir_path = 'styles/'; $dir_path = 'css/';
else else
continue; continue;
$file_url = plugin_dir_url(__DIR__).$dir_path.$file; $file_url = PLUGIN_URL.$dir_path.$file;
$file_path = plugin_dir_path(__DIR__).$dir_path.$file; $file_path = PLUGIN_DIR.$dir_path.$file;
$file_version = date("ymd-Gis", filemtime($file_path)); $file_version = date("ymd-Gis", filemtime($file_path));
if ($file_ext === "js") { if ($file_ext === "js") {
@@ -51,7 +51,6 @@ function add_var_to_front($var_array) {
$js_var = 'const ' . $key . ' = '; $js_var = 'const ' . $key . ' = ';
$js_var .= json_encode($var); $js_var .= json_encode($var);
$js_var .= ';'; $js_var .= ';';
console_log("in php, js_var: " . $js_var);
wp_add_inline_script('myscript', $js_var, 'before'); wp_add_inline_script('myscript', $js_var, 'before');
} }
} }

View File

@@ -5,8 +5,7 @@
https://stackify.com/how-to-log-to-console-in-php/ https://stackify.com/how-to-log-to-console-in-php/
*/ */
function console_log($output) { function console_log($output) {
global $CONSOLE_OFF; if (CONSOLE_OFF)
if ($CONSOLE_OFF)
return; return;
$json_output = json_encode($output, JSON_HEX_TAG); $json_output = json_encode($output, JSON_HEX_TAG);
$js_code = '<script>console.log(' . $json_output . ');</script>'; $js_code = '<script>console.log(' . $json_output . ');</script>';

View File

@@ -7,6 +7,7 @@
- can be a string of 1 filename - can be a string of 1 filename
- or an array of strings of filenames - or an array of strings of filenames
( https://stackoverflow.com/q/4747876/9497573 ) ( 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 2. list of variables to make available to this files
- in the form of key => val - in the form of key => val
- recommanded to do it with compact() - recommanded to do it with compact()
@@ -22,8 +23,7 @@ https://stackoverflow.com/a/4402045/9497573
*/ */
function create_html($files, $vars) { function create_html($files, $vars) {
$files = (array)$files; $files = (array)$files;
$html_dir = PLUGIN_DIR.'html/';
$html_dir = plugin_dir_path(__DIR__).'html/';
extract($vars); extract($vars);
ob_start(); ob_start();

View File

@@ -0,0 +1,14 @@
<?php
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
globals variables
const vs define : https://stackoverflow.com/questions/2447791/php-define-vs-const
*/
/* switch console_log
const CONSOLE_OFF = true;
*/
const CONSOLE_OFF = false;
?>

View File

@@ -9,14 +9,15 @@ Author URI:
*/ */
/* /*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
globale variable to desable server side plugin dir root
console_log all at once
*/ */
define( 'PLUGIN_DIR', plugin_dir_path(__FILE__) );
define( 'PLUGIN_URL', plugin_dir_url(__FILE__) );
$CONSOLE_OFF = true;
$CONSOLE_OFF = false;
@@ -24,10 +25,10 @@ $CONSOLE_OFF = false;
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
inclusions inclusions
*/ */
include_once(PLUGIN_DIR . '/php/utils/globals.php');
include_once(dirname(__FILE__) . '/utils/console_log.php'); include_once(PLUGIN_DIR . '/php/utils/console_log.php');
include_once(dirname(__FILE__) . '/utils/add_to_front.php'); include_once(PLUGIN_DIR . '/php/utils/add_to_front.php');
include_once(dirname(__FILE__) . '/utils/create_html.php'); include_once(PLUGIN_DIR . '/php/utils/create_html.php');
@@ -36,7 +37,6 @@ include_once(dirname(__FILE__) . '/utils/create_html.php');
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
plugin shortcode plugin shortcode
*/ */
function main_shortcode() { function main_shortcode() {
add_files_to_front( array( add_files_to_front( array(
@@ -75,11 +75,12 @@ function main_shortcode() {
add_shortcode('wp_model_plugin', 'main_shortcode'); add_shortcode('wp_model_plugin', 'main_shortcode');
/* /*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
menu plugin menu plugin
*/ */
function plugin_menu() { function plugin_menu() {
add_menu_page( add_menu_page(
'wp model plugin', // webpage title 'wp model plugin', // webpage title