Skip to content

Commit

Permalink
Make admin pages more DRY.
Browse files Browse the repository at this point in the history
  • Loading branch information
chesio committed May 31, 2016
1 parent ad77e4a commit 8478235
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 835 deletions.
57 changes: 57 additions & 0 deletions all-in-one-wp-security/admin/wp-security-admin-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,63 @@
/* Parent class for all admin menu classes */
abstract class AIOWPSecurity_Admin_Menu
{
/* Specify all the tabs of menu in the following array */
var $menu_tabs;

/**
* Renders menu page
* @param string $title Page title
*/
function __construct($title)
{
$this->render_menu_page($this->get_current_tab(), $title);
}

/**
* Returns current tab from _GET parameter (checks it against defined tabs first).
* If no current tab is given or value is invalid, returns first tab.
*
* @return string Current tab key
*/
function get_current_tab()
{
$tab_keys = array_keys($this->menu_tabs);
return isset($_GET['tab']) && isset($this->menu_tabs[$_GET['tab']]) ? $_GET['tab'] : $tab_keys[0];
}

/**
* Render page
* @param string $current_tab Current tab key
* @param string $title Page title
*/
function render_menu_page($current_tab, $title)
{
?>
<div class="wrap">
<h2><?php echo esc_html($title); // echo page title ?></h2>
<?php $this->render_menu_tabs($current_tab); // render page tab navigation ?>
<div id="poststuff"><div id="post-body">
<?php call_user_func(array($this, $this->menu_tabs_handler[$current_tab])); // render current tab content ?>
</div></div>
</div><!-- end of wrap -->
<?php
}

/**
* Renders our tabs of menu as nav items
* @param string $current_tab Current tab key (to highlight tab nav item)
*/
function render_menu_tabs($current_tab)
{
echo '<h2 class="nav-tab-wrapper">';
foreach ( $this->menu_tabs as $tab_key => $tab_caption )
{
$active = $current_tab == $tab_key ? 'nav-tab-active' : '';
echo '<a class="nav-tab ' . $active . '" href="?page=' . $this->menu_page_slug . '&tab=' . $tab_key . '">' . $tab_caption . '</a>';
}
echo '</h2>';
}

/**
* Shows postbox for settings menu
*
Expand Down
60 changes: 5 additions & 55 deletions all-in-one-wp-security/admin/wp-security-blacklist-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,18 @@
class AIOWPSecurity_Blacklist_Menu extends AIOWPSecurity_Admin_Menu
{
var $menu_page_slug = AIOWPSEC_BLACKLIST_MENU_SLUG;

/* Specify all the tabs of this menu in the following array */
var $menu_tabs;

var $menu_tabs_handler = array(
'tab1' => 'render_tab1',
);

function __construct()
{
$this->render_menu_page();
}

function set_menu_tabs()
);

function __construct()
{
$this->menu_tabs = array(
'tab1' => __('Ban Users', 'all-in-one-wp-security-and-firewall'),
'tab1' => __('Ban Users', 'all-in-one-wp-security-and-firewall'),
);
}

function get_current_tab()
{
$tab_keys = array_keys($this->menu_tabs);
$tab = isset( $_GET['tab'] ) ? sanitize_text_field($_GET['tab']) : $tab_keys[0];
return $tab;
}

/*
* Renders our tabs of this menu as nav items
*/
function render_menu_tabs()
{
$current_tab = $this->get_current_tab();

echo '<h2 class="nav-tab-wrapper">';
foreach ( $this->menu_tabs as $tab_key => $tab_caption )
{
$active = $current_tab == $tab_key ? 'nav-tab-active' : '';
echo '<a class="nav-tab ' . $active . '" href="?page=' . $this->menu_page_slug . '&tab=' . $tab_key . '">' . $tab_caption . '</a>';
}
echo '</h2>';
}

/*
* The menu rendering goes here
*/
function render_menu_page()
{
echo '<div class="wrap">';
echo '<h2>'.__('Blacklist Manager','all-in-one-wp-security-and-firewall').'</h2>';//Interface title
$this->set_menu_tabs();
$tab = $this->get_current_tab();
$this->render_menu_tabs();
?>
<div id="poststuff"><div id="post-body">
<?php
//$tab_keys = array_keys($this->menu_tabs);
call_user_func(array(&$this, $this->menu_tabs_handler[$tab]));
?>
</div></div>
</div><!-- end of wrap -->
<?php
parent::__construct(__('Blacklist Manager','all-in-one-wp-security-and-firewall'));
}

function render_tab1()
Expand Down
85 changes: 19 additions & 66 deletions all-in-one-wp-security/admin/wp-security-brute-force-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,35 @@
class AIOWPSecurity_Brute_Force_Menu extends AIOWPSecurity_Admin_Menu
{
var $menu_page_slug = AIOWPSEC_BRUTE_FORCE_MENU_SLUG;

/* Specify all the tabs of this menu in the following array */
var $menu_tabs;

var $menu_tabs_handler = array(
'tab1' => 'render_tab1',
'tab2' => 'render_tab2',
'tab3' => 'render_tab3',
'tab4' => 'render_tab4',
'tab5' => 'render_tab5',
);

function __construct()
{
$this->render_menu_page();
}

function set_menu_tabs()
{
$this->menu_tabs = array(
'tab1' => __('Rename Login Page','all-in-one-wp-security-and-firewall'),
'tab2' => __('Cookie Based Brute Force Prevention', 'all-in-one-wp-security-and-firewall'),
'tab3' => __('Login Captcha', 'all-in-one-wp-security-and-firewall'),
'tab4' => __('Login Whitelist', 'all-in-one-wp-security-and-firewall'),
'tab5' => __('Honeypot', 'all-in-one-wp-security-and-firewall'),

);
}

function get_current_tab()
{
$tab_keys = array_keys($this->menu_tabs);
$tab = isset( $_GET['tab'] ) ? sanitize_text_field($_GET['tab']) : $tab_keys[0];
return $tab;
}
);

/*
* Renders our tabs of this menu as nav items
*/
function render_menu_tabs()
function __construct()
{
$current_tab = $this->get_current_tab();

echo '<h2 class="nav-tab-wrapper">';
foreach ( $this->menu_tabs as $tab_key => $tab_caption )
{
if (AIOWPSecurity_Utility::is_multisite_install() && get_current_blog_id() != 1
&& stristr($tab_caption, "Rename Login Page") === false && stristr($tab_caption, "Login Captcha") === false){
//Suppress the all Brute Force menu tabs if site is a multi site AND not the main site except "rename login" and "captcha"
}else{
$active = $current_tab == $tab_key ? 'nav-tab-active' : '';
echo '<a class="nav-tab ' . $active . '" href="?page=' . $this->menu_page_slug . '&tab=' . $tab_key . '">' . $tab_caption . '</a>';
}
if (AIOWPSecurity_Utility::is_multisite_install() && get_current_blog_id() != 1) {
// Suppress the all Brute Force menu tabs if site is a multi site AND not the main site except "rename login" and "captcha"
$this->menu_tabs = array(
'tab1' => __('Rename Login Page','all-in-one-wp-security-and-firewall'),
'tab3' => __('Login Captcha', 'all-in-one-wp-security-and-firewall'),
);
}
echo '</h2>';
}

/*
* The menu rendering goes here
*/
function render_menu_page()
{
echo '<div class="wrap">';
echo '<h2>'.__('Brute Force','all-in-one-wp-security-and-firewall').'</h2>';//Interface title
$this->set_menu_tabs();
$tab = $this->get_current_tab();
$this->render_menu_tabs();
?>
<div id="poststuff"><div id="post-body">
<?php
//$tab_keys = array_keys($this->menu_tabs);
call_user_func(array(&$this, $this->menu_tabs_handler[$tab]));
?>
</div></div>
</div><!-- end of wrap -->
<?php
else {
$this->menu_tabs = array(
'tab1' => __('Rename Login Page','all-in-one-wp-security-and-firewall'),
'tab2' => __('Cookie Based Brute Force Prevention', 'all-in-one-wp-security-and-firewall'),
'tab3' => __('Login Captcha', 'all-in-one-wp-security-and-firewall'),
'tab4' => __('Login Whitelist', 'all-in-one-wp-security-and-firewall'),
'tab5' => __('Honeypot', 'all-in-one-wp-security-and-firewall'),
);
}

parent::__construct(__('Brute Force','all-in-one-wp-security-and-firewall'));
}

function render_tab1()
Expand Down
52 changes: 2 additions & 50 deletions all-in-one-wp-security/admin/wp-security-dashboard-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

class AIOWPSecurity_Dashboard_Menu extends AIOWPSecurity_Admin_Menu
{
var $dashboard_menu_page_slug = AIOWPSEC_MAIN_MENU_SLUG;

var $menu_tabs;
var $menu_page_slug = AIOWPSEC_MAIN_MENU_SLUG;

var $menu_tabs_handler = array(
'tab1' => 'render_tab1',
Expand All @@ -15,11 +13,6 @@ class AIOWPSecurity_Dashboard_Menu extends AIOWPSecurity_Admin_Menu
);

function __construct()
{
$this->render_menu_page();
}

function set_menu_tabs()
{
$this->menu_tabs = array(
'tab1' => __('Dashboard', 'all-in-one-wp-security-and-firewall'),
Expand All @@ -28,49 +21,8 @@ function set_menu_tabs()
'tab4' => __('Permanent Block List', 'all-in-one-wp-security-and-firewall'),
'tab5' => __('AIOWPS Logs', 'all-in-one-wp-security-and-firewall'),
);
}

function get_current_tab()
{
$tab_keys = array_keys($this->menu_tabs);
$tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : $tab_keys[0];
return $tab;
}

/*
* Renders our tabs of this menu as nav items
*/
function render_menu_tabs()
{
$current_tab = $this->get_current_tab();

echo '<h2 class="nav-tab-wrapper">';
foreach ($this->menu_tabs as $tab_key => $tab_caption) {
$active = $current_tab == $tab_key ? 'nav-tab-active' : '';
echo '<a class="nav-tab ' . $active . '" href="?page=' . $this->dashboard_menu_page_slug . '&tab=' . $tab_key . '">' . $tab_caption . '</a>';
}
echo '</h2>';
}

/*
* The menu rendering goes here
*/
function render_menu_page()
{
echo '<div class="wrap">';
echo '<h2>' . __('Dashboard', 'all-in-one-wp-security-and-firewall') . '</h2>';//Interface title
$this->set_menu_tabs();
$tab = $this->get_current_tab();
$this->render_menu_tabs();
?>
<div id="poststuff"><div id="post-body">
<?php
//$tab_keys = array_keys($this->menu_tabs);
call_user_func(array(&$this, $this->menu_tabs_handler[$tab]));
?>
</div></div>
</div><!-- end of wrap -->
<?php
parent::__construct(__('Dashboard', 'all-in-one-wp-security-and-firewall'));
}

function render_tab1()
Expand Down
65 changes: 7 additions & 58 deletions all-in-one-wp-security/admin/wp-security-database-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@
class AIOWPSecurity_Database_Menu extends AIOWPSecurity_Admin_Menu
{
var $menu_page_slug = AIOWPSEC_DB_SEC_MENU_SLUG;

/* Specify all the tabs of this menu in the following array */
var $menu_tabs;

var $menu_tabs_handler = array(
'tab1' => 'render_tab1',
'tab1' => 'render_tab1',
'tab2' => 'render_tab2',
);

function __construct()
{
$this->render_menu_page();
}

function set_menu_tabs()
);

function __construct()
{
if (AIOWPSecurity_Utility::is_multisite_install() && get_current_blog_id() != 1){
//Suppress the DB prefix change tab if site is a multi site AND not the main site
Expand All @@ -31,54 +23,11 @@ function set_menu_tabs()
'tab2' => __('DB Backup', 'all-in-one-wp-security-and-firewall'),
);
}

}

function get_current_tab()
{
$tab_keys = array_keys($this->menu_tabs);
$tab = isset( $_GET['tab'] ) ? sanitize_text_field($_GET['tab']) : $tab_keys[0];
return $tab;
}

/*
* Renders our tabs of this menu as nav items
*/
function render_menu_tabs()
{
$current_tab = $this->get_current_tab();

echo '<h2 class="nav-tab-wrapper">';
foreach ( $this->menu_tabs as $tab_key => $tab_caption )
{
$active = $current_tab == $tab_key ? 'nav-tab-active' : '';
echo '<a class="nav-tab ' . $active . '" href="?page=' . $this->menu_page_slug . '&tab=' . $tab_key . '">' . $tab_caption . '</a>';
}
echo '</h2>';
parent::__construct(__('Database Security','all-in-one-wp-security-and-firewall'));
}

/*
* The menu rendering goes here
*/
function render_menu_page()
{
echo '<div class="wrap">';
echo '<h2>'.__('Database Security','all-in-one-wp-security-and-firewall').'</h2>';//Interface title
$this->set_menu_tabs();
$tab = $this->get_current_tab();
$this->render_menu_tabs();
?>
<div id="poststuff"><div id="post-body">
<?php
//$tab_keys = array_keys($this->menu_tabs);
call_user_func(array(&$this, $this->menu_tabs_handler[$tab]));
?>
</div></div>
</div><!-- end of wrap -->
<?php
}

function render_tab1()

function render_tab1()
{
global $wpdb, $aio_wp_security;
$old_db_prefix = $wpdb->prefix;
Expand Down
Loading

0 comments on commit 8478235

Please sign in to comment.