wip patches added to options

This commit is contained in:
asus
2024-03-24 12:37:38 +01:00
parent 5c93938b53
commit 94dbe05f87
3 changed files with 87 additions and 19 deletions

View File

@@ -1,6 +1,13 @@
<!--
{"hide_show":false,"calculations":false}
-->
<!-- https://developer.wordpress.org/reference/hooks/admin_post_action/ -->
<form method="POST" action="<?php echo admin_url( 'admin-post.php' ); ?>">
<input type="hidden" name="action" value="add_patches">
<input type="hidden" name="action" value="<?php echo $admin_post_patches; ?>">
<?php wp_nonce_field($nonce['_action'], $nonce['_name']); ?>
<div>
<input type="checkbox" id="calculation_patch" name="calculation" checked />

View File

@@ -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;

View File

@@ -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) {
}
}
?>