- added const dir path and url
- better file organisation
This commit is contained in:
@@ -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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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>';
|
||||||
@@ -4,12 +4,13 @@
|
|||||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
@param two arguments :
|
@param two arguments :
|
||||||
1. html files to include in front
|
1. html files to include in front
|
||||||
- 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()
|
||||||
ex: create_html( "file.html", compact("var1","var2",) );
|
ex: create_html( "file.html", compact("var1","var2",) );
|
||||||
ex: create_html( array("file1.html", "file2.html"), array("var1"=>"value") );
|
ex: create_html( array("file1.html", "file2.html"), array("var1"=>"value") );
|
||||||
@return a string of html code
|
@return a string of html code
|
||||||
@@ -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();
|
||||||
14
plugins/wp_model_plugin/php/utils/globals.php
Normal file
14
plugins/wp_model_plugin/php/utils/globals.php
Normal 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;
|
||||||
|
|
||||||
|
?>
|
||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user