From 94dbe05f879d35aa12343c0b51d95a7d8d83ce21 Mon Sep 17 00:00:00 2001 From: asus Date: Sun, 24 Mar 2024 12:37:38 +0100 Subject: [PATCH] wip patches added to options --- plugins/fbpatch/html/menu.html | 9 +++- plugins/fbpatch/menu/menu_content.php | 27 +++++++---- plugins/fbpatch/php/fbpatch_class.php | 70 ++++++++++++++++++++++++--- 3 files changed, 87 insertions(+), 19 deletions(-) diff --git a/plugins/fbpatch/html/menu.html b/plugins/fbpatch/html/menu.html index 350098e..902a2e1 100644 --- a/plugins/fbpatch/html/menu.html +++ b/plugins/fbpatch/html/menu.html @@ -1,6 +1,13 @@ + + +
- +
diff --git a/plugins/fbpatch/menu/menu_content.php b/plugins/fbpatch/menu/menu_content.php index f84db73..1962cdc 100644 --- a/plugins/fbpatch/menu/menu_content.php +++ b/plugins/fbpatch/menu/menu_content.php @@ -16,8 +16,9 @@ if (!defined('ABSPATH')) { * */ function plugin_content() { -// Fbpatch::get_patchs(); + $patches = Fbpatch::get_patchs(); $nonce = Fbpatch::NONCE; + $admin_post_patches = Fbpatch::ADMIN_POST_PATCH_CHOICE; ob_start(); include(plugin_dir_path(__DIR__) . '/html/menu.html'); $html = ob_get_clean(); @@ -28,25 +29,31 @@ function plugin_content() { -function choose_patches() { +function patches_choice() { $nonce = Fbpatch::NONCE; if (!isset($_POST[$nonce['_name']])) { - \FBPATCH\redirect_menu($_POST); - return; + \FBPATCH\redirect_menu_referer($_POST); + exit; } if (!wp_verify_nonce($_POST[$nonce['_name']], $nonce['_action'])) { - \FBPATCH\redirect_menu($_POST); - return; + \FBPATCH\redirect_menu_referer($_POST); + exit; } - error_log("is logged in: " . json_encode(is_user_logged_in())); - \FBPATCH\redirect_menu($_POST); + + /* + * + * + */ + + + \FBPATCH\redirect_menu_referer($_POST); } -add_action('admin_post_add_patches', __NAMESPACE__.'\choose_patches'); +add_action('admin_post_'.Fbpatch::ADMIN_POST_PATCH_CHOICE, __NAMESPACE__.'\patches_choice'); -function redirect_menu($post) { +function redirect_menu_referer($post) { if (!isset($post)) { wp_redirect(admin_url(), 301); exit; diff --git a/plugins/fbpatch/php/fbpatch_class.php b/plugins/fbpatch/php/fbpatch_class.php index be53f3c..02c60c2 100644 --- a/plugins/fbpatch/php/fbpatch_class.php +++ b/plugins/fbpatch/php/fbpatch_class.php @@ -20,24 +20,78 @@ class Fbpatch { const SLUG_TOOGLE_ADMIN_MENU = ['_name'=>'toogle_admin_menu_url_fbpatch', 'toggle'=>'toggle', 'show'=>'show', 'hide'=>'hide']; const OPTION_TOGGLE_MENU = ['_name'=>'toggle_admin_menu_option_fbpatch', 'show'=>'show', 'hide'=>'hide']; const NONCE = ['_name'=>'nonce_name', '_action'=>'action_name']; + const ADMIN_POST_PATCH_CHOICE = 'add_patches'; - private static $_patchs = ['calculations', 'hide_show']; -// const PATCH_CALCULATIONS = ['_name'=>'calculations', 'title'=>'calculations title', '_description'=>'description---']; -// const PATCH_HIDE_SHOW = ['_name'=>'hide_chow', '_title'=>'hide/show title', '_description'=>'description...']; + private static $_patches = ['_name'=>'fbpatch_list_of_patches', 'calculations', 'hide_show']; + //private static $_patches = ['_name'=>'fbpatch_list_of_patches', 'hide_show']; - private static function set_patchs() { - foreach (self::$_patchs as $patch) { - error_log("patch : " . $patch); + private static function set_option_patchs() { + error_log("---"); + /* + * get the list of patches in option + * create option if needed + * + */ + $raw_patches_option = get_option(self::$_patches['_name']); + error_log("raw_patches_option: " . json_encode($raw_patches_option)); + if (false === $raw_patches_option) { + add_option(self::$_patches['_name']); } + $patches_option = unserialize($raw_patches_option); + if (empty($patches_option)) { + $patches_option = array(); + } + + /* + * if the option miss patches, add them + * + */ + error_log("patches_option before 1: " . json_encode($patches_option)); + foreach (self::$_patches as $key => $patch) { + if ($key === '_name') { + continue; + } + if (isset($patches_option[$patch])) { + continue; + } + $patches_option[$patch] = false; + } + error_log("patches_option after 1: " . json_encode($patches_option)); + + /* + * if the option has additional patches, delete them + * + */ + error_log("patches_option before 2: " . json_encode($patches_option)); + foreach ($patches_option as $key => $patch) { + if (in_array($key, self::$_patches)) { + continue; + } + unset($patches_option[$key]); + } + error_log("patches_option after 2: " . json_encode($patches_option)); + + /* + * change the option list with the update patches + * + */ + ksort($patches_option); + error_log("ksorted patches_option: " . json_encode($patches_option)); + $serialize_patches_option = serialize($patches_option); + error_log("serialize patches_option: " . json_encode($serialize_patches_option)); + update_option(self::$_patches['_name'], $serialize_patches_option); } public static function get_patchs() { - self::set_patchs(); + self::set_option_patchs(); + $patches = get_option(self::$_patches['_name']); + return serialize($patches); } - public static function update_patchs($query) { + public static function set_patchs($query) { } } + ?>