Ticket #5111: 5111.r6603.2.diff

File 5111.r6603.2.diff, 5.9 KB (added by hansengel, 4 years ago)

Moves class WP_User_Search into wp-admin/includes/users.php and makes it pluggable

  • wp-admin/users.php

     
    2020        $redirect = 'users.php'; 
    2121} 
    2222 
    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  
    118  
    11923switch ($action) { 
    12024 
    12125case 'promote': 
  • wp-admin/includes/users.php

     
     1<?php 
     2 
     3// WP_User_Search class 
     4// by Mark Jaquith 
     5 
     6if (!class_exists('WP_User_Search')) { 
     7        class WP_User_Search { 
     8                var $results; 
     9                var $search_term; 
     10                var $page; 
     11                var $raw_page; 
     12                var $users_per_page = 50; 
     13                var $first_user; 
     14                var $last_user; 
     15                var $query_limit; 
     16                var $query_from_where; 
     17                var $total_users_for_query = 0; 
     18                var $too_many_total_users = false; 
     19                var $search_errors; 
     20 
     21                function WP_User_Search ($search_term = '', $page = '') { // constructor 
     22                        $this->search_term = $search_term; 
     23                        $this->raw_page = ( '' == $page ) ? false : (int) $page; 
     24                        $this->page = (int) ( '' == $page ) ? 1 : $page; 
     25 
     26                        $this->prepare_query(); 
     27                        $this->query(); 
     28                        $this->prepare_vars_for_template_usage(); 
     29                        $this->do_paging(); 
     30                } 
     31 
     32                function prepare_query() { 
     33                        global $wpdb; 
     34                        $this->first_user = ($this->page - 1) * $this->users_per_page; 
     35                        $this->query_limit = 'LIMIT ' . $this->first_user . ',' . $this->users_per_page; 
     36                        if ( $this->search_term ) { 
     37                                $searches = array(); 
     38                                $search_sql = 'AND ('; 
     39                                foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col ) 
     40                                        $searches[] = $col . " LIKE '%$this->search_term%'"; 
     41                                $search_sql .= implode(' OR ', $searches); 
     42                                $search_sql .= ')'; 
     43                        } 
     44                        $this->query_from_where = "FROM $wpdb->users WHERE 1=1 $search_sql"; 
     45 
     46                } 
     47 
     48                function query() { 
     49                        global $wpdb; 
     50                        $this->results = $wpdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_limit); 
     51 
     52                        if ( $this->results ) 
     53                                $this->total_users_for_query = $wpdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit 
     54                        else 
     55                                $this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!')); 
     56                } 
     57 
     58                function prepare_vars_for_template_usage() { 
     59                        $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone 
     60                } 
     61 
     62                function do_paging() { 
     63                        if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results 
     64                                $this->paging_text = paginate_links( array( 
     65                                        'total' => ceil($this->total_users_for_query / $this->users_per_page), 
     66                                        'current' => $this->page, 
     67                                        'prev_text' => __('&laquo; Previous Page'), 
     68                                        'next_text' => __('Next Page &raquo;'), 
     69                                        'base' => 'users.php?%_%', 
     70                                        'format' => 'userspage=%#%', 
     71                                        'add_args' => array( 'usersearch' => urlencode($this->search_term) ) 
     72                                ) ); 
     73                        } 
     74                } 
     75 
     76                function get_results() { 
     77                        return (array) $this->results; 
     78                } 
     79 
     80                function page_links() { 
     81                        echo $this->paging_text; 
     82                } 
     83 
     84                function results_are_paged() { 
     85                        if ( $this->paging_text ) 
     86                                return true; 
     87                        return false; 
     88                } 
     89 
     90                function is_search() { 
     91                        if ( $this->search_term ) 
     92                                return true; 
     93                        return false; 
     94                } 
     95        } 
     96} 
     97 
     98?> 
     99 No newline at end of file