/app_old/membershipincludes/classes/class.membersearch.php

  1. <?php 
  2. if(!class_exists('M_Member_Search')) { 
  3.  
  4. class M_Member_Search extends WP_User_Search { 
  5.  
  6. var $sub_id = false; 
  7. var $level_id = false; 
  8. var $active = false; 
  9.  
  10. function M_Member_Search($search_term = '', $page = '', $sub_id = false, $level_id = false, $active = false) { 
  11. $this->search_term = $search_term; 
  12. $this->raw_page = ( '' == $page ) ? false : (int) $page; 
  13. $this->page = (int) ( '' == $page ) ? 1 : $page; 
  14.  
  15. if(!empty($sub_id)) { 
  16. $this->sub_id = $sub_id; 
  17.  
  18. if(!empty($level_id)) { 
  19. $this->level_id = $level_id; 
  20.  
  21. if(!empty($active)) { 
  22. $this->active = $active; 
  23.  
  24. $this->prepare_query(); 
  25. $this->query(); 
  26. $this->prepare_vars_for_template_usage(); 
  27. $this->do_paging(); 
  28.  
  29. function do_paging() { 
  30. if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results 
  31. $args = array(); 
  32. if( ! empty($this->search_term) ) { 
  33. $args['s'] = urlencode($this->search_term); 
  34.  
  35. if( ! empty($this->role) ) { 
  36. $args['role'] = urlencode($this->role); 
  37. if( ! empty($this->sub_id) ) { 
  38. $args['sub_op'] = urlencode($this->sub_id); 
  39. $args['doactionsub'] = 'Filter'; 
  40. if( ! empty($this->level_id) ) { 
  41. $args['level_op'] = urlencode($this->level_id); 
  42. $args['doactionlevel'] = 'Filter'; 
  43.  
  44.  
  45. $this->paging_text = paginate_links( array( 
  46. 'total' => ceil($this->total_users_for_query / $this->users_per_page),  
  47. 'current' => $this->page,  
  48. 'base' => 'admin.php?page=membershipmembers&%_%',  
  49. 'format' => 'userspage=%#%',  
  50. 'add_args' => $args 
  51. ) ); 
  52. if ( $this->paging_text ) { 
  53. $this->paging_text = sprintf( '<span class="displaying-num">' . __( 'Displaying %s–%s of %s', 'membership' ) . '</span>%s',  
  54. number_format_i18n( ( $this->page - 1 ) * $this->users_per_page + 1 ),  
  55. number_format_i18n( min( $this->page * $this->users_per_page, $this->total_users_for_query ) ),  
  56. number_format_i18n( $this->total_users_for_query ),  
  57. $this->paging_text 
  58. ); 
  59.  
  60. function prepare_query() { 
  61.  
  62.  
  63. global $wpdb, $wp_version; 
  64.  
  65. $this->first_user = ($this->page - 1) * $this->users_per_page; 
  66.  
  67. $this->query_limit = $wpdb->prepare(" LIMIT %d, %d", $this->first_user, $this->users_per_page); 
  68. $this->query_orderby = ' ORDER BY user_login'; 
  69.  
  70. $search_sql = ''; 
  71.  
  72. if ( $this->search_term ) { 
  73. $searches = array(); 
  74. $search_sql = 'AND ('; 
  75. foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col ) 
  76. $searches[] = $col . " LIKE '%$this->search_term%'"; 
  77. $search_sql .= implode(' OR ', $searches); 
  78. $search_sql .= ')'; 
  79.  
  80. // The following code changes in WP3 and above 
  81.  
  82. // We are on version 3.0 or above 
  83. $this->query_from = " FROM $wpdb->users"; 
  84. $this->query_where = " WHERE 1=1 $search_sql"; 
  85.  
  86. if ( $this->role ) { 
  87. $this->query_from .= " INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id"; 
  88. $this->query_where .= $wpdb->prepare(" AND $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' AND $wpdb->usermeta.meta_value LIKE %s", '%' . $this->role . '%'); 
  89. } elseif ( is_multisite() ) { 
  90. $level_key = $wpdb->prefix . 'capabilities'; // wpmu site admins don't have user_levels 
  91. $this->query_from .= ", $wpdb->usermeta"; 
  92. $this->query_where .= " AND $wpdb->users.ID = $wpdb->usermeta.user_id AND meta_key = '{$level_key}'"; 
  93.  
  94.  
  95. if( $this->sub_id ) { 
  96. $sql = $wpdb->prepare( "SELECT user_id FROM " . membership_db_prefix($wpdb, "membership_relationships") . " WHERE sub_id = %d", $this->sub_id ); 
  97.  
  98. $subs = $wpdb->get_col( $sql ); 
  99.  
  100. if(!empty($subs)) { 
  101. $this->query_where .= " AND {$wpdb->users}.ID IN (" . implode(', ', $subs) . ")"; 
  102. // wp 2.9.2 and lower 
  103. $this->query_from_where .= " AND {$wpdb->users}.ID IN (" . implode(', ', $subs) . ")"; 
  104. } else { 
  105. $this->query_where .= " AND {$wpdb->users}.ID IN (0)"; 
  106. // wp 2.9.2 and lower 
  107. $this->query_from_where .= " AND {$wpdb->users}.ID IN (0)"; 
  108.  
  109. if( $this->level_id ) { 
  110. $sql = $wpdb->prepare( "SELECT user_id FROM " . membership_db_prefix($wpdb, "membership_relationships") . " WHERE level_id = %d", $this->level_id ); 
  111.  
  112. $levels = $wpdb->get_col( $sql ); 
  113.  
  114. if(!empty($levels)) { 
  115. $this->query_where .= " AND {$wpdb->users}.ID IN (" . implode(', ', $levels) . ")"; 
  116. // wp 2.9.2 and lower 
  117. $this->query_from_where .= " AND {$wpdb->users}.ID IN (" . implode(', ', $levels) . ")"; 
  118. } else { 
  119. $this->query_where .= " AND {$wpdb->users}.ID IN (0)"; 
  120. // wp 2.9.2 and lower 
  121. $this->query_from_where .= " AND {$wpdb->users}.ID IN (0)"; 
  122.  
  123.  
  124. if($this->active) { 
  125. $sql = $wpdb->prepare( "SELECT user_id FROM " . $wpdb->usermeta . " WHERE meta_key = '" . membership_db_prefix($wpdb, 'membership_active', false) . "' AND meta_value = %s", 'no' ); 
  126. $actives = $wpdb->get_col( $sql ); 
  127.  
  128. if(!empty($actives)) { 
  129. if($this->active == 'yes') { 
  130. $this->query_where .= " AND {$wpdb->users}.ID NOT IN (" . implode(', ', $actives) . ")"; 
  131. // wp 2.9.2 and lower 
  132. $this->query_from_where .= " AND {$wpdb->users}.ID NOT IN (" . implode(', ', $actives) . ")"; 
  133. } else { 
  134. // no 
  135. $this->query_where .= " AND {$wpdb->users}.ID IN (" . implode(', ', $actives) . ")"; 
  136. // wp 2.9.2 and lower 
  137. $this->query_from_where .= " AND {$wpdb->users}.ID IN (" . implode(', ', $actives) . ")"; 
  138. } else { 
  139. if($this->active == 'yes') { 
  140. $this->query_where .= " AND {$wpdb->users}.ID NOT IN (0)"; 
  141. // wp 2.9.2 and lower 
  142. $this->query_from_where .= " AND {$wpdb->users}.ID NOT IN (0)"; 
  143. } else { 
  144. // no 
  145. $this->query_where .= " AND {$wpdb->users}.ID IN (0)"; 
  146. // wp 2.9.2 and lower 
  147. $this->query_from_where .= " AND {$wpdb->users}.ID IN (0)"; 
  148.  
  149. do_action_ref_array( 'pre_user_search', array( &$this ) ); 
  150.  
  151.  
  152.  
  153. ?> 
.