more improves in optin gestion in class, automatic handling for basic cases
This commit is contained in:
@@ -487,9 +487,7 @@ class Plgntls_xtx {
|
||||
if ($file->ext === 'html')
|
||||
include($file->path);
|
||||
}
|
||||
$html = ob_get_clean();
|
||||
|
||||
return $html;
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -883,11 +881,8 @@ class Plgntls_xtx {
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
*/
|
||||
|
||||
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',
|
||||
'_callback'=>__CLASS__.'::toggle_plugin_menu',
|
||||
'_default'=>'hide',
|
||||
'show'=>'show',
|
||||
'hide'=>'hide',
|
||||
@@ -919,7 +914,7 @@ class Plgntls_xtx {
|
||||
$menu_options = array('callback'=>$menu_options);
|
||||
}
|
||||
|
||||
self::_create_menu($options);
|
||||
self::_create_menu($menu_options);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1004,18 +999,13 @@ class Plgntls_xtx {
|
||||
* handle the toggle menu when url is reached
|
||||
*
|
||||
*/
|
||||
public static function toggle_plugin_menu($request, $option_name) {
|
||||
error_log("inside toggle_plugin_menu");
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
self::redirect_menu_referer();
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
@@ -1077,12 +1067,38 @@ class Plgntls_xtx {
|
||||
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
|
||||
*
|
||||
*/
|
||||
if (false === self::_init_option($option)) {
|
||||
$option_name = self::_init_option($option);
|
||||
if (false === $option_name) {
|
||||
return;
|
||||
}
|
||||
return get_option($option['_name']);
|
||||
|
||||
$option_data = get_option($option_name);
|
||||
if (!is_string($option_data)) {
|
||||
$option_data = serialize($option_data);
|
||||
}
|
||||
return $option_data;
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -1109,11 +1125,12 @@ class Plgntls_xtx {
|
||||
if (false === $option_data) {
|
||||
return null;
|
||||
}
|
||||
$form = '<form method="'.$method.'" action="'.admin_menu('admin-post.php').'">';
|
||||
$form .= '<input type="hidden" name="action" value="'.$option_data['_action'].'">';
|
||||
$form .= '<input type="hidden" name="option_name" value="'.$option_data['_name'].'">';
|
||||
$form .= wp_nonce_field($option_data["_nonce_action"], $option_data["_nonce_name"], true, true);
|
||||
return $fields;
|
||||
ob_start();
|
||||
echo '<form method="'.$method.'" action="'.admin_url('admin-post.php').'">';
|
||||
echo '<input type="hidden" name="action" value="'.$option_data['_action'].'">';
|
||||
echo '<input type="hidden" name="option_name" value="'.$option_data['_name'].'">';
|
||||
wp_nonce_field($option_data["_nonce_action"], $option_data["_nonce_name"], true, true);
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
|
||||
@@ -1136,8 +1153,19 @@ class Plgntls_xtx {
|
||||
* 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;
|
||||
}
|
||||
@@ -1145,7 +1173,7 @@ class Plgntls_xtx {
|
||||
return false;
|
||||
}
|
||||
if (isset($option['_db'])) {
|
||||
return true;
|
||||
return $option['_name'];
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1164,14 +1192,14 @@ class Plgntls_xtx {
|
||||
* if you use another action, it will not trigger the class handler function
|
||||
* - _nonce_action
|
||||
* - _nonce_name
|
||||
* - _callback
|
||||
* - _callback, default
|
||||
*
|
||||
*/
|
||||
$default_option = array(
|
||||
'_action'=>self::$_options_action,
|
||||
'_nonce_action'=>'nonce_action_'.$name,
|
||||
'_nonce_name'=>'nonce_name_'.$name,
|
||||
'_callback'=>'',
|
||||
'_callback'=>__CLASS__.'::default_handle_admin_post_option',
|
||||
);
|
||||
foreach ($default_option as $key => $value) {
|
||||
if (!isset($option[$key])) {
|
||||
@@ -1194,7 +1222,13 @@ class Plgntls_xtx {
|
||||
$options_unserialized[$name] = $option;
|
||||
update_option(self::$_options_list, serialize($options_unserialized), '', 'no');
|
||||
|
||||
return true;
|
||||
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]);
|
||||
}
|
||||
|
||||
|
||||
@@ -1223,6 +1257,7 @@ class Plgntls_xtx {
|
||||
$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;
|
||||
}
|
||||
@@ -1240,12 +1275,49 @@ class Plgntls_xtx {
|
||||
/*
|
||||
* if nonce passed, call the callback
|
||||
* - with the remaining of the request
|
||||
* - and the option name
|
||||
* - the option name
|
||||
* - and the option value
|
||||
*
|
||||
*/
|
||||
$nonce_callback($request, $option_name);
|
||||
$option_value = self::get_option_safe($option_name);
|
||||
if (false === $option_value) {
|
||||
self::redirect_menu_referer();
|
||||
exit;
|
||||
}
|
||||
$nonce_callback($request, $option_name, $option_value);
|
||||
|
||||
/*
|
||||
* then redirects
|
||||
*
|
||||
*/
|
||||
self::redirect_menu_referer();
|
||||
exit;
|
||||
}
|
||||
|
||||
public static function default_handle_admin_post_option($request, $option_name, $option_data) {
|
||||
/*
|
||||
* 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_data as $key) {
|
||||
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());
|
||||
|
||||
Reference in New Issue
Block a user