/**
 * Options page for plugin
 * @uses global $menu, $submenu, $current user, $storedmenu, $storedsubmenu
 */
function useradminsimplifier_options_page()
{
    $uas_options = uas_get_admin_options();
    $uas_selecteduser = isset($_POST['uas_user_select']) ? $_POST['uas_user_select'] : '';
    global $menu;
    global $submenu;
    global $current_user;
    global $storedmenu;
    global $storedsubmenu;
    if (!isset($storedmenu)) {
        $storedmenu = $menu;
    }
    if (!isset($storedsubmenu)) {
        $storedsubmenu = $submenu;
    }
    // Store selections to apply later in display loop where every menu option is iterated
    $nowselected = array();
    $menusectionsubmitted = false;
    if (isset($uas_options['selecteduser']) && $uas_options['selecteduser'] != $uas_selecteduser) {
        // User was changed
        $uas_options['selecteduser'] = $uas_selecteduser;
    } else {
        $uas_options['selecteduser'] = $uas_selecteduser;
        // Process submitted menu selections
        if (isset($_POST['uas_reset'])) {
            // Reset options for this user by clearing all their options
            unset($uas_options[$uas_selecteduser]);
        } else {
            if (isset($_POST['menuselection']) && is_array($_POST['menuselection'])) {
                $menusectionsubmitted = true;
                foreach ($_POST['menuselection'] as $key => $value) {
                    // Disable this menu for this user
                    $nowselected[$uas_selecteduser][$value] = 1;
                }
            }
        }
    }
    ?>
<div class="wrap">
	<h2>
		<?php 
    esc_html_e('User Admin Simplifier', 'user_admin_simplifier');
    ?>
	</h2>
	<form action="" method="post" id="uas_options_form" class="uas_options_form">
		<div class="uas_container" id="chooseauser">
			<h3>
				<?php 
    esc_html_e('Choose a user', 'user_admin_simplifier');
    ?>
:
			</h3>
			<select id="uas_user_select" name="uas_user_select" >
				<option>
<?php 
    $blogusers = get_users('orderby=nicename');
    foreach ($blogusers as $user) {
        echo '<option value="' . $user->user_nicename . '" ';
        echo $user->user_nicename == $uas_selecteduser ? 'selected' : '';
        echo '>' . $user->user_nicename . '</option>';
    }
    ?>
			</select>
			</div>
<?php 
    if (isset($_POST['uas_user_select'])) {
        ?>
	<div class="uas_container" id="choosemenus">
		<h3>
			<?php 
        esc_html_e('Select menus/submenus to disable for this user', 'user_admin_simplifier');
        ?>
:
		</h3>
		<input class="uas_dummy" style="display:none;" type="checkbox" checked="checked" value="uas_dummy" id="menuselection[]" name="menuselection[]">
<?php 
        //lets start with top level menus stored in global $menu
        //will add submenu support if needed later
        $rowcount = 0;
        foreach ($storedmenu as $menuitem) {
            $menuuseroption = 0;
            if (!('wp-menu-separator' == $menuitem[4])) {
                //reset							$uas_options[ $uas_selecteduser ][ $menuitem[5] ] = 0;
                if ($menusectionsubmitted) {
                    if (isset($nowselected[$uas_selecteduser][sanitize_key($menuitem[5])])) {
                        //any selected options for this user/menu
                        $menuuseroption = $uas_options[$uas_selecteduser][sanitize_key($menuitem[5])] = $nowselected[$uas_selecteduser][sanitize_key($menuitem[5])];
                    } else {
                        $menuuseroption = $uas_options[$uas_selecteduser][sanitize_key($menuitem[5])] = 0;
                    }
                }
                if (isset($uas_options[$uas_selecteduser][sanitize_key($menuitem[5])])) {
                    //any saved options for this user/menu
                    $menuuseroption = $uas_options[$uas_selecteduser][sanitize_key($menuitem[5])];
                } else {
                    $menuuseroption = 0;
                    $uas_options[$uas_selecteduser][sanitize_key($menuitem[5])] = 0;
                }
                echo '<p class=' . (0 == $rowcount++ % 2 ? '"menumain"' : '"menualternate"') . '>' . '<input type="checkbox" name="menuselection[]" id="menuselection[]" ' . 'value="' . sanitize_key($menuitem[5]) . '" ' . (1 == $menuuseroption ? 'checked="checked"' : '') . ' /> ' . uas_clean_menu_name($menuitem[0]) . "</p>";
                if (!strpos($menuitem[0], 'pending-count')) {
                    //top level menu items with pending count span don't have submenus
                    $topmenu = $menuitem[2];
                    if (isset($storedsubmenu[$topmenu])) {
                        //display submenus
                        echo '<div class="submenu unselected"><a href="javascript:;">' . esc_html__('Show submenus', 'user_admin_simplifier') . '</a></div><div class="submenuinner">';
                        $subrowcount = 0;
                        foreach ($storedsubmenu[$topmenu] as $subsub) {
                            $combinedname = sanitize_key($menuitem[5] . $subsub[2]);
                            $submenuuseroption = 0;
                            if ($menusectionsubmitted) {
                                //deal with submitted checkboxes
                                if (isset($nowselected[$uas_selecteduser][$combinedname])) {
                                    // selected option for this user/submenu
                                    $submenuuseroption = $uas_options[$uas_selecteduser][$combinedname] = $nowselected[$uas_selecteduser][$combinedname];
                                } else {
                                    $uas_options[$uas_selecteduser][$combinedname] = 0;
                                }
                            }
                            if (isset($uas_options[$uas_selecteduser][$combinedname])) {
                                // now show saved options for this user/submenu
                                $submenuuseroption = $uas_options[$uas_selecteduser][$combinedname];
                            } else {
                                $uas_options[$uas_selecteduser][$combinedname] = 0;
                            }
                            echo '<p class=' . (0 == $subrowcount++ % 2 ? '"submain"' : '"subalternate"') . '>' . '<input type="checkbox" name="menuselection[]" id="menuselection[]" ' . 'value="' . $combinedname . '" ' . (1 == $submenuuseroption ? 'checked="checked"' : '') . ' /> ' . uas_clean_menu_name($subsub[0]) . '</p>';
                        }
                        echo '</div>';
                    }
                }
            }
        }
        ?>
	<input name="uas_save" type="submit" id="uas_save" value="<?php 
        esc_attr_e('Save Changes', 'user_admin_simplifier');
        ?>
" /> <br />
	<?php 
        esc_html_e('or', 'user_admin_simplifier');
        ?>
:
	<input name="uas_reset" type="submit" id="uas_reset" value="<?php 
        esc_attr_e('Clear User Settings', 'user_admin_simplifier');
        ?>
" />
	</div>
	<?php 
    }
    ?>
</form>
</div>
 <?php 
    uas_save_admin_options($uas_options);
}
function useradminsimplifier_options_page()
{
    $uas_options = uas_get_admin_options();
    $uas_selecteduser = isset($_POST['uas_user_select']) ? $_POST['uas_user_select'] : '';
    global $menu;
    global $current_user;
    $nowselected = array();
    $menusectionsubmitted = false;
    if (isset($uas_options['selecteduser']) && $uas_options['selecteduser'] != $uas_selecteduser) {
        //user was changed
        $uas_options['selecteduser'] = $uas_selecteduser;
    } else {
        $uas_options['selecteduser'] = $uas_selecteduser;
        //process submitted menu selections
        if (isset($_POST['menuselection']) && is_array($_POST['menuselection'])) {
            $menusectionsubmitted = true;
            foreach ($_POST['menuselection'] as $key => $value) {
                $nowselected[$uas_selecteduser][$value] = 1;
                //disable this menu for this user
            }
        }
    }
    ?>
<div class="wrap">
    <h2> 
		<?php 
    esc_html_e('User Admin Simplifier', 'user_admin_simplifier');
    ?>
    </h2>
    
    <form action="" method="post" id="uas_options_form" class="uas_options_form">
      	<div class="uas_container" id="chooseauser">
        <h3>
        	<?php 
    esc_html_e('Choose a user', 'user_admin_simplifier');
    ?>
: 
        </h3>
        <select id="uas_user_select" name="uas_user_select" >
        <option>
<?php 
    $blogusers = get_users('orderby=nicename');
    foreach ($blogusers as $user) {
        echo '<option value="' . $user->user_nicename . '" ';
        echo $user->user_nicename == $uas_selecteduser ? 'selected' : '';
        echo '>' . $user->user_nicename . '</option>';
    }
    ?>
		 </select>
          </div>
         
<?php 
    if (isset($_POST['uas_user_select'])) {
        ?>
        
    <div class="uas_container" id="choosemenus">
        <h3>
            <?php 
        esc_html_e('Select menus to disable for this user', 'user_admin_simplifier');
        ?>
: 
      </h3> 
        <input class="uas_dummy" style="display:none;" type="checkbox" checked="checked" value="uas_dummy" id="menuselection[]" name="menuselection[]">
<?php 
        //lets start with top level menus stored in global $menu
        //will add submenu support if needed later
        $rowcount = 0;
        foreach ($menu as $menuitem) {
            $menuuseroption = 0;
            if (!('wp-menu-separator' == $menuitem[4])) {
                //reset							$uas_options[$uas_selecteduser][$menuitem[5]]=0;
                if ($menusectionsubmitted) {
                    if (isset($nowselected[$uas_selecteduser][$menuitem[5]])) {
                        //any selected options for this user/menu
                        $menuuseroption = $uas_options[$uas_selecteduser][$menuitem[5]] = $nowselected[$uas_selecteduser][$menuitem[5]];
                    } else {
                        $menuuseroption = $uas_options[$uas_selecteduser][$menuitem[5]] = 0;
                    }
                }
                if (isset($uas_options[$uas_selecteduser][$menuitem[5]])) {
                    //any saved options for this user/menu
                    $menuuseroption = $uas_options[$uas_selecteduser][$menuitem[5]];
                } else {
                    $menuuseroption = 0;
                    $uas_options[$uas_selecteduser][$menuitem[5]] = 0;
                }
                //check if selected user has capability for menu
                //user_can( $user, $capability )
                //echo ( implode ( " ~ ",$menuitem ) );
                //don't allow current user to diable their own access to the plugin
                echo '<p' . (0 == $rowcount++ % 2 ? '' : ' class="alternate"') . '>' . '<input type="checkbox" name="menuselection[]" id="menuselection[]" ' . 'value="' . $menuitem[5] . '" ' . (1 == $menuuseroption ? 'checked="checked"' : '') . ($uas_selecteduser == $current_user->user_nicename && "toplevel_page_useradminsimplifier/useradminsimplifier" == $menuitem[5] ? ' disabled ' : '') . ' /> ' . uas_clean_menu_name($menuitem[0]) . "</p>";
            }
            //menu separator
        }
        ?>
	<input name="uas_save" type="submit" id="uas_save" value="Save Changes" /> <br />
<br />            <?php 
        esc_html_e('or', 'user_admin_simplifier');
        ?>
: 

	<input name="uas_reset" type="button" id="uas_reset" value="Clear User Settings" />

    </div>
 <?php 
    }
    ?>

   </form>
</div>
 <?php 
    uas_save_admin_options($uas_options);
}