diff --git a/plugins/xtxpatch/php/classes/plgntls_class.php b/plugins/xtxpatch/php/classes/plgntls_class.php index fa39dd3..5a5b1ef 100644 --- a/plugins/xtxpatch/php/classes/plgntls_class.php +++ b/plugins/xtxpatch/php/classes/plgntls_class.php @@ -117,6 +117,9 @@ class Plgntls_xtx { * 'add menu' init * */ + add_filter("plugin_action_links_".self::$_plugin_dir."/".self::$_main_file, function($links) { + return self::_add_link_to_plugin($links); + }); /* @@ -128,7 +131,9 @@ class Plgntls_xtx { 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')); + add_action('admin_post_'.self::$_options_action, function() { + self::_handle_admin_post_option(); + }); } @@ -882,7 +887,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'=>'', + '_callback'=>__CLASS__.'::toggle_plugin_menu', '_default'=>'hide', 'show'=>'show', 'hide'=>'hide', @@ -895,15 +900,25 @@ class Plgntls_xtx { * see _create_menu below for more informations * */ - public static function add_menu($options) { + public static function add_menu($menu_options) { + /* + * will init the filter for the toggle + * + */ self::init_class(); - if (empty($options)) { + + if (empty($menu_options)) { return; } - else if (is_string($options)) { - $options = array('callback'=>$options); + + /* + * if menu_options is only a string, we assume it is the callback + * + */ + if (is_string($menu_options)) { + $menu_options = array('callback'=>$menu_options); } - add_filter("plugin_action_links_".self::$_plugin_dir."/".self::$_main_file, array(__CLASS__, '_add_link_to_plugin')); + self::_create_menu($options); } @@ -961,19 +976,21 @@ class Plgntls_xtx { * triggered by filter “plugin_action_links_{$plugin_file}” * */ - public static function _add_link_to_plugin($links) { + 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 = "show menu"; + $link = "".$state." menu"; $links[] = $link; return $links; @@ -983,46 +1000,21 @@ class Plgntls_xtx { /* + * callback of the option toggle_menu * handle the toggle menu when url is reached - * triggered by template_redirect hook * */ - public static function toggle_plugin_menu() { - $slug_toggle = self::SLUG_TOOGLE_ADMIN_MENU; - $toggle_menu = self::OPTION_TOGGLE_MENU; + public static function toggle_plugin_menu($request, $option_name) { + error_log("inside toggle_plugin_menu"); - global $wp; - $current_slug = $wp->request; - if ($current_slug !== $slug_toggle) { - return; + if ($request[$option_name] === 'show') { + update_option($option_name, 'show'); + } + else if ($request[$option_name] === 'hide') { + update_option($option_name, 'hide'); } - $show = null; - if (!isset($_GET)) { - $show = null; - } - else if (empty($_GET)) { - $show = null; - } - if (!isset($_GET[$slug_toggle])) { - $show = null; - } - else if ($_GET['toggle'] === 'show') { - $show = true; - } - else if ($_GET['toggle'] === 'hide') { - $show = false; - } - - if ($show === true) { - update_option($toggle_menu['_name'], $toggle_menu['show']); - } - else if ($show === false) { - update_option($toggle_menu['_name'], $toggle_menu['hide']); - } - - $plugins_menu_url = admin_url('plugins.php'); - wp_redirect($plugins_menu_url, 301); + self::redirect_menu_referer(); exit; } @@ -1093,15 +1085,8 @@ class Plgntls_xtx { return get_option($option['_name']); } - public static function get_option_link_href($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']); + public static function get_option_link_href($option_name) { + $option_data = self::_get_option_data($option_name); if (false === $option_data) { return null; } @@ -1119,15 +1104,15 @@ class Plgntls_xtx { * 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); + public static function open_form_option($option_name, $method = "post") { + $option_data = self::_get_option_data($option_name); if (false === $option_data) { return null; } $form = '
'; $form .= ''; $form .= ''; - $form .= wp_nonce_field($option_data["_nonce_action"], $option_data["_nonce_name"]); + $form .= wp_nonce_field($option_data["_nonce_action"], $option_data["_nonce_name"], true, true); return $fields; } @@ -1148,6 +1133,8 @@ class Plgntls_xtx { /* * 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 * */ private static function _init_option($option) { @@ -1157,10 +1144,13 @@ class Plgntls_xtx { if (!isset($option['_default'])) { return false; } + if (isset($option['_db'])) { + return true; + } /* - * if wp option exists already, just get it - * otherwise, add it + * if wp option does not already exists, just add it + * otherwise, get it and check the values * */ $name = $option['_name']; @@ -1169,70 +1159,102 @@ class Plgntls_xtx { } /* - * if self::_options does not contains the option yet, add it - * also check that it contains every fields + * 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_option = array( + '_action'=>self::$_options_action, + '_nonce_action'=>'nonce_action_'.$name, + '_nonce_name'=>'nonce_name_'.$name, + '_callback'=>'', + ); + 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 * */ - $to_set = false; $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; - $tmp_option = $option; - } - // checks all the fields - if (!isset($tmp_option['_action'])) { - $to_set = true; - $tmp_option['_action'] = self::$_options_action; - } - 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 the options_list - if ($to_set === true) { - $options_unserialized[$name] = $tmp_option; - update_option(self::$_options_list, serialize($options_unserialized), '', 'no'); - } + $option['_db'] = null; + $options_unserialized[$name] = $option; + update_option(self::$_options_list, serialize($options_unserialized), '', 'no'); return true; } - public static function _handle_admin_post_option() { + 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']) + ) { + 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)) { - // redirect to referer + self::redirect_menu_referer(); exit; } -error_log("request: " . json_encode($request)); + /* + * if nonce passed, call the callback + * - with the remaining of the request + * - and the option name + * + */ + $nonce_callback($request, $option_name); + } + + public static function redirect_menu_referer() { + if (wp_get_referer()) { + wp_safe_redirect(wp_get_referer()); + exit; + } + else { + wp_safe_redirect(admin_url()); + exit; + } }