UM_Admin_Users

The Ultimate Member UM Admin Users class.

Defined (1)

The class is defined in the following location(s).

/admin/core/um-admin-users.php  
  1. class UM_Admin_Users { 
  2.  
  3. function __construct() { 
  4.  
  5. $this->custom_role = 'um_role'; 
  6.  
  7. add_filter('manage_users_columns', array(&$this, 'manage_users_columns') ); 
  8.  
  9. add_action('manage_users_custom_column', array(&$this, 'manage_users_custom_column'), 10, 3); 
  10.  
  11. add_action('restrict_manage_users', array(&$this, 'restrict_manage_users') ); 
  12.  
  13. add_action('admin_init', array(&$this, 'um_bulk_users_edit'), 9); 
  14.  
  15. add_filter('views_users', array(&$this, 'views_users') ); 
  16.  
  17. add_filter('pre_user_query', array(&$this, 'sort_by_newest') ); 
  18.  
  19. add_filter('pre_user_query', array(&$this, 'custom_users_filter') ); 
  20.  
  21. add_filter('user_row_actions', array(&$this, 'user_row_actions'), 10, 2); 
  22.  
  23.  
  24. /*** 
  25. *** @Custom row actions for users page 
  26. ***/ 
  27. function user_row_actions($actions, $user_object) { 
  28.  
  29. $user_id = $user_object->ID; 
  30. um_fetch_user( $user_id ); 
  31.  
  32. $actions['frontend_profile'] = "<a class='' href='" . um_user_profile_url() . "'>" . __( 'View profile', 'ultimatemember') . "</a>"; 
  33.  
  34. if ( um_user('submitted') ) { 
  35. $actions['view_info'] = '<a href="#" data-modal="UM_preview_registration" data-modal-size="smaller" data-dynamic-content="um_admin_review_registration" data-arg1="'.$user_id.'" data-arg2="edit_registration">' . __('Info', 'ultimatemember') . '</a>'; 
  36.  
  37. $actions = apply_filters('um_admin_user_row_actions', $actions, $user_id ); 
  38.  
  39. return $actions; 
  40.  
  41. /*** 
  42. *** @sort users by newest first 
  43. ***/ 
  44. function sort_by_newest( $query ) { 
  45. global $wpdb, $pagenow; 
  46.  
  47. if ( is_admin() && $pagenow == 'users.php' ) { 
  48. if (!isset($_REQUEST['orderby'])) { 
  49. $query->query_vars["order"] = 'desc'; 
  50. $query->query_orderby = " ORDER BY user_registered ".($query->query_vars["order"] == "desc" ? "desc " : "asc ");//set sort order 
  51.  
  52. return $query; 
  53.  
  54.  
  55. /*** 
  56. *** @custom users filter 
  57. ***/ 
  58. function custom_users_filter( $query ) { 
  59. global $wpdb, $pagenow; 
  60.  
  61. if ( is_admin() && $pagenow=='users.php' && isset($_GET[ $this->custom_role ]) && $_GET[ $this->custom_role ] != '') { 
  62.  
  63. $role = $_GET[ $this->custom_role ]; 
  64. $query->query_where = 
  65. str_replace('WHERE 1=1',  
  66. "WHERE 1=1 AND {$wpdb->users}.ID IN ( 
  67. SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta 
  68. WHERE {$wpdb->usermeta}.meta_key = 'role' 
  69. AND {$wpdb->usermeta}.meta_value = '{$role}')",  
  70. $query->query_where 
  71. ); 
  72.  
  73.  
  74. if ( is_admin() && $pagenow=='users.php' && isset($_GET[ 'status' ]) && $_GET[ 'status' ] != '') { 
  75.  
  76. $status = urldecode($_GET[ 'status' ]); 
  77.  
  78. if ( $status == 'needs-verification') { 
  79. $query->query_where = str_replace('WHERE 1=1',  
  80. "WHERE 1=1 AND {$wpdb->users}.ID IN ( 
  81. SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta 
  82. WHERE {$wpdb->usermeta}.meta_key = '_um_verified' 
  83. AND {$wpdb->usermeta}.meta_value = 'pending')",  
  84. $query->query_where 
  85. ); 
  86. } else { 
  87. $query->query_where = str_replace('WHERE 1=1',  
  88. "WHERE 1=1 AND {$wpdb->users}.ID IN ( 
  89. SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta 
  90. WHERE {$wpdb->usermeta}.meta_key = 'account_status' 
  91. AND {$wpdb->usermeta}.meta_value = '{$status}')",  
  92. $query->query_where 
  93. ); 
  94.  
  95.  
  96. return $query; 
  97.  
  98.  
  99. /*** 
  100. *** @Change the roles with UM roles 
  101. ***/ 
  102. function views_users( $views ) { 
  103. global $ultimatemember, $query; 
  104.  
  105. remove_filter('pre_user_query', array(&$this, 'custom_users_filter') ); 
  106.  
  107. $old_views = $views; 
  108. $views = array(); 
  109.  
  110. if ( !isset($_REQUEST[ $this->custom_role ]) && !isset($_REQUEST['status']) ) { 
  111. $views['all'] = '<a href="'.admin_url('users.php').'" class="current">All <span class="count">('.$ultimatemember->query->count_users().')</span></a>'; 
  112. } else { 
  113. $views['all'] = '<a href="'.admin_url('users.php').'">All <span class="count">('.$ultimatemember->query->count_users().')</span></a>'; 
  114.  
  115. $status = array( 
  116. 'approved' => __('Approved', 'ultimatemember'),  
  117. 'awaiting_admin_review' => __('Pending review', 'ultimatemember'),  
  118. 'awaiting_email_confirmation' => __('Waiting e-mail confirmation', 'ultimatemember'),  
  119. 'inactive' => __('Inactive', 'ultimatemember'),  
  120. 'rejected' => __('Rejected', 'ultimatemember') 
  121. ); 
  122.  
  123. $ultimatemember->query->count_users_by_status( 'unassigned' ); 
  124.  
  125. foreach( $status as $k => $v ) { 
  126. if ( isset($_REQUEST['status']) && $_REQUEST['status'] == $k ) { 
  127. $current = 'class="current"'; 
  128. } else { 
  129. $current = ''; 
  130.  
  131. $views[ $k ] = '<a href="'.admin_url('users.php').'?status='.$k.'" ' . $current . '>'. $v . ' <span class="count">('.$ultimatemember->query->count_users_by_status( $k ).')</span></a>'; 
  132.  
  133. $views = apply_filters('um_admin_views_users', $views ); 
  134.  
  135. // remove all filters 
  136. unset($old_views['all']); 
  137.  
  138. // add separator 
  139. $views['subsep'] = '<span></span>'; 
  140.  
  141. // merge views 
  142. foreach( $old_views as $key => $view ) { 
  143. $views[ $key ] = $view; 
  144.  
  145. return $views; 
  146.  
  147. /*** 
  148. *** @Bulk user editing actions 
  149. ***/ 
  150. function um_bulk_users_edit() { 
  151. global $ultimatemember; 
  152.  
  153. $admin_err = 0; 
  154.  
  155. if (isset($_REQUEST) && !empty ($_REQUEST) ) { 
  156.  
  157. // bulk change role 
  158. if (isset($_REQUEST['users']) && is_array($_REQUEST['users']) && isset($_REQUEST['um_changeit']) && $_REQUEST['um_changeit'] != '' && isset($_REQUEST['um_change_role']) && !empty($_REQUEST['um_change_role']) ) { 
  159.  
  160. if ( ! current_user_can( 'edit_users' ) ) 
  161. wp_die( __( 'You do not have enough permissions to do that.', 'ultimatemember') ); 
  162.  
  163. check_admin_referer('bulk-users'); 
  164.  
  165. $users = $_REQUEST['users']; 
  166. $new_role = current( array_filter( $_REQUEST['um_change_role'] ) ); 
  167.  
  168. foreach($users as $user_id) { 
  169. $ultimatemember->user->set( $user_id ); 
  170. // change role for non-wp admins or non-community admins only 
  171. if ( current_user_can('manage_options') || ( !um_user('super_admin') || um_user('role') != 'admin' ) ) { 
  172. $ultimatemember->user->set_role( $new_role ); 
  173. } else { 
  174. $admin_err = 1; 
  175.  
  176. if ( $admin_err == 0 ) { 
  177.  
  178. $uri = admin_url('users.php'); 
  179.  
  180. $uri = $this->set_redirect_uri( $uri ); 
  181.  
  182. $uri = add_query_arg( 'update', 'users_role_updated', $uri ); 
  183.  
  184. wp_redirect( $uri ); 
  185.  
  186. exit; 
  187.  
  188. } else { 
  189.  
  190. $uri = admin_url('users.php'); 
  191.  
  192. $uri = $this->set_redirect_uri( $uri ); 
  193.  
  194. $uri = add_query_arg( 'update', 'err_admin_role', $uri ); 
  195.  
  196. wp_redirect( $uri ); 
  197.  
  198. exit; 
  199.  
  200. } else if ( isset($_REQUEST['um_changeit']) && $_REQUEST['um_changeit'] != '' ) { 
  201.  
  202. $uri = admin_url('users.php'); 
  203.  
  204. $uri = $this->set_redirect_uri( $uri ); 
  205.  
  206. wp_redirect( $uri ); 
  207.  
  208. exit; 
  209.  
  210.  
  211. // bulk edit users 
  212. if ( isset($_REQUEST['users']) && is_array($_REQUEST['users']) && isset($_REQUEST['um_bulkedit']) && $_REQUEST['um_bulkedit'] != '' && isset($_REQUEST['um_bulk_action']) && !empty($_REQUEST['um_bulk_action']) ) { 
  213.  
  214. if ( ! current_user_can( 'edit_users' ) ) 
  215. wp_die( __( 'You do not have enough permissions to do that.', 'ultimatemember') ); 
  216.  
  217. check_admin_referer('bulk-users'); 
  218.  
  219. $users = $_REQUEST['users']; 
  220. $bulk_action = current( array_filter( $_REQUEST['um_bulk_action']) ); 
  221.  
  222. foreach($users as $user_id) { 
  223. $ultimatemember->user->set( $user_id ); 
  224. if ( !um_user('super_admin') ) { 
  225.  
  226. do_action("um_admin_user_action_hook", $bulk_action); 
  227.  
  228. do_action("um_admin_user_action_{$bulk_action}_hook"); 
  229.  
  230. } else { 
  231. $admin_err = 1; 
  232.  
  233. // Finished. redirect now 
  234. if ( $admin_err == 0 ) { 
  235.  
  236. $uri = admin_url('users.php'); 
  237.  
  238. $uri = $this->set_redirect_uri( $uri ); 
  239.  
  240. $uri = add_query_arg( 'update', 'users_updated', $uri ); 
  241.  
  242. wp_redirect( $uri ); 
  243.  
  244. exit; 
  245. } else { 
  246. wp_redirect( admin_url('users.php?update=err_users_updated') ); 
  247. exit; 
  248.  
  249. } else if ( isset($_REQUEST['um_bulkedit']) && $_REQUEST['um_bulkedit'] != '' ) { 
  250.  
  251. $uri = admin_url('users.php'); 
  252.  
  253. $uri = $this->set_redirect_uri( $uri ); 
  254.  
  255. wp_redirect( $uri ); 
  256.  
  257. exit; 
  258.  
  259.  
  260. // filter by user role 
  261. if ( isset($_REQUEST['um_filter_role']) && ! isset( $_REQUEST['um_filter_processed'] ) && ( ! isset( $_REQUEST['new_role'] ) || empty( $_REQUEST['new_role'] ) ) && $_REQUEST['um_filter_role'] ) { 
  262. $filter_role = current( array_filter( $_REQUEST['um_filter_role'] ) ); 
  263. $uri = add_query_arg('um_role', $filter_role); 
  264. $uri = add_query_arg('s', $_REQUEST['s'], $uri); 
  265. $uri = add_query_arg('um_filter_processed', true, $uri); 
  266.  
  267. exit( wp_redirect( $uri ) ); 
  268.  
  269.  
  270. /*** 
  271. *** @Add UM roles to users admin 
  272. ***/ 
  273. function restrict_manage_users() { 
  274. global $ultimatemember; 
  275. ?> 
  276.  
  277. <div style="float:right;margin:0 4px"> 
  278.  
  279. <label class="screen-reader-text" for="um_filter_role"><?php _e('Filter by', 'ultimatemember'); ?></label> 
  280. <select name="um_filter_role[]" id="um_filter_role" class="" style="width: 120px"> 
  281. <option value="0"><?php _e('Filter by', 'ultimatemember'); ?></option> 
  282. <?php 
  283. $roles = $ultimatemember->query->get_roles(); 
  284. $um_filter_role = isset( $_REQUEST['um_filter_role'] )? current( array_filter($_REQUEST['um_filter_role']) ):''; 
  285. foreach( $roles as $role => $role_name ) { ?> 
  286. <option value="<?php echo urlencode( $role ); ?>" <?php selected($role, $um_filter_role); ?>><?php echo $role_name; ?></option> 
  287. <?php } ?> 
  288. </select> 
  289.  
  290. <input name="um_role" id="um_role" class="button" value="<?php _e('Filter'); ?>" type="submit" /> 
  291.  
  292. </div> 
  293.  
  294. <div style="float:right;margin:0 4px"> 
  295.  
  296. <label class="screen-reader-text" for="um_bulk_action"><?php _e('UM Action', 'ultimatemember'); ?></label> 
  297. <select name="um_bulk_action[]" id="um_bulk_action" class="" style="width: 200px"> 
  298. <option value="0"><?php _e('UM Action', 'ultimatemember'); ?></option> 
  299. <?php echo $ultimatemember->user->get_bulk_admin_actions(); ?> 
  300. </select> 
  301.  
  302. <input name="um_bulkedit" id="um_bulkedit" class="button" value="<?php _e('Apply'); ?>" type="submit" /> 
  303.  
  304. </div> 
  305.  
  306. <div style="float:right;margin:0 4px"> 
  307.  
  308. <label class="screen-reader-text" for="um_change_role"><?php _e('Community role…', 'ultimatemember'); ?></label> 
  309. <select name="um_change_role[]" id="um_change_role" class="" style="width: 160px"> 
  310. <?php foreach($ultimatemember->query->get_roles( $add_default = 'Community role…' ) as $key => $value) { ?> 
  311. <option value="<?php echo $key; ?>"><?php echo $value; ?></option> 
  312. <?php } ?> 
  313. </select> 
  314.  
  315. <input name="um_changeit" id="um_changeit" class="button" value="<?php _e('Change', 'ultimatemember'); ?>" type="submit" /> 
  316.  
  317. </div> 
  318.  
  319. <?php if( isset( $_REQUEST['status'] ) && ! empty( $_REQUEST['status'] ) ) { ?> 
  320. <input type="hidden" name="status" id="um_status" value="<?php echo esc_attr( $_REQUEST['status'] );?>"/> 
  321. <?php } ?> 
  322.  
  323. <?php 
  324.  
  325.  
  326. /*** 
  327. *** @add user columns 
  328. ***/ 
  329. function manage_users_columns($columns) { 
  330.  
  331. $admin = new UM_Admin_Metabox(); 
  332.  
  333. $columns['um_role'] = __('Community Role', 'ultimatemember') . $admin->_tooltip( __('This is the membership role set by Ultimate Member plugin', 'ultimatemember') ); 
  334.  
  335. return $columns; 
  336.  
  337. /*** 
  338. *** @show user columns 
  339. ***/ 
  340. function manage_users_custom_column($value, $column_name, $user_id) { 
  341. global $ultimatemember; 
  342.  
  343. if ( $this->custom_role == $column_name ) { 
  344.  
  345. if ( get_option( "um_cache_userdata_{$user_id}" ) ) { 
  346. delete_option( "um_cache_userdata_{$user_id}" ); 
  347. um_fetch_user( $user_id ); 
  348. return $ultimatemember->user->get_role_name( um_user('role') ); 
  349.  
  350.  
  351. return $value; 
  352.  
  353. /** 
  354. * Sets redirect URI  
  355. * @param string $uri  
  356. */ 
  357. function set_redirect_uri( $uri ) { 
  358.  
  359. if( isset( $_REQUEST['s'] ) && ! empty( $_REQUEST['s'] ) ) { 
  360. $uri = add_query_arg( 's', $_REQUEST['s'], $uri ); 
  361.  
  362. if( isset( $_REQUEST['status'] ) && ! empty( $_REQUEST['status'] ) ) { 
  363. $uri = add_query_arg( 'status', $_REQUEST['status'], $uri ); 
  364.  
  365. if( isset( $_REQUEST['um_filter_role'] ) && ! empty( $_REQUEST['um_filter_role'] ) ) { 
  366. foreach ( $_REQUEST['um_filter_role'] as $key => $value) { 
  367. $uri = add_query_arg( 'um_filter_role', $value, $uri ); 
  368.  
  369. return $uri; 
  370.  
  371.