/core/um-filters-members.php

  1. <?php 
  2.  
  3. /*** 
  4. *** @Members Filter Hooks 
  5. ***/ 
  6. add_filter('um_prepare_user_query_args', 'um_prepare_user_query_args', 10, 2); 
  7. add_filter('um_prepare_user_query_args', 'um_add_search_to_query', 50, 2); 
  8. add_filter('um_prepare_user_query_args', 'um_search_usernames_emails', 51, 2); 
  9. add_filter('um_prepare_user_query_args', 'um_remove_special_users_from_list', 99, 2); 
  10.  
  11. /*** 
  12. *** @WP API user search 
  13. ***/ 
  14. function um_search_usernames_emails( $query_args, $args ) { 
  15. global $ultimatemember; 
  16. extract( $args ); 
  17.  
  18. $query = $ultimatemember->permalinks->get_query_array(); 
  19. $arr_columns = array(); 
  20.  
  21. foreach( $ultimatemember->members->core_search_fields as $key ) { 
  22.  
  23. if ( isset( $query[ $key ] ) && ! empty( $query[ $key ] ) ) { 
  24. $arr_columns[] = $key; 
  25. $query_args['search'] = '*' . $query[ $key ] .'*'; 
  26.  
  27.  
  28.  
  29. if( ! empty( $arr_columns ) ) { 
  30. $query_args['search_columns'] = $arr_columns; 
  31. return $query_args; 
  32.  
  33. /*** 
  34. *** @Remove users we do not need to show in directory 
  35. ***/ 
  36. function um_remove_special_users_from_list( $query_args, $args ) { 
  37. global $ultimatemember; 
  38. extract( $args ); 
  39.  
  40. $query_args['meta_query']['relation'] = 'AND'; 
  41.  
  42. if ( ! um_user_can('can_edit_everyone') ) { 
  43.  
  44. $query_args['meta_query'][] = array( 
  45. 'key' => 'account_status',  
  46. 'value' => 'approved',  
  47. 'compare' => '=' 
  48. ); 
  49.  
  50.  
  51. if ( ! um_user_can('can_edit_everyone') && um_get_option('account_hide_in_directory') ) { 
  52. $query_args['meta_query'][] = array( 
  53. "relation" => "OR",  
  54. array( 
  55. 'key' => 'hide_in_members',  
  56. 'value' => '',  
  57. 'compare' => 'NOT EXISTS' 
  58. ),  
  59. array( 
  60. 'key' => 'hide_in_members',  
  61. 'value' => 'Yes',  
  62. 'compare' => 'NOT LIKE' 
  63. ); 
  64.  
  65. if( um_user_can('can_view_all') && um_user_can('can_view_roles') ) { 
  66.  
  67. $role = um_user('role'); 
  68.  
  69. $permissions = $ultimatemember->query->role_data( $role ); 
  70.  
  71. if ( isset( $permissions['can_view_roles'] ) && is_serialized( $permissions['can_view_roles'] ) ) { 
  72. $roles = unserialize( $permissions['can_view_roles'] ); 
  73. }else{ 
  74. $roles = $permissions['can_view_roles']; 
  75.  
  76. if( $roles && is_array( $roles ) ) {  
  77. $query_args['meta_query'][] = array( 
  78. 'key' => 'role',  
  79. 'value' => $roles,  
  80. 'compare' => 'IN' 
  81. ); 
  82.  
  83. }  
  84.  
  85. return $query_args; 
  86.  
  87. /*** 
  88. *** @adds search parameters 
  89. ***/ 
  90. function um_add_search_to_query( $query_args, $args ) { 
  91. global $ultimatemember; 
  92. extract( $args ); 
  93.  
  94. if ( isset( $_REQUEST['um_search'] ) ) { 
  95.  
  96. $query = $ultimatemember->permalinks->get_query_array(); 
  97.  
  98. // if searching 
  99. if( isset( $query['search'] ) ) { 
  100. $query_args['search'] = '*' . um_filter_search( $query['search'] ) . '*'; 
  101. unset( $query['search'] ); 
  102.  
  103. if ( $query && is_array( $query ) ) { 
  104. foreach( $query as $field => $value ) { 
  105.  
  106. if(in_array($field, array('members_page'))) continue; 
  107.  
  108. $serialize_value = serialize( strval( $value ) ); 
  109.  
  110. if ( $value && $field != 'um_search' && $field != 'page_id' ) { 
  111.  
  112. if( strstr( $field, 'role_') ) { 
  113. $field = 'role'; 
  114.  
  115. if ( !in_array( $field, $ultimatemember->members->core_search_fields ) ) { 
  116.  
  117. $field_query = array( 
  118. array( 
  119. 'key' => $field,  
  120. 'value' => trim( $value ),  
  121. 'compare' => '=',  
  122. ),  
  123. array( 
  124. 'key' => $field,  
  125. 'value' => trim( $value ),  
  126. 'compare' => 'LIKE',  
  127. ),  
  128. array( 
  129. 'key' => $field,  
  130. 'value' => trim( $serialize_value ),  
  131. 'compare' => 'LIKE',  
  132. ),  
  133. 'relation' => 'OR',  
  134. ); 
  135.  
  136.  
  137. $field_query = apply_filters("um_query_args_{$field}__filter", $field_query ); 
  138. $query_args['meta_query'][] = $field_query; 
  139.  
  140.  
  141.  
  142.  
  143.  
  144. // allow filtering 
  145. $query_args = apply_filters('um_query_args_filter', $query_args ); 
  146.  
  147. if ( count ($query_args['meta_query']) == 1 ) { 
  148. unset( $query_args['meta_query'] ); 
  149.  
  150. return $query_args; 
  151.  
  152.  
  153. /*** 
  154. *** @adds main parameters 
  155. ***/ 
  156. function um_prepare_user_query_args($query_args, $args) { 
  157. global $ultimatemember; 
  158. extract( $args ); 
  159.  
  160. $query_args['fields'] = 'ID'; 
  161.  
  162. $query_args['number'] = 0; 
  163.  
  164. $query_args['meta_query']['relation'] = 'AND'; 
  165.  
  166. // must have a profile photo 
  167. if ( $has_profile_photo == 1 ) { 
  168. if( um_get_option('use_gravatars') ) { 
  169. $query_args['meta_query'][] = array( 
  170. 'relation' => 'OR',  
  171. array( 
  172. 'key' => 'synced_profile_photo', // addons 
  173. 'value' => '',  
  174. 'compare' => '!=' 
  175. ),  
  176. array( 
  177. 'key' => 'profile_photo', // from upload form 
  178. 'value' => '',  
  179. 'compare' => '!=' 
  180. ),  
  181. array( 
  182. 'key' => 'synced_gravatar_hashed_id', // gravatar 
  183. 'value' => '',  
  184. 'compare' => '!=' 
  185.  
  186. ); 
  187. }else{ 
  188. $query_args['meta_query'][] = array( 
  189. 'relation' => 'OR',  
  190. array( 
  191. 'key' => 'synced_profile_photo', // addons 
  192. 'value' => '',  
  193. 'compare' => '!=' 
  194. ),  
  195. array( 
  196. 'key' => 'profile_photo', // from upload form 
  197. 'value' => '',  
  198. 'compare' => '!=' 
  199. ); 
  200.  
  201. // must have a cover photo 
  202. if ( $has_cover_photo == 1 ) { 
  203. $query_args['meta_query'][] = array( 
  204. 'key' => 'cover_photo',  
  205. 'value' => '',  
  206. 'compare' => '!=' 
  207. ); 
  208.  
  209.  
  210. // show specific usernames 
  211. if ( isset( $show_these_users ) && $show_these_users && is_array( $show_these_users ) ) { 
  212. foreach( $show_these_users as $username ) { 
  213. $users_array[] = username_exists( $username ); 
  214. $query_args['include'] = $users_array; 
  215.  
  216. // add roles to appear in directory 
  217. if ( !empty( $roles ) ) { 
  218.  
  219. $query_args['meta_query'][] = array( 
  220. 'key' => 'role',  
  221. 'value' => $roles,  
  222. 'compare' => 'IN' 
  223. ); 
  224.  
  225.  
  226. // sort members by 
  227. $query_args['order'] = 'ASC'; 
  228.  
  229. if ( isset( $sortby ) ) { 
  230.  
  231.  
  232. if ( $sortby == 'other' && $sortby_custom ) { 
  233.  
  234. $query_args['meta_key'] = $sortby_custom; 
  235. $query_args['orderby'] = 'meta_value, display_name'; 
  236.  
  237. } else if ( in_array( $sortby, array( 'last_name', 'first_name' ) ) ) { 
  238.  
  239. $query_args['meta_key'] = $sortby; 
  240. $query_args['orderby'] = 'meta_value'; 
  241.  
  242. } else { 
  243.  
  244. if ( strstr( $sortby, '_desc' ) ) { 
  245. $sortby = str_replace('_desc', '', $sortby); 
  246. $order = 'DESC'; 
  247.  
  248. if ( strstr( $sortby, '_asc' ) ) { 
  249. $sortby = str_replace('_asc', '', $sortby); 
  250. $order = 'ASC'; 
  251.  
  252. $query_args['orderby'] = $sortby; 
  253.  
  254.  
  255. if ( isset( $order ) ) { 
  256. $query_args['order'] = $order; 
  257.  
  258. $query_args = apply_filters('um_modify_sortby_parameter', $query_args, $sortby); 
  259.  
  260.  
  261. return $query_args; 
  262.  
  263. /*** 
  264. *** @sorting by last login date 
  265. ***/ 
  266. add_filter('um_modify_sortby_parameter', 'um_sortby_last_login', 100, 2 ); 
  267. function um_sortby_last_login( $query_args, $sortby ) { 
  268. if ( $sortby == 'last_login' ) { 
  269. $query_args['orderby'] = 'meta_value_num'; 
  270. $query_args['order'] = 'desc'; 
  271. $query_args['meta_key'] = '_um_last_login'; 
  272. return $query_args; 
  273.  
  274. /*** 
  275. *** @sorting random 
  276. ***/ 
  277. add_filter('pre_user_query', 'um_modify_sortby_randomly'); 
  278. function um_modify_sortby_randomly( $query ) { 
  279.  
  280. if( um_is_session_started() === FALSE ) { 
  281. @session_start(); 
  282.  
  283. // Reset seed on load of initial  
  284. if( ! isset( $_REQUEST['members_page'] ) || $_REQUEST['members_page'] == 0 || $_REQUEST['members_page'] == 1 ) { 
  285. if( isset( $_SESSION['seed'] ) ) { 
  286. unset( $_SESSION['seed'] ); 
  287.  
  288. // Get seed from session variable if it exists 
  289. $seed = false; 
  290. if( isset( $_SESSION['seed'] ) ) { 
  291. $seed = $_SESSION['seed']; 
  292.  
  293. // Set new seed if none exists 
  294. if ( ! $seed ) { 
  295. $seed = rand(); 
  296. $_SESSION['seed'] = $seed; 
  297.  
  298. if($query->query_vars["orderby"] == 'random') { 
  299. $query->query_orderby = 'ORDER by RAND('. $seed.')'; 
  300.  
  301. return $query; 
  302.  
  303. /*** 
  304. *** @hook in the member results array 
  305. ***/ 
  306. add_filter('um_prepare_user_results_array', 'um_prepare_user_results_array', 50, 2); 
  307. function um_prepare_user_results_array($result) { 
  308.  
  309. if ( empty( $result['users_per_page'] ) ) { 
  310. $result['no_users'] = 1; 
  311. } else { 
  312. $result['no_users'] = 0; 
  313.  
  314. return $result; 
  315.  
  316. /** 
  317. * Retrieves search filter options from a callback 
  318. * @param $atts array 
  319. * @return $atts array 
  320. */ 
  321. add_filter('um_search_select_fields', 'um_search_select_fields'); 
  322. function um_search_select_fields( $atts ) { 
  323.  
  324. global $ultimatemember; 
  325.  
  326. if( isset( $atts['custom_dropdown_options_source'] ) && ! empty( $atts['custom_dropdown_options_source'] ) ) { 
  327. $atts['custom'] = true; 
  328. $atts['options'] = $ultimatemember->fields->get_options_from_callback( $atts, $atts['type'] ); 
  329.  
  330. return $atts; 
  331.  
  332. /** 
  333. * Filter gender query argument 
  334. * @param array $field_query  
  335. * @return array 
  336. */ 
  337. add_filter('um_query_args_gender__filter', 'um_query_args_gender__filter'); 
  338. function um_query_args_gender__filter( $field_query ) { 
  339.  
  340. unset( $field_query[1] ); 
  341.  
  342. return $field_query; 
.