Last active
October 18, 2016 13:45
-
-
Save yratof/c85896ea8c0952640e17 to your computer and use it in GitHub Desktop.
Promote & Demote users through user.php
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
/* Update the users role inside the users page with a fucking button! */ | |
/** Manage custom columns on instances of 'WP_List_Table' */ | |
add_action('init', 'my_setup_custom_columns', 110, 0); | |
function my_setup_custom_columns(){ | |
/** Manage columns on the WP_Users_List_Table */ | |
add_filter('manage_users_columns', 'my_add_custom_columns'); | |
/** Populate additional columns on the WP_Users_List_Table */ | |
add_action('manage_users_custom_column', 'my_fill_custom_columns_return', 10, 3); | |
} | |
/** | |
* Add additional custom columns to the list table views in the admin area | |
* | |
* @param required array $columns The columns that currently exist | |
*/ | |
function my_add_custom_columns($columns){ | |
/** Grab the page that is currently being viewed */ | |
global $pagenow; | |
/** Users specific columns (http://www.blog.com/wp-admin/users.php) */ | |
if($pagenow === 'users.php') : | |
$new_columns['my-change-user-role'] = __('Change Role'); | |
endif; | |
$columns = $columns + $new_columns; | |
return $columns; | |
} | |
/** | |
* Fill the custom columns by returning data | |
* | |
* @param required string $column_value The current column value (in this case 'null' since the columns are new) | |
* @param required string $column_name The name of the column to return data for | |
* @param required integer $object_id The ID of the curent object to fill the column for | |
* @return string $column_value The value to fill the column with | |
*/ | |
function my_fill_custom_columns_return($column_value, $column_name, $object_id){ | |
switch($column_name) : | |
/** | |
* ID (the ID of the current object) | |
*/ | |
case 'my-change-user-role' : | |
$user = get_userdata($object_id); | |
$role = $user->roles[0]; | |
if($role === 'partner') : | |
$column_value = '<span user-id="' . $object_id . '" new-role="prospect" class="change-user-role">Downgrade to a prospect</span>'; | |
elseif($role === 'prospect') : | |
$column_value = '<span user-id="' . $object_id . '" new-role="partner" class="change-user-role">Upgrade to a Partner</span>'; | |
else: | |
$column_value = '—'; | |
endif; | |
break; | |
endswitch; | |
return $column_value; | |
} | |
/** | |
* Change the role of a given user | |
*/ | |
add_action('wp_ajax_change_user_role', 'my_update_user_role'); | |
function my_update_user_role(){ | |
/** Update the user role */ | |
$args = array( | |
'ID' => $_POST['user_id'], | |
'role' => $_POST['new_role'] | |
); | |
$result = wp_update_user($args); | |
/** Check whether or not the update was successful */ | |
if(is_wp_error($user_id)) : | |
$message = 'An unexpected error occurred, please try again.'; | |
$url = false; | |
else : | |
$message = 'success'; | |
$url = admin_url('users.php'); | |
endif; | |
$return = array( | |
'message' => $message, | |
'url' => $url | |
); | |
echo json_encode((object)$return); | |
wp_die(); // This is required to terminate immediately and return a proper response | |
} | |
// Put Javascript file on Users page | |
function upgrade_user($hook) { | |
if ( 'users.php' != $hook ) { | |
return; | |
} | |
wp_enqueue_script( 'upgrader', get_template_directory_uri() . '/library/js/upgrade_user.js' ); | |
} | |
add_action( 'admin_enqueue_scripts', 'upgrade_user' ); | |
/* Add a quick style to the users.php page */ | |
add_action('admin_head', 'upgrader_user_button'); | |
function upgrader_user_button() { | |
echo '<style> | |
.change-user-role{ | |
background: firebrick; | |
border: medium none; | |
border-radius: 2px; | |
color: white; | |
cursor: pointer; | |
display: inline-block; | |
font-style: normal; | |
font-weight: normal; | |
outline: medium none; | |
padding: 7px 14px; | |
text-align: center; | |
text-decoration: none; | |
text-indent: 0; | |
} | |
/* If button is "Upgrade to partner" - we\'ll give it something to shout about*/ | |
.change-user-role[new-role="partner"] { | |
background: limeGreen; | |
} | |
.wp-list-table.users .manage-column.column-my-change-user-role { | |
width: 150px; | |
} | |
.wp-list-table.users span.change-user-role { | |
color: #0074A2; | |
cursor: pointer; | |
} | |
.wp-list-table.users span.change-user-role:hover { | |
color: #2EA2CC; | |
} | |
</style>'; | |
} |
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
jQuery(function($){ | |
$(document).ready(function(){ | |
updateUser.init(); | |
}); | |
var updateUser = { | |
/** | |
* Psuedo constructor | |
*/ | |
init : function(){ | |
this.userChangeRoleButtons = $('.wp-list-table.users span.change-user-role'); // Grab all instance of the buttons to change the user role | |
this.createEvents(); // Create the events to be handled by this object | |
}, // init | |
/** | |
* Create the events that are to be handled by this object | |
*/ | |
createEvents : function(){ | |
var t = this; // This object | |
/** | |
* Handle clicks of the change user role buttons | |
*/ | |
this.userChangeRoleButtons.on('click', function(e){ | |
t.changeRole($(this)); | |
}); | |
}, // createEvents | |
/** | |
* Updates the user role as required | |
*/ | |
changeRole : function(clicked){ | |
var t = this, // This object | |
user_id = clicked.attr('user-id'), | |
new_role = clicked.attr('new-role'); | |
var data = { // The data object to pass to the AJAX request | |
action: 'change_user_role', | |
user_id: user_id, | |
new_role: new_role | |
}; | |
/** Make the AJAX request to update the download count */ | |
var action_jqxhr = $.post(ajaxurl, data, function(response){ | |
/** Parse the JSON response */ | |
var obj = JSON.parse(response); | |
/** Ensure that there were no errors and update the 'Change role' button text */ | |
if(obj.message.indexOf('error') === -1){ // There was no error, redirect the user back to this page | |
top.location.replace(obj.url); | |
} else { // There was an error, tell the user about it | |
alert(obj.message); // Show the error to the user | |
} | |
}).fail(function(){ // There was an AJAX error... | |
alert('An unexpected error occured, please try again.'); | |
}); | |
} // changeRole | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment