Last active
August 29, 2015 13:56
-
-
Save wturnerharris/8938324 to your computer and use it in GitHub Desktop.
Class to display user capabilities in a table in the profile editor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WP_Profile_Caps class. | |
* | |
* | |
* @package WP_Profile_Caps | |
* @since WP_Profile_Caps 0.1 | |
*/ | |
if(realpath(__FILE__) === realpath($_SERVER["SCRIPT_FILENAME"])) | |
exit("Do not access this file directly."); | |
if ( !class_exists('WP_Profile_Caps')) { | |
class WP_Profile_Caps { | |
/* Properties */ | |
/* Methods */ | |
/** | |
* | |
* Constructor: Filters and Actions. | |
* | |
* @return void | |
*/ | |
function __construct(){ | |
if ( is_admin() ) { | |
add_action( 'show_user_profile', array(&$this, 'show_user_profile_fields') ); | |
add_action( 'edit_user_profile', array(&$this, 'show_user_profile_fields') ); | |
add_action( 'personal_options_update', array(&$this, 'save_user_profile_fields') ); | |
add_action( 'edit_user_profile_update', array(&$this, 'save_user_profile_fields') ); | |
} | |
} | |
function show_user_profile_fields( $user ) { | |
global $table_prefix; | |
if ( !is_object($user) || !isset($user->caps) || !is_array($user->caps)) return; | |
if ( !current_user_can('administrator') ) return FALSE; | |
$caps = $user->get_role_caps(); | |
$roles = $user->roles; | |
$user_roles = array(); | |
$role_list = get_option( $table_prefix.'user_roles' ); | |
foreach ($roles as $role) { | |
$user_roles[] = $role_list[$role]['name']; | |
} | |
?><h3><?php _e('User Capabilities'); ?> [Roles: <?php echo implode(', ', $user_roles); ?>]</h3> | |
<table class="widefat"> | |
<thead> | |
<tr> | |
<th>Remove</th> | |
<th>Capability</th> | |
<th>Status</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php foreach( $caps as $cap => $enabled): ($i=isset($i)?$i+1:0); ?><tr<?php echo ($i%2?' class="alternate"':''); ?>> | |
<td><input type="checkbox" name="<?php echo $table_prefix; ?>capabilities[<?php echo $cap; ?>]" /></td> | |
<td><?php echo $cap; ?></td> | |
<td><?php echo ($enabled?'En':'Dis').'abled'; ?></td> | |
</tr><?php endforeach; ?> | |
</tbody> | |
</table> | |
<?php | |
} | |
function save_user_profile_fields( $user_id ) { | |
global $table_prefix; | |
if ( !current_user_can( 'edit_user', $user_id ) || !isset($_POST[$table_prefix.'capabilities'])) | |
return FALSE; | |
if ( !current_user_can('administrator') ) | |
return FALSE; | |
$caps = $_POST[$table_prefix.'capabilities']; | |
$user = get_user_by('id', $user_id); | |
foreach( $caps as $cap => $enabled) $user->add_cap($cap, !user_can($user, $cap)); | |
} | |
} | |
new WP_Profile_Caps(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment