Make WordPress Core


Ignore:
Timestamp:
06/11/2006 05:56:56 PM (19 years ago)
Author:
ryan
Message:

WP_User_Search from Mark Jaquith. #2793

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/users.php

    r3862 r3864  
    2020    $redirect = 'users.php';
    2121}
     22
     23
     24// WP_User_Search class
     25// by Mark Jaquith
     26
     27
     28class WP_User_Search {
     29    var $results;
     30    var $search_term;
     31    var $page;
     32    var $raw_page;
     33    var $users_per_page = 50;
     34    var $first_user;
     35    var $last_user;
     36    var $query_limit;
     37    var $query_from_where;
     38    var $total_users_for_query = 0;
     39    var $too_many_total_users = false;
     40    var $search_errors;
     41
     42    function WP_User_Search ($search_term = '', $page = '') { // constructor
     43        $this->search_term = $search_term;
     44        $this->raw_page = ( '' == $page ) ? false : (int) $page;
     45        $this->page = (int) ( '' == $page ) ? 1 : $page;
     46
     47        $this->prepare_query();
     48        $this->query();
     49        $this->prepare_vars_for_template_usage();
     50        $this->do_paging();
     51    }
     52
     53    function prepare_query() {
     54        global $wpdb;
     55        $this->first_user = ($this->page - 1) * $this->users_per_page;
     56        $this->query_limit = 'LIMIT ' . $this->first_user . ',' . $this->users_per_page;
     57        if ( $this->search_term ) {
     58            $searches = array();
     59            $search_sql = 'AND (';
     60            foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col )
     61                $searches[] = $col . " LIKE '%$this->search_term%'";
     62            $search_sql .= implode(' OR ', $searches);
     63            $search_sql .= ')';
     64        }
     65        $this->query_from_where = "FROM $wpdb->users WHERE 1=1 $search_sql";
     66
     67        if ( !$_GET['update'] && !$this->search_term && !$this->raw_page && $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users") > $this->users_per_page )
     68            $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);
     69    }
     70
     71    function query() {
     72        global $wpdb;
     73        $this->results = $wpdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_limit);
     74
     75        if ( $this->results )
     76            $this->total_users_for_query = $wpdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit
     77        else
     78            $this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!'));
     79    }
     80
     81    function prepare_vars_for_template_usage() {
     82        $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone
     83    }
     84
     85    function do_paging() {
     86        if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results
     87            $prev_page = ( $this->page > 1) ? true : false;
     88            $next_page = ( ($this->page * $this->users_per_page) < $this->total_users_for_query ) ? true : false;
     89            $this->paging_text = '';
     90            if ( $prev_page )
     91                $this->paging_text .= '<p class="alignleft"><a href="' . add_query_arg(array('usersearch' => $this->search_term, 'userspage' => $this->page - 1), 'users.php?') . '">&laquo; Previous Page</a></p>';
     92            if ( $next_page )
     93                $this->paging_text .= '<p class="alignright"><a href="' . add_query_arg(array('usersearch' => $this->search_term, 'userspage' => $this->page + 1), 'users.php?') . '">Next Page &raquo;</a></p>';
     94            if ( $prev_page || $next_page )
     95                $this->paging_text .= '<br style="clear:both" />';
     96        }
     97    }
     98
     99    function get_results() {
     100        return $this->results;
     101    }
     102
     103    function page_links() {
     104        echo $this->paging_text;
     105    }
     106
     107    function results_are_paged() {
     108        if ( $this->paging_text )
     109            return true;
     110        return false;
     111    }
     112
     113    function is_search() {
     114        if ( $this->search_term )
     115            return true;
     116        return false;
     117    }
     118}
     119
    22120
    23121switch ($action) {
     
    173271    include('admin-header.php');
    174272
    175     /* Paging and Search by Mark Jaquith, June 6th, 2006 */
    176 
    177     $users_per_page = 50;
    178 
    179     $page = (int) $_GET['userspage'];
    180     if ( !$page )
    181         $page = 1;
    182 
    183     $starton = ($page - 1) * $users_per_page;
    184 
    185     $limit = 'LIMIT ' . $starton . ',' .  $users_per_page;
    186 
    187     $search_term = $_GET['usersearch'];
    188     if ( $search_term ) {
    189         $searches = array();
    190         $search_sql = 'AND (';
    191         foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col )
    192             $searches[] = $col . " LIKE '%$search_term%'";
    193         $search_sql .= implode(' OR ', $searches);
    194         $search_sql .= ')';
    195         $search_term = stripslashes($search_term); // done with DB, from now on we want slashes gone
    196     }
    197 
    198     if ( !$_GET['update'] && !$search_term && !$_GET['userspage'] && $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users") > $users_per_page )
    199         $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);
    200 
    201     $from_where = "FROM $wpdb->users WHERE 1=1 $search_sql";
    202     $userids = $wpdb->get_col('SELECT ID ' . $from_where . $limit);
    203 
    204     if ( $userids )
    205         $total_users_for_this_query = $wpdb->get_var('SELECT COUNT(ID) ' . $from_where); // no limit
    206     else
    207         $search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!'));
    208 
    209     // Now for the paging
    210     if ( $total_users_for_this_query > $users_per_page ) { // have to page the results
    211         $prev_page = ( $page > 1) ? true : false;
    212         $next_page = ( ($page * $users_per_page) < $total_users_for_this_query ) ? true : false;
    213         $paging_text = '';
    214         if ( $prev_page )
    215             $paging_text .= '<p class="alignleft"><a href="' . add_query_arg(array('usersearch' => $search_term, 'userspage' => $page - 1), 'users.php?') . '">&laquo; Previous Page</a></p>';
    216         if ( $next_page )
    217             $paging_text .= '<p class="alignright"><a href="' . add_query_arg(array('usersearch' => $search_term, 'userspage' => $page + 1), 'users.php?') . '">Next Page &raquo;</a></p>';
    218         if ( $prev_page || $next_page )
    219             $paging_text .= '<br style="clear:both" />';
    220     }
    221 
    222     // Clean up, we're done with these variables
    223     unset($prev_page, $next_page, $limit, $searches, $search_sql, $col);
     273    // Query the users
     274    $wp_user_search = new WP_User_Search($_GET['usersearch'], $_GET['userspage']);
    224275
    225276    // Make the user objects
    226     foreach ( (array) $userids as $userid ) {
     277    foreach ( $wp_user_search->get_results() as $userid ) {
    227278        $tmp_user = new WP_User($userid);
    228279        $roles = $tmp_user->roles;
     
    276327<?php endif; ?>
    277328
    278 <?php if ( $too_many_users ) : ?>
     329<?php if ( $wp_user_search->too_many_total_users ) : ?>
    279330    <div id="message" class="updated">
    280         <p><?php echo $too_many_users; ?></p>
     331        <p><?php echo $wp_user_search->too_many_total_users; ?></p>
    281332    </div>
    282333<?php endif; ?>
     
    284335<div class="wrap">
    285336
    286     <?php if ( $search_term ) : ?>
    287         <h2><?php printf(__('Users Matching "%s" by Role'), $search_term); ?></h2>
     337    <?php if ( $wp_user_search->is_search() ) : ?>
     338        <h2><?php printf(__('Users Matching "%s" by Role'), $wp_user_search->search_term); ?></h2>
    288339    <?php else : ?>
    289340        <h2><?php _e('User List by Role'); ?></h2>
     
    291342
    292343    <form action="" method="get" name="search" id="search">
    293         <p><input type="text" name="usersearch" id="usersearch" value="<?php echo wp_specialchars($search_term); ?>" /> <input type="submit" value="<?php _e('Search for users &raquo;'); ?>" /></p>
     344        <p><input type="text" name="usersearch" id="usersearch" value="<?php echo wp_specialchars($wp_user_search->search_term); ?>" /> <input type="submit" value="<?php _e('Search for users &raquo;'); ?>" /></p>
    294345    </form>
    295346
    296     <?php if ( is_wp_error( $search_errors ) ) : ?>
     347    <?php if ( is_wp_error( $wp_user_search->search_errors ) ) : ?>
    297348        <div class="error">
    298349            <ul>
    299350            <?php
    300                 foreach ( $search_errors->get_error_messages() as $message )
     351                foreach ( $wp_user_search->search_errors->get_error_messages() as $message )
    301352                    echo "<li>$message</li>";
    302353            ?>
     
    306357
    307358
    308 <?php if ( $userids ) : ?>
    309 
    310     <?php if ( $search_term ) : ?>
     359<?php if ( $wp_user_search->get_results() ) : ?>
     360
     361    <?php if ( $wp_user_search->is_search() ) : ?>
    311362        <p><a href="users.php"><?php _e('&laquo; Back to All Users'); ?></a></p>
    312363    <?php endif; ?>
    313364
    314     <h3><?php printf(__('Results %1$s - %2$s of %3$s shown below'), $starton + 1, min($starton + $users_per_page, $total_users_for_this_query), $total_users_for_this_query); ?></h3>
    315 
    316     <?php if ( $paging_text ) : ?>
    317         <div class="user-paging-text"><?php echo $paging_text; ?></p></div>
     365    <h3><?php printf(__('Results %1$s - %2$s of %3$s shown below'), $wp_user_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); ?></h3>
     366
     367    <?php if ( $wp_user_search->results_are_paged() ) : ?>
     368        <div class="user-paging-text"><?php $wp_user_search->page_links(); ?></p></div>
    318369    <?php endif; ?>
    319370
     
    354405</table>
    355406
    356 <?php if ( $paging_text ) : ?>
    357     <div class="user-paging-text"><?php echo $paging_text; ?></div>
     407<?php if ( $wp_user_search->results_are_paged() ) : ?>
     408    <div class="user-paging-text"><?php $wp_user_search->page_links(); ?></div>
    358409<?php endif; ?>
    359410
Note: See TracChangeset for help on using the changeset viewer.