| | 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 | 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?') . '">« 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 »</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 | |
|---|
| 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?') . '">« 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 »</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']); |
|---|
| 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> |
|---|