WordPress.org

Make WordPress Core

Changeset 6615


Ignore:
Timestamp:
01/14/2008 09:55:17 PM (12 years ago)
Author:
ryan
Message:

Move WP_User_Search to admin includes. Props Viper007Bond. fixes #5111

Location:
trunk/wp-admin
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/user.php

    r6354 r6615  
    282282}
    283283
     284// WP_User_Search class
     285// by Mark Jaquith
     286
     287if ( !class_exists('WP_User_Search') ) :
     288class WP_User_Search {
     289    var $results;
     290    var $search_term;
     291    var $page;
     292    var $raw_page;
     293    var $users_per_page = 50;
     294    var $first_user;
     295    var $last_user;
     296    var $query_limit;
     297    var $query_from_where;
     298    var $total_users_for_query = 0;
     299    var $too_many_total_users = false;
     300    var $search_errors;
     301
     302    function WP_User_Search ($search_term = '', $page = '') { // constructor
     303        $this->search_term = $search_term;
     304        $this->raw_page = ( '' == $page ) ? false : (int) $page;
     305        $this->page = (int) ( '' == $page ) ? 1 : $page;
     306
     307        $this->prepare_query();
     308        $this->query();
     309        $this->prepare_vars_for_template_usage();
     310        $this->do_paging();
     311    }
     312
     313    function prepare_query() {
     314        global $wpdb;
     315        $this->first_user = ($this->page - 1) * $this->users_per_page;
     316        $this->query_limit = 'LIMIT ' . $this->first_user . ',' . $this->users_per_page;
     317        if ( $this->search_term ) {
     318            $searches = array();
     319            $search_sql = 'AND (';
     320            foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col )
     321                $searches[] = $col . " LIKE '%$this->search_term%'";
     322            $search_sql .= implode(' OR ', $searches);
     323            $search_sql .= ')';
     324        }
     325        $this->query_from_where = "FROM $wpdb->users WHERE 1=1 $search_sql";
     326
     327    }
     328
     329    function query() {
     330        global $wpdb;
     331        $this->results = $wpdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_limit);
     332
     333        if ( $this->results )
     334            $this->total_users_for_query = $wpdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit
     335        else
     336            $this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!'));
     337    }
     338
     339    function prepare_vars_for_template_usage() {
     340        $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone
     341    }
     342
     343    function do_paging() {
     344        if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results
     345            $this->paging_text = paginate_links( array(
     346                'total' => ceil($this->total_users_for_query / $this->users_per_page),
     347                'current' => $this->page,
     348                'prev_text' => __('« Previous Page'),
     349                'next_text' => __('Next Page »'),
     350                'base' => 'users.php?%_%',
     351                'format' => 'userspage=%#%',
     352                'add_args' => array( 'usersearch' => urlencode($this->search_term) )
     353            ) );
     354        }
     355    }
     356
     357    function get_results() {
     358        return (array) $this->results;
     359    }
     360
     361    function page_links() {
     362        echo $this->paging_text;
     363    }
     364
     365    function results_are_paged() {
     366        if ( $this->paging_text )
     367            return true;
     368        return false;
     369    }
     370
     371    function is_search() {
     372        if ( $this->search_term )
     373            return true;
     374        return false;
     375    }
     376}
     377endif;
     378
    284379?>
  • trunk/wp-admin/users.php

    r6570 r6615  
    2020    $redirect = 'users.php';
    2121}
    22 
    23 
    24 // WP_User_Search class
    25 // by Mark Jaquith
    26 
    27 
    28 class 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     }
    68 
    69     function query() {
    70         global $wpdb;
    71         $this->results = $wpdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_limit);
    72 
    73         if ( $this->results )
    74             $this->total_users_for_query = $wpdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit
    75         else
    76             $this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!'));
    77     }
    78 
    79     function prepare_vars_for_template_usage() {
    80         $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone
    81     }
    82 
    83     function do_paging() {
    84         if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results
    85             $this->paging_text = paginate_links( array(
    86                 'total' => ceil($this->total_users_for_query / $this->users_per_page),
    87                 'current' => $this->page,
    88                 'prev_text' => __('« Previous Page'),
    89                 'next_text' => __('Next Page »'),
    90                 'base' => 'users.php?%_%',
    91                 'format' => 'userspage=%#%',
    92                 'add_args' => array( 'usersearch' => urlencode($this->search_term) )
    93             ) );
    94         }
    95     }
    96 
    97     function get_results() {
    98         return (array) $this->results;
    99     }
    100 
    101     function page_links() {
    102         echo $this->paging_text;
    103     }
    104 
    105     function results_are_paged() {
    106         if ( $this->paging_text )
    107             return true;
    108         return false;
    109     }
    110 
    111     function is_search() {
    112         if ( $this->search_term )
    113             return true;
    114         return false;
    115     }
    116 }
    117 
    11822
    11923switch ($action) {
Note: See TracChangeset for help on using the changeset viewer.