diff --git a/plugins/xtxpatch/php/classes/plgntls_class.php b/plugins/xtxpatch/php/classes/plgntls_class.php index 1e20660..5dd03fc 100644 --- a/plugins/xtxpatch/php/classes/plgntls_class.php +++ b/plugins/xtxpatch/php/classes/plgntls_class.php @@ -108,8 +108,9 @@ class Plgntls_xtx { * 2. debug logs * 3. add to front * 4. add menu +* 5. handle options * -* END{ 4{ 3{ 2{ 1{ +* END{ 5{ 4{ 3{ 2{ 1{ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ @@ -157,9 +158,10 @@ class Plgntls_xtx { */ private static $_plugin_dir_path; - private static $_plugin_name; + 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; @@ -167,49 +169,53 @@ class Plgntls_xtx { * 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)) + 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::set_root_dir(); 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::set_root_dir(); return(self::$_root_url); } @@ -826,8 +832,17 @@ class Plgntls_xtx { * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - const SLUG_TOOGLE_ADMIN_MENU = ['_name'=>'toogle_admin_menu_url_xtxpatch', 'toggle'=>'toggle', 'show'=>'show', 'hide'=>'hide']; - const OPTION_TOGGLE_MENU = ['_name'=>'toggle_admin_menu_option_xtxpatch', 'show'=>'show', 'hide'=>'hide']; + const SLUG_TOOGLE_ADMIN_MENU = "toggle_admin_menu_url_xtxpatch"; + const ACTION_TOOGLE_ADMIN_MENU = "toggle_admin_menu_url_xtxpatch"; + const OPTION_TOGGLE_MENU = [ + '_name'=>'toggle_admin_menu_option_xtxpatch', + '_default'=>'hide', + 'show'=>'show', + 'hide'=>'hide', + ]; + + + public static function add_menu($options) { if (empty($options)) { @@ -839,53 +854,52 @@ class Plgntls_xtx { add_action('admin_menu', function() use ($options) { return self::_create_menu($options); }); - add_filter('plugin_action_links_xtxpatch/xtxpatch.php', array(__CLASS__, '_add_link_to_plugin')); + 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')); } /* + * triggered by filter "admin_menu" * * page_title -> (optional, default 'name') - * name -> (optional, default _plugin_name) + * name -> (optional, default _plugin_dir) * capability -> (optional, default 'manage_options') * slug -> (optional, default 'name') * callback -> required - * toggle -> (optionale, default true) + * toggle -> (optional, default true) * */ - private static function _create_menu($options) { - if (!isset($options['name'])) { - $options['name'] = self::$_plugin_name; + private static function _create_menu($menu_options) { + if (!isset($menu_options['name'])) { + $menu_options['name'] = self::$_plugin_dir; } $default = array( - 'page_title'=> $options['name'], - 'name' => $options['name'], + 'page_title'=> $menu_options['name'], + 'name' => $menu_options['name'], 'capability'=> 'manage_options', - 'slug' => $options['name'], - 'callback' => $options['callback'], + 'slug' => $menu_options['name'], + 'callback' => $menu_options['callback'], 'toggle' => true, ); foreach ($default as $key => $value) { - if (!isset($options[$key])) { - $options[$key] = $value; + if (!isset($menu_options[$key])) { + $menu_options[$key] = $value; } } - if (false === $options['toggle']) { - add_menu_page($options['page_title'], $options['name'], $options['capability'], $options['slug'], $options['callback']); + 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($options['page_title'], $options['name'], $options['capability'], $options['slug'], $options['callback']); + 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; - if (false === get_option($toggle_menu['_name'])) { - add_option($toggle_menu['_name'], $toggle_menu['hide'], '', 'no'); - } - - $toggle = get_option($toggle_menu['_name']); + $toggle = self::get_option_safe($toggle_menu); if ($toggle === $toggle_menu['hide']) { remove_menu_page($menu_slug); @@ -897,25 +911,38 @@ class Plgntls_xtx { /* * add link under the plugin in the plugins admin page + * triggered by filter “plugin_action_links_{$plugin_file}” * */ public static function _add_link_to_plugin($links) { - $slug_toggle = self::SLUG_TOOGLE_ADMIN_MENU; - $toggle_menu = self::OPTION_TOGGLE_MENU; + $option_toggle = self::OPTION_TOGGLE_MENU; - $toggle = get_option($toggle_menu['_name']); + $toggle = self::get_option_safe($option_toggle); - if ($toggle === $toggle_menu['hide']) { - $links[] = 'show menu'; - } - else if ($toggle === $toggle_menu['show']) { - $links[] = 'hide menu'; + if (!in_array($toggle, ['hide', 'show'])) { + return $links; } + + $link = "show menu"; + $links[] = $link; + return $links; } + + 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)); + } + + /* * handle the toggle menu when url is reached + * triggered by template_redirect hook * */ public static function _toggle_plugin_menu() { @@ -924,7 +951,7 @@ class Plgntls_xtx { global $wp; $current_slug = $wp->request; - if ($current_slug !== $slug_toggle['_name']) { + if ($current_slug !== $slug_toggle) { return; } @@ -935,13 +962,13 @@ class Plgntls_xtx { else if (empty($_GET)) { $show = null; } - if (!isset($_GET[$slug_toggle['toggle']])) { + if (!isset($_GET[$slug_toggle])) { $show = null; } - else if ($_GET[$slug_toggle['toggle']] === $slug_toggle['show']) { + else if ($_GET['toggle'] === 'show') { $show = true; } - else if ($_GET[$slug_toggle['toggle']] === $slug_toggle['hide']) { + else if ($_GET['toggle'] === 'hide') { $show = false; } @@ -962,6 +989,172 @@ class Plgntls_xtx { + + + + + + + + /* + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * 5 * + * * * * * * * + *})( handle options * + * * * * * * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + */ + + + 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)) { + return; + } + return get_option($option['_name']); + } + + public static function get_option_link_href($option) { + $option_data = self::_get_option_data($option); + if (false === $option_data) { + return null; + } + $href = admin_url('admin-post.php'); + $href .= "?action=" . $option_data['_action']; + + 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, $method = "post") { + $option_data = self::_get_option_data($option); + if (false === $option_data) { + return null; + } + $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)) { + return false; + } + return self::$_options[$option['_name']]; + } + + /* + * a valid option must contains '_name' and '_default' at least + * + */ + private static function _init_option($option) { + if (!isset($option['_name'])) { + return false; + } + if (!isset($option['_default'])) { + return false; + } + + /* + * if wp option exists already, just get it + * otherwise, add it + * + */ + $name = $option['_name']; + if (false === get_option($name)) { + add_option($name, $option['_default'], '', 'no'); + } + + /* + * if self::_options does not contains the option yet, add it + * also check that it contains every fields + * - _action + * - _nonce_action + * - _nonce_name + * + */ + $to_set = false; + if (isset(self::$_options[$name])) { + $tmp_option = self::$_options[$name]; + } + else { + $to_set = true; + $tmp_option = $option; + } + // checks all the fields + if (!isset($tmp_option['_action'])) { + $to_set = true; + $tmp_option['_action'] = 'action_' . $name; + } + if (!isset($tmp_option['_nonce_action'])) { + $to_set = true; + $tmp_option['_nonce_action'] = 'nonce_action_' . $name; + } + if (!isset($tmp_option['_nonce_name'])) { + $to_set = true; + $tmp_option['_nonce_name'] = 'nonce_name_' . $name; + } + // if needed, assigns the new value to options + if ($to_set === true) { + self::$_options[$name] = $tmp_option; + } + + return true; + } + + + + + + + + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}) end