diff --git a/plugins/cipf_plugin/plgntls_class.php b/plugins/cipf_plugin/plgntls_class.php index 8e84072..94eef7f 100644 --- a/plugins/cipf_plugin/plgntls_class.php +++ b/plugins/cipf_plugin/plgntls_class.php @@ -8,172 +8,47 @@ if (!defined('ABSPATH')) { } -/** - * include those two lines at the top of the main plugin file - * - * include_once( plugin_dir_path(__FILE__) . '/php/utils/plugin_tools.php'); - * Plgntls::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: - * - * $var_1 = 'value_1'; - * $var_2 = 'value_2'; - * $var_3 = 'value_3'; - * return Plgntls::add_to_front( - * array( // files added in order by types - * 'path/to/style1.css', // - * 'path/to/script1.js', // - * 'style1_css' => 'path/to/style2.css', // -> depends on style1.css - * 'http://my_url1.com', // - * '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 - * '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; - * ) - * ); - * - */ +/* +* 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: +* +* $var_1 = 'value_1'; +* $var_2 = 'value_2'; +* $var_3 = 'value_3'; +* return Plgntls::add_to_front( +* array( // files added in order by types +* 'path/to/style1.css', // +* 'path/to/script1.js', // +* 'style1_css' => 'path/to/style2.css', // -> depends on style1.css +* 'http://my_url1.com', // +* '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 +* '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 { - /* - * const declarations - * - */ - // ACF - const ACF_CARD_STATE = ['_name'=>'etat_carte', 'new'=>'Commande', 'renew'=>'Renouvellement']; // radio button - const ACF_CARD_PAYMENT_METHOD = ['_name'=>'paiement', 'paypal'=>'Paypal', 'transfert'=>'Virement']; // radio button - const ACF_CARD_PRICE_CHOICE = ['_name'=>'tarif', 'low'=>'10', 'high'=>'15']; // radio button - const ACF_CARD_PRICE_DELIVERY = ['_name'=>'livraison', 'pdf'=>'0', 'post'=>'5']; // radio button - const ACF_PROF_IS_ACTIV = ['_name'=>'compte-actif', 'activ'=>'Actif', 'inactiv'=>'Inactif']; // radio button - const ACF_PROF_CGV = ['_name'=>'cgv', 'cgv'=>'cgv']; // checkbox - const ACF_CARD_PRICE_TOTAL = ['_name'=>'somme_a_regler']; // number - const ACF_CARD_NUMBER = ['_name'=>'numero_de_la_carte']; // number - const ACF_CARD_EXPIRATION = ['_name'=>'fin_de_validite']; // date picker - const ACF_CARD_PAYMENT_STATE = ['_name'=>'etat_paiement', 'started'=>'en_cours', 'success'=>'reussi', 'failure'=>'echec', 'nothing'=>'aucun']; // radio button - const ACF_ACCOUNT_STATE = ['_name'=>'etat_compte', 'new'=>'nouveau prof', 'to_pay'=>'doit payer', 'valid'=>'carte valide', 'waiting_invalid'=>'en attente invalide', 'waiting_valid'=>'en attente valide', 'expired'=>'carte expiree']; - const ACF_TRANSFERT_STATE = ['_name'=>'etat_virement', 'success'=>'virement validé']; - const ACF_PAGE_STATE = ['_name'=>'etat_page_partenaire', 'publish'=>'Publiee', 'draft'=>'Brouillon']; - - // META - const META_PAYEMENT_STATUS = 'cipf_payement_status'; - const META_ORDER_ID = 'cipf_order_id'; - - // SLUG - const SLUG_PROF_INACTIV = 'validation-en-cours'; - const SLUG_COMMAND_CARD = 'commande'; - const SLUG_PAGE_REDIRECTION = 'redirection_cipf'; - const SLUG_PAYPAL_PAGE = 'paiement'; - const SLUG_PAYPAL_REDIRECTION_SUCCESS = self::SLUG_PAGE_REDIRECTION; - const SLUG_PAYPAL_REDIRECTION_FAILURE = self::SLUG_PAGE_REDIRECTION; - const SLUG_ADMIN_VALIDATE_PROF = 'admin_activate_prof_cipf'; // for admin_modif_prof.php - const SLUG_PARTNER_REGISTRATION = 'compte-partenaire'; - const SLUG_PARTNER_CREATE_PAGE = 'ma-page-partenaire'; - - // URL - const URL_BASE_REST_ROUTE = 'cipf_plugin/api/v1'; // for routes, in php/paypal/routes.php && php/admin_modif_prof.php - - // QUERY - const QUERY_TOGGLE_PARTNER_PAGE = 'toggle_partner_page_cipf'; - - // PAYPAL - const PAYPAL_SBOX_API_BASE_URL = "https://api-m.sandbox.paypal.com"; - const PAYPAL_LIVE_API_BASE_URL = "https://api-m.paypal.com"; - const PAYPAL_HUGO_SBOX_CLIENT_ID = "AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl"; - const PAYPAL_HUGO_SBOX_CLIENT_SECRET = "EGunIhGRjPvn0Z8wXO0JsdhET30OStTAH_IyRsmhimEN23_qiRSFD-ql4tvnulKJw6TitZ-vU-ytc4A-"; - const PAYPAL_HUGO_LIVE_CLIENT_ID = "Aedn5e8z__hPBvKirqw5bwlhI9ChG8_N6c1xbgybYyBr4B4oP8uVzmVdH1QVKdPQKf6bWg7orPV4PDrO"; - const PAYPAL_HUGO_LIVE_CLIENT_SECRET = "EGeGwfHGxHxsjnC-tH8W0IL4nN3_xlc3sXFRPCQOw5uUoWae3eOgghuDKMnZc5DVGTbP6yIjVJ1BaAra"; - const PAYPAL_DIEGO_SBOX_CLIENT_ID = "AegZZ6vDrTBzUNGf-UOVoUwh51YU4cvjGGPQkOQ7gM3H2xij9Pdkf751WO1ZvLoFjqbeNN-M5F6WqrdL"; - const PAYPAL_DIEGO_SBOX_CLIENT_SECRET = "ENH2n-trvWrs4B6IWdhl7NM_Wp-Rpyo5ONJJMjJFevGVJ2wtSdORPXFx-vPZ2RQGV0RUQzAp6qt4_qVn"; - const PAYPAL_DIEGO_LIVE_CLIENT_ID = "Abp6y2Outx8bMsEQRXBjH7qYK7-sTHmCMWlmJcw0Ctl5c9XpNsbGt5Vl1tD1ZmFhuBGGjT8ec3FKoLCo"; - const PAYPAL_DIEGO_LIVE_CLIENT_SECRET = "EFLFOATSMM1m9BZwfj209qAeYDrsH-ltcFnVBmyPAJM-KjQiMtUVDQNPeyikDO1Y41yMiKu_IoELp_vD"; - //const PAYPAL_CLIENT_ID = self::PAYPAL_HUGO_LIVE_CLIENT_ID; - //const PAYPAL_CLIENT_SECRET = self::PAYPAL_HUGO_LIVE_CLIENT_SECRET; - //const PAYPAL_API_BASE_URL = self::PAYPAL_HUGO_LIVE_API_BASE_URL; - const PAYPAL_MESSAGE_SUCCESS = 'Paiement reussi, vous allez être redirigé-es vers votre espace'; - const PAYPAL_MESSAGE_FAILURE = 'Paiement raté, vous allez être redirigé-es vers votre espace'; - - // ROLES - const ROLE_PROF = 'professeur__professeure'; - const ROLE_PARTNER = 'partenaire'; - const ROLE_FIPF = 'fipf'; - const ROLE_ADMIN = 'administrator'; - - // SHORTCODES - - // OPTIONS - const OPTION_PAYPAL = [ - '_name'=>'cipf_paypal_credentials', - '_default' => [ - 'is_sandbox' =>true, - 'client_id' =>'AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl', - 'client_secret'=>'EGunIhGRjPvn0Z8wXO0JsdhET30OStTAH_IyRsmhimEN23_qiRSFD-ql4tvnulKJw6TitZ-vU-ytc4A-', - ], - ]; - const OPTION_EMAIL = [ - '_name'=>'cipf_email_registration', - '_default'=>[ - 'email'=> -'Bonjour, -Vous venez de créer un compte sur le site carteprof.org avec l’identifiant : $$user_login$$ -La FIPF', - 'is_email_prof'=>true, - 'is_email_partner'=>true, - ], - ]; - const OPTION_PAYMENT = [ - '_name'=>'cipf_payment_messages', - '_default'=>[ - 'success'=> -'Paiement réussi, -vous allez être redirigés vers votre espace', - 'failure'=> -'Paiement échoué, -vous allez être redirigés vers votre espace', - ], - ]; - - - // ADMIN MENU - const SLUG_TOOGLE_ADMIN_MENU = ['_name'=>'toogle_admin_menu_url_cipf', 'toggle'=>'toggle', 'show'=>'show', 'hide'=>'hide']; - const TOGGLE_ADMIN_MENU = ['_name'=>'toggle_admin_menu_option_cipf', 'show'=>'show', 'hide'=>'hide']; - const ADMIN_POST_PAYPAL = 'paypal_credentials_CIPF'; - const ADMIN_MENU_NONCE_PAYPAL = ['_name'=>'nonce_paypal_cipf', '_action'=>'action_admin_menu_paypal_cipf']; - const ADMIN_POST_EMAIL_REGISTRATION = 'email_registration_message_CIPF'; - const ADMIN_MENU_NONCE_EMAIL_REGISTRATION = ['_name'=>'nonce_email_cipf', '_action'=>'action_admin_menu_email_registration_cipf']; - const ADMIN_POST_PAYMENT_MESSAGES = 'payment_messages_CIPF'; - const ADMIN_MENU_NONCE_PAYMENT_MESSAGES = ['_name'=>'nonce_payment_cipf', '_action'=>'action_admin_menu_payment_messages_cipf']; - - // FORMS - const FORM_PROF_COMMANDE_ID = 'prof_commande'; - const FORM_PROF_TRANSFERT_ID = 'prof_valide_virement'; - - // OTHER - 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 USER_INFO_DATE_FORMAT = 'd/m/Y'; // for user_infos.php (date format : https://www.php.net/manual/fr/datetime.format.php) - const ADMIN_VALIDATE_PROF_FIELD = 'admin_activate_prof_cipf'; // for admin_modif_prof.php - const INPUT_HIDDEN_ROLE = 'cipf_user_role'; - - /* * 0 : dont output * 1 : output only level 1 - not always on top of function, output default, only prevent if specifically level 2 @@ -183,68 +58,85 @@ vous allez être redirigés vers votre espace', */ private static $_DEBUG_INFOS = 0; - 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; private static $_instance_count = 0; private static $_adding_count = 0; + private static $_init_count = 0; - private $_first_script; - private $_first_style; - private $_prefix; - private $_js_dependencies; - private $_css_dependencies; - private $_scripts_attributes; + private $_first_script = null; + private $_first_style = null; + private $_js_dependencies = array(); + private $_css_dependencies = array(); + private $_scripts_attributes = array(); + + private static $_prefix = null; + private static $_options_action = null; + private static $_options_list = null; /* */ public function __construct() { - if (!isset(self::$_plugin_name, self::$_file_name, self::$_file_dir_path, self::$_plugin_dir_path)) - self::set_root_dir(); ++self::$_instance_count; - $this->_prefix = "PLGNTLS"; - $this->_first_script = null; - $this->_first_style = null; - $this->_js_dependencies = array(); - $this->_css_dependencies = array(); - $this->_scripts_attributes = array(); + self::init_class(); } - /* - * for debug purposes - * - */ - public static function debug_infos($level = null) { - if (self::$_DEBUG_INFOS === 0) { + + + + public static function init_class() { + if (self::$_init_count > 0) { return; } - else if (self::$_DEBUG_INFOS === 1 && $level === 2) { - return; + ++self::$_init_count; + + self::$_prefix = strtoupper(__CLASS__); + + /* + * 'set paths and urls' init + * + */ + self::_init_root_dir(); + + + /* + * 'debug logs' init + * + */ + + + /* + * 'add to front' init + * + */ + + + /* + * 'add menu' init + * + */ + add_filter("plugin_action_links_".self::$_plugin_dir."/".self::$_main_file, function($links) { + return self::_add_link_to_plugin($links); + }); + + + /* + * 'handle options' init + * + */ + self::$_options_action = 'action_for_admin_post_options_'.self::$_prefix; + self::$_options_list = 'list_of_options_for_this_plugin_'.self::$_prefix; + if (false === get_option(self::$_options_list)) { + add_option(self::$_options_list, '', '', 'no'); } - $trace = debug_backtrace(); - $function = $trace[1]['function']; - $file = $trace[0]['file']; - $line = $trace[0]['line']; - error_log("-debug: function '".$function."' (in ".$file.", line ".$line .')'); + add_action('admin_post_'.self::$_options_action, function() { + self::_handle_admin_post_option(); + }); } - public static function add_to_front($srcs_arr = array(), $vars = array()) { - $instance = new self(); - return $instance->_add_to_front($srcs_arr, $vars); - } - - - - - @@ -254,10 +146,16 @@ vous allez être redirigés vers votre espace', /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* PRIVATES FUNCTIONS +* ( +* ACTIONS FUNCTIONS * * 1. set paths and urls -* 2. add to front +* 2. debug logs +* 3. add to front +* 4. add menu +* 5. handle options +* +* END{ 5{ 4{ 3{ 2{ 1{ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ @@ -282,7 +180,7 @@ vous allez être redirigés vers votre espace', * * * 1 * * * * * * * * - *)( set paths and urls * + *})( set paths and urls * * * * * * * * * * * * @@ -304,54 +202,66 @@ vous allez être redirigés vers votre espace', * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + private static $_plugin_dir_path; + private static $_plugin_dir; + private static $_file_dir_path; + private static $_file_name; + private static $_main_file; + private static $_root_path; + private static $_root_url; + /* * ex: * /home/www-data/cipf_plugin/php/test/test2/test3/test.php * _plugin_dir_path /home/www-data - * _plugin_name cipf_plugin + * _plugin_dir cipf_plugin * _file_dir_path php/test/test2/test3 * _file_name test.php * _root_path /home/www-data/cipf_plugin/ + * _main_file main_file,php * * /home/www-data/cipf_plugin/test.php * _plugin_dir_path /home/www-data - * _plugin_name cipf_plugin + * _plugin_dir cipf_plugin * _file_dir_path '' * _file_name test.php * _root_path /home/www-data/cipf_plugin/ + * _main_file main_file,php * */ - private static function set_root_dir() { - if (isset(self::$_plugin_name, self::$_file_name, self::$_file_dir_path, self::$_plugin_dir_path)) + private static function _init_root_dir() { + if (isset(self::$_plugin_dir, self::$_file_name, self::$_file_dir_path, self::$_plugin_dir_path)) return ; /* * it uses exploded_path_path by removing data from the array * so order is important ! - * plugin_name / path / to / file.php - * exploded [plugin_name, path, to, file.php] - * plugin_name plugin_name [path, to, file.php] - * file_name [path, to] file.php - * file_dir_name path / to + * plugin_dir / path / to / file.php + * exploded [plugin_dir, path, to, file.php] + * plugin_dir plugin_dir [path, to, file.php] + * file_name [path, to] file.php + * file_dir_name path / to */ $exploded_plugin_path = explode('/', plugin_basename( __FILE__ )); - self::$_plugin_name = array_shift($exploded_plugin_path); + self::$_plugin_dir = 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.'/'); + + // https://wordpress.stackexchange.com/questions/19900/how-to-get-main-plugin-theme-file + $plugin_data = get_plugins("/".self::$_plugin_dir); + self::$_main_file = array_keys($plugin_data)[0]; + + self::$_root_path = self::$_plugin_dir_path.'/'.self::$_plugin_dir.'/'; + self::$_root_url = plugins_url(self::$_plugin_dir.'/'); } 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(); + self::init_class(); return(self::$_root_path); } 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(); + self::init_class(); return(self::$_root_url); } @@ -386,7 +296,74 @@ vous allez être redirigés vers votre espace', * * * 2 * * * * * * * * - *)( add to front * + *})( debugs logs * + * * * * * * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ + + public static function debug_infos($level = null) { + if (self::$_DEBUG_INFOS === 0) { + return; + } + else if (self::$_DEBUG_INFOS === 1 && $level === 2) { + return; + } + $trace = debug_backtrace(); + $function = $trace[1]['function']; + $file = $trace[0]['file']; + $line = $trace[0]['line']; + } + + + + + + + + + + + + + + /* + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * 3 * + * * * * * * * + *})( add to front * * * * * * * * * * * * @@ -410,6 +387,11 @@ vous allez être redirigés vers votre espace', + public static function add_to_front($srcs_arr = array(), $vars = array()) { + $instance = new self(); + return $instance->_add_to_front($srcs_arr, $vars); + } + private function _add_to_front($srcs_arr, $vars) { /* * even if the function is called with no srcs @@ -504,9 +486,7 @@ vous allez être redirigés vers votre espace', if ($file->ext === 'html') include($file->path); } - $html = ob_get_clean(); - - return $html; + return ob_get_clean(); } /* @@ -525,7 +505,7 @@ vous allez être redirigés vers votre espace', if (is_null($vars_arr)) return ; - $object_name = $this->_prefix . "_data"; + $object_name = self::$_prefix . "_data"; $vars_json = json_encode($vars_arr); // note : we need to use 'var' instead of 'let' or 'const', // because their scope is restricted to the if statement @@ -849,9 +829,532 @@ vous allez être redirigés vers votre espace', + + + + + + + + + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - *)( end + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * 4 * + * * * * * * * + *})( add menu * + * * * * * * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ + + const OPTION_TOGGLE_MENU = [ + '_name'=>'toggle_admin_menu_option_xtxpatch', + '_default'=>'hide', + 'show'=>'show', + 'hide'=>'hide', + ]; + + + + /* + * options is the options needed to set a new menu + * see _create_menu below for more informations + * + */ + public static function add_menu($menu_options) { + /* + * will init the filter for the toggle + * + */ + self::init_class(); + + if (empty($menu_options)) { + return; + } + + /* + * if menu_options is only a string, we assume it is the callback + * + */ + if (is_string($menu_options)) { + $menu_options = array('callback'=>$menu_options); + } + + self::_create_menu($menu_options); + } + + /* + * triggered by filter "admin_menu" + * + * page_title -> (optional, default 'name') + * name -> (optional, default _plugin_dir) + * capability -> (optional, default 'manage_options') + * slug -> (optional, default 'name') + * callback -> required + * toggle -> (optional, default true) + * + */ + private static function _create_menu($menu_options) { + if (!isset($menu_options['name'])) { + $menu_options['name'] = self::$_plugin_dir; + } + $default = array( + 'page_title'=> $menu_options['name'], + 'name' => $menu_options['name'], + 'capability'=> 'manage_options', + 'slug' => $menu_options['name'], + 'callback' => $menu_options['callback'], + 'toggle' => true, + ); + foreach ($default as $key => $value) { + if (!isset($menu_options[$key])) { + $menu_options[$key] = $value; + } + } + + if (false === $menu_options['toggle']) { + add_menu_page($menu_options['page_title'], $menu_options['name'], $menu_options['capability'], $menu_options['slug'], $menu_options['callback']); + } + else { + self::_toggle_menu($menu_options['page_title'], $menu_options['name'], $menu_options['capability'], $menu_options['slug'], $menu_options['callback']); + } + } + private static function _toggle_menu($menu_page_title, $menu_title, $menu_capability, $menu_slug, $menu_callback) { + $toggle_menu = self::OPTION_TOGGLE_MENU; + + $toggle = self::get_option_safe($toggle_menu); + + if ($toggle === $toggle_menu['hide']) { + remove_menu_page($menu_slug); + } + else if ($toggle === $toggle_menu['show']) { + add_menu_page($menu_page_title, $menu_title, $menu_capability, $menu_slug, $menu_callback); + } + } + + /* + * add link under the plugin in the plugins admin page + * triggered by filter “plugin_action_links_{$plugin_file}” + * + */ + private static function _add_link_to_plugin($links) { + $option_toggle = self::OPTION_TOGGLE_MENU; + + $toggle = self::get_option_safe($option_toggle); + if (false === $toggle) { + return $links; + } + if (!in_array($toggle, ['hide', 'show'])) { + return $links; + } + $state = $toggle === 'show' ? 'hide' : 'show'; + + $link = "".$state." menu"; + $links[] = $link; + + return $links; + } + + + + + /* + * callback of the option toggle_menu + * handle the toggle menu when url is reached + * + */ + public static function toggle_plugin_menu($request, $option_name, $option_value) { + if ($request[$option_name] === 'show') { + update_option($option_name, 'show'); + } + else if ($request[$option_name] === 'hide') { + update_option($option_name, 'hide'); + } + } + + + + + + + + + + + + + + /* + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * 5 * + * * * * * * * + *})( handle options * + * * * * * * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ + + + + public static function get_option_safe($option) { + /* + * first init option, in case it was not already + * it will returns the option name or false + * it also works if the option is the name directly + * + */ + $option_name = self::_init_option($option); + if (false === $option_name) { + return; + } + + /* + * get option + * try to unserialize it + * if unserialization failed, it means it was not serialized + * - unserialize returns false if cannot unserialize, + * - which can also means it unserialized successfully the value 'false' + * - so checking for this special case with the serialized value of 'false' + * + */ + $option_data = get_option($option_name); + $false_serialized = serialize(false); + $option_unserialized = @unserialize($option_data); + if ($option_unserialized === false && $option_data != $false_serialized) { + $ret_option = $option_data; + } + else { + $ret_option = $option_unserialized; + } + + return $ret_option; + } + + public static function update_option_safe($option, $option_data) { + /* + * first init option, in case it was not already + * it will returns the option name or false + * it also works if the option is the name directly + * + */ + $option_name = self::_init_option($option); + if (false === $option_name) { + return; + } + + if (!is_string($option_data)) { + $option_data = serialize($option_data); + } + update_option($option_name, $option_data); + } + + public static function get_option_link_href($option_name) { + $option_data = self::_get_option_data($option_name); + if (false === $option_data) { + return null; + } + $href = admin_url('admin-post.php'); + $href .= "?action=" . $option_data['_action']; + $href .= "&" . $option_data['_nonce_name'] . "=" . wp_create_nonce($option_data['_nonce_action']); + $href .= "&option_name=" . $option_data['_name']; + + return $href; + } + + /* + * this replaces the form opening tag
+ * it creates this tag with the appropriate action, + * and add 3 hidden inputs fields for form action and nonce + * + */ + public static function open_form_option($option_name, $method = "post") { + $option_data = self::_get_option_data($option_name); + if (false === $option_data) { + return null; + } + ob_start(); + echo ''; + echo ''; + echo ''; + wp_nonce_field($option_data["_nonce_action"], $option_data["_nonce_name"], true, true); + return ob_get_clean(); + } + + + private static function _get_option_data($option_name) { + if (!is_string($option_name)) { + return false; + } + + $options_serialized = get_option(self::$_options_list); + $options_unserialized = unserialize($options_serialized); + if (!isset($options_unserialized[$option_name])) { + return false; + } + + return $options_unserialized[$option_name]; + } + + /* + * a valid option_data must contains '_name' and '_default' at least + * if the option was retrieve in the database, + * there is no need to check it again : special field _db = null + * + * returns the option name if ok, or false + * + */ + private static function _init_option($option) { + if (is_string($option)) { + if (self::_is_in_list_option($option)) { + return $option; + } + else { + return false; + } + } + + if (!isset($option['_name'])) { + return false; + } + if (!isset($option['_default'])) { + return false; + } + if (isset($option['_db'])) { + return $option['_name']; + } + + /* + * if wp option does not already exists, just add it + * otherwise, get it and check the values + * + */ + $name = $option['_name']; + if (false === get_option($name)) { + add_option($name, $option['_default'], '', 'no'); + } + + /* + * checks all the default fields : + * - _action, default 'action_for_admin_post_options_'.self::_prefix + * if you use another action, it will not trigger the class handler function + * - _nonce_action + * - _nonce_name + * - _callback, default + * + */ + $default_option = array( + '_action'=>self::$_options_action, + '_nonce_action'=>'nonce_action_'.$name, + '_nonce_name'=>'nonce_name_'.$name, + '_callback'=>__CLASS__.'::default_handle_admin_post_option', + ); + foreach ($default_option as $key => $value) { + if (!isset($option[$key])) { + $option[$key] = $value; + } + } + + /* + * override saved option with new one + * or we could compare each fields and update them if needed + * - but is it not more heavy in computing actions ? + * also add the field '_db' equal to anything (null by default) + * to mark it as coming from this database + * and differentiate it from new options + * + */ + $options_serialized = get_option(self::$_options_list); + $options_unserialized = unserialize($options_serialized); + $option['_db'] = null; + $options_unserialized[$name] = $option; + update_option(self::$_options_list, serialize($options_unserialized), '', 'no'); + + return $name; + } + + private static function _is_in_list_option($option_name) { + $options_serialized = get_option(self::$_options_list); + $options_unserialized = unserialize($options_serialized); + return isset($options_unserialized[$option_name]); + } + + + private static function _handle_admin_post_option() { + if (!isset($_REQUEST)) { + return; + } + $request = $_REQUEST; + + /* + * get values from the request, and unset them : + * - name of the option -> to get the option_data + * then get the option data : + * - nonce infos -> to check the nonce (nonce_name and nonce_action) + * - callback + * with nonce name, get the nonce from the request, and check it + * + */ + unset($request['action']); + $option_name = $request['option_name']; + unset($request['option_name']); + // option data : + $option_data = self::_get_option_data($option_name); + if (!isset( + $option_data['_nonce_name'], + $option_data['_nonce_action'], + $option_data['_callback']) + ) { + error_log("there has been a problem, the option data don't contains the necessary informations (request: " . json_encode($_REQUEST) . ")(option data: " . json_encode($option_data)); + self::redirect_menu_referer(); + exit; + } + $nonce_name = $option_data['_nonce_name']; + $nonce_action = $option_data['_nonce_action']; + $nonce_callback = $option_data['_callback']; + // check the nonce + $nonce = $request[$nonce_name]; + unset($request[$nonce_name]); + if (!wp_verify_nonce($nonce, $nonce_action)) { + self::redirect_menu_referer(); + exit; + } + + /* + * if nonce passed, call the callback + * - with the remaining of the request + * - the option name + * - the option default value + * - and the option default data to have the fields + * + $nonce_callback($request, $option_name, $option_data['_default']); + */ + $option_value = self::get_option_safe($option_name); + if (false === $option_value) { + self::redirect_menu_referer(); + exit; + } + $nonce_callback($request, $option_name, $option_value, $option_data['_default']); + + /* + * then redirects + * + */ + self::redirect_menu_referer(); + exit; + } + + public static function default_handle_admin_post_option($request, $option_name, $option_data, $option_default) { + /* + * in case the option only contains one value, just update it + * + */ + if (is_string($option_data)) { + self::update_option_safe($option_name, $request[$option_name]); + return; + } + + /* + * else, assuming it's a 1 dimension array + * update each values + * + */ + foreach ($option_default as $key => $value) { + if (isset($request[$key])) { + $option_data[$key] = $request[$key]; + } + } + self::update_option_safe($option_name, $option_data); + } + + + public static function redirect_menu_referer() { + if (wp_get_referer()) { + wp_safe_redirect(wp_get_referer()); + exit; + } + else { + wp_safe_redirect(admin_url()); + exit; + } + } + + + + + + + + + /* + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + *}) end * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ } diff --git a/plugins/xtxpatch/php/classes/plgntls_class.php b/plugins/xtxpatch/php/classes/plgntls_class.php index f285edf..508dc1c 100644 --- a/plugins/xtxpatch/php/classes/plgntls_class.php +++ b/plugins/xtxpatch/php/classes/plgntls_class.php @@ -505,7 +505,7 @@ class Plgntls_xtx { if (is_null($vars_arr)) return ; - $object_name = $this->_prefix . "_data"; + $object_name = self::$_prefix . "_data"; $vars_json = json_encode($vars_arr); // note : we need to use 'var' instead of 'let' or 'const', // because their scope is restricted to the if statement