- 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

@@ -19,10 +19,9 @@ if (!defined('ABSPATH')) {
/* /*
* plugin dir root * plugin dir root
PLGNTLS_class::set_root_dir( plugin_dir_path(__FILE__), plugin_dir_url(__FILE__) );
*/ */
include_once( plugin_dir_path(__FILE__) . '/utils/plgntls_class.php'); include_once( plugin_dir_path(__FILE__) . '/utils/plgntls_class.php');
PLGNTLS_class::set_root_dir( plugin_dir_path(__FILE__), plugin_dir_url(__FILE__) );
@@ -30,26 +29,27 @@ PLGNTLS_class::set_root_dir( plugin_dir_path(__FILE__), plugin_dir_url(__FILE__)
* general inclusions * general inclusions
*/ */
// utils : // utils :
include_once(PLGNTLS_class::get_path() . 'php/utils/globals.php'); include_once(PLGNTLS_class::root_path() . 'php/utils/globals.php');
include_once(PLGNTLS_class::get_path() . 'utils/console_log.php'); include_once(PLGNTLS_class::root_path() . 'utils/console_log.php');
include_once(PLGNTLS_class::get_path() . 'php/admin_menu/example_menu.php'); include_once(PLGNTLS_class::root_path() . 'php/admin_menu/example_menu.php');
include_once(PLGNTLS_class::get_path() . 'php/paypal/paypal.php'); include_once(PLGNTLS_class::root_path() . 'php/paypal/paypal.php');
include_once(PLGNTLS_class::root_path() . '/php/user_infos.php');
include_once(PLGNTLS_class::root_path() . '/php/hide_admin.php');
//include_once(PLGNTLS_class::root_path() . '/php/menus.php');
include_once(PLGNTLS_class::root_path() . 'php/register_partenaires.php');
include_once(PLGNTLS_class::root_path() . 'php/redirections.php');
include_once(PLGNTLS_class::root_path() . 'php/author_restriction.php');
include_once(PLGNTLS_class::root_path() . 'php/filter_mail.php');
include_once(PLGNTLS_class::root_path() . 'php/prof_check_page.php');
include_once(PLGNTLS_class::root_path() . 'php/renew_card.php');
include_once(PLGNTLS_class::get_path() . '/php/user_infos.php');
include_once(PLGNTLS_class::get_path() . '/php/hide_admin.php');
//include_once(PLGNTLS_class::get_path() . '/php/menus.php');
include_once(PLGNTLS_class::get_path() . 'php/register_partenaires.php');
include_once(PLGNTLS_class::get_path() . 'php/redirections.php');
include_once(PLGNTLS_class::get_path() . 'php/author_restriction.php');
include_once(PLGNTLS_class::get_path() . 'php/filter_mail.php');
include_once(PLGNTLS_class::get_path() . 'php/prof_check_page.php');
include_once(PLGNTLS_class::get_path() . 'php/renew_card.php');
// form builder patch : // form builder patch :
//include_once(PLGNTLS_class::get_path() . 'php/form_builder_patch/url_validation.php'); //include_once(PLGNTLS_class::root_path() . 'php/form_builder_patch/url_validation.php');
//include_once(PLGNTLS_class::get_path() . 'php/form_builder_patch/multiple_modals.php'); //include_once(PLGNTLS_class::root_path() . 'php/form_builder_patch/multiple_modals.php');

View File

@@ -1,7 +1,7 @@
import { resultMessage } from './result_message.js'; import { resultMessage } from './result_message.js';
import { PLGNTLS_fetch } from '../../utils/plgntls_fetch.js'; import { PLGNTLS_fetch } from '../../utils/plgntls_fetch.js';
/* /*
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend * @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
* *

View File

@@ -18,7 +18,7 @@ if (!defined('ABSPATH')) {
function add_my_jquery_patch() function add_my_jquery_patch()
{ {
$handle = 'jquery_validator_url_patch'; $handle = 'jquery_validator_url_patch';
$url = PLGNTLS_class::get_url() . 'js/form_builder_patch/url_validation.js'; $url = PLGNTLS_class::root_url() . 'js/form_builder_patch/url_validation.js';
$dependencies = array('de_fb_validate'); $dependencies = array('de_fb_validate');
$version = ''; $version = '';
$defer = true; $defer = true;

View File

@@ -9,14 +9,14 @@ if (!defined('ABSPATH')) {
include_once(PLGNTLS_class::get_path() . '/php/utils/http_errors.php'); include_once(PLGNTLS_class::root_path() . '/php/utils/http_errors.php');
include_once(PLGNTLS_class::get_path() . '/php/paypal/route_api_utils.php'); include_once(PLGNTLS_class::root_path() . '/php/paypal/route_api_utils.php');
include_once(PLGNTLS_class::get_path() . '/php/paypal/user_can_pay.php'); include_once(PLGNTLS_class::root_path() . '/php/paypal/user_can_pay.php');
include_once(PLGNTLS_class::get_path() . '/php/paypal/update_user_payment.php'); include_once(PLGNTLS_class::root_path() . '/php/paypal/update_user_payment.php');
include_once(PLGNTLS_class::get_path() . '/php/paypal/shortcode.php'); include_once(PLGNTLS_class::root_path() . '/php/paypal/shortcode.php');
include_once(PLGNTLS_class::get_path() . '/php/paypal/route_api_orders.php'); include_once(PLGNTLS_class::root_path() . '/php/paypal/route_api_orders.php');
include_once(PLGNTLS_class::get_path() . '/php/paypal/route_api_orders_capture.php'); include_once(PLGNTLS_class::root_path() . '/php/paypal/route_api_orders_capture.php');
include_once(PLGNTLS_class::get_path() . '/php/paypal/routes.php'); include_once(PLGNTLS_class::root_path() . '/php/paypal/routes.php');

View File

@@ -138,12 +138,14 @@ function failure_payment_for_user_CIPF($user_id, $order_id, $status) {
$acf_id = 'user_'.$user_id; $acf_id = 'user_'.$user_id;
/* /*
* remove the order_id from user meta * remove the order_id from user meta
* *
*/ */
delete_user_meta($user_id, $meta_order_id, $order_id); delete_user_meta($user_id, $meta_order_id, $order_id);
/* /*
* change payement status to failed * change payement status to failed
* - 'started' -> at order creation * - 'started' -> at order creation
@@ -156,13 +158,11 @@ function failure_payment_for_user_CIPF($user_id, $order_id, $status) {
/* /*
* change user profil : * other changes on user :
* - to active * - pofil is inactive
* - card state is renewal
* *
*/ */
update_field($prof_is_activ['_name'], $prof_is_activ['activ'], $acf_id); update_field($prof_is_activ['_name'], $prof_is_activ['inactiv'], $acf_id);
update_field($acf_card_state['_name'], $acf_card_state['renew'], $acf_id);
} }
@@ -188,12 +188,6 @@ function validate_payment_for_user_CIPF($user_id, $order_id) {
*/ */
delete_user_meta($user_id, $meta_order_id, $order_id); delete_user_meta($user_id, $meta_order_id, $order_id);
/*
* change payement status to success
*
*/
update_field($acf_payment_status['_name'], $acf_payment_status['success'], $acf_id);
/* /*
* acf uses 'Y-m-d H:i:s' format : https://www.advancedcustomfields.com/resources/date-time-picker/ * acf uses 'Y-m-d H:i:s' format : https://www.advancedcustomfields.com/resources/date-time-picker/
* *
@@ -246,15 +240,15 @@ function validate_payment_for_user_CIPF($user_id, $order_id) {
update_field($acf_card_expiration['_name'], $date_plus_one_year->format($acf_date_format), $acf_id); update_field($acf_card_expiration['_name'], $date_plus_one_year->format($acf_date_format), $acf_id);
/* /*
* change user profil : * change user :
* - to active * - profile is active
* - card state is renewal * - card state is renewal
* - cannot renew * - payement status is success
* *
*/ */
update_field($prof_is_activ['_name'], $prof_is_activ['activ'], $acf_id); update_field($prof_is_activ['_name'], $prof_is_activ['activ'], $acf_id);
update_field($acf_card_state['_name'], $acf_card_state['renew'], $acf_id); update_field($acf_card_state['_name'], $acf_card_state['renew'], $acf_id);
//update_field($acf_prof_can_renew['_name'], $acf_prof_can_renew['cannot'] , $acf_id); update_field($acf_payment_status['_name'], $acf_payment_status['success'], $acf_id);
} }

View File

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