From aaf62a34828817ef72b5b07380adb40d209c5d3e Mon Sep 17 00:00:00 2001 From: ryan Date: Sun, 11 Jun 2006 17:56:56 +0000 Subject: [PATCH] WP_User_Search from Mark Jaquith. #2793 git-svn-id: http://svn.automattic.com/wordpress/trunk@3864 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/users.php | 179 +++++++++++++++++++++++++++++---------------- 1 file changed, 115 insertions(+), 64 deletions(-) diff --git a/wp-admin/users.php b/wp-admin/users.php index 5f5f0269c7..4cef1f121d 100644 --- a/wp-admin/users.php +++ b/wp-admin/users.php @@ -20,6 +20,104 @@ if ( empty($_POST) ) { $redirect = 'users.php'; } + +// WP_User_Search class +// by Mark Jaquith + + +class WP_User_Search { + var $results; + var $search_term; + var $page; + var $raw_page; + var $users_per_page = 50; + var $first_user; + var $last_user; + var $query_limit; + var $query_from_where; + var $total_users_for_query = 0; + var $too_many_total_users = false; + var $search_errors; + + function WP_User_Search ($search_term = '', $page = '') { // constructor + $this->search_term = $search_term; + $this->raw_page = ( '' == $page ) ? false : (int) $page; + $this->page = (int) ( '' == $page ) ? 1 : $page; + + $this->prepare_query(); + $this->query(); + $this->prepare_vars_for_template_usage(); + $this->do_paging(); + } + + function prepare_query() { + global $wpdb; + $this->first_user = ($this->page - 1) * $this->users_per_page; + $this->query_limit = 'LIMIT ' . $this->first_user . ',' . $this->users_per_page; + if ( $this->search_term ) { + $searches = array(); + $search_sql = 'AND ('; + foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col ) + $searches[] = $col . " LIKE '%$this->search_term%'"; + $search_sql .= implode(' OR ', $searches); + $search_sql .= ')'; + } + $this->query_from_where = "FROM $wpdb->users WHERE 1=1 $search_sql"; + + if ( !$_GET['update'] && !$this->search_term && !$this->raw_page && $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users") > $this->users_per_page ) + $this->too_many_total_users = sprintf(__('Because this blog has more than %s users, they cannot all be shown on one page. Use the paging or search functionality in order to find the user you want to edit.'), $this->users_per_page); + } + + function query() { + global $wpdb; + $this->results = $wpdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_limit); + + if ( $this->results ) + $this->total_users_for_query = $wpdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit + else + $this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!')); + } + + function prepare_vars_for_template_usage() { + $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone + } + + function do_paging() { + if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results + $prev_page = ( $this->page > 1) ? true : false; + $next_page = ( ($this->page * $this->users_per_page) < $this->total_users_for_query ) ? true : false; + $this->paging_text = ''; + if ( $prev_page ) + $this->paging_text .= '

« Previous Page

'; + if ( $next_page ) + $this->paging_text .= '

Next Page »

'; + if ( $prev_page || $next_page ) + $this->paging_text .= '
'; + } + } + + function get_results() { + return $this->results; + } + + function page_links() { + echo $this->paging_text; + } + + function results_are_paged() { + if ( $this->paging_text ) + return true; + return false; + } + + function is_search() { + if ( $this->search_term ) + return true; + return false; + } +} + + switch ($action) { case 'promote': @@ -172,58 +270,11 @@ default: include('admin-header.php'); - /* Paging and Search by Mark Jaquith, June 6th, 2006 */ - - $users_per_page = 50; - - $page = (int) $_GET['userspage']; - if ( !$page ) - $page = 1; - - $starton = ($page - 1) * $users_per_page; - - $limit = 'LIMIT ' . $starton . ',' . $users_per_page; - - $search_term = $_GET['usersearch']; - if ( $search_term ) { - $searches = array(); - $search_sql = 'AND ('; - foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col ) - $searches[] = $col . " LIKE '%$search_term%'"; - $search_sql .= implode(' OR ', $searches); - $search_sql .= ')'; - $search_term = stripslashes($search_term); // done with DB, from now on we want slashes gone - } - - if ( !$_GET['update'] && !$search_term && !$_GET['userspage'] && $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users") > $users_per_page ) - $too_many_users = sprintf(__('Because this blog has more than %s users, they cannot all be shown on one page. Use the paging or search functionality in order to find the user you want to edit.'), $users_per_page); - - $from_where = "FROM $wpdb->users WHERE 1=1 $search_sql"; - $userids = $wpdb->get_col('SELECT ID ' . $from_where . $limit); - - if ( $userids ) - $total_users_for_this_query = $wpdb->get_var('SELECT COUNT(ID) ' . $from_where); // no limit - else - $search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!')); - - // Now for the paging - if ( $total_users_for_this_query > $users_per_page ) { // have to page the results - $prev_page = ( $page > 1) ? true : false; - $next_page = ( ($page * $users_per_page) < $total_users_for_this_query ) ? true : false; - $paging_text = ''; - if ( $prev_page ) - $paging_text .= '

« Previous Page

'; - if ( $next_page ) - $paging_text .= '

Next Page »

'; - if ( $prev_page || $next_page ) - $paging_text .= '
'; - } - - // Clean up, we're done with these variables - unset($prev_page, $next_page, $limit, $searches, $search_sql, $col); + // Query the users + $wp_user_search = new WP_User_Search($_GET['usersearch'], $_GET['userspage']); // Make the user objects - foreach ( (array) $userids as $userid ) { + foreach ( $wp_user_search->get_results() as $userid ) { $tmp_user = new WP_User($userid); $roles = $tmp_user->roles; $role = array_shift($roles); @@ -275,29 +326,29 @@ default: - +too_many_total_users ) : ?>
-

+

too_many_total_users; ?>

- -

+ is_search() ) : ?> +

search_term); ?>

- + search_errors ) ) : ?>
    get_error_messages() as $message ) + foreach ( $wp_user_search->search_errors->get_error_messages() as $message ) echo "
  • $message
  • "; ?>
@@ -305,16 +356,16 @@ default: - +get_results() ) : ?> - + is_search() ) : ?>

-

+

first_user + 1, min($wp_user_search->first_user + $wp_user_search->users_per_page, $wp_user_search->total_users_for_query), $wp_user_search->total_users_for_query); ?>

- -

+ results_are_paged() ) : ?> +
page_links(); ?>

@@ -353,8 +404,8 @@ foreach ( (array) $roleclass as $user_object ) { - -
+results_are_paged() ) : ?> +
page_links(); ?>