diff --git a/plugins/xtxpatch/php/classes/plgntls_class.php b/plugins/xtxpatch/php/classes/plgntls_class.php index 5dd03fc..fa39dd3 100644 --- a/plugins/xtxpatch/php/classes/plgntls_class.php +++ b/plugins/xtxpatch/php/classes/plgntls_class.php @@ -60,38 +60,78 @@ class Plgntls_xtx { private static $_instance_count = 0; private static $_adding_count = 0; + private static $_init_count = 0; private $_first_script = null; private $_first_style = null; - private $_prefix = 'PLGNTLS_XTX'; 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() { ++self::$_instance_count; + self::init_class(); } - public static function debug_infos($level = null) { - self::_debug_infos($level); + + + + public static function init_class() { + if (self::$_init_count > 0) { + 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 + * + */ + + + /* + * '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'); + } + add_action('admin_post_'.self::$_options_action, array(__CLASS__, '_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); - } - - - - - @@ -102,7 +142,7 @@ class Plgntls_xtx { /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ( -* PRIVATE FUNCTIONS +* ACTIONS FUNCTIONS * * 1. set paths and urls * 2. debug logs @@ -165,6 +205,7 @@ class Plgntls_xtx { private static $_root_path; private static $_root_url; + /* * ex: * /home/www-data/cipf_plugin/php/test/test2/test3/test.php @@ -184,7 +225,7 @@ class Plgntls_xtx { * _main_file main_file,php * */ - private static function set_root_dir() { + private static function _init_root_dir() { if (isset(self::$_plugin_dir, self::$_file_name, self::$_file_dir_path, self::$_plugin_dir_path)) return ; /* @@ -211,11 +252,11 @@ class Plgntls_xtx { self::$_root_url = plugins_url(self::$_plugin_dir.'/'); } public static function root_path() { - self::set_root_dir(); + self::init_class(); return(self::$_root_path); } public static function root_url() { - self::set_root_dir(); + self::init_class(); return(self::$_root_url); } @@ -272,7 +313,7 @@ class Plgntls_xtx { * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - private static function _debug_infos($level) { + public static function debug_infos($level = null) { if (self::$_DEBUG_INFOS === 0) { return; } @@ -342,6 +383,11 @@ class Plgntls_xtx { + 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 @@ -836,6 +882,7 @@ class Plgntls_xtx { const ACTION_TOOGLE_ADMIN_MENU = "toggle_admin_menu_url_xtxpatch"; const OPTION_TOGGLE_MENU = [ '_name'=>'toggle_admin_menu_option_xtxpatch', + '_callback'=>'', '_default'=>'hide', 'show'=>'show', 'hide'=>'hide', @@ -843,21 +890,21 @@ class Plgntls_xtx { - + /* + * options is the options needed to set a new menu + * see _create_menu below for more informations + * + */ public static function add_menu($options) { + self::init_class(); if (empty($options)) { return; } else if (is_string($options)) { $options = array('callback'=>$options); } - add_action('admin_menu', function() use ($options) { - return self::_create_menu($options); - }); - self::set_root_dir(); add_filter("plugin_action_links_".self::$_plugin_dir."/".self::$_main_file, array(__CLASS__, '_add_link_to_plugin')); - add_action('template_redirect', array(__CLASS__, '_toggle_plugin_menu')); - add_action('admin_post_'.self::ACTION_TOOGLE_ADMIN_MENU, array(__CLASS__, '_toggle_plugin_menu_action')); + self::_create_menu($options); } /* @@ -933,11 +980,6 @@ class Plgntls_xtx { } - public static function _toggle_plugin_menu_action() { -error_log("_POST" . json_encode($_POST)); -error_log("_GET" . json_encode($_GET)); -error_log("_REQUEST" . json_encode($_REQUEST)); - } /* @@ -945,7 +987,7 @@ error_log("_REQUEST" . json_encode($_REQUEST)); * triggered by template_redirect hook * */ - public static function _toggle_plugin_menu() { + public static function toggle_plugin_menu() { $slug_toggle = self::SLUG_TOOGLE_ADMIN_MENU; $toggle_menu = self::OPTION_TOGGLE_MENU; @@ -1039,12 +1081,10 @@ error_log("_REQUEST" . json_encode($_REQUEST)); */ - private static $_options = array(); public static function get_option_safe($option) { /* * first init option, in case it was not already - * then get it * */ if (false === self::_init_option($option)) { @@ -1054,12 +1094,21 @@ error_log("_REQUEST" . json_encode($_REQUEST)); } public static function get_option_link_href($option) { - $option_data = self::_get_option_data($option); + /* + * first init option, in case it was not already + * + */ + if (false === self::_init_option($option)) { + return null; + } + $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; } @@ -1077,20 +1126,28 @@ error_log("_REQUEST" . json_encode($_REQUEST)); } $form = '
'; $form .= ''; + $form .= ''; $form .= wp_nonce_field($option_data["_nonce_action"], $option_data["_nonce_name"]); return $fields; } - private static function _get_option_data($option) { - if (false === self::_init_option($option)) { + private static function _get_option_data($option_name) { + if (!is_string($option_name)) { return false; } - return self::$_options[$option['_name']]; + + $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 must contains '_name' and '_default' at least + * a valid option_data must contains '_name' and '_default' at least * */ private static function _init_option($option) { @@ -1114,14 +1171,17 @@ error_log("_REQUEST" . json_encode($_REQUEST)); /* * if self::_options does not contains the option yet, add it * also check that it contains every fields - * - _action + * - _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 * */ $to_set = false; - if (isset(self::$_options[$name])) { - $tmp_option = self::$_options[$name]; + $options_serialized = get_option(self::$_options_list); + $options_unserialized = unserialize($options_serialized); + if (isset($options_unserialized[$name])) { + $tmp_option = $options_unserialized[$name]; } else { $to_set = true; @@ -1130,7 +1190,7 @@ error_log("_REQUEST" . json_encode($_REQUEST)); // checks all the fields if (!isset($tmp_option['_action'])) { $to_set = true; - $tmp_option['_action'] = 'action_' . $name; + $tmp_option['_action'] = self::$_options_action; } if (!isset($tmp_option['_nonce_action'])) { $to_set = true; @@ -1140,15 +1200,42 @@ error_log("_REQUEST" . json_encode($_REQUEST)); $to_set = true; $tmp_option['_nonce_name'] = 'nonce_name_' . $name; } - // if needed, assigns the new value to options + // if needed, assigns the new value to the options_list if ($to_set === true) { - self::$_options[$name] = $tmp_option; + $options_unserialized[$name] = $tmp_option; + update_option(self::$_options_list, serialize($options_unserialized), '', 'no'); } return true; } + public static function _handle_admin_post_option() { + if (!isset($_REQUEST)) { + return; + } + $request = $_REQUEST; + + unset($request['action']); + $option_name = $request['option_name']; + unset($request['option_name']); + + $option_data = self::_get_option_data($option_name); + $nonce_name = $option_data['_nonce_name']; + $nonce_action = $option_data['_nonce_action']; + + $nonce = $request[$nonce_name]; + unset($request[$nonce_name]); + + if (!wp_verify_nonce($nonce, $nonce_action)) { + // redirect to referer + exit; + } + +error_log("request: " . json_encode($request)); + } + + diff --git a/plugins/xtxpatch/php/menu/admin_menu.php b/plugins/xtxpatch/php/menu/admin_menu.php index cdc0bf0..d2f5943 100644 --- a/plugins/xtxpatch/php/menu/admin_menu.php +++ b/plugins/xtxpatch/php/menu/admin_menu.php @@ -18,7 +18,10 @@ if (!defined('ABSPATH')) { * creates the plugin menu * */ -\Plgntls_xtx::add_menu(__NAMESPACE__.'\menu_content'); +function add_plugin_menu() { + \Plgntls_xtx::add_menu(__NAMESPACE__.'\menu_content'); +} +add_action('admin_menu', __NAMESPACE__.'\add_plugin_menu');