/core/um-user.php

  1. <?php 
  2.  
  3. class UM_User { 
  4.  
  5. function __construct() { 
  6.  
  7. $this->id = 0; 
  8. $this->usermeta = null; 
  9. $this->data = null; 
  10. $this->profile = null; 
  11.  
  12. $this->banned_keys = array( 
  13. 'metabox', 'postbox', 'meta-box',  
  14. 'dismissed_wp_pointers', 'session_tokens',  
  15. 'screen_layout', 'wp_user-', 'dismissed',  
  16. 'cap_key', 'wp_capabilities',  
  17. 'managenav', 'nav_menu', 'user_activation_key',  
  18. 'level_', 'wp_user_level' 
  19. ); 
  20.  
  21. add_action('init', array(&$this, 'set'), 1); 
  22.  
  23. $this->preview = false; 
  24.  
  25. // a list of keys that should never be in wp_usermeta 
  26. $this->update_user_keys = array( 
  27. 'user_email',  
  28. 'user_pass',  
  29. 'user_password',  
  30. 'display_name',  
  31. ); 
  32.  
  33. $this->target_id = null; 
  34.  
  35. // When the cache should be cleared 
  36. add_action('um_delete_user_hook', array(&$this, 'remove_cached_queue') ); 
  37. add_action('um_new_user_registration_plain', array(&$this, 'remove_cached_queue') ); 
  38. add_action('um_after_user_status_is_changed_hook', array(&$this, 'remove_cached_queue') ); 
  39.  
  40. // When user cache should be cleared 
  41. add_action('um_after_user_updated', array(&$this, 'remove_cache') ); 
  42. add_action('um_after_user_account_updated', array(&$this, 'remove_cache') ); 
  43. add_action('personal_options_update', array(&$this, 'remove_cache') ); 
  44. add_action('edit_user_profile_update', array(&$this, 'remove_cache') ); 
  45. add_action('um_when_role_is_set', array(&$this, 'remove_cache') ); 
  46. add_action('um_when_status_is_set', array(&$this, 'remove_cache') ); 
  47.  
  48. add_action( 'show_user_profile', array( $this, 'community_role_edit' ) ); 
  49. add_action( 'edit_user_profile', array( $this, 'community_role_edit' ) ); 
  50. add_action( 'personal_options_update', array( $this, 'community_role_save' ) ); 
  51. add_action( 'edit_user_profile_update', array( $this, 'community_role_save' ) ); 
  52.  
  53.  
  54. /** 
  55. * Allow changing community role 
  56. */ 
  57. function community_role_edit( $user ) { 
  58. global $ultimatemember; 
  59. if ( current_user_can( 'edit_users' ) && current_user_can( 'edit_user', $user->ID ) ) { 
  60.  
  61. $um_user_role = get_user_meta($user->ID, 'role', true); 
  62. ?> 
  63. <table class="form-table"> 
  64. <tbody> 
  65. <tr> 
  66. <th> 
  67. <label for="um_role"><?php _e( 'Community Role', 'ultimatemember' ); ?></label> 
  68. </th> 
  69. <td> 
  70. <select name="um_role" id="um_role"> 
  71. <?php foreach( $ultimatemember->query->get_roles() as $key => $value ) { ?> 
  72. <option value="<?php echo $key; ?>" <?php selected( $um_user_role, $key ); ?> ><?php echo $value; ?></option> 
  73. <?php } ?> 
  74. </select> 
  75. <span class="description"><?php _e( 'Assign or change the community role for this user', 'ultimatemember' ); ?></span> 
  76. </td> 
  77. </tr> 
  78. </tbody> 
  79. </table> 
  80. <?php } 
  81.  
  82. /** 
  83. * Save community role 
  84. */ 
  85. public function community_role_save( $user_id ) { 
  86. if ( current_user_can( 'edit_user', $user_id ) && isset( $_POST['um_role'] ) ) { 
  87. update_user_meta( $user_id, 'role', sanitize_title_with_dashes( $_POST['um_role'] ) ); 
  88. delete_option( "um_cache_userdata_{$user_id}" ); 
  89.  
  90. /*** 
  91. *** @Remove cached queue from Users backend 
  92. ***/ 
  93. function remove_cached_queue() { 
  94. delete_option('um_cached_users_queue'); 
  95.  
  96. /*** 
  97. *** @Converts object to array 
  98. ***/ 
  99. function toArray($obj) 
  100. if (is_object($obj)) $obj = (array)$obj; 
  101. if (is_array($obj)) { 
  102. $new = array(); 
  103. foreach ($obj as $key => $val) { 
  104. $new[$key] = $this->toArray($val); 
  105. } else { 
  106. $new = $obj; 
  107.  
  108. return $new; 
  109.  
  110. function get_cached_data( $user_id ) { 
  111.  
  112. $disallow_cache = um_get_option('um_profile_object_cache_stop'); 
  113. if( $disallow_cache ) { 
  114. return ''; 
  115.  
  116. if ( is_numeric( $user_id ) && $user_id > 0 ) { 
  117. $find_user = get_option("um_cache_userdata_{$user_id}"); 
  118. if ( $find_user ) { 
  119. $find_user = apply_filters('um_user_permissions_filter', $find_user, $user_id); 
  120. return $find_user; 
  121. return ''; 
  122.  
  123. function setup_cache( $user_id, $profile ) { 
  124.  
  125. $disallow_cache = um_get_option('um_profile_object_cache_stop'); 
  126. if( $disallow_cache ) { 
  127. return ''; 
  128.  
  129. update_option( "um_cache_userdata_{$user_id}", $profile ); 
  130.  
  131. function remove_cache( $user_id ) { 
  132. delete_option( "um_cache_userdata_{$user_id}" ); 
  133.  
  134. /** 
  135. * @function set() 
  136. * 
  137. * @description This method lets you set a user. For example, to retrieve a profile or anything related to that user. 
  138. * 
  139. * @usage <?php $ultimatemember->user->set( $user_id, $clean = false ); ?> 
  140. * 
  141. * @param $user_id (numeric) (optional) Which user to retrieve. A numeric user ID 
  142. * @param $clean (boolean) (optional) Should be true or false. Basically, if you did not provide a user ID It will set the current logged in user as a profile 
  143. * 
  144. * @returns This API method does not return anything. It sets user profile and permissions and allow you to retrieve any details for that user. 
  145. * 
  146. * @example The following example makes you set a user and retrieve their display name after that using the user API. 
  147.   
  148. <?php 
  149.   
  150. $ultimatemember->user->set( 12 ); 
  151. $display_name = $ultimatemember->user->profile['display_name']; // Should print user display name 
  152.   
  153. ?> 
  154.   
  155. * 
  156. * 
  157. */ 
  158. function set( $user_id = null, $clean = false ) { 
  159. global $ultimatemember; 
  160.  
  161. if ( isset( $this->profile ) ) { 
  162. unset( $this->profile ); 
  163.  
  164. if ($user_id) { 
  165. $this->id = $user_id; 
  166. } elseif (is_user_logged_in() && $clean == false ) { 
  167. $this->id = get_current_user_id(); 
  168. } else { 
  169. $this->id = 0; 
  170.  
  171. if ( $this->get_cached_data( $this->id ) ) { 
  172. $this->profile = $this->get_cached_data( $this->id ); 
  173. } else { 
  174.  
  175. if ($user_id) { 
  176.  
  177. $this->id = $user_id; 
  178. $this->usermeta = get_user_meta($user_id); 
  179. $this->data = get_userdata($this->id); 
  180.  
  181. } elseif (is_user_logged_in() && $clean == false ) { 
  182.  
  183. $this->id = get_current_user_id(); 
  184. $this->usermeta = get_user_meta($this->id); 
  185. $this->data = get_userdata($this->id); 
  186.  
  187. } else { 
  188.  
  189. $this->id = 0; 
  190. $this->usermeta = null; 
  191. $this->data = null; 
  192.  
  193.  
  194. // we have a user, populate a profile 
  195. if ( $this->id && $this->toArray($this->data) ) { 
  196.  
  197. // add user data 
  198. $this->data = $this->toArray($this->data); 
  199.  
  200. foreach( $this->data as $k=>$v ) { 
  201. if ($k == 'roles') { 
  202. $this->profile['wp_roles'] = implode(', ', $v); 
  203. } else if (is_array($v)) { 
  204. foreach($v as $k2 => $v2) { 
  205. $this->profile[$k2] = $v2; 
  206. } else { 
  207. $this->profile[$k] = $v; 
  208.  
  209. // add account status 
  210. if ( !isset( $this->usermeta['account_status'][0] ) ) { 
  211. $this->usermeta['account_status'][0] = 'approved'; 
  212.  
  213. if ( $this->usermeta['account_status'][0] == 'approved' ) { 
  214. $this->usermeta['account_status_name'][0] = __('Approved', 'ultimatemember'); 
  215.  
  216. if ( $this->usermeta['account_status'][0] == 'awaiting_email_confirmation' ) { 
  217. $this->usermeta['account_status_name'][0] = __('Awaiting E-mail Confirmation', 'ultimatemember'); 
  218.  
  219. if ( $this->usermeta['account_status'][0] == 'awaiting_admin_review' ) { 
  220. $this->usermeta['account_status_name'][0] = __('Pending Review', 'ultimatemember'); 
  221.  
  222. if ( $this->usermeta['account_status'][0] == 'rejected' ) { 
  223. $this->usermeta['account_status_name'][0] = __('Membership Rejected', 'ultimatemember'); 
  224.  
  225. if ( $this->usermeta['account_status'][0] == 'inactive' ) { 
  226. $this->usermeta['account_status_name'][0] = __('Membership Inactive', 'ultimatemember'); 
  227.  
  228. // add user meta 
  229. foreach($this->usermeta as $k=>$v) { 
  230. if ( $k == 'display_name') continue; 
  231. $this->profile[$k] = $v[0]; 
  232.  
  233. // add permissions 
  234. $user_role = $this->get_role(); 
  235. $this->role_meta = $ultimatemember->query->role_data( $user_role ); 
  236. $this->role_meta = apply_filters('um_user_permissions_filter', $this->role_meta, $this->id); 
  237.  
  238. $this->profile = array_merge( $this->profile, (array)$this->role_meta); 
  239.  
  240. $this->profile['super_admin'] = ( is_super_admin( $this->id ) ) ? 1 : 0; 
  241.  
  242. // clean profile 
  243. $this->clean(); 
  244.  
  245. // Setup cache 
  246. $this->setup_cache( $this->id, $this->profile ); 
  247.  
  248.  
  249.  
  250.  
  251. /*** 
  252. *** @reset user data 
  253. ***/ 
  254. function reset( $clean = false ) { 
  255. $this->set(0, $clean); 
  256.  
  257. /*** 
  258. *** @Clean user profile 
  259. ***/ 
  260. function clean() { 
  261. foreach($this->profile as $key => $value) { 
  262. foreach($this->banned_keys as $ban) { 
  263. if (strstr($key, $ban) || is_numeric($key) ) 
  264. unset($this->profile[$key]); 
  265.  
  266. /** 
  267. * @function auto_login() 
  268. * 
  269. * @description This method lets you auto sign-in a user to your site. 
  270. * 
  271. * @usage <?php $ultimatemember->user->auto_login( $user_id, $rememberme = false ); ?> 
  272. * 
  273. * @param $user_id (numeric) (required) Which user ID to sign in automatically 
  274. * @param $rememberme (boolean) (optional) Should be true or false. If you want the user sign in session to use cookies, use true 
  275. * 
  276. * @returns Sign in the specified user automatically. 
  277. * 
  278. * @example The following example lets you sign in a user automatically by their ID. 
  279.   
  280. <?php $ultimatemember->user->auto_login( 2 ); ?> 
  281.   
  282. * 
  283. * 
  284. * @example The following example lets you sign in a user automatically by their ID and makes the plugin remember their session. 
  285.   
  286. <?php $ultimatemember->user->auto_login( 10, true ); ?> 
  287.   
  288. * 
  289. * 
  290. */ 
  291. function auto_login( $user_id, $rememberme = 0 ) { 
  292.  
  293. wp_set_current_user( $user_id ); 
  294.  
  295. wp_set_auth_cookie( $user_id, $rememberme ); 
  296.  
  297. $user = get_user_by('ID', $user_id ); 
  298.  
  299. do_action( 'wp_login', $user->user_login, $user ); 
  300.  
  301.  
  302. /*** 
  303. *** @Set user's registration details 
  304. ***/ 
  305. function set_registration_details( $submitted ) { 
  306.  
  307. if ( isset( $submitted['user_pass'] ) ) { 
  308. unset( $submitted['user_pass'] ); 
  309.  
  310. if ( isset( $submitted['user_password'] ) ) { 
  311. unset( $submitted['user_password'] ); 
  312.  
  313. if ( isset( $submitted['confirm_user_password'] ) ) { 
  314. unset( $submitted['confirm_user_password'] ); 
  315.  
  316. $submitted = apply_filters('um_before_save_filter_submitted', $submitted ); 
  317.  
  318. do_action('um_before_save_registration_details', $this->id, $submitted ); 
  319.  
  320. update_user_meta( $this->id, 'submitted', $submitted ); 
  321.  
  322. do_action('um_after_save_registration_details', $this->id, $submitted ); 
  323.  
  324.  
  325. /*** 
  326. *** @A plain version of password 
  327. ***/ 
  328. function set_plain_password( $plain ) { 
  329. update_user_meta( $this->id, '_um_cool_but_hard_to_guess_plain_pw', $plain ); 
  330.  
  331. /** 
  332. * Set last login for new registered users 
  333. */ 
  334. function set_last_login() { 
  335. update_user_meta( $this->id, '_um_last_login', current_time( 'timestamp' ) ); 
  336.  
  337. function set_role( $role ) { 
  338.  
  339. do_action('um_when_role_is_set', um_user('ID') ); 
  340.  
  341. do_action('um_before_user_role_is_changed'); 
  342.  
  343. $this->profile['role'] = $role; 
  344.  
  345. do_action('um_member_role_upgrade', $role, $this->profile['role'] ); 
  346.  
  347. $this->update_usermeta_info('role'); 
  348.  
  349. do_action('um_after_user_role_is_changed'); 
  350.  
  351. do_action('um_after_user_role_is_updated', um_user('ID'), $role ); 
  352.  
  353.  
  354. /*** 
  355. *** @Set user's account status 
  356. ***/ 
  357. function set_status( $status ) { 
  358.  
  359. do_action( 'um_when_status_is_set', um_user('ID') ); 
  360.  
  361. $this->profile['account_status'] = $status; 
  362.  
  363. $this->update_usermeta_info('account_status'); 
  364.  
  365. do_action( 'um_after_user_status_is_changed_hook' ); 
  366.  
  367. do_action( 'um_after_user_status_is_changed', $status); 
  368.  
  369.  
  370. /*** 
  371. *** @Set user's hash for password reset 
  372. ***/ 
  373. function password_reset_hash() { 
  374. global $ultimatemember; 
  375.  
  376. $this->profile['reset_pass_hash'] = $ultimatemember->validation->generate(); 
  377. $this->update_usermeta_info('reset_pass_hash'); 
  378.  
  379.  
  380. /*** 
  381. *** @Set user's hash 
  382. ***/ 
  383. function assign_secretkey() { 
  384. global $ultimatemember; 
  385.  
  386. do_action('um_before_user_hash_is_changed'); 
  387.  
  388. $this->profile['account_secret_hash'] = $ultimatemember->validation->generate(); 
  389. $this->update_usermeta_info('account_secret_hash'); 
  390.  
  391. do_action('um_after_user_hash_is_changed'); 
  392.  
  393.  
  394. /*** 
  395. *** @password reset email 
  396. ***/ 
  397. function password_reset() { 
  398. global $ultimatemember; 
  399. $this->password_reset_hash(); 
  400. $ultimatemember->mail->send( um_user('user_email'), 'resetpw_email' ); 
  401.  
  402.  
  403. /*** 
  404. *** @password changed email 
  405. ***/ 
  406. function password_changed() { 
  407. global $ultimatemember; 
  408. $ultimatemember->mail->send( um_user('user_email'), 'changedpw_email' ); 
  409.  
  410. /** 
  411. * @function approve() 
  412. * 
  413. * @description This method approves a user membership and sends them an optional welcome/approval e-mail. 
  414. * 
  415. * @usage <?php $ultimatemember->user->approve(); ?> 
  416. * 
  417. * @returns Approves a user membership. 
  418. * 
  419. * @example Approve a pending user and allow him to sign-in to your site. 
  420.   
  421. <?php 
  422.   
  423. um_fetch_user( 352 ); 
  424. $ultimatemember->user->approve(); 
  425.   
  426. ?> 
  427.   
  428. * 
  429. * 
  430. */ 
  431. function approve() { 
  432. global $ultimatemember; 
  433.  
  434. $user_id = um_user('ID'); 
  435. delete_option( "um_cache_userdata_{$user_id}" ); 
  436.  
  437. if ( um_user('account_status') == 'awaiting_admin_review' ) { 
  438. $this->password_reset_hash(); 
  439. $ultimatemember->mail->send( um_user('user_email'), 'approved_email' ); 
  440.  
  441. } else { 
  442. $this->password_reset_hash(); 
  443. $ultimatemember->mail->send( um_user('user_email'), 'welcome_email'); 
  444.  
  445. $this->set_status('approved'); 
  446. $this->delete_meta('account_secret_hash'); 
  447. $this->delete_meta('_um_cool_but_hard_to_guess_plain_pw'); 
  448.  
  449. do_action('um_after_user_is_approved', um_user('ID') ); 
  450.  
  451.  
  452. /*** 
  453. *** @pending email 
  454. ***/ 
  455. function email_pending() { 
  456. global $ultimatemember; 
  457. $this->assign_secretkey(); 
  458. $this->set_status('awaiting_email_confirmation'); 
  459. $ultimatemember->mail->send( um_user('user_email'), 'checkmail_email' ); 
  460.  
  461. /** 
  462. * @function pending() 
  463. * 
  464. * @description This method puts a user under manual review by administrator and sends them an optional e-mail. 
  465. * 
  466. * @usage <?php $ultimatemember->user->pending(); ?> 
  467. * 
  468. * @returns Puts a user under review and sends them an email optionally. 
  469. * 
  470. * @example An example of putting a user pending manual review 
  471.   
  472. <?php 
  473.   
  474. um_fetch_user( 54 ); 
  475. $ultimatemember->user->pending(); 
  476.   
  477. ?> 
  478.   
  479. * 
  480. * 
  481. */ 
  482. function pending() { 
  483. global $ultimatemember; 
  484. $this->set_status('awaiting_admin_review'); 
  485. $ultimatemember->mail->send( um_user('user_email'), 'pending_email' ); 
  486.  
  487. /** 
  488. * @function reject() 
  489. * 
  490. * @description This method rejects a user membership and sends them an optional e-mail. 
  491. * 
  492. * @usage <?php $ultimatemember->user->reject(); ?> 
  493. * 
  494. * @returns Rejects a user membership. 
  495. * 
  496. * @example Reject a user membership example 
  497.   
  498. <?php 
  499.   
  500. um_fetch_user( 114 ); 
  501. $ultimatemember->user->reject(); 
  502.   
  503. ?> 
  504.   
  505. * 
  506. * 
  507. */ 
  508. function reject() { 
  509. global $ultimatemember; 
  510. $this->set_status('rejected'); 
  511. $ultimatemember->mail->send( um_user('user_email'), 'rejected_email' ); 
  512.  
  513. /** 
  514. * @function deactivate() 
  515. * 
  516. * @description This method deactivates a user membership and sends them an optional e-mail. 
  517. * 
  518. * @usage <?php $ultimatemember->user->deactivate(); ?> 
  519. * 
  520. * @returns Deactivates a user membership. 
  521. * 
  522. * @example Deactivate a user membership with the following example 
  523.   
  524. <?php 
  525.   
  526. um_fetch_user( 32 ); 
  527. $ultimatemember->user->deactivate(); 
  528.   
  529. ?> 
  530.   
  531. * 
  532. * 
  533. */ 
  534. function deactivate() { 
  535. global $ultimatemember; 
  536. $this->set_status('inactive'); 
  537.  
  538. do_action('um_after_user_is_inactive', um_user('ID') ); 
  539.  
  540. $ultimatemember->mail->send( um_user('user_email'), 'inactive_email' ); 
  541.  
  542. /*** 
  543. *** @delete user 
  544. ***/ 
  545. function delete( $send_mail = true ) { 
  546. global $ultimatemember; 
  547.  
  548. do_action( 'um_delete_user_hook' ); 
  549. do_action( 'um_delete_user', um_user('ID') ); 
  550.  
  551. // send email notifications 
  552. if ( $send_mail ) { 
  553. $ultimatemember->mail->send( um_user('user_email'), 'deletion_email' ); 
  554. $ultimatemember->mail->send( um_admin_email(), 'notification_deletion', array('admin' => true ) ); 
  555.  
  556. // remove uploads 
  557. $ultimatemember->files->remove_dir( um_user_uploads_dir() ); 
  558.  
  559. // remove user 
  560. if ( is_multisite() ) { 
  561.  
  562. if ( !function_exists('wpmu_delete_user') ) { 
  563. require_once( ABSPATH . 'wp-admin/includes/ms.php' ); 
  564.  
  565. wpmu_delete_user( $this->id ); 
  566.  
  567. } else { 
  568.  
  569. if ( !function_exists('wp_delete_user') ) { 
  570. require_once( ABSPATH . 'wp-admin/includes/user.php' ); 
  571.  
  572. wp_delete_user( $this->id ); 
  573.  
  574.  
  575.  
  576. /** 
  577. * @function get_role() 
  578. * 
  579. * @description This method gets a user role in slug format. e.g. member 
  580. * 
  581. * @usage <?php $ultimatemember->user->get_role(); ?> 
  582. * 
  583. * @returns The user role's slug. 
  584. * 
  585. * @example Do something if the user's role is paid-member 
  586.   
  587. <?php 
  588.   
  589. um_fetch_user( 12 ); 
  590.   
  591. if ( $ultimatemember->user->get_role() == 'paid-member' ) { 
  592. // Show this to paid customers 
  593. } else { 
  594. // You are a free member 
  595. } 
  596.   
  597. ?> 
  598.   
  599. * 
  600. * 
  601. */ 
  602. function get_role() { 
  603. global $ultimatemember; 
  604.  
  605. if (isset($this->profile['role']) && !empty( $this->profile['role'] ) ) { 
  606. return $this->profile['role']; 
  607. } else { 
  608. if ( $this->profile['wp_roles'] == 'administrator' ) { 
  609. return 'admin'; 
  610. } else { 
  611. return 'member'; 
  612.  
  613. function get_role_name( $slug, $return_role_id = false ) { 
  614. global $wpdb, $ultimatemember; 
  615.  
  616. if( isset( $ultimatemember->profile->arr_user_roles[ 'is_'.$return_role_id ][ $slug ] ) ) { 
  617. return $ultimatemember->profile->arr_user_roles[ 'is_'.$return_role_id ][ $slug ]; 
  618.  
  619. $args = array( 
  620. 'posts_per_page' => 1,  
  621. 'post_type' => 'um_role',  
  622. 'name' => $slug,  
  623. 'post_status' => array('publish'),  
  624. ); 
  625.  
  626. $roles = new WP_Query( $args ); 
  627. $role_id = 0; 
  628. $role_title = ''; 
  629.  
  630. if ( $roles->have_posts() ) { 
  631. while ( $roles->have_posts() ) { 
  632. $roles->the_post(); 
  633. $role_id = get_the_ID(); 
  634. $role_title = get_the_title(); 
  635.  
  636. wp_reset_query();  
  637.  
  638. $ultimatemember->profile->arr_user_roles[ 'is_1' ][ $slug ] = $role_id; 
  639. $ultimatemember->profile->arr_user_roles[ 'is_' ][ $slug ] = $role_title; 
  640.  
  641. if( $return_role_id ) { 
  642. return $role_id; 
  643.  
  644. return $role_title; 
  645.  
  646. /** 
  647. * Get role slug by ID 
  648. * @param integer $id  
  649. * @return string 
  650. */ 
  651. function get_role_slug_by_id( $id ) { 
  652. global $wpdb, $ultimatemember; 
  653.  
  654.  
  655. $args = array( 
  656. 'posts_per_page' => 1,  
  657. 'post_type' => 'um_role',  
  658. 'page_id' => $id,  
  659. 'post_status' => array('publish'),  
  660. ); 
  661.  
  662. $roles = new WP_Query( $args ); 
  663. $role_slug = ''; 
  664.  
  665. if ( $roles->have_posts() ) { 
  666. $role_slug = $roles->post->post_name; 
  667.  
  668. wp_reset_query();  
  669.  
  670. return $role_slug; 
  671.  
  672. /*** 
  673. *** @Update one key in user meta 
  674. ***/ 
  675. function update_usermeta_info( $key ) { 
  676. // delete the key first just in case 
  677. delete_user_meta( $this->id, $key ); 
  678. update_user_meta( $this->id, $key, $this->profile[$key] ); 
  679.  
  680. /** 
  681. * @function delete_meta() 
  682. * 
  683. * @description This method can be used to delete user's meta key. 
  684. * 
  685. * @usage <?php $ultimatemember->user->delete_meta( $key ); ?> 
  686. * 
  687. * @param $key (string) (required) The meta field key to remove from user 
  688. * 
  689. * @returns This method will not return anything. The specified meta key will be deleted from database for the specified user. 
  690. * 
  691. * @example Delete user's age field 
  692.   
  693. <?php 
  694.   
  695. um_fetch_user( 15 ); 
  696. $ultimatemember->user->delete_meta( 'age' ); 
  697.   
  698. ?> 
  699.   
  700. * 
  701. * 
  702. */ 
  703. function delete_meta( $key ) { 
  704. delete_user_meta( $this->id, $key ); 
  705.  
  706. /*** 
  707. *** @Get all bulk actions 
  708. ***/ 
  709. function get_bulk_admin_actions() { 
  710. $output = ''; 
  711. $actions = array(); 
  712. $actions = apply_filters('um_admin_bulk_user_actions_hook', $actions ); 
  713. foreach($actions as $id => $arr ) { 
  714. if ( isset($arr['disabled'])) { 
  715. $arr['disabled'] = 'disabled'; 
  716. } else { 
  717. $arr['disabled'] = ''; 
  718.  
  719. $output .= '<option value="' . $id . '" '. $arr['disabled'] . '>' . $arr['label'] . '</option>'; 
  720. return $output; 
  721.  
  722. /*** 
  723. *** @Get admin actions for individual user 
  724. ***/ 
  725. function get_admin_actions() { 
  726. $items = ''; 
  727. $actions = array(); 
  728. $actions = apply_filters('um_admin_user_actions_hook', $actions ); 
  729. if ( !isset( $actions ) || empty( $actions ) ) return false; 
  730. foreach($actions as $id => $arr ) { 
  731. $url = add_query_arg('um_action', $id ); 
  732. $url = add_query_arg('uid', um_profile_id(), $url ); 
  733. $items[] = '<a href="' . $url .'" class="real_url '.$id.'-item">' . $arr['label'] . '</a>'; 
  734. return $items; 
  735.  
  736. /** 
  737. * @function is_private_profile() 
  738. * 
  739. * @description This method checks if give user profile is private. 
  740. * 
  741. * @usage <?php $ultimatemember->user->is_private_profile( $user_id ); ?> 
  742. * 
  743. * @param $user_id (numeric) (required) A user ID must be passed to check if the user profile is private 
  744. * 
  745. * @returns Returns true if user profile is private and false if user profile is public. 
  746. * 
  747. * @example This example display a specific user's name If his profile is public 
  748.   
  749. <?php 
  750.   
  751. um_fetch_user( 60 ); 
  752. $is_private = $ultimatemember->user->is_private_profile( 60 ); 
  753. if ( !$is_private ) { 
  754. echo 'User is public and his name is ' . um_user('display_name'); 
  755. } 
  756.   
  757. ?> 
  758.   
  759. * 
  760. * 
  761. */ 
  762. function is_private_profile( $user_id ) { 
  763. $privacy = get_user_meta( $user_id, 'profile_privacy', true ); 
  764. if ( $privacy == __('Only me', 'ultimatemember') ) { 
  765. return true; 
  766. return false; 
  767.  
  768. /** 
  769. * @function is_approved() 
  770. * 
  771. * @description This method can be used to determine If a certain user is approved or not. 
  772. * 
  773. * @usage <?php $ultimatemember->user->is_approved( $user_id ); ?> 
  774. * 
  775. * @param $user_id (numeric) (required) The user ID to check approval status for 
  776. * 
  777. * @returns True if user is approved and false if user is not approved. 
  778. * 
  779. * @example Do something If a user's membership is approved 
  780.   
  781. <?php 
  782.   
  783. if ( $ultimatemember->user->is_approved( 55 ) { 
  784. // User account is approved 
  785. } else { 
  786. // User account is not approved 
  787. } 
  788.   
  789. ?> 
  790.   
  791. * 
  792. * 
  793. */ 
  794. function is_approved( $user_id ) { 
  795. $status = get_user_meta( $user_id, 'account_status', true ); 
  796. if ( $status == 'approved' || $status == '' ) { 
  797. return true; 
  798. return false; 
  799.  
  800. /*** 
  801. *** @Is private 
  802. ***/ 
  803. function is_private_case( $user_id, $case ) { 
  804. $privacy = get_user_meta( $user_id, 'profile_privacy', true ); 
  805.  
  806. if ( $privacy == $case ) { 
  807. $bool = apply_filters('um_is_private_filter_hook', false, $privacy, $user_id ); 
  808. return $bool; 
  809.  
  810. return false; 
  811.  
  812. /*** 
  813. *** @update files 
  814. ***/ 
  815. function update_files( $changes ) { 
  816.  
  817. global $ultimatemember; 
  818.  
  819. foreach( $changes as $key => $uri ) { 
  820. $src = um_is_temp_upload( $uri ); 
  821. $ultimatemember->files->new_user_upload( $this->id, $src, $key ); 
  822.  
  823.  
  824. /*** 
  825. *** @update profile 
  826. ***/ 
  827. function update_profile( $changes ) { 
  828.  
  829. global $ultimatemember; 
  830.  
  831. $args['ID'] = $this->id; 
  832. $changes = apply_filters('um_before_update_profile', $changes, $this->id); 
  833.  
  834. // save or update profile meta 
  835. foreach( $changes as $key => $value ) { 
  836. if ( !in_array( $key, $this->update_user_keys ) ) { 
  837.  
  838. update_user_meta( $this->id, $key, $value ); 
  839.  
  840. } else { 
  841.  
  842. $args[$key] = esc_attr( $changes[$key] ); 
  843.  
  844.  
  845.  
  846.  
  847. // update user 
  848. if ( count( $args ) > 1 ) { 
  849. wp_update_user( $args ); 
  850.  
  851.  
  852. /*** 
  853. *** @user exists by meta key and value 
  854. ***/ 
  855. function user_has_metadata( $key, $value ) { 
  856.  
  857. global $ultimatemember; 
  858. $value = $ultimatemember->validation->safe_name_in_url( $value ); 
  859.  
  860. $ids = get_users(array( 'fields' => 'ID', 'meta_key' => $key, 'meta_value' => $value, 'meta_compare' => '=') ); 
  861. if ( !isset( $ids ) || empty( $ids ) ) return false; 
  862. foreach( $ids as $k => $id ) { 
  863. if ( $id == um_user('ID') ) { 
  864. unset( $ids[$k] ); 
  865. } else { 
  866. $duplicates[] = $id; 
  867. if ( isset( $duplicates ) && !empty( $duplicates ) ) 
  868. return count( $duplicates ); 
  869. return false; 
  870.  
  871.  
  872. /*** 
  873. *** @user exists by name 
  874. ***/ 
  875. function user_exists_by_name( $value ) { 
  876.  
  877. global $ultimatemember; 
  878.  
  879. // Permalink base 
  880. $permalink_base = um_get_option('permalink_base'); 
  881.  
  882. $raw_value = $value; 
  883. $value = $ultimatemember->validation->safe_name_in_url( $value ); 
  884. $value = um_clean_user_basename( $value ); 
  885.  
  886. // Search by Profile Slug 
  887. $args = array( 
  888. "fields" => array("ID"),  
  889. 'meta_query' => array( 
  890. 'relation' => 'OR',  
  891. array( 
  892. 'key' => 'um_user_profile_url_slug_'.$permalink_base,  
  893. 'value' => strtolower( $raw_value ),  
  894. 'compare' => '=' 
  895.  
  896.  
  897. ); 
  898.  
  899.  
  900. $ids = new WP_User_Query( $args ); 
  901.  
  902. if( $ids->total_users > 0 ) { 
  903. $um_user_query = current( $ids->get_results() ); 
  904. return $um_user_query->ID; 
  905.  
  906. // Search by Display Name or ID 
  907. $args = array( 
  908. "fields" => array("ID"),  
  909. "search" => $value,  
  910. 'search_columns' => array( 'display_name', 'ID' ) 
  911. );  
  912.  
  913. $ids = new WP_User_Query( $args ); 
  914.  
  915. if( $ids->total_users > 0 ) { 
  916. $um_user_query = current( $ids->get_results() ); 
  917. return $um_user_query->ID; 
  918.  
  919.  
  920. // Search By User Login 
  921. $value = str_replace(".", "_", $value ); 
  922. $value = str_replace(" ", "", $value ); 
  923.  
  924. $args = array( 
  925. "fields" => array("ID"),  
  926. "search" => $value,  
  927. 'search_columns' => array( 
  928. 'user_login',  
  929. ); 
  930.  
  931. $ids = new WP_User_Query( $args ); 
  932.  
  933. if( $ids->total_users > 0 ) { 
  934. $um_user_query = current( $ids->get_results() ); 
  935. return $um_user_query->ID; 
  936.  
  937. return false; 
  938.  
  939.  
  940. /** 
  941. * @function user_exists_by_id() 
  942. * 
  943. * @description This method checks if a user exists or not in your site based on the user ID. 
  944. * 
  945. * @usage <?php $ultimatemember->user->user_exists_by_id( $user_id ); ?> 
  946. * 
  947. * @param $user_id (numeric) (required) A user ID must be passed to check if the user exists 
  948. * 
  949. * @returns Returns true if user exists and false if user does not exist. 
  950. * 
  951. * @example Basic Usage 
  952.   
  953. <?php 
  954.   
  955. $boolean = $ultimatemember->user->user_exists_by_id( 15 ); 
  956. if ( $boolean ) { 
  957. // That user exists 
  958. } 
  959.   
  960. ?> 
  961.   
  962. * 
  963. * 
  964. */ 
  965. function user_exists_by_id( $user_id ) { 
  966. $aux = get_userdata( intval( $user_id ) ); 
  967. if($aux==false) { 
  968. return false; 
  969. } else { 
  970. return $user_id; 
  971. /** 
  972. * @function user_exists_by_email_as_username() 
  973. * 
  974. * @description This method checks if a user exists or not in your site based on the user email as username 
  975. * 
  976. * @usage <?php $ultimatemember->user->user_exists_by_email_as_username( $slug ); ?> 
  977. * 
  978. * @param $slug (string) (required) A user slug must be passed to check if the user exists 
  979. * 
  980. * @returns Returns true if user exists and false if user does not exist. 
  981. * 
  982. * @example Basic Usage 
  983.   
  984. <?php 
  985.   
  986. $boolean = $ultimatemember->user->user_exists_by_email_as_username( 'calumgmail-com' ); 
  987. if ( $boolean ) { 
  988. // That user exists 
  989. } 
  990.   
  991. ?> 
  992.   
  993. * 
  994. * 
  995. */ 
  996. function user_exists_by_email_as_username( $slug ) { 
  997.  
  998. $user_id = false; 
  999.  
  1000. $ids = get_users( array( 'fields' => 'ID', 'meta_key' => 'um_email_as_username_'.$slug ) ); 
  1001. if ( isset( $ids[0] ) && ! empty( $ids[0] ) ) { 
  1002. $user_id = $ids[0]; 
  1003.  
  1004. return $user_id; 
  1005.  
.