Remove Dashboard Access - Version 1.0

Version Description

  • Complete rewrite!
  • New: Limit dashboard access for Admins only or by capability
  • New: Allow/disallow edit-profile access
  • New: Choose your own redirect URL
  • New Filter: rda_default_access_cap - Change default access capability
  • New Filter: rda_toolbar_nodes - Filter which back-end Toolbar nodes are hidden
  • New Filter: rda_frontend_toolbar_nodes - Filter which front-end Toolbar nodes are hidden
Download this release

Release Info

Developer DrewAPicture
Plugin Icon 128x128 Remove Dashboard Access
Version 1.0
Comparing to
See all releases

Code changes from version 0.4 to 1.0

inc/class-rda-remove-access.php ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Remove Dashboard Access Class
4
+ *
5
+ * @since 1.0
6
+ */
7
+
8
+ if ( ! class_exists( 'RDA_Remove_Access' ) ) {
9
+ class RDA_Remove_Access {
10
+
11
+ /**
12
+ * @var $capability
13
+ *
14
+ * String with capability passed from RDA_Options{}
15
+ *
16
+ * @since 1.0
17
+ */
18
+ var $capability;
19
+
20
+ /**
21
+ * @var $settings
22
+ *
23
+ * Array of settings passed from RDA_Options{}
24
+ *
25
+ * @since 1.0
26
+ */
27
+ var $settings = array();
28
+
29
+ /**
30
+ * RDA Remove Access Init
31
+ *
32
+ * @since 1.0
33
+ *
34
+ * @param string $capability Capability passed from RDA_Options instance.
35
+ * @param array $settings Settings array passed from RDA_Options instance.
36
+ */
37
+ function __construct( $capability, $settings ) {
38
+ if ( ! $capability )
39
+ return; // Bail
40
+ else
41
+ $this->capability = $capability;
42
+
43
+ $this->settings = $settings;
44
+
45
+ add_action( 'plugins_loaded', array( $this, 'is_user_allowed' ) );
46
+ }
47
+
48
+ /**
49
+ * Determine if user is allowed to access the Dashboard
50
+ *
51
+ * @since 1.0
52
+ *
53
+ * @uses current_user_can() Checks whether the current user has the specified capability.
54
+ * @return null Bail if the current user has the requisite capability.
55
+ */
56
+ function is_user_allowed() {
57
+ if ( $this->capability && ! current_user_can( $this->capability ) && ! defined( 'DOING_AJAX' ) )
58
+ $this->bdth_hooks();
59
+ else
60
+ return; // Bail
61
+ }
62
+
63
+ /**
64
+ * "Batten down the hatches" Hooks
65
+ *
66
+ * dashboard_redirect - Handles redirecting disallowed users.
67
+ * hide_menus - Hides the admin menus with CSS (not ideal but will suffice).
68
+ * hide_toolbar_items - Hides various Toolbar items on front and back-end.
69
+ *
70
+ * @since 1.0
71
+ */
72
+ function bdth_hooks() {
73
+ add_action( 'admin_init', array( $this, 'dashboard_redirect' ) );
74
+ add_action( 'admin_head', array( $this, 'hide_menus' ) );
75
+ add_action( 'admin_bar_menu', array( $this, 'hide_toolbar_items' ), 999 );
76
+ }
77
+
78
+ /**
79
+ * Dashboard Redirect
80
+ *
81
+ * @since 0.1
82
+ *
83
+ * @uses global $pagenow Used to determine the current page.
84
+ * @uses wp_redirect() Used to redirect disallowed users to chosen URL.
85
+ */
86
+ function dashboard_redirect() {
87
+ global $pagenow;
88
+ if ( 'profile.php' != $pagenow || $this->settings['enable_profile'] != 1 ) {
89
+ wp_redirect( $this->settings['redirect_url'] );
90
+ exit;
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Hide Admin Menus
96
+ *
97
+ * @since 1.0
98
+ *
99
+ * @todo Determine why 'Tools' menu can't be easily unset from admin menu
100
+ * @return null
101
+ */
102
+ function hide_menus() {
103
+ ?>
104
+ <style type="text/css">
105
+ #adminmenuback, #adminmenuwrap {
106
+ display: none;
107
+ }
108
+ .wrap {
109
+ margin-top: 1.5%;
110
+ }
111
+ #wpcontent {
112
+ margin-left: 2%;
113
+ }
114
+ <?php
115
+ }
116
+
117
+ /**
118
+ * Hide Toolbar Items
119
+ *
120
+ * @since 1.0
121
+ *
122
+ * @uses apply_filters() to make front-end and back-end Toolbar node arrays filterable.
123
+ * @param global $wp_admin_bar For remove_node() method access.
124
+ */
125
+ function hide_toolbar_items( $wp_admin_bar ) {
126
+ $edit_profile = $this->settings['enable_profile'] == 0 ? 'edit-profile' : '';
127
+ if ( is_admin() ) {
128
+ $ids = array( 'about', 'comments', 'new-content', $edit_profile );
129
+ $nodes = apply_filters( 'rda_toolbar_nodes', $ids );
130
+ } else {
131
+ $ids = array( 'about', 'dashboard', 'comments', 'new-content', 'edit', $edit_profile );
132
+ $nodes = apply_filters( 'rda_frontend_toolbar_nodes', $ids );
133
+ }
134
+ foreach ( $nodes as $id ) {
135
+ $wp_admin_bar->remove_menu( $id );
136
+ }
137
+ }
138
+
139
+ } // RDA_Remove_Access
140
+
141
+ } // class_exists
languages/remove-wp-dashboard-access.pot ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ msgid ""
2
+ msgstr ""
3
+ "Project-Id-Version: Remove Dashboard Access\n"
4
+ "POT-Creation-Date: 2013-02-15 21:54-0700\n"
5
+ "PO-Revision-Date: 2013-02-15 21:54-0700\n"
6
+ "Last-Translator: \n"
7
+ "Language-Team: Drew Jaynes (DrewAPicture) <info@drewapicture.com>\n"
8
+ "Language: English\n"
9
+ "MIME-Version: 1.0\n"
10
+ "Content-Type: text/plain; charset=UTF-8\n"
11
+ "Content-Transfer-Encoding: 8bit\n"
12
+ "X-Generator: Poedit 1.5.5\n"
13
+ "X-Poedit-KeywordsList: __;_e;esc_attr__;esc_attr_e;esc_html__;esc_html_e;_x;"
14
+ "_ex;esc_attr_x;esc_html_x;_n;_nx;_n_noop;_nx_noop\n"
15
+ "X-Poedit-Basepath: ../\n"
16
+ "X-Poedit-SearchPath-0: .\n"
17
+
18
+ #: remove-wp-dashboard-access.php:91 remove-wp-dashboard-access.php:110
19
+ msgid "Dashboard Access Settings"
20
+ msgstr ""
21
+
22
+ #: remove-wp-dashboard-access.php:92
23
+ msgid "Dashboard Access"
24
+ msgstr ""
25
+
26
+ #: remove-wp-dashboard-access.php:135
27
+ msgid "Access Controls"
28
+ msgstr ""
29
+
30
+ #: remove-wp-dashboard-access.php:136
31
+ msgid "User Access:"
32
+ msgstr ""
33
+
34
+ #: remove-wp-dashboard-access.php:137
35
+ msgid "User Profile Access:"
36
+ msgstr ""
37
+
38
+ #: remove-wp-dashboard-access.php:140
39
+ msgid "Redirection Settings"
40
+ msgstr ""
41
+
42
+ #: remove-wp-dashboard-access.php:141
43
+ msgid "Redirect URL:"
44
+ msgstr ""
45
+
46
+ #: remove-wp-dashboard-access.php:150
47
+ msgid ""
48
+ "Dashboard access can be restricted to Administrators only (default) or users "
49
+ "with a specific capability."
50
+ msgstr ""
51
+
52
+ #: remove-wp-dashboard-access.php:171
53
+ msgid "Administrators only"
54
+ msgstr ""
55
+
56
+ #: remove-wp-dashboard-access.php:175
57
+ msgid "Limit by capability:"
58
+ msgstr ""
59
+
60
+ #: remove-wp-dashboard-access.php:180
61
+ #, php-format
62
+ msgid "You can find out more about specific %s in the Codex."
63
+ msgstr ""
64
+
65
+ #: remove-wp-dashboard-access.php:183
66
+ msgid "Roles and Capabilities"
67
+ msgstr ""
68
+
69
+ #: remove-wp-dashboard-access.php:267
70
+ msgid " Allow users to edit their profiles in the dashboard."
71
+ msgstr ""
72
+
73
+ #: remove-wp-dashboard-access.php:277
74
+ #, php-format
75
+ msgid ""
76
+ "Users who lack the selected role or capability will be redirected to a URL "
77
+ "you specify. Left blank, default is: <strong>%s</strong>"
78
+ msgstr ""
79
+
80
+ #: remove-wp-dashboard-access.php:290
81
+ msgid "Redirect users to:"
82
+ msgstr ""
83
+
84
+ #: remove-wp-dashboard-access.php:312
85
+ #, php-format
86
+ msgid "Please enter a properly-formed URL. For example: %s"
87
+ msgstr ""
88
+
89
+ #: remove-wp-dashboard-access.php:345
90
+ msgid "Settings"
91
+ msgstr ""
readme.txt CHANGED
@@ -1,20 +1,29 @@
1
  === Plugin Name ===
2
- Contributors: DrewAPicture
3
  Donate link: http://www.werdswords.com
4
  Tags: dashboard, access, users, administration
5
- Requires at least: 3.0
6
- Tested up to: 3.2.1
7
- Stable tag: 0.4
8
 
9
- This plugin completely removes non-admin user access to the Dashboard. Non-admins are automatically redirected the site's homepage.
10
 
11
  == Description ==
12
 
13
- This plugin uses the 'manage_options' capability to check whether users are administrators or not. Users lacking this capability will be automatically redirected to the site's homepage.
 
 
 
 
 
 
 
14
 
15
  A full list of capabilities and their associated roles can be found here: http://codex.wordpress.org/Roles_and_Capabilities
16
 
17
- TODO: Provide options to choose your own capability type.
 
 
18
 
19
  == Installation ==
20
 
@@ -23,30 +32,92 @@ TODO: Provide options to choose your own capability type.
23
 
24
  == Frequently Asked Questions ==
25
 
26
- = What happens to non-admin users who try to login to the Dashboard? =
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- Non-admin users will be automatically redirected to the site's homepage
 
 
 
 
 
 
 
 
29
 
30
  == Changelog ==
31
 
32
- = 0.4 = Refined DOING_AJAX check for logged-out users, props @nacin and @BoiteAWeb
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- = 0.3 = Changed cap to manage_options, replaced PHP_SELF with DOING_AJAX
35
 
36
- = 0.2 = Replaced preg_match with admin-ajax test. Added compatibility with rewritten dashboard URLs.
37
 
38
- = 0.1 = Submitted to repository
 
 
 
 
 
 
39
 
40
  == Upgrade Notice ==
41
 
42
- = 0.4 = Refined DOING_AJAX check for logged-out users
43
 
44
- = 0.3 = Improved function.
45
 
46
- = 0.2 = No additional files were added.
47
 
48
- = 0.1 = Initial submission
49
 
50
- == Screenshots ==
 
 
 
 
51
 
52
- 1. No screenshots.
1
  === Plugin Name ===
2
+ Contributors: DrewAPicture, 10up
3
  Donate link: http://www.werdswords.com
4
  Tags: dashboard, access, users, administration
5
+ Requires at least: 3.1
6
+ Tested up to: 3.5.1
7
+ Stable tag: 1.0
8
 
9
+ This plugin limits user access to the dashboard based on whether users have a chosen capability. Disallowed users are redirected to a chosen URL.
10
 
11
  == Description ==
12
 
13
+ Remove Dashboard Access was completely rewritten for version 1.0!
14
+
15
+ New features include:
16
+
17
+ * Limit Dashboard access to Administrators only, or limit by specific capability.
18
+ * Allow/disallow user profile access
19
+ * Choose your own redirect URL
20
+ * (<a href="http://wordpress.org/extend/plugins/remove-dashboard-access-for-non-admins/other_notes/">more info</a>)
21
 
22
  A full list of capabilities and their associated roles can be found here: http://codex.wordpress.org/Roles_and_Capabilities
23
 
24
+ <strong>Contribute to RDA</strong>
25
+
26
+ This plugin is in active development <a href="https://github.com/DrewAPicture/remove-dashboard-access" target="_new">on GitHub</a>. If you'd like to contribute, pull requests are welcome!
27
 
28
  == Installation ==
29
 
32
 
33
  == Frequently Asked Questions ==
34
 
35
+ = What happens to disallowed users who try to login to the Dashboard? =
36
+
37
+ Users lacking the chosen capability or role will be redirected to the URL set in Settings > Dashboard Access.
38
+
39
+ == Other Notes ==
40
+
41
+ <strong>Capabilities</strong>
42
+
43
+ * In v1.0+, you can limit Dashboard access to Admins or by selecting a capability. More information on WordPress' default roles and capabilities can be found here: http://codex.wordpress.org/Roles_and_Capabilities
44
+
45
+ <strong>User Profile Access</strong>
46
+
47
+ * In v1.0+, you can allow or disallow the ability for all users to edit their profiles in the Dashboard. Users lacking the chosen capability won't be able to access any other sections of the Dashboard.
48
+
49
+ <strong>Hiding other plugins/themes' Toolbar menus</strong>
50
+
51
+ v1.0+ hides some built-in WordPress Toolbar menus by default, but can be extended to hide menus from other plugins or themes via two filters: `rda_toolbar_nodes`, and `rda_frontend_toolbar_nodes`.
52
+
53
+ How to find the menu (node) id:
54
+
55
+ * In the HTML page source, look for the `<li>` container for the menu node you're targeting. It should take the form of `<li id="wp-admin-bar-SOMETHING">`
56
+ * In `<li id="wp-admin-bar-SOMETHING">`, you want the "SOMETHING" part.
57
+
58
+ How to filter the disallowed Toolbar nodes on the front-end:
59
+ `
60
+ function hide_some_toolbar_menu( $ids ) {
61
+ $ids[] = 'SOMETHING';
62
+ return $ids;
63
+ }
64
+ add_filter( 'rda_frontend_toolbar_nodes', 'hide_some_toolbar_menu' );
65
+ `
66
+
67
+ Common plugin Toolbar menus and their ids:
68
 
69
+ * <a href="http://wordpress.org/extend/plugins/jetpack/">JetPack by WordPress.com</a> (Notifications) - 'notes'
70
+ * <a href="http://wordpress.org/extend/plugins/wordpress-seo/">WordPress SEO by Yoast</a> - 'wpseo-menu'
71
+ * <a href="http://wordpress.org/extend/plugins/w3-total-cache/">W3 Total Cache</a> - 'w3tc'
72
+
73
+ == Screenshots ==
74
+
75
+ 1. The new 1.0 accesss options screen.
76
+
77
+ 2. Allow users to access their profile settings (only).
78
 
79
  == Changelog ==
80
 
81
+ = 1.0 =
82
+
83
+ * Complete rewrite!
84
+ * New: Limit dashboard access for Admins only or by capability
85
+ * New: Allow/disallow edit-profile access
86
+ * New: Choose your own redirect URL
87
+ * New Filter: `rda_default_access_cap` - Change default access capability
88
+ * New Filter: `rda_toolbar_nodes` - Filter which back-end Toolbar nodes are hidden
89
+ * New Filter: `rda_frontend_toolbar_nodes` - Filter which front-end Toolbar nodes are hidden
90
+
91
+ = 0.4 =
92
+
93
+ * Refined DOING_AJAX check for logged-out users, props @nacin and @BoiteAWeb
94
 
95
+ = 0.3 =
96
 
97
+ * Changed cap to manage_options, replaced PHP_SELF with DOING_AJAX
98
 
99
+ = 0.2 =
100
+
101
+ * Replaced preg_match with admin-ajax test. Added compatibility with rewritten dashboard URLs.
102
+
103
+ = 0.1 =
104
+
105
+ * Submitted to repository
106
 
107
  == Upgrade Notice ==
108
 
109
+ = 0.4 =
110
 
111
+ * Refined DOING_AJAX check for logged-out users
112
 
113
+ = 0.3 =
114
 
115
+ * Improved function.
116
 
117
+ = 0.2 =
118
+
119
+ * No additional files were added.
120
+
121
+ = 0.1 =
122
 
123
+ * Initial submission
remove-wp-dashboard-access.php CHANGED
@@ -1,19 +1,350 @@
1
  <?php
2
- /*
3
- Plugin Name: Remove Dashboard Access for Non-Admins
4
- Plugin URI: http://www.werdswords.com
5
- Description: Removes Dashboard access for non-admin users
6
- Version: 0.4
7
- Author: DrewAPicture
8
- Author URI: http://www.werdswords.com
9
- License: GPLv2
10
  */
11
 
12
- add_action('admin_init', 'no_mo_dashboard');
13
- function no_mo_dashboard() {
14
- if (!current_user_can('manage_options') && !defined('DOING_AJAX') ) {
15
- wp_redirect(site_url()); exit;
16
- }
17
- }
18
 
19
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
+ /**
3
+ * Plugin Name: Remove Dashboard Access
4
+ * Plugin URI: http://www.werdswords.com
5
+ * Description: Removes Dashboard access for certain users based on capability.
6
+ * Version: 1.0
7
+ * Author: Drew Jaynes (DrewAPicture)
8
+ * Author URI: http://www.drewapicture.com
9
+ * License: GPLv2
10
  */
11
 
12
+ // Load options instance
13
+ if ( class_exists( 'RDA_Options' ) )
14
+ $load = new RDA_Options;
 
 
 
15
 
16
+ // RDA_Remove_Access Class
17
+ require_once( dirname( __FILE__ ) . '/inc/class-rda-remove-access.php' );
18
+
19
+ // Run it
20
+ if ( class_exists( 'RDA_Remove_Access' ) )
21
+ new RDA_Remove_Access( $load->capability(), $load->settings );
22
+
23
+
24
+ class RDA_Options {
25
+
26
+ /**
27
+ * @var $settings rda-settings options array
28
+ *
29
+ * @since 1.0
30
+ */
31
+ var $settings = array();
32
+
33
+ /**
34
+ * Init
35
+ *
36
+ * @since 1.0
37
+ */
38
+ function __construct() {
39
+ load_plugin_textdomain( 'remove_dashboard_access', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
40
+
41
+ $this->settings = (array) get_option( 'rda-settings' );
42
+ $this->hooks();
43
+ }
44
+
45
+ /**
46
+ * Action Hooks and Filters
47
+ *
48
+ * activate - Setup options array on activation.
49
+ * options_page - Adds the options page.
50
+ * options_setup - Register options pages settings sections and fields.
51
+ * access_switch_js - Prints jQuery script via admin_head-$suffix for the options page.
52
+ * settings_link - Adds a 'Settings' link to the plugin row links via plugin_action_links_$plugin.
53
+ *
54
+ * @since 1.0
55
+ */
56
+ function hooks() {
57
+ register_activation_hook( __FILE__, array( $this, 'activate' ) );
58
+
59
+ add_action( 'admin_menu', array( $this, 'options_page' ) );
60
+ add_action( 'admin_init', array( $this, 'options_setup' ) );
61
+ add_action( 'admin_head-settings_page_dashboard-access', array( $this, 'access_switch_js' ) );
62
+ add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'settings_link' ) );
63
+ }
64
+
65
+ /**
66
+ * Activation Hook
67
+ *
68
+ * Setup options array on activation.
69
+ *
70
+ * @since 1.0
71
+ */
72
+ function activate() {
73
+ $options = array(
74
+ 'access_switch' => 'manage_options',
75
+ 'access_cap' => 'manage_options',
76
+ 'enable_profile' => 1,
77
+ 'redirect_url' => home_url()
78
+ );
79
+ update_option( 'rda-settings', $options );
80
+ }
81
+
82
+ /**
83
+ * Options page: Remove Access
84
+ *
85
+ * @since 1.0
86
+ *
87
+ * @uses add_options_page() to add a submenu under 'Settings'
88
+ */
89
+ function options_page() {
90
+ add_options_page(
91
+ __( 'Dashboard Access Settings', 'remove_dashboard_access' ),
92
+ __( 'Dashboard Access', 'remove_dashboard_access' ),
93
+ 'manage_options',
94
+ 'dashboard-access',
95
+ array( $this, 'options_page_cb' )
96
+ );
97
+ }
98
+
99
+ /**
100
+ * Options page: callback
101
+ *
102
+ * Outputs the form for the 'Remove Access' submenu
103
+ *
104
+ * @since 1.0
105
+ */
106
+ function options_page_cb() {
107
+ ?>
108
+ <div class="wrap">
109
+ <?php screen_icon(); ?>
110
+ <h2><?php _e( 'Dashboard Access Settings', 'remove_dashboard_access' ); ?></h2>
111
+ <form action="options.php" method="POST" id="rda-options-form">
112
+ <?php
113
+ settings_fields( 'rda-options' );
114
+ do_settings_sections( 'dashboard-access' );
115
+ submit_button();
116
+ ?>
117
+ </form>
118
+ </div><!-- .wrap -->
119
+ <?php
120
+ }
121
+
122
+ /**
123
+ * Register settings and settings sections.
124
+ *
125
+ * @since 1.0
126
+ *
127
+ * @uses register_setting() Registers the 'rda-options' settings group
128
+ * @uses add_settings_section() Adds the settings sections
129
+ * @uses add_settings_filed() Adds the settings fields
130
+ */
131
+ function options_setup() {
132
+ register_setting( 'rda-options', 'rda-settings', array( $this, 'sanitize_options' ) );
133
+
134
+ // Permissions
135
+ add_settings_section( 'rda-permissions', __( 'Access Controls', 'remove_dashboard_access' ), array( $this, 'access_desc_cb' ), 'dashboard-access' );
136
+ add_settings_field( 'access-switch', __( 'User Access:', 'remove_dashboard_access' ), array( $this, 'access_switch_cb' ), 'dashboard-access', 'rda-permissions' );
137
+ add_settings_field( 'profile-enable', __( 'User Profile Access:', 'remove_dashboard_access' ), array( $this, 'profile_enable_cb' ), 'dashboard-access', 'rda-permissions' );
138
+
139
+ // Redirect
140
+ add_settings_section( 'rda-redirect', __( 'Redirection Settings', 'remove_dashboard_access' ), array( $this, 'redirect_desc_cb' ), 'dashboard-access' );
141
+ add_settings_field( 'redirect-url', __( 'Redirect URL:', 'remove_dashboard_access' ), array( $this, 'url_redirect_cb' ), 'dashboard-access', 'rda-redirect' );
142
+ }
143
+
144
+ /**
145
+ * Access Controls Description
146
+ *
147
+ * @since 1.0
148
+ */
149
+ function access_desc_cb() {
150
+ _e( 'Dashboard access can be restricted to Administrators only (default) or users with a specific capability.', 'remove_dashboard_access' );
151
+ }
152
+
153
+ /**
154
+ * Capability-type radio switch display callback
155
+ *
156
+ * Displays the radio button switch for choosing which
157
+ * capability users need to access the Dashboard. Mimics
158
+ * 'Page on front' UI in options-reading.php for a more
159
+ * integrated feel.
160
+ *
161
+ * @since 1.0
162
+ *
163
+ * @uses checked() Activates the checked attribute on the selected option.
164
+ * @uses $this->caps_dropdown() Displays the capabilities dropdown paired with the second access switch radio option.
165
+ */
166
+ function access_switch_cb() {
167
+ $switch = esc_attr( $this->settings['access_switch'] );
168
+ ?>
169
+ <p><label>
170
+ <input name="rda-settings[access_switch]" type="radio" value="manage_options" class="tag" <?php checked( 'manage_options', $switch ); ?> />
171
+ <?php _e( 'Administrators only', 'remove_dashboard_access' ); ?>
172
+ </label></p>
173
+ <p><label>
174
+ <input name="rda-settings[access_switch]" type="radio" value="capability" class="tag" <?php checked( 'capability', $switch ); ?> />
175
+ <?php _e( 'Limit by capability:', 'remove_dashboard_access' ); ?>
176
+ </label>
177
+ <?php $this->output_caps_dropdown(); ?>
178
+ </p>
179
+ <p>
180
+ <?php printf( __( 'You can find out more about specific %s in the Codex.', 'remove_dashboard_access' ),
181
+ sprintf( '<a href="%1$s" target="_new">%2$s</a>',
182
+ esc_url( 'http://codex.wordpress.org/Roles_and_Capabilities' ),
183
+ esc_html( __( 'Roles and Capabilities', 'remove_dashboard_access' ) )
184
+ )
185
+ ); ?>
186
+ </p>
187
+ <?php
188
+ }
189
+
190
+ /**
191
+ * Capability-type radio switch jQuery script
192
+ *
193
+ * When the 'Limit by capability' radio option is selected the script
194
+ * enables the capabilities drop-down. Default state is disabled.
195
+ *
196
+ * @since 1.0
197
+ */
198
+ function access_switch_js() {
199
+ ?>
200
+ <script type="text/javascript">
201
+ jQuery(document).ready(function($){
202
+ var section = $('#rda-options-form'),
203
+ capType = section.find('input:radio[value="capability"]'),
204
+ selects = section.find('select'),
205
+ check_disabled = function(){
206
+ selects.prop( 'disabled', ! capType.prop('checked') );
207
+ };
208
+ check_disabled();
209
+ section.find('input:radio').change(check_disabled);
210
+ });
211
+ </script>
212
+ <?php
213
+ }
214
+
215
+ /**
216
+ * Capability-type switch drop-down
217
+ *
218
+ * @since 1.0
219
+ *
220
+ * @uses global $wp_roles to derive an array of capabilities.
221
+ */
222
+ function output_caps_dropdown() {
223
+ global $wp_roles;
224
+
225
+ $capabilities = array();
226
+ foreach ( $wp_roles->role_objects as $key => $role ) {
227
+ if ( is_array( $role->capabilities ) ) {
228
+ foreach ( $role->capabilities as $cap => $grant )
229
+ $capabilities[$cap] = $cap;
230
+ }
231
+ }
232
+
233
+ // Gather legacy user levels
234
+ $levels = array(
235
+ 'level_0','level_1', 'level_2', 'level_3',
236
+ 'level_4', 'level_5', 'level_6', 'level_7',
237
+ 'level_8', 'level_9', 'level_10'
238
+ );
239
+
240
+ // Remove levels from caps array
241
+ $capabilities = array_diff( $capabilities, $levels );
242
+
243
+ // Alphabetize for nicer display
244
+ ksort( $capabilities );
245
+
246
+ // Start <select> element, plus default first option
247
+ print( '<select name="rda-settings[access_cap]"><option selected="selected" value="manage_options">--- Select One ---</option>' );
248
+
249
+ // Build capabilities dropdown
250
+ foreach ( $capabilities as $capability => $value ) {
251
+ printf( '<option value="%1$s" %2$s>%3$s</option>', esc_attr( $value ), selected( $this->settings['access_cap'], $value ), esc_html( $capability ) );
252
+ }
253
+ print( '</select>' );
254
+ }
255
+
256
+ /**
257
+ * Enable profile access checkbox display callback
258
+ *
259
+ * @since 1.0
260
+ *
261
+ * @uses checked() Outputs the checked attribute when the option is enabled.
262
+ */
263
+ function profile_enable_cb() {
264
+ printf( '<input name="rda-settings[enable_profile]" type="checkbox" value="1" class="code" %1$s/>%2$s',
265
+ checked( esc_attr( $this->settings['enable_profile'] ), true, false ),
266
+ /* Translators: The leading space is intentional to space the text away from the checkbox */
267
+ __( ' Allow users to edit their profiles in the dashboard.', 'remove_dashboard_access' )
268
+ );
269
+ }
270
+
271
+ /**
272
+ * Redirect Settings Title & Description
273
+ *
274
+ * @since 1.0
275
+ */
276
+ function redirect_desc_cb() {
277
+ printf( __( 'Users who lack the selected role or capability will be redirected to a URL you specify. Left blank, default is: <strong>%s</strong>', 'remove_dashboard_access' ), home_url() );
278
+ }
279
+
280
+ /**
281
+ * Redirect URL display callback
282
+ *
283
+ * Default value is home_url(). $this->sanitize_options() handles validation and escaping.
284
+ *
285
+ * @since 1.0
286
+ */
287
+ function url_redirect_cb() {
288
+ ?>
289
+ <p><label>
290
+ <?php _e( 'Redirect users to:', 'remove_dashboard_access' ); ?>
291
+ <input name="rda-settings[redirect_url]" class="regular-text" type="text" value="<?php echo esc_attr( $this->settings['redirect_url'] ); ?>" />
292
+ </label></p>
293
+ <?php
294
+ }
295
+
296
+ /**
297
+ * Sanitize options values for 'rda-settings'
298
+ *
299
+ * @since 1.0
300
+ *
301
+ * @param array $options Options array to sanitize and validate.
302
+ * @return array $options The sanitized options array values.
303
+ */
304
+ function sanitize_options( $options ) {
305
+ $options['access_switch'] = esc_attr( $options['access_switch'] );
306
+ $options['enable_profile'] = ( isset( $options['enable_profile'] ) && true == $options['enable_profile'] ) ? true : false;
307
+ if ( empty( $options['redirect_url'] ) )
308
+ // If Redirect URL is empty, use the home_url()
309
+ $options['redirect_url'] = home_url();
310
+ elseif ( ! preg_match( '|^\S+://\S+\.\S+.+$|', $options['redirect_url'] ) )
311
+ // Malformed URL, throw a validation error
312
+ add_settings_error( 'rda-settings[redirect_url]', 'invalid-url', sprintf( __( 'Please enter a properly-formed URL. For example: %s', 'remove_dashboard_access' ), esc_url( home_url() ) ) );
313
+ else
314
+ $options['redirect_url'] = esc_url_raw( $options['redirect_url'] );
315
+ return $options;
316
+ }
317
+
318
+ /**
319
+ * Required capability for Dashboard access
320
+ *
321
+ * @since 1.0
322
+ *
323
+ * @return string $this->settings['access_cap'] if isset, otherwise, 'manage_options' (filterable)
324
+ */
325
+ function capability() {
326
+ if ( isset( $this->settings['access_cap'] ) )
327
+ return $this->settings['access_cap'];
328
+ else
329
+ return apply_filters( 'rda_default_access_cap', 'manage_options' );
330
+ }
331
+
332
+ /**
333
+ * Plugins list 'Settings' row link
334
+ *
335
+ * @since 1.0
336
+ *
337
+ * @param array $links Row links array to filter.
338
+ * @return array $links Filtered links array.
339
+ */
340
+ function settings_link( $links ) {
341
+ return array_merge(
342
+ array( 'settings' => sprintf(
343
+ '<a href="%1$s">%2$s</a>',
344
+ esc_url( admin_url( 'options-general.php?page=dashboard-access' ) ),
345
+ esc_attr( __( 'Settings', 'remove_dashboard_access' ) )
346
+ ) ), $links
347
+ );
348
+ }
349
+
350
+ } // RDA_Options
uninstall.php ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Remove Dashboard Access Uninstall
4
+ *
5
+ * @since 1.0
6
+ */
7
+
8
+ delete_option( 'rda-settings' );