| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Dashboard Commander |
| 4 |
Plugin URI: http://www.warpconduit.net/wordpress-plugins/dashboard-commander/ |
| 5 |
Description: Command your admin dashboard. Manage built-in widgets and dynamically registered widgets. Hide widgets depending upon user capabilities. Plugin is based upon Dashboard Heaven by Dave Kinkead. |
| 6 |
Version: 1.0 |
| 7 |
Author: Josh Hartman |
| 8 |
Author URI: http://www.warpconduit.net |
| 9 |
License: GPL2 |
| 10 |
*/ |
| 11 |
/* |
| 12 |
Copyright 2011 Josh Hartman |
| 13 |
|
| 14 |
This program is free software; you can redistribute it and/or modify |
| 15 |
it under the terms of the GNU General Public License, version 2, as |
| 16 |
published by the Free Software Foundation. |
| 17 |
|
| 18 |
This program is distributed in the hope that it will be useful, |
| 19 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
GNU General Public License for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License |
| 24 |
along with this program; if not, write to the Free Software |
| 25 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 |
*/ |
| 27 |
|
| 28 |
add_action('wp_dashboard_setup', 'dcmd_wp_dashboard_setup', 99); |
| 29 |
add_action('admin_init', 'dcmd_admin_init'); |
| 30 |
add_action('admin_menu', 'dcmd_admin_menu'); |
| 31 |
add_action('admin_notices', 'dcmd_admin_notices'); |
| 32 |
|
| 33 |
register_deactivation_hook(__FILE__, 'dcmd_deactivate'); |
| 34 |
|
| 35 |
/** |
| 36 |
* Display admin notifications |
| 37 |
* @args none |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
function dcmd_admin_notices(){ |
| 41 |
$widgets = get_option('dcmd_dashboard_widgets'); |
| 42 |
if(!$widgets){ |
| 43 |
echo sprintf('<div class="updated"><p>%s</p></div>', 'To complete the installation of <strong>Dashboard Commander</strong> you must <a href="'.get_admin_url().'index.php"/><strong>visit your dashboard</strong></a> once and then go to <strong>Settings > Dashboard Commander</strong> to configure who has access to each widget.'); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Initialize plugin |
| 49 |
* @args none |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
function dcmd_admin_init() { |
| 53 |
$widgets = get_option('dcmd_dashboard_widgets'); |
| 54 |
if($widgets){ |
| 55 |
foreach($widgets as $widget) { |
| 56 |
register_setting('dcmd_options', 'dcmd_'.$widget['id']); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Deactivate plugin |
| 63 |
* @args none |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
function dcmd_deactivate() { |
| 67 |
$widgets = get_option('dcmd_dashboard_widgets'); |
| 68 |
foreach($widgets as $widget) { |
| 69 |
delete_option('dcmd_'.$widget['id']); |
| 70 |
} |
| 71 |
delete_option('dcmd_dashboard_widgets'); |
| 72 |
unregister_setting('dh-options'); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Loop through the dashboard widgets and remove depending on current user capabilities |
| 77 |
* @args none |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
function dcmd_wp_dashboard_setup() { |
| 81 |
global $wp_meta_boxes; |
| 82 |
$widgets = dcmd_get_dashboard_widgets(); |
| 83 |
update_option('dcmd_dashboard_widgets', $widgets); |
| 84 |
foreach ($widgets as $widget){ |
| 85 |
if(!current_user_can(get_option('dcmd_'.$widget['id']))) { |
| 86 |
unset($wp_meta_boxes['dashboard'][$widget['context']][$widget['priority']][$widget['id']]); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get array of capabilities or generate dropdown list for options page |
| 93 |
* @args bool (optional) $selectlist, bool (optional) $name |
| 94 |
* @return array|string |
| 95 |
*/ |
| 96 |
function dcmd_get_capabilities($selectlist = FALSE, $name = FALSE) { |
| 97 |
global $wp_roles; |
| 98 |
$option = get_option($name); |
| 99 |
if ($selectlist) { |
| 100 |
$cap = '<select name="' . $name .'">'; |
| 101 |
$cap .= '<option value="do-everything">Nobody</option>'; |
| 102 |
foreach ($wp_roles->roles['administrator']['capabilities'] as $key=>$val) { |
| 103 |
$cap .= '<option value="' . $key . '"'; |
| 104 |
if ($option == $key) $cap .= ' selected="yes"'; |
| 105 |
$cap .= '>' . $key . '</option>'; |
| 106 |
} |
| 107 |
$cap .= '</select>'; |
| 108 |
return $cap; |
| 109 |
} else { |
| 110 |
return $wp_roles->roles['administrator']['capabilities']; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Generate an array of registered dashboard widgets |
| 116 |
* @args none |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
function dcmd_get_dashboard_widgets() { |
| 120 |
global $wp_meta_boxes; |
| 121 |
$widgets = array(); |
| 122 |
if (isset($wp_meta_boxes['dashboard'])) { |
| 123 |
foreach($wp_meta_boxes['dashboard'] as $context=>$data){ |
| 124 |
foreach($data as $priority=>$data){ |
| 125 |
foreach($data as $widget=>$data){ |
| 126 |
//echo $context.' > '.$priority.' > '.$widget.' > '.$data['title']."\n"; |
| 127 |
$widgets[$widget] = array('id' => $widget, |
| 128 |
'title' => strip_tags(preg_replace('/( |)<span.*span>/im', '', $data['title'])), |
| 129 |
'context' => $context, |
| 130 |
'priority' => $priority |
| 131 |
); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
return $widgets; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Add options page to admin menu |
| 141 |
* @args none |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
function dcmd_admin_menu() { |
| 145 |
if (function_exists('add_options_page')) { |
| 146 |
add_options_page('Dashboard Commander', 'Dashboard Commander', 'manage_options', __FILE__, 'dcmd_admin_page'); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Generate options page content |
| 152 |
* @args none |
| 153 |
* @return string |
| 154 |
*/ |
| 155 |
function dcmd_admin_page() { |
| 156 |
if (empty($title)) $title = __('Dashboard Commander Options'); |
| 157 |
$widgets = get_option('dcmd_dashboard_widgets'); |
| 158 |
?> |
| 159 |
<div class="wrap"> |
| 160 |
<?php screen_icon(); ?> |
| 161 |
<h2><?php echo esc_html($title); ?></h2> |
| 162 |
<?php if($widgets): ?> |
| 163 |
<form method="post" action="options.php"> |
| 164 |
<?php settings_fields('dcmd_options'); ?> |
| 165 |
<p>Select the minimum access level you want to make a dashboard widget visible to.</p> |
| 166 |
<table class="form-table"> |
| 167 |
<tbody> |
| 168 |
<?php foreach($widgets as $widget): ?> |
| 169 |
<tr valign="top"> |
| 170 |
<th scope="row"> |
| 171 |
<label for="<?php echo 'dcmd_'.$widget['id'] ?>"><?php echo $widget['title'] ?></label> |
| 172 |
</th> |
| 173 |
<td><?php echo(dcmd_get_capabilities(TRUE, 'dcmd_'.$widget['id'])); ?></td> |
| 174 |
</tr> |
| 175 |
<?php endforeach; ?> |
| 176 |
</tbody> |
| 177 |
</table> |
| 178 |
<p class="submit"> |
| 179 |
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /> |
| 180 |
</p> |
| 181 |
</form> |
| 182 |
<?php endif; ?> |
| 183 |
</div> |
| 184 |
<?php |
| 185 |
} |