Skip to content

Commit

Permalink
Cache and display results with ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbetz committed Jul 5, 2012
1 parent adc5bd0 commit fd81516
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 70 deletions.
21 changes: 17 additions & 4 deletions bulk-user-management.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ function __construct() {
add_action( 'wpmu_activate_user', array( $this, 'add_to_blogs' ), 5, 3 );
add_action( 'wpmu_signup_user_notification_email', array( $this, 'invite_message' ), 5, 5 );

//Handle GET and POST requests
// Invalidate cache when a user is added or removed from a site
add_action( 'add_user_to_blog', array( $this, 'invalidate_cache' ), 5, 3 );
add_action( 'remove_user_from_blog', array( $this, 'invalidate_cache' ), 5, 2 );

// Handle GET and POST requests
add_action( 'admin_init', array( $this, 'handle_promote_users_form' ) );
add_action( 'admin_init', array( $this, 'handle_remove_users_form' ) );
add_action( 'admin_init', array( $this, 'handle_invite_users_form' ) );
Expand Down Expand Up @@ -108,16 +112,27 @@ public function show_users() {
$bulk_users_table = new Bulk_User_Table();
$bulk_users_table->prepare_items();
$bulk_users_table->display();
if ( $bulk_users_table->has_items() ) {
$bulk_users_table->inline_edit();
$bulk_users_table->bulk_remove();
}
exit();
}

public function invalidate_cache( $u, $remove_blogid, $blogid = false ) {
if ( false === $blogid ) // remove_user_from_blog
wp_cache_delete( $remove_blogid, 'bum_blog_users' );
else // add_user_to_blog
wp_cache_delete( $blogid, 'bum_blog_users' );
}

/**
* Generate the users page
*/
public function users_page() {

$bulk_users_table = new Bulk_User_Table();
//$bulk_users_table->prepare_items();
$bulk_users_table->prepare_items( false );
wp_enqueue_script('bulk-user-management-inline-edit');
wp_enqueue_style('bulk-user-management');

Expand Down Expand Up @@ -574,7 +589,6 @@ public function promote_users($blogids = array(), $userids = array(), $role) {
foreach ( $userids as $id ) {
foreach ( $blogids as $blogid ) {
add_user_to_blog( $blogid, $id, $role );
wp_cache_delete( $blogid, 'bum_blog_users' );
}
}
}
Expand Down Expand Up @@ -634,7 +648,6 @@ public function remove_users($blogids = array(), $userids = array()) {
foreach ( $userids as $userid ) {
foreach ( $blogids as $blogid ) {
remove_user_from_blog( $userid, $blogid );
wp_cache_delete( $blogid, 'bum_blog_users' );
}
}
}
Expand Down
104 changes: 57 additions & 47 deletions includes/class-bulk-user-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ function get_columns(){

function get_sortable_columns() {
$sortable_columns = array(
'username' => array( 'username', '' == $_REQUEST['orderby'] || ( 'username' == $_REQUEST['orderby'] && 'desc' != $_REQUEST['order'] ) ),
'name' => array( 'name', 'name' == $_REQUEST['orderby'] && 'asc' == $_REQUEST['order'] ),
'email' => array( 'email', 'email' == $_REQUEST['orderby'] && 'asc' == $_REQUEST['order'] )
'username' => array( 'user_login', true ),
'name' => array( 'display_name', false ),
'email' => array( 'user_email', false )
);
return $sortable_columns;
}
Expand Down Expand Up @@ -118,8 +118,8 @@ function get_blog_ids( $cap ) {
return $blog_ids;
}

function prepare_items() {
global $wpdb, $usersearch;
function prepare_items( $queryitems = true ) {
global $wpdb;
$screen = get_current_screen();

//TODO: make this work again
Expand All @@ -132,8 +132,6 @@ function prepare_items() {
// }
$per_page = 20;

$usersearch = isset( $_REQUEST['s'] ) ? $_REQUEST['s'] : '';

$paged = $this->get_pagenum();

$columns = $this->get_columns();
Expand All @@ -143,57 +141,69 @@ function prepare_items() {

$this->process_bulk_action();

$blog_ids = $this->get_blog_ids( 'list_users' );
if ( $queryitems ) {

$query = array();
foreach ( $blog_ids as $blogid ) {
$blog_ids = $this->get_blog_ids( 'list_users' );

$args = array(
'blog_id' => $blogid,
'search' => $usersearch,
);
$query = array();
foreach ( $blog_ids as $blogid ) {

if ( '' !== $args['search'] )
$args['search'] = '*' . $args['search'] . '*';
$args = array(
'blog_id' => $blogid
);

$users = wp_cache_get( $blogid, 'bum_blog_users' );
if ( false === $users ) {
$users = get_users( $args );
wp_cache_set( $blogid, $users, 'bum_blog_users', 60 * 60 * 24 );
}
$users = wp_cache_get( $blogid, 'bum_blog_users' );
if ( false === $users ) {
$users = get_users( $args );
wp_cache_set( $blogid, $users, 'bum_blog_users', 60 * 60 * 24 );
}

foreach ( $users as $user ) {
if ( !in_array( $user, $query ) )
$query[] = $user;
foreach ( $users as $user ) {
if ( !in_array( $user, $query ) )
$query[] = $user;
}
}
}

usort( $query, function( $a, $b ){
$orderby = isset( $_REQUEST['orderby'] ) ? esc_attr( $_REQUEST['orderby'] ) : 'username';
$order = isset( $_REQUEST['order'] ) && 'desc' == $_REQUEST['order'] ? -1 : 1;

switch ( $orderby ) {
case 'name':
$cmp = strnatcmp( strtolower( $a->display_name ), strtolower( $b->display_name ) );
break;
case 'email':
$cmp = strnatcmp( strtolower( $a->user_email ), strtolower( $b->user_email ) );
break;
case 'username':
default:
$cmp = strnatcmp( strtolower( $a->user_login ), strtolower( $b->user_login ) );
break;
// orderby and order
usort( $query, function( $a, $b ){
$orderby = isset( $_REQUEST['orderby'] ) ? esc_attr( $_REQUEST['orderby'] ) : 'user_login';
$order = isset( $_REQUEST['order'] ) && 'desc' == $_REQUEST['order'] ? -1 : 1;

switch ( $orderby ) {
case 'display_name':
$cmp = strnatcmp( strtolower( $a->display_name ), strtolower( $b->display_name ) );
break;
case 'user_email':
$cmp = strnatcmp( strtolower( $a->user_email ), strtolower( $b->user_email ) );
break;
case 'user_login':
default:
$cmp = strnatcmp( strtolower( $a->user_login ), strtolower( $b->user_login ) );
break;
}

return $cmp * $order;
});

// search
$users = array();
if ( isset( $_REQUEST['s'] ) && '' != $_REQUEST['s'] ) {
foreach ( $query as $user ) {
if ( stristr( $user->user_login, $_REQUEST['s'] ) )
$users[] = $user;
}
} else {
$users = $query;
}

return $cmp * $order;
});
$this->items = array_slice( $users, $per_page * ($paged-1), $per_page);

$this->items = array_slice( $query, $per_page * ($paged-1), $per_page);
$this->set_pagination_args( array(
"total_items" => count( $query ),
"per_page" => $per_page,
) );

$this->set_pagination_args( array(
"total_items" => count( $query ),
"per_page" => $per_page,
) );
}

}

Expand Down
52 changes: 33 additions & 19 deletions js/bulk-user-management-inline-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ inlineEditUser = {
init : function(){
var t = this, bulkRow = $('#bulk-edit');

$('.bulk-users-form').html("<img src='" + images + "/wpspin_light.gif'>");
t.showTable();

$('.widefat tbody tr').each(function(){
var id = $(this).find('th.check-column input[type="checkbox"]').val();
$(this).attr('id', 'inline_'+id);
});

// Submit bulk edit option
$('#doaction, #doaction2').click(function(e){
t.cancel();
Expand All @@ -21,7 +15,6 @@ inlineEditUser = {
t.setBulk();
}
if ( $('select[name="'+n+'"]').val() == 'remove' ) {
console.log('removed');
e.preventDefault();
t.remove();
}
Expand All @@ -37,6 +30,8 @@ inlineEditUser = {
setBulk : function(){
var te = '', c = true;

console.log( $('#bulk-edit') );

$('#bulk-edit td').attr('colspan', $('.widefat:first thead th:visible').length);
$('table.widefat tbody').prepend( $('#bulk-edit') );
$('#bulk-edit').addClass('inline-editor').show();
Expand Down Expand Up @@ -90,27 +85,33 @@ inlineEditUser = {
$(".inline-editor").hide();
},

showTable: function(paged, orderby, order, search) {
showTable: function() {
var t = this;

var data = {
action: 'bulk_user_management_show_form',
paged: paged || getParameterByName('paged'),
search: search || getParameterByName('search'),
orderby: orderby || getParameterByName('orderby'),
order: order || getParameterByName('order')
paged: getParameterByName('paged'),
s: getParameterByName('s'),
orderby: getParameterByName('orderby'),
order: getParameterByName('order')
};

$(".actions").prepend("<img src='" + images + "/wpspin_light.gif'>");
$(".actions").prepend("<img class='ajax-spinner' src='" + images + "/wpspin_light.gif'>");
$(".wp-list-table").animate({"opacity":".4"});

$.post(ajaxurl, data, function(response) {
$('.bulk-users-form').html(response);
$('a[href*="admin-ajax.php"]').click(function(){
$('.widefat tbody').html( $(response).find('#the-list').html() );

$('.tablenav-pages').remove();
$('.tablenav br').before( $(response).find('.tablenav-pages').first() );

$('a.disabled').click(function() { return false; });
$('a[href*="admin-ajax.php"]').not('.disabled').click(function(){
var url = $(this).attr("href");
var paged = getParameterByName( 'paged', url ) || 1;
var orderby = getParameterByName( 'orderby', url ) || getParameterByName( 'orderby', window.location.search ) || false;
var order = getParameterByName( 'order', url ) || getParameterByName( 'order', window.location.search ) || false;
var search = getParameterByName( 's', url ) || false;
var queryString = "";

if ( paged != 1 )
Expand All @@ -122,14 +123,27 @@ inlineEditUser = {
if ( order )
queryString += "&order=" + order;

if ( search )
queryString += "&s=" + search;

// Update the URL without refreshing the page if possible
if ( history && history.pushState )
history.pushState("", "", "?page=bulk_user_management" + queryString);
else
window.location = "?page=bulk_user_management" + queryString;

// Load the table with updated state
t.showTable(paged, orderby, order, search);
t.init();

// Update the URL without refreshing the page
history.pushState("", "", "?page=bulk_user_management" + queryString);

return false;
});
}).success(function(){
$(".ajax-spinner").hide();
$(".wp-list-table").animate({"opacity":"1"});
$('.widefat tbody tr').each(function(){
var id = $(this).find('th.check-column input[type="checkbox"]').val();
$(this).attr('id', 'inline_'+id);
});
});
}
};
Expand Down

0 comments on commit fd81516

Please sign in to comment.