505 lines
16 KiB
PHP
505 lines
16 KiB
PHP
<?php
|
|
|
|
/*
|
|
* it means someone outside wp is accessing the file, in this case kill it.
|
|
*/
|
|
if (!defined('ABSPATH')) {
|
|
die('You can not access this file!');
|
|
}
|
|
|
|
|
|
/**
|
|
* include those two lines at the top of the main plugin file
|
|
*
|
|
* include_once( plugin_dir_path(__FILE__) . '/php/utils/plugin_tools.php');
|
|
* PLGNTLS_class::set_root_dir( plugin_dir_path(__FILE__), plugin_dir_url(__FILE__) );
|
|
*
|
|
* PLGNTLS means PLUGIN TOOLS
|
|
*
|
|
* .add_to_front() : this method is made to add files and codes all at once
|
|
* otherwise use the wp functions (wp_enqueue_*, wp_add_inline_*, etc)
|
|
*
|
|
*
|
|
* ex:
|
|
*
|
|
* $my_plugin_class = new PLGNTLS_class();
|
|
* $var_1 = 'value_1';
|
|
* $var_2 = 'value_2';
|
|
* $var_3 = 'value_3';
|
|
* return $my_plugin_class->add_to_front(
|
|
* array( // files added in order by types
|
|
* 'path/to/style1.css', //
|
|
* 'path/to/script1.js', //
|
|
* 'path/to/style2.css', // -> depends on style1.css
|
|
* 'http://my_url1.com', //
|
|
* '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
|
|
* 'handle_name' => 'path/to/script3.js', // -> depends on the script with handle 'handle_name'
|
|
* array( //
|
|
* 'path/to/script4.js', // | add a script with attributes
|
|
* 'attribute_1' => 'value_1', // -> | first element of array must be
|
|
* 'attribute_2' => 'value_2', // | the script, without explicit key
|
|
* ), //
|
|
* array( 'js' => 'var_js' ), // -> add inline js, only first element will be used
|
|
* array( 'css' => 'var_css' ), // -> add inline css, only first element will be used
|
|
* ),
|
|
* compact( // these variables are added to html and js files
|
|
* 'var_1', // - in html files you can access them directly
|
|
* 'var_2', // - in js files they are properties of object PLGNTLS_data
|
|
* 'var_3', // like PLGNTLS_data.var_1;
|
|
* )
|
|
* );
|
|
*
|
|
*/
|
|
|
|
class PLGNTLS_class
|
|
{
|
|
const ACF_CARD_STATE = 'etat_carte';
|
|
const ACF_CARD_PAYMENT_METHOD = 'paiement';
|
|
const ACF_CARD_PRICE_CHOICE = 'tarif';
|
|
const ACF_CARD_PRICE_DELIVERY = 'livraison';
|
|
const ACF_CARD_PRICE_TOTAL = 'somme_a_regler';
|
|
const ACF_CARD_EXPIRATION = 'fin_de_validite';
|
|
const ACF_PROF_IS_ACTIV = 'compte-actif';
|
|
const ACF_PROF_CAN_RENEW = 'renouvellement_possible';
|
|
const ACF_PROF_CGV = 'cgv';
|
|
|
|
const CARD_RENEW_PERIOD = 31; // int : number of days before expiration when renew card start to be possible
|
|
const CARD_VALIDITY_TIME = '1 year'; // string : time of validity of the card (ex: '1 month' or '1 year' or '60 days')
|
|
|
|
const SLUG_PROF_INACTIV = 'validation-en-cours';
|
|
const SLUG_RENEW_CARD = 'commande';
|
|
const SLUG_PAGE_REDIRECTION = 'redirection_cipf';
|
|
const SLUG_PAYPAL_REDIRECTION_SUCCESS = self::SLUG_PAGE_REDIRECTION;
|
|
const SLUG_PAYPAL_REDIRECTION_FAILURE = self::SLUG_PAGE_REDIRECTION;
|
|
|
|
const USER_INFO_DATE_FORMAT = 'd/m/Y'; // for user_infos.php (date format : https://www.php.net/manual/fr/datetime.format.php)
|
|
|
|
const PAYPAL_CLIENT_ID = "AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl";
|
|
const PAYPAL_CLIENT_SECRET = "EGunIhGRjPvn0Z8wXO0JsdhET30OStTAH_IyRsmhimEN23_qiRSFD-ql4tvnulKJw6TitZ-vU-ytc4A-";
|
|
const PAYPAL_API_BASE_URL = "https://api-m.sandbox.paypal.com";
|
|
const PAYPAL_MESSAGE_SUCCESS = '`paiement reussi`';
|
|
const PAYPAL_MESSAGE_FAILURE = '`paiement raté`';
|
|
|
|
|
|
|
|
private static $_root_path;
|
|
private static $_root_url;
|
|
|
|
private $_first_script;
|
|
private $_first_style;
|
|
private $_prefix;
|
|
private $_js_dependencies;
|
|
private $_css_dependencies;
|
|
private $_scripts_attributes;
|
|
|
|
/**
|
|
*/
|
|
public function __construct() {
|
|
$this->_prefix = "PLGNTLS";
|
|
$this->_first_script = null;
|
|
$this->_first_style = null;
|
|
$this->_js_dependencies = array();
|
|
$this->_css_dependencies = array();
|
|
$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 ))
|
|
return ;
|
|
if (isset( self::$_root_url ))
|
|
return ;
|
|
self::$_root_path = $path;
|
|
self::$_root_url = $url;
|
|
}
|
|
public static function get_path() {
|
|
return(self::$_root_path);
|
|
}
|
|
public static function get_url() {
|
|
return(self::$_root_url);
|
|
}
|
|
|
|
|
|
public function add_to_front($srcs_arr = null, $vars = null) {
|
|
if (!is_array($vars))
|
|
$vars = array();
|
|
if (!is_null($srcs_arr))
|
|
$this->add_fetch($srcs_arr, $vars);
|
|
|
|
$srcs = array();
|
|
foreach($srcs_arr as $src_key => $src_value) {
|
|
$init = $this->init_src($src_key, $src_value);
|
|
if ($init !== null)
|
|
$srcs[] = $init;
|
|
}
|
|
if (!is_null($srcs_arr))
|
|
$this->add_srcs_to_front($srcs);
|
|
|
|
if (!is_null($vars))
|
|
$this->add_vars_to_front($vars);
|
|
return $this->create_html($srcs, $vars);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
* PRIVATES FUNCTIONS
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
* 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'));
|
|
// 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/
|
|
$fetch_nonce = array("fetch_nonce" => wp_create_nonce('wp_rest'));
|
|
$fetch_url = array("fetch_url" => get_site_url() . "/wp-json");
|
|
$vars = array_merge($vars, $fetch_nonce);
|
|
$vars = array_merge($vars, $fetch_url);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* @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();
|
|
if (!is_null($vars))
|
|
extract($vars);
|
|
|
|
ob_start();
|
|
foreach($files as $file) {
|
|
if ($file->ext === 'html')
|
|
include($file->path);
|
|
}
|
|
$html = ob_get_clean();
|
|
|
|
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
|
|
*/
|
|
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";
|
|
|
|
$vars_json = json_encode($vars_arr);
|
|
$front = "let $object_name = $vars_json;";
|
|
wp_add_inline_script($handle, $front, 'before');
|
|
}
|
|
|
|
/**
|
|
* @param array : list of files :
|
|
* - with their path from root of their type of file (ex: from js/ to .js files)
|
|
* - and with their extension
|
|
* @param boolean
|
|
* - to add ajax script and variables
|
|
* - default to true
|
|
*/
|
|
private function add_srcs_to_front($srcs_arr) {
|
|
//wp_enqueue_script(<give_it_a_name>, /url/to/script, [depends on], version, defer? );
|
|
//wp_enqueue_style( <give_it_a_name>, /url/to/script, [depends on], version, media );
|
|
|
|
$previous_css_basename = '';
|
|
$previous_js_basename = '';
|
|
foreach ($srcs_arr as $src) {
|
|
if ($src->inline !== null) {
|
|
if ($src->inline === "js")
|
|
$this->add_inline_script($src);
|
|
else if ($src->inline === "css")
|
|
$this->add_inline_style($src);
|
|
}
|
|
else if (in_array($src->ext, array("js", "url"))) {
|
|
$this->add_script($src, $previous_js_basename);
|
|
$previous_js_basename = $src->handle;
|
|
}
|
|
else if ($src->ext === "css") {
|
|
$this->add_style($src, $previous_css_basename);
|
|
$previous_css_basename = $src->handle;
|
|
}
|
|
}
|
|
|
|
// https://developer.wordpress.org/reference/hooks/wp_script_attributes/
|
|
// 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
|
|
*/
|
|
/*
|
|
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))
|
|
$this->_first_script = $script->handle;
|
|
$depends_on = $this->check_dependencies($script, $previous_js_basename);
|
|
if ($depends_on !== null)
|
|
wp_enqueue_script( $script->handle, $script->url, $depends_on, $script->version, true);
|
|
}
|
|
private function add_style($style, $previous_css_basename) {
|
|
if (is_null($this->_first_style))
|
|
$this->_first_style = $style->handle;
|
|
$depends_on = $this->check_dependencies($style, $previous_css_basename);
|
|
if ($depends_on !== null)
|
|
wp_enqueue_style( $style->handle, $style->url, $depends_on, $style->version, '');
|
|
}
|
|
private function add_inline_script($src) {
|
|
$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) {
|
|
error_log("inside add_inline_style");
|
|
$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);
|
|
}
|
|
|
|
|
|
private function add_attributes_to_script($attr) {
|
|
if (empty($attr['id']))
|
|
return $attr;
|
|
$handle = $attr['id'];
|
|
if (isset($this->_scripts_attributes[$handle]))
|
|
$script = $this->_scripts_attributes[$handle];
|
|
else
|
|
return $attr;
|
|
|
|
foreach($script as $key => $value){
|
|
$attr[$key] = $value;
|
|
}
|
|
unset($this->_scripts_attributes[$handle]);
|
|
|
|
return $attr;
|
|
}
|
|
|
|
private function check_dependencies(&$src, $previous_basename)
|
|
{
|
|
if (in_array($src->ext, array("js", "url")))
|
|
{
|
|
global $wp_scripts;
|
|
$already_enqueued = array_search($src->handle, $wp_scripts->queue);
|
|
if ($already_enqueued !== false)
|
|
return null;
|
|
}
|
|
else if ($src->ext === "css")
|
|
{
|
|
global $wp_styles;
|
|
$already_enqueued = array_search($src->handle, $wp_styles->queue);
|
|
if ($already_enqueued !== false)
|
|
return null;
|
|
}
|
|
|
|
$depends_on = array();
|
|
if (isset($src->depends) && $src->depends !== '')
|
|
$depends_on[] = $src->depends;
|
|
if (isset($previous_basename) && $previous_basename !== '')
|
|
$depends_on[] = $previous_basename;
|
|
|
|
return $depends_on;
|
|
}
|
|
|
|
/**
|
|
* @param string : name of the file, with its path from its extension directory
|
|
* - from js/ root for .js files
|
|
* - from css/ root for .css files
|
|
* @return object / null :
|
|
* - null if file is not valid
|
|
* - or an object with all the necessary infos :
|
|
* 0. src : array("name.js", ...) -> "name.js"
|
|
* "name.js" -> "name.js"
|
|
* 1. ext : name.js -> "js"
|
|
* 2. basename : name.js -> "name"
|
|
* 3. handle : name.js -> "name_js"
|
|
* 4. url : url to file in wordpress
|
|
* 5. path : path to file in server
|
|
* 6. version : used to avoid browser caching
|
|
* 7. depends : string if depends on a handle, or ''
|
|
* 8. attr : associative array of html attribute like 'type'=>'module'
|
|
* 9. inline : array("js" => "name.js") -> js
|
|
* array("css" => "name.css") -> css
|
|
*/
|
|
private function init_src($key, $value) {
|
|
if (empty($value))
|
|
return null;
|
|
$src = (object)[
|
|
'src' => null,
|
|
'ext' => null,
|
|
'basename' => null,
|
|
'handle' => null,
|
|
'url' => null,
|
|
'path' => null,
|
|
'version' => null,
|
|
'depends' => null,
|
|
'attr' => null,
|
|
'inline' => null,
|
|
];
|
|
|
|
// 7. depends
|
|
$src->depends = '';
|
|
if (is_string($key))
|
|
$src->depends = $key;
|
|
|
|
// 0. src
|
|
// 8. attr
|
|
// 9. inline
|
|
// first element of array is used, so must not be empty
|
|
// value => ['path/to/file', 'key1'=>'value1', 'key2'=>'value2']
|
|
// src => 'path/to/file'
|
|
// attr => ['key1'=>'value1', 'key2'=>'value2']
|
|
if (is_array($value)){
|
|
$first_key = array_keys($value)[0];
|
|
if (empty($value[$first_key]))
|
|
return null;
|
|
if ($first_key === 0) { // is a script file or url with attributes
|
|
$src->src = array_shift($value);
|
|
$src->attr = $value;
|
|
}
|
|
else if ($first_key === "js" || $first_key === "css") { // is an inline code
|
|
$src->src = $value[$first_key];
|
|
$src->inline = $first_key;
|
|
return $src; // inline only needs 'depends', 'src' and 'inline'
|
|
}
|
|
}
|
|
else {
|
|
$src->src = $value;
|
|
$src->attr = null;
|
|
}
|
|
|
|
// 1. ext
|
|
if(filter_var($src->src, FILTER_VALIDATE_URL))
|
|
$src->ext = "url";
|
|
else
|
|
$src->ext = pathinfo($src->src, PATHINFO_EXTENSION);
|
|
if (! in_array($src->ext, array("js", "css", "html", "url")))
|
|
return null;
|
|
|
|
// 2. basename
|
|
// 3. handle
|
|
// 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")
|
|
{
|
|
$url = parse_url($src->src);
|
|
$src->basename = $url['host'] . $url['path'];
|
|
}
|
|
else
|
|
$src->basename = pathinfo($src->src, PATHINFO_BASENAME);
|
|
$src->handle = "PLGNTLS_" . str_replace(array('.', '/'), "_", $src->basename);
|
|
|
|
// 4. url
|
|
// 5. path
|
|
// 6. version
|
|
if ($src->ext === "url") {
|
|
$src->url = $src->src;
|
|
$src->path = null;
|
|
$src->version = null;
|
|
}
|
|
else {
|
|
$src->url = $this->get_url().$src->src;
|
|
$src->path = $this->get_path().$src->src;
|
|
$src->version = date("ymd-Gis", filemtime($src->path));
|
|
}
|
|
|
|
// if ext is 'js' or 'url' and attr is not empty
|
|
// also add to global variable to access in 'wp_script_attributes' filter
|
|
if ($src->ext === 'js' || $src->ext === 'url') {
|
|
if ($src->attr !== null) {
|
|
$this->_scripts_attributes[$src->handle.'-js'] = $src->attr;
|
|
}
|
|
}
|
|
|
|
return $src;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
?>
|