2017-07-26 14:57:52 +03:00
< ? php
namespace um\core ;
2023-09-04 13:24:28 +03:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
2017-07-26 14:57:52 +03:00
2018-03-26 01:27:46 +03:00
if ( ! class_exists ( 'um\core\User' ) ) {
2018-03-20 13:24:38 +02:00
/**
* Class User
* @package um\core
*/
class User {
2021-09-21 18:26:57 +03:00
/**
* Data that we will set before the update profile to compare it after update
*
* @var null
*/
public $previous_data = null ;
2023-04-17 20:16:15 +03:00
/**
* @var int
*/
public $id = 0 ;
2021-09-21 18:26:57 +03:00
2018-03-20 13:24:38 +02:00
/**
2023-04-17 20:16:15 +03:00
* @var null
*/
public $usermeta = null ;
/**
* @var null
*/
public $data = null ;
/**
* @var null
*/
public $profile = null ;
/**
* @var null
2018-03-20 13:24:38 +02:00
*/
2023-04-17 20:16:15 +03:00
public $cannot_edit = null ;
2018-03-20 13:24:38 +02:00
2023-04-17 20:16:15 +03:00
/**
* @var null
*/
public $deleted_user_id = null ;
/**
* @var array|string[]
*/
public $banned_keys = array ();
2018-03-20 13:24:38 +02:00
2023-04-17 20:16:15 +03:00
/**
* @var bool
*/
public $preview = false ;
/**
* @var bool
*/
public $send_mail_on_delete = true ;
/**
* A list of keys that should never be in wp_usermeta.
* @var array|string[]
*/
public $update_user_keys = array ();
/**
* @var null
*/
public $target_id = null ;
2023-06-27 10:24:54 +03:00
public $updating_process = false ;
2023-09-04 13:24:28 +03:00
/**
* @var array
*/
public $cached_user = array ();
2023-04-17 20:16:15 +03:00
/**
* User constructor.
*/
public function __construct () {
2020-10-26 17:21:42 +02:00
global $wpdb ;
2018-03-20 13:24:38 +02:00
$this -> banned_keys = array (
2023-04-17 20:16:15 +03:00
'metabox' ,
'postbox' ,
'meta-box' ,
'dismissed_wp_pointers' ,
'session_tokens' ,
'screen_layout' ,
2023-06-28 11:17:28 +03:00
$wpdb -> get_blog_prefix () . 'user-' ,
2023-04-17 20:16:15 +03:00
'dismissed' ,
'cap_key' ,
$wpdb -> get_blog_prefix () . 'capabilities' ,
'managenav' ,
'nav_menu' ,
'user_activation_key' ,
'level_' ,
$wpdb -> get_blog_prefix () . 'user_level' ,
2018-03-20 13:24:38 +02:00
);
// a list of keys that should never be in wp_usermeta
$this -> update_user_keys = array (
'user_email' ,
'user_pass' ,
'user_password' ,
'display_name' ,
2018-03-21 21:15:46 +08:00
'user_url' ,
2018-03-20 13:24:38 +02:00
'role' ,
);
2023-04-17 20:16:15 +03:00
add_action ( 'init' , array ( & $this , 'set' ), 1 );
2018-03-20 13:24:38 +02:00
// When the cache should be cleared
2019-08-08 00:36:33 +03:00
add_action ( 'um_delete_user' , array ( & $this , 'remove_cache' ), 10 , 1 );
2018-05-14 17:55:47 +03:00
2018-03-20 13:24:38 +02:00
// When user cache should be cleared
2019-08-08 00:36:33 +03:00
add_action ( 'um_after_user_updated' , array ( & $this , 'remove_cache' ) );
add_action ( 'um_after_user_account_updated' , array ( & $this , 'remove_cache' ) );
add_action ( 'personal_options_update' , array ( & $this , 'remove_cache' ) );
2018-03-20 13:24:38 +02:00
//add_action('edit_user_profile_update', array(&$this, 'remove_cache') );
2019-08-08 00:36:33 +03:00
add_action ( 'um_when_role_is_set' , array ( & $this , 'remove_cache' ) );
add_action ( 'um_when_status_is_set' , array ( & $this , 'remove_cache' ) );
2018-03-20 13:24:38 +02:00
add_action ( 'show_user_profile' , array ( $this , 'profile_form_additional_section' ), 10 );
add_action ( 'user_new_form' , array ( $this , 'profile_form_additional_section' ), 10 );
add_action ( 'edit_user_profile' , array ( $this , 'profile_form_additional_section' ), 10 );
add_filter ( 'um_user_profile_additional_fields' , array ( $this , 'secondary_role_field' ), 1 , 2 );
2017-07-26 14:57:52 +03:00
2017-11-06 15:28:07 +02:00
//on every update of user profile (hook from wp_update_user)
2018-03-20 13:24:38 +02:00
add_action ( 'profile_update' , array ( & $this , 'profile_update' ), 10 , 2 ); // user_id and old_user_data
2017-07-26 14:57:52 +03:00
2018-03-20 13:24:38 +02:00
//on user update profile page
//add_action( 'edit_user_profile_update', array( &$this, 'profile_update' ), 10, 1 );
2017-07-26 14:57:52 +03:00
2018-03-20 13:24:38 +02:00
add_action ( 'user_register' , array ( & $this , 'user_register_via_admin' ), 10 , 1 );
add_action ( 'user_register' , array ( & $this , 'set_gravatar' ), 11 , 1 );
2017-11-26 13:16:43 +02:00
2019-05-29 18:51:22 +03:00
if ( is_multisite () ) {
add_action ( 'added_existing_user' , array ( & $this , 'add_um_role_existing_user' ), 10 , 2 );
add_action ( 'wpmu_activate_user' , array ( & $this , 'add_um_role_wpmu_new_user' ), 10 , 1 );
}
2018-01-19 12:50:54 +02:00
2018-03-20 13:24:38 +02:00
add_action ( 'init' , array ( & $this , 'check_membership' ), 10 );
2018-05-14 17:55:47 +03:00
2018-11-21 14:01:18 +02:00
if ( is_multisite () ) {
add_action ( 'wpmu_delete_user' , array ( & $this , 'delete_user_handler' ), 10 , 1 );
2019-01-22 16:34:13 +02:00
} else {
add_action ( 'delete_user' , array ( & $this , 'delete_user_handler' ), 10 , 1 );
2018-11-21 14:01:18 +02:00
}
2020-01-06 11:05:32 +02:00
add_action ( 'updated_user_meta' , array ( & $this , 'on_update_usermeta' ), 10 , 4 );
add_action ( 'added_user_meta' , array ( & $this , 'on_update_usermeta' ), 10 , 4 );
add_action ( 'deleted_user_meta' , array ( & $this , 'on_delete_usermeta' ), 10 , 4 );
2022-06-20 17:29:31 +03:00
add_action ( 'update_user_meta' , array ( & $this , 'flush_um_count_users_transient_update' ), 10 , 4 );
add_action ( 'added_user_meta' , array ( & $this , 'flush_um_count_users_transient_add' ), 10 , 4 );
add_action ( 'delete_user_meta' , array ( & $this , 'flush_um_count_users_transient_delete' ), 10 , 4 );
2023-06-27 10:24:54 +03:00
add_action ( 'update_user_metadata' , array ( & $this , 'avoid_banned_keys' ), 10 , 3 );
2022-06-20 17:29:31 +03:00
}
2023-06-28 11:17:28 +03:00
/**
* It validates the meta_key for wp_usermeta table.
* Avoid to handle `meta_key` by the UM Forms if it contains $this->banned_keys inside.
*
* @since 2.6.5
*
* @param string $meta_key Usermeta key.
* @return bool
*/
2023-06-30 21:55:59 +03:00
public function is_metakey_banned ( $meta_key , $context = '' ) {
2023-06-28 11:17:28 +03:00
$is_banned = false ;
foreach ( $this -> banned_keys as $ban ) {
2023-06-29 19:56:54 +03:00
if ( is_numeric ( $meta_key ) || false !== stripos ( $meta_key , $ban ) || false !== stripos ( remove_accents ( $meta_key ), $ban ) ) {
2023-06-28 11:17:28 +03:00
$is_banned = true ;
break ;
}
}
2023-06-30 21:55:59 +03:00
if ( ! $is_banned && 'submission' === $context && ! in_array ( $meta_key , UM () -> form () -> usermeta_whitelist , true ) ) {
$is_banned = true ;
}
2023-06-28 11:17:28 +03:00
return $is_banned ;
}
2023-06-27 10:24:54 +03:00
/**
* Low-level checking to avoid updating banned user metakeys while UM Forms submission.
*
2023-06-28 11:17:28 +03:00
* @since 2.6.4
*
2023-06-30 21:55:59 +03:00
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
2023-06-27 10:24:54 +03:00
*
* @return null|bool
*/
public function avoid_banned_keys ( $check , $object_id , $meta_key ) {
if ( false === $this -> updating_process ) {
return $check ;
}
2023-06-30 21:55:59 +03:00
if ( $this -> is_metakey_banned ( $meta_key , 'submission' ) ) {
2023-06-27 10:24:54 +03:00
$check = false ;
}
return $check ;
}
2022-06-20 17:29:31 +03:00
/**
* @param $meta_ids
* @param $object_id
* @param $meta_key
* @param $_meta_value
*/
public function flush_um_count_users_transient_update ( $meta_ids , $object_id , $meta_key , $_meta_value ) {
if ( 'account_status' !== $meta_key ) {
return ;
}
2022-08-12 23:07:16 +03:00
// related to the User role > Registration Options Metabox > Registration status 2nd and 3rd option
if ( in_array ( $_meta_value , array ( 'checkmail' , 'pending' ), true ) ) {
return ;
}
2022-08-12 02:53:25 +03:00
$pending_statuses = array (
'awaiting_email_confirmation' ,
'awaiting_admin_review' ,
);
2022-06-20 17:29:31 +03:00
$old = get_user_meta ( $object_id , $meta_key , true );
2022-08-12 16:20:14 +03:00
if ( $old === $_meta_value ) {
return ;
}
2022-08-12 23:07:16 +03:00
// related to the User role > Registration Options Metabox > Registration status 2nd and 3rd option
if ( ! in_array ( $old , array ( 'checkmail' , 'pending' ), true ) ) {
2022-08-12 02:53:25 +03:00
// deduct old transient count
2022-08-12 23:07:16 +03:00
$count = get_transient ( " um_count_users_ { $old } " );
2022-08-12 02:53:25 +03:00
if ( false !== $count ) {
if ( ! is_numeric ( $count ) ) {
2022-08-12 23:07:16 +03:00
delete_transient ( " um_count_users_ { $old } " );
2022-08-12 02:53:25 +03:00
} else {
if ( 0 < $count ) {
$count -- ;
} else {
$count = 0 ;
}
2022-08-12 23:07:16 +03:00
set_transient ( " um_count_users_ { $old } " , $count );
}
}
if ( in_array ( $old , $pending_statuses , true ) && ! in_array ( $_meta_value , $pending_statuses , true ) ) {
// deduct old transient count
$count = get_transient ( 'um_count_users_pending_dot' );
if ( false !== $count ) {
if ( ! is_numeric ( $count ) ) {
delete_transient ( 'um_count_users_pending_dot' );
} else {
if ( 0 < $count ) {
$count -- ;
} else {
$count = 0 ;
}
set_transient ( 'um_count_users_pending_dot' , $count );
}
2022-08-12 02:53:25 +03:00
}
}
}
// add new transient count
$count = get_transient ( " um_count_users_ { $_meta_value } " );
if ( false !== $count ) {
if ( is_numeric ( $count ) ) {
$count ++ ;
} else {
$count = 1 ;
}
} else {
$count = 1 ;
}
set_transient ( " um_count_users_ { $_meta_value } " , $count );
if ( in_array ( $_meta_value , $pending_statuses , true ) && ! in_array ( $old , $pending_statuses , true ) ) {
// add new transient count
2022-08-12 23:07:16 +03:00
$count = get_transient ( 'um_count_users_pending_dot' );
2022-08-12 02:53:25 +03:00
if ( false !== $count ) {
if ( is_numeric ( $count ) ) {
$count ++ ;
} else {
$count = 1 ;
}
} else {
$count = 1 ;
}
2022-08-12 23:07:16 +03:00
set_transient ( 'um_count_users_pending_dot' , $count );
2022-06-20 17:29:31 +03:00
}
}
/**
* @param $meta_ids
* @param $object_id
* @param $meta_key
* @param $_meta_value
*/
public function flush_um_count_users_transient_add ( $meta_ids , $object_id , $meta_key , $_meta_value ) {
if ( 'account_status' !== $meta_key ) {
return ;
}
2022-08-12 23:07:16 +03:00
// related to the User role > Registration Options Metabox > Registration status 2nd and 3rd option
if ( in_array ( $_meta_value , array ( 'checkmail' , 'pending' ), true ) ) {
return ;
}
2022-08-12 02:53:25 +03:00
$pending_statuses = array (
'awaiting_email_confirmation' ,
'awaiting_admin_review' ,
);
// add new transient count
$count = get_transient ( " um_count_users_ { $_meta_value } " );
if ( false !== $count ) {
if ( is_numeric ( $count ) ) {
$count ++ ;
} else {
$count = 1 ;
}
} else {
$count = 1 ;
}
set_transient ( " um_count_users_ { $_meta_value } " , $count );
if ( in_array ( $_meta_value , $pending_statuses , true ) ) {
// add new transient count
2022-08-12 23:07:16 +03:00
$pending_count = get_transient ( 'um_count_users_pending_dot' );
if ( false !== $pending_count ) {
if ( is_numeric ( $pending_count ) ) {
$pending_count ++ ;
2022-08-12 02:53:25 +03:00
} else {
2022-08-12 23:07:16 +03:00
$pending_count = 1 ;
2022-08-12 02:53:25 +03:00
}
} else {
2022-08-12 23:07:16 +03:00
$pending_count = 1 ;
2022-08-12 02:53:25 +03:00
}
2022-08-12 23:07:16 +03:00
set_transient ( 'um_count_users_pending_dot' , $pending_count );
2022-06-20 17:29:31 +03:00
}
}
/**
* @param $meta_ids
* @param $object_id
* @param $meta_key
* @param $_meta_value
*/
public function flush_um_count_users_transient_delete ( $meta_ids , $object_id , $meta_key , $_meta_value ) {
if ( 'account_status' !== $meta_key ) {
return ;
}
$value = ( '' !== $_meta_value ) ? $_meta_value : get_user_meta ( $object_id , $meta_key , true );
2022-08-12 23:07:16 +03:00
// related to the User role > Registration Options Metabox > Registration status 2nd and 3rd option
if ( in_array ( $value , array ( 'checkmail' , 'pending' ), true ) ) {
return ;
}
2022-08-12 02:53:25 +03:00
$pending_statuses = array (
'awaiting_email_confirmation' ,
'awaiting_admin_review' ,
);
// deduct old transient count
$count = get_transient ( " um_count_users_ { $value } " );
if ( false !== $count ) {
if ( ! is_numeric ( $count ) ) {
delete_transient ( " um_count_users_ { $value } " );
} else {
if ( 0 < $count ) {
$count -- ;
} else {
$count = 0 ;
}
set_transient ( " um_count_users_ { $value } " , $count );
}
}
if ( in_array ( $value , $pending_statuses , true ) ) {
// deduct old transient count
2022-08-12 23:07:16 +03:00
$count = get_transient ( 'um_count_users_pending_dot' );
2022-08-12 02:53:25 +03:00
if ( false !== $count ) {
if ( ! is_numeric ( $count ) ) {
2022-08-12 23:07:16 +03:00
delete_transient ( 'um_count_users_pending_dot' );
2022-08-12 02:53:25 +03:00
} else {
if ( 0 < $count ) {
$count -- ;
} else {
$count = 0 ;
}
2022-08-12 23:07:16 +03:00
set_transient ( 'um_count_users_pending_dot' , $count );
2022-08-12 02:53:25 +03:00
}
}
2022-06-20 17:29:31 +03:00
}
2020-01-06 11:05:32 +02:00
}
/**
* When you delete usermeta connected with member directory - reset it to default value
*
* @param int|array $meta_ids
* @param int $object_id
* @param string $meta_key
* @param mixed $_meta_value
*/
function on_delete_usermeta ( $meta_ids , $object_id , $meta_key , $_meta_value ) {
2022-06-06 19:02:03 +03:00
if ( $this -> deleted_user_id ) {
return ;
}
2020-01-06 11:05:32 +02:00
$metakeys = array ( 'account_status' , 'hide_in_members' , 'synced_gravatar_hashed_id' , 'synced_profile_photo' , 'profile_photo' , 'cover_photo' , '_um_verified' );
if ( ! in_array ( $meta_key , $metakeys ) ) {
return ;
}
$md_data = get_user_meta ( $object_id , 'um_member_directory_data' , true );
if ( empty ( $md_data ) ) {
$md_data = array (
2023-09-09 02:21:15 +03:00
'account_status' => 'approved' ,
'hide_in_members' => UM () -> member_directory () -> get_hide_in_members_default (),
'profile_photo' => false ,
'cover_photo' => false ,
'verified' => false ,
2020-01-06 11:05:32 +02:00
);
}
switch ( $meta_key ) {
case 'account_status' :
$md_data [ 'account_status' ] = 'approved' ;
break ;
case 'hide_in_members' :
2020-01-14 22:22:11 +02:00
$md_data [ 'hide_in_members' ] = UM () -> member_directory () -> get_hide_in_members_default ();
2020-01-06 11:05:32 +02:00
break ;
case 'synced_gravatar_hashed_id' :
if ( UM () -> options () -> get ( 'use_gravatars' ) ) {
2023-09-09 02:21:15 +03:00
$profile_photo = get_user_meta ( $object_id , 'profile_photo' , true );
2020-01-06 11:05:32 +02:00
$synced_profile_photo = get_user_meta ( $object_id , 'synced_profile_photo' , true );
$md_data [ 'profile_photo' ] = ! empty ( $profile_photo ) || ! empty ( $synced_profile_photo );
}
break ;
case 'synced_profile_photo' :
$profile_photo = get_user_meta ( $object_id , 'profile_photo' , true );
$synced_gravatar_hashed_id = false ;
if ( UM () -> options () -> get ( 'use_gravatars' ) ) {
$synced_gravatar_hashed_id = get_user_meta ( $object_id , 'synced_gravatar_hashed_id' , true );
}
$md_data [ 'profile_photo' ] = ! empty ( $profile_photo ) || ! empty ( $synced_gravatar_hashed_id );
break ;
case 'profile_photo' :
$synced_profile_photo = get_user_meta ( $object_id , 'synced_profile_photo' , true );
$synced_gravatar_hashed_id = false ;
if ( UM () -> options () -> get ( 'use_gravatars' ) ) {
$synced_gravatar_hashed_id = get_user_meta ( $object_id , 'synced_gravatar_hashed_id' , true );
}
$md_data [ 'profile_photo' ] = ! empty ( $synced_profile_photo ) || ! empty ( $synced_gravatar_hashed_id );
break ;
case 'cover_photo' :
$md_data [ 'cover_photo' ] = false ;
break ;
case '_um_verified' :
$md_data [ 'verified' ] = false ;
break ;
}
update_user_meta ( $object_id , 'um_member_directory_data' , $md_data );
}
/**
* When you add/update usermeta connected with member directories - set this data to member directory metakey
*
* @param int $meta_id
* @param int $object_id
* @param string $meta_key
* @param mixed $_meta_value
*/
function on_update_usermeta ( $meta_id , $object_id , $meta_key , $_meta_value ) {
$metakeys = array ( 'account_status' , 'hide_in_members' , 'synced_gravatar_hashed_id' , 'synced_profile_photo' , 'profile_photo' , 'cover_photo' , '_um_verified' );
if ( ! in_array ( $meta_key , $metakeys ) ) {
return ;
}
$md_data = get_user_meta ( $object_id , 'um_member_directory_data' , true );
if ( empty ( $md_data ) ) {
$md_data = array (
2023-09-09 02:21:15 +03:00
'account_status' => 'approved' ,
'hide_in_members' => UM () -> member_directory () -> get_hide_in_members_default (),
'profile_photo' => false ,
'cover_photo' => false ,
'verified' => false ,
2020-01-06 11:05:32 +02:00
);
}
switch ( $meta_key ) {
case 'account_status' :
$md_data [ 'account_status' ] = $_meta_value ;
break ;
case 'hide_in_members' :
2020-01-14 22:22:11 +02:00
$hide_in_members = UM () -> member_directory () -> get_hide_in_members_default ();
2020-01-06 11:05:32 +02:00
if ( ! empty ( $_meta_value ) ) {
if ( $_meta_value == 'Yes' || $_meta_value == __ ( 'Yes' , 'ultimate-member' ) ||
2022-06-20 17:29:31 +03:00
array_intersect ( array ( 'Yes' , __ ( 'Yes' , 'ultimate-member' ) ), $_meta_value ) ) {
2020-01-06 11:05:32 +02:00
$hide_in_members = true ;
2020-01-14 22:22:11 +02:00
} else {
$hide_in_members = false ;
2020-01-06 11:05:32 +02:00
}
}
$md_data [ 'hide_in_members' ] = $hide_in_members ;
break ;
case 'synced_gravatar_hashed_id' :
if ( UM () -> options () -> get ( 'use_gravatars' ) ) {
if ( empty ( $md_data [ 'profile_photo' ] ) ) {
$md_data [ 'profile_photo' ] = ! empty ( $_meta_value );
}
}
break ;
case 'synced_profile_photo' :
case 'profile_photo' :
if ( empty ( $md_data [ 'profile_photo' ] ) ) {
$md_data [ 'profile_photo' ] = ! empty ( $_meta_value );
}
break ;
case 'cover_photo' :
$md_data [ 'cover_photo' ] = ! empty ( $_meta_value );
break ;
case '_um_verified' :
$md_data [ 'verified' ] = $_meta_value == 'verified' ? true : false ;
break ;
}
update_user_meta ( $object_id , 'um_member_directory_data' , $md_data );
2018-05-14 17:55:47 +03:00
}
/**
* @param $user_id
*/
2023-10-03 14:34:59 +03:00
public function delete_user_handler ( $user_id ) {
2018-05-14 17:55:47 +03:00
um_fetch_user ( $user_id );
2022-06-06 19:02:03 +03:00
$this -> deleted_user_id = $user_id ;
2018-05-14 17:55:47 +03:00
/**
* UM hook
*
* @type action
* @title um_delete_user_hook
* @description On delete user
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_delete_user_hook', 'function_name', 10 );
* @example
* <?php
* add_action( 'um_delete_user_hook', 'my_delete_user', 10 );
* function my_delete_user() {
* // your code here
* }
* ?>
*/
do_action ( 'um_delete_user_hook' );
/**
* UM hook
*
* @type action
* @title um_delete_user
* @description On delete user
* @input_vars
* [{"var":"$user_id","type":"int","desc":"User ID"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_delete_user', 'function_name', 10, 1 );
* @example
* <?php
* add_action( 'um_delete_user', 'my_delete_user', 10, 1 );
* function my_delete_user( $user_id ) {
* // your code here
* }
* ?>
*/
do_action ( 'um_delete_user' , um_user ( 'ID' ) );
2023-10-03 14:34:59 +03:00
// remove user's comments
if ( UM () -> options () -> get ( 'delete_comments' ) ) {
$user = get_user_by ( 'id' , um_user ( 'ID' ) );
$comments = array_merge ( get_comments ( 'author_email=' . $user -> user_email ), get_comments ( 'user_id=' . um_user ( 'ID' ) ) );
foreach ( $comments as $comment ) {
wp_delete_comment ( $comment -> comment_ID , true );
}
}
2018-05-14 17:55:47 +03:00
// send email notifications
if ( $this -> send_mail_on_delete ) {
UM () -> mail () -> send ( um_user ( 'user_email' ), 'deletion_email' );
$emails = um_multi_admin_email ();
if ( ! empty ( $emails ) ) {
foreach ( $emails as $email ) {
UM () -> mail () -> send ( $email , 'notification_deletion' , array ( 'admin' => true ) );
}
}
}
// remove uploads
2019-01-22 16:34:13 +02:00
UM () -> files () -> remove_dir ( UM () -> files () -> upload_temp );
2018-09-12 16:09:05 +03:00
UM () -> files () -> remove_dir ( UM () -> uploader () -> get_upload_base_dir () . um_user ( 'ID' ) . DIRECTORY_SEPARATOR );
2022-06-20 17:29:31 +03:00
delete_transient ( 'um_count_users_unassigned' );
2022-08-12 23:07:16 +03:00
delete_transient ( 'um_count_users_pending_dot' );
2018-03-20 13:24:38 +02:00
}
2017-07-26 14:57:52 +03:00
2018-03-20 13:24:38 +02:00
/**
*
*/
function check_membership () {
2019-08-08 00:36:33 +03:00
if ( ! is_user_logged_in () ) {
2018-03-20 13:24:38 +02:00
return ;
2019-08-08 00:36:33 +03:00
}
2018-01-19 12:50:54 +02:00
2018-03-20 13:24:38 +02:00
um_fetch_user ( get_current_user_id () );
$status = um_user ( 'account_status' );
2018-01-19 12:50:54 +02:00
2018-03-20 13:24:38 +02:00
if ( 'rejected' == $status ) {
wp_logout ();
session_unset ();
exit ( wp_redirect ( um_get_core_page ( 'login' ) ) );
}
2018-01-19 12:50:54 +02:00
2018-03-20 13:24:38 +02:00
um_reset_user ();
}
2018-01-19 12:50:54 +02:00
2017-11-26 13:16:43 +02:00
/**
* Multisite add existing user
*
* @param $user_id
* @param $result
*/
function add_um_role_existing_user ( $user_id , $result ) {
// Bail if no user ID was passed
2019-05-29 18:51:22 +03:00
if ( empty ( $user_id ) ) {
2017-11-26 13:16:43 +02:00
return ;
2019-05-29 18:51:22 +03:00
}
2017-11-26 13:16:43 +02:00
2020-10-29 12:20:47 +02:00
if ( ! empty ( $_POST [ 'um-role' ] ) && current_user_can ( 'promote_users' ) ) {
2021-06-29 02:51:54 +03:00
if ( ! user_can ( $user_id , sanitize_key ( $_POST [ 'um-role' ] ) ) ) {
UM () -> roles () -> set_role ( $user_id , sanitize_key ( $_POST [ 'um-role' ] ) );
2017-11-26 13:16:43 +02:00
}
}
$this -> remove_cache ( $user_id );
}
/**
* Multisite add existing user
*
* @param $user_id
*/
function add_um_role_wpmu_new_user ( $user_id ) {
// Bail if no user ID was passed
2019-08-08 00:36:33 +03:00
if ( empty ( $user_id ) ) {
2017-11-26 13:16:43 +02:00
return ;
2019-08-08 00:36:33 +03:00
}
2017-11-26 13:16:43 +02:00
2020-10-29 12:20:47 +02:00
if ( ! empty ( $_POST [ 'um-role' ] ) && current_user_can ( 'promote_users' ) ) {
2021-06-29 02:51:54 +03:00
if ( ! user_can ( $user_id , sanitize_key ( $_POST [ 'um-role' ] ) ) ) {
UM () -> roles () -> set_role ( $user_id , sanitize_key ( $_POST [ 'um-role' ] ) );
2017-11-26 13:16:43 +02:00
}
}
$this -> remove_cache ( $user_id );
}
2018-02-12 16:27:34 +02:00
/**
2023-09-25 21:08:35 +03:00
* @param int $user_id
* @param bool $raw
2018-03-20 13:24:38 +02:00
*
* @return bool|mixed
*/
2023-09-25 21:08:35 +03:00
public function get_profile_slug ( $user_id , $raw = false ) {
2018-02-12 16:27:34 +02:00
// Permalink base
$permalink_base = UM () -> options () -> get ( 'permalink_base' );
2023-09-11 15:41:29 +03:00
if ( 'custom_meta' === $permalink_base ) {
$custom_meta = UM () -> options () -> get ( 'permalink_base_custom_meta' );
if ( empty ( $custom_meta ) ) {
// Set default permalink base if custom meta is empty.
$permalink_base = 'user_login' ;
$meta_key = 'um_user_profile_url_slug_' . $permalink_base ;
} else {
$meta_key = $custom_meta ;
}
} else {
$meta_key = 'um_user_profile_url_slug_' . $permalink_base ;
}
$profile_slug = get_user_meta ( $user_id , $meta_key , true );
2017-07-26 14:57:52 +03:00
2023-09-25 21:08:35 +03:00
if ( $raw ) {
return $profile_slug ;
}
2018-02-18 13:27:46 +02:00
//get default username permalink if it's empty then return false
if ( empty ( $profile_slug ) ) {
2023-09-09 02:21:15 +03:00
if ( 'user_login' !== $permalink_base ) {
2019-08-08 00:36:33 +03:00
$profile_slug = get_user_meta ( $user_id , 'um_user_profile_url_slug_user_login' , true );
2018-02-18 13:27:46 +02:00
}
if ( empty ( $profile_slug ) ) {
return false ;
}
2018-02-12 16:27:34 +02:00
}
2017-07-26 14:57:52 +03:00
2018-02-18 13:27:46 +02:00
return $profile_slug ;
}
/**
* @param $user_id
*
* @return bool|string
*/
function get_profile_link ( $user_id ) {
$profile_slug = $this -> get_profile_slug ( $user_id );
if ( empty ( $profile_slug ) ) {
return false ;
2018-02-12 16:27:34 +02:00
}
2017-07-26 14:57:52 +03:00
2018-02-18 13:27:46 +02:00
return UM () -> permalinks () -> profile_permalink ( $profile_slug );
}
2023-09-09 02:21:15 +03:00
public function generate_user_hash ( $user_id ) {
$user_id = absint ( $user_id );
$append = 0 ;
while ( 1 ) {
$user_in_url = '~' . substr ( strrev ( md5 ( uniqid ( 'um_user_hash' . $append , true ) . $user_id ) ), 0 , 18 );
$slug_exists_user_id = UM () -> permalinks () -> slug_exists_user_id ( $user_in_url );
if ( empty ( $slug_exists_user_id ) || $user_id === $slug_exists_user_id ) {
break ;
}
$append ++ ;
}
return $user_in_url ;
}
2018-02-18 13:27:46 +02:00
/**
2018-12-07 14:00:23 +02:00
* Generate User Profile Slug and save to meta
2018-08-08 13:22:08 +03:00
*
* @param int $user_id
* @param bool $force
2018-02-18 13:27:46 +02:00
*/
2023-09-09 02:21:15 +03:00
public function generate_profile_slug ( $user_id , $force = false ) {
2018-02-12 16:27:34 +02:00
$userdata = get_userdata ( $user_id );
2017-07-26 14:57:52 +03:00
2018-02-12 16:27:34 +02:00
if ( empty ( $userdata ) ) {
2018-02-18 13:27:46 +02:00
return ;
2018-02-12 16:27:34 +02:00
}
2017-07-26 14:57:52 +03:00
2018-02-18 13:27:46 +02:00
delete_option ( " um_cache_userdata_ { $user_id } " );
2023-09-25 21:08:35 +03:00
$current_profile_slug = $this -> get_profile_slug ( $user_id , true );
2018-02-18 13:27:46 +02:00
2023-09-09 02:21:15 +03:00
$user_in_url = '' ;
2018-02-18 13:27:46 +02:00
$permalink_base = UM () -> options () -> get ( 'permalink_base' );
// User ID
2023-09-09 02:21:15 +03:00
if ( 'user_id' === $permalink_base ) {
2018-02-18 13:27:46 +02:00
$user_in_url = $user_id ;
}
2023-09-09 02:21:15 +03:00
if ( 'hash' === $permalink_base ) {
2023-09-14 19:54:58 +08:00
if ( empty ( $current_profile_slug ) ) {
$user_in_url = $this -> generate_user_hash ( $user_id );
} else {
$user_in_url = $current_profile_slug ;
}
2023-09-09 02:21:15 +03:00
}
2023-09-11 15:41:29 +03:00
if ( 'custom_meta' === $permalink_base ) {
$custom_meta = UM () -> options () -> get ( 'permalink_base_custom_meta' );
if ( empty ( $custom_meta ) ) {
// Set default permalink base if custom meta is empty.
$permalink_base = 'user_login' ;
} else {
$user_in_url = rawurlencode ( get_user_meta ( $user_id , $custom_meta , true ) );
2023-09-11 16:18:56 +03:00
/**
* Filters the user profile slug when custom meta is set.
*
* @param {string} $slug User profile slug.
* @param {int} $user_id User ID.
* @param {string} $metakey Meta key.
*
* @return {string} User profile slug.
*
2023-10-10 18:20:15 +03:00
* @since 2.7.0
2023-09-11 16:18:56 +03:00
* @hook um_custom_meta_permalink_base_generate_user_slug
*
* @example <caption>Use base64encode value as user slug.</caption>
* function my_custom_meta_permalink_base_generate_user_slug( $slug, $user_id, $metakey ) {
* // your code here
* $slug = base64encode( $user_id );
* update_user_meta( $user_id, $metakey, $slug );
* $slug = rawurlencode( $slug );
* return $slug;
* }
* add_filter( 'um_custom_meta_permalink_base_generate_user_slug', 'my_custom_meta_permalink_base_generate_user_slug', 10, 3 );
*/
2023-09-11 15:41:29 +03:00
$user_in_url = apply_filters ( 'um_custom_meta_permalink_base_generate_user_slug' , $user_in_url , $user_id , $custom_meta );
2023-10-09 19:50:44 +03:00
if ( empty ( $user_in_url ) ) {
$user_in_url = $userdata -> user_login ;
if ( is_email ( $user_in_url ) ) {
$user_email = $user_in_url ;
$user_in_url = str_replace ( '@' , '' , $user_in_url );
if ( ( $pos = strrpos ( $user_in_url , '.' ) ) !== false ) {
$search_length = strlen ( '.' );
$user_in_url = substr_replace ( $user_in_url , '-' , $pos , $search_length );
}
update_user_meta ( $user_id , " um_email_as_username_ { $user_in_url } " , $user_email );
} else {
$user_in_url = urlencode ( $user_in_url );
}
update_user_meta ( $user_id , " um_user_profile_url_slug_user_login " , $user_in_url );
}
2023-09-11 15:41:29 +03:00
}
}
2018-02-12 16:27:34 +02:00
// Username
2023-09-09 02:21:15 +03:00
if ( 'user_login' === $permalink_base ) {
2017-07-26 14:57:52 +03:00
2018-02-12 16:27:34 +02:00
$user_in_url = $userdata -> user_login ;
2017-07-26 14:57:52 +03:00
2018-02-12 16:27:34 +02:00
if ( is_email ( $user_in_url ) ) {
2017-07-26 14:57:52 +03:00
2018-02-12 16:27:34 +02:00
$user_email = $user_in_url ;
$user_in_url = str_replace ( '@' , '' , $user_in_url );
2017-07-26 14:57:52 +03:00
2018-02-12 16:27:34 +02:00
if ( ( $pos = strrpos ( $user_in_url , '.' ) ) !== false ) {
$search_length = strlen ( '.' );
$user_in_url = substr_replace ( $user_in_url , '-' , $pos , $search_length );
}
update_user_meta ( $user_id , " um_email_as_username_ { $user_in_url } " , $user_email );
2017-07-26 14:57:52 +03:00
2018-02-12 16:27:34 +02:00
} else {
2018-12-07 14:00:23 +02:00
$user_in_url = urlencode ( $user_in_url );
2018-02-12 16:27:34 +02:00
}
}
2017-07-26 14:57:52 +03:00
2018-02-12 16:27:34 +02:00
// Fisrt and Last name
$full_name_permalinks = array ( 'name' , 'name_dash' , 'name_plus' );
2023-09-09 02:21:15 +03:00
if ( in_array ( $permalink_base , $full_name_permalinks , true ) ) {
$separated = array (
'name' => '.' ,
'name_dash' => '-' ,
'name_plus' => '+' ,
);
$separate = $separated [ $permalink_base ];
$first_name = $userdata -> first_name ;
$last_name = $userdata -> last_name ;
$full_name = trim ( sprintf ( '%s %s' , $first_name , $last_name ) );
$full_name = preg_replace ( '/\s+/' , ' ' , $full_name ); // Remove double spaces
2018-02-12 16:27:34 +02:00
$profile_slug = UM () -> permalinks () -> profile_slug ( $full_name , $first_name , $last_name );
2023-09-09 02:21:15 +03:00
$append = 0 ;
$username = $full_name ;
$_username = $full_name ;
2018-02-12 16:27:34 +02:00
2018-02-18 13:27:46 +02:00
while ( 1 ) {
2023-09-09 02:21:15 +03:00
$username = $_username . ( empty ( $append ) ? '' : " $append " );
2018-02-18 13:27:46 +02:00
$slug_exists_user_id = UM () -> permalinks () -> slug_exists_user_id ( $profile_slug . ( empty ( $append ) ? '' : " { $separate } { $append } " ) );
if ( empty ( $slug_exists_user_id ) || $user_id == $slug_exists_user_id ) {
break ;
2018-02-12 16:27:34 +02:00
}
2018-02-18 13:27:46 +02:00
$append ++ ;
2018-02-12 16:27:34 +02:00
}
2018-02-13 11:21:40 +02:00
2018-02-12 16:27:34 +02:00
$user_in_url = UM () -> permalinks () -> profile_slug ( $username , $first_name , $last_name );
2018-02-18 13:27:46 +02:00
if ( empty ( $user_in_url ) ) {
2018-02-13 11:21:40 +02:00
$user_in_url = $userdata -> user_login ;
2017-07-26 14:57:52 +03:00
2018-02-18 13:27:46 +02:00
if ( is_email ( $user_in_url ) ) {
2017-07-26 14:57:52 +03:00
2018-02-18 13:27:46 +02:00
$user_email = $user_in_url ;
$user_in_url = str_replace ( '@' , '' , $user_in_url );
if ( ( $pos = strrpos ( $user_in_url , '.' ) ) !== false ) {
$search_length = strlen ( '.' );
$user_in_url = substr_replace ( $user_in_url , '-' , $pos , $search_length );
}
update_user_meta ( $user_id , " um_email_as_username_ { $user_in_url } " , $user_email );
} else {
$user_in_url = sanitize_title ( $user_in_url );
}
}
$user_in_url = trim ( $user_in_url , $separate );
2018-02-12 16:27:34 +02:00
}
2018-01-23 17:12:33 +02:00
2018-08-08 13:22:08 +03:00
$user_in_url = apply_filters ( 'um_change_user_profile_slug' , $user_in_url , $user_id );
2023-09-09 02:21:15 +03:00
if ( $force || empty ( $current_profile_slug ) || $current_profile_slug !== $user_in_url ) {
2018-02-18 13:27:46 +02:00
update_user_meta ( $user_id , " um_user_profile_url_slug_ { $permalink_base } " , $user_in_url );
}
2018-02-12 16:27:34 +02:00
}
2017-07-26 14:57:52 +03:00
2018-02-12 16:27:34 +02:00
/**
2018-03-20 13:24:38 +02:00
* Backend user creation
*
* @param $user_id
*/
2018-02-12 16:27:34 +02:00
function user_register_via_admin ( $user_id ) {
2019-05-29 18:51:22 +03:00
if ( empty ( $user_id ) ) {
2018-02-12 16:27:34 +02:00
return ;
2019-05-29 18:51:22 +03:00
}
2017-07-26 14:57:52 +03:00
2018-02-12 16:27:34 +02:00
if ( is_admin () ) {
//if there custom 2 role not empty
2020-10-29 12:20:47 +02:00
if ( ! empty ( $_POST [ 'um-role' ] ) && current_user_can ( 'promote_users' ) ) {
2018-02-12 16:27:34 +02:00
$user = get_userdata ( $user_id );
2021-06-29 02:51:54 +03:00
$user -> add_role ( sanitize_key ( $_POST [ 'um-role' ] ) );
UM () -> user () -> profile [ 'role' ] = sanitize_key ( $_POST [ 'um-role' ] );
2018-02-12 16:27:34 +02:00
UM () -> user () -> update_usermeta_info ( 'role' );
}
2023-06-30 21:55:59 +03:00
/** This action is documented in ultimate-member/includes/common/um-actions-register.php */
do_action ( 'um_user_register' , $user_id , $_POST , null );
2018-02-12 16:27:34 +02:00
}
2017-07-26 14:57:52 +03:00
2022-06-20 17:29:31 +03:00
delete_transient ( 'um_count_users_unassigned' );
2018-02-12 16:27:34 +02:00
}
2017-07-26 14:57:52 +03:00
2017-11-06 15:28:07 +02:00
/**
2018-03-20 13:24:38 +02:00
* On wp_update_user function complete
*
* @param int $user_id
* @param \WP_User $old_data
*/
2018-02-12 16:27:34 +02:00
function profile_update ( $user_id , $old_data ) {
// Bail if no user ID was passed
if ( empty ( $user_id ) ) {
return ;
}
2017-07-26 14:57:52 +03:00
2018-02-12 16:27:34 +02:00
$old_roles = $old_data -> roles ;
$userdata = get_userdata ( $user_id );
$new_roles = $userdata -> roles ;
2017-11-06 15:28:07 +02:00
2019-05-29 18:51:22 +03:00
if ( is_admin () ) {
2020-10-29 12:20:47 +02:00
if ( ! empty ( $_POST [ 'um-role' ] ) && current_user_can ( 'promote_users' ) ) {
2021-06-29 02:51:54 +03:00
$new_roles = array_merge ( $new_roles , array ( sanitize_key ( $_POST [ 'um-role' ] ) ) );
if ( ! user_can ( $user_id , sanitize_key ( $_POST [ 'um-role' ] ) ) ) {
UM () -> roles () -> set_role ( $user_id , sanitize_key ( $_POST [ 'um-role' ] ) );
2019-05-29 18:51:22 +03:00
}
2018-02-12 16:27:34 +02:00
}
}
2017-11-06 15:28:07 +02:00
2018-03-05 16:35:51 +02:00
/**
* UM hook
*
* @type action
* @title um_after_member_role_upgrade
* @description Action on user registration
* @input_vars
* [{"var":"$new_roles","type":"array","desc":"User new roles"},
* {"var":"$old_roles","type":"array","desc":"Old roles"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_after_member_role_upgrade', 'function_name', 10, 2 );
* @example
* <?php
* add_action( 'um_after_member_role_upgrade', 'my_after_member_role_upgrade', 10, 2 );
* function my_after_member_role_upgrade( $new_roles, $old_roles ) {
* // your code here
* }
* ?>
*/
2019-01-24 19:20:29 +02:00
do_action ( 'um_after_member_role_upgrade' , $new_roles , $old_roles , $user_id );
2018-02-12 13:31:54 +02:00
2018-02-12 16:27:34 +02:00
//Update permalink
2018-02-18 13:27:46 +02:00
$this -> generate_profile_slug ( $user_id , true );
2018-02-12 13:31:54 +02:00
2018-02-12 16:27:34 +02:00
$this -> remove_cache ( $user_id );
}
2017-11-06 15:28:07 +02:00
/**
* Additional section for WP Profile page with UM data fields
*
* @param \WP_User $userdata User data
* @return void
*/
function profile_form_additional_section ( $userdata ) {
2018-03-02 09:55:49 +02:00
/**
* UM hook
*
* @type filter
* @title um_user_profile_additional_fields
* @description Make additional content section
* @input_vars
* [{"var":"$content","type":"array","desc":"Additional section content"},
* {"var":"$userdata","type":"array","desc":"Userdata"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_user_profile_additional_fields', 'function_name', 10, 2 ); ?>
* @example
* <?php
* add_filter( 'um_user_profile_additional_fields', 'my_admin_pending_queue', 10, 2 );
* function my_admin_pending_queue( $content, $userdata ) {
* // your code here
* return $content;
* }
* ?>
*/
2017-11-06 15:28:07 +02:00
$section_content = apply_filters ( 'um_user_profile_additional_fields' , '' , $userdata );
2017-11-26 13:16:43 +02:00
if ( ! empty ( $section_content ) && ! ( is_multisite () && is_network_admin () ) ) {
2017-11-06 15:28:07 +02:00
2017-11-17 14:14:51 +02:00
if ( $userdata !== 'add-new-user' && $userdata !== 'add-existing-user' ) { ?>
2020-02-21 09:01:18 +02:00
<h3 id="um_user_screen_block"><?php esc_html_e( 'Ultimate Member', 'ultimate-member' ); ?></h3>
2023-09-09 02:21:15 +03:00
<?php
}
2017-11-06 15:28:07 +02:00
echo $section_content;
}
}
2017-07-26 14:57:52 +03:00
2018-03-20 13:24:38 +02:00
/**
* Default interface for setting a ultimatemember role
*
* @param string $content Section HTML
* @param \WP_User $userdata User data
* @return string
*/
public function secondary_role_field( $content, $userdata ) {
$roles = array();
2020-12-16 13:37:13 +02:00
$role_keys = get_option( 'um_roles', array() );
2018-03-20 13:24:38 +02:00
if ( $role_keys ) {
foreach ( $role_keys as $role_key ) {
$role_meta = get_option( "um_role_{$role_key}_meta" );
if ( $role_meta ) {
2019-08-08 00:36:33 +03:00
$roles[ 'um_' . $role_key ] = $role_meta;
2018-03-20 13:24:38 +02:00
}
}
}
2019-08-08 00:36:33 +03:00
if ( empty( $roles ) ) {
2018-03-20 13:24:38 +02:00
return $content;
2019-08-08 00:36:33 +03:00
}
2018-03-20 13:24:38 +02:00
global $pagenow;
2019-08-08 00:36:33 +03:00
if ( 'profile.php' == $pagenow ) {
2018-03-20 13:24:38 +02:00
return $content;
2019-08-08 00:36:33 +03:00
}
2018-03-20 13:24:38 +02:00
2023-09-09 02:21:15 +03:00
$style = '';
2018-03-20 13:24:38 +02:00
$user_role = false;
if ( $userdata !== 'add-new-user' && $userdata !== 'add-existing-user' ) {
// Bail if current user cannot edit users
2019-08-08 00:36:33 +03:00
if ( ! current_user_can( 'edit_user', $userdata->ID ) ) {
2018-03-20 13:24:38 +02:00
return $content;
2019-08-08 00:36:33 +03:00
}
2018-03-20 13:24:38 +02:00
$user_role = UM()->roles()->get_um_user_role( $userdata->ID );
2019-08-08 00:36:33 +03:00
if ( $user_role && ! empty( $userdata->roles ) && count( $userdata->roles ) == 1 ) {
2018-03-20 13:24:38 +02:00
$style = 'style="display:none;"';
2019-08-08 00:36:33 +03:00
}
2018-03-20 13:24:38 +02:00
}
$class = ( $userdata == 'add-existing-user' ) ? 'um_role_existing_selector_wrapper' : 'um_role_selector_wrapper';
2023-09-09 02:21:15 +03:00
ob_start();
?>
2018-03-20 13:24:38 +02:00
2023-09-09 02:21:15 +03:00
<div id="<?php echo esc_attr( $class ); ?>" <?php echo $style; ?>>
2018-03-20 13:24:38 +02:00
<table class="form-table">
<tbody>
<tr>
<th><label for="um-role"><?php esc_html_e( 'Ultimate Member Role', 'ultimate-member' ); ?></label></th>
<td>
<select name="um-role" id="um-role">
2023-09-09 02:21:15 +03:00
<option value="" <?php selected( empty( $user_role ) ); ?>><?php esc_html_e( '— No role for Ultimate Member —', 'ultimate-member' ); ?></option>
2018-03-20 13:24:38 +02:00
<?php foreach ( $roles as $role_id => $details ) { ?>
2019-08-08 00:36:33 +03:00
<option <?php selected( $user_role, $role_id ); ?> value="<?php echo esc_attr( $role_id ); ?>"><?php echo esc_html( $details['name'] ); ?></option>
2018-03-20 13:24:38 +02:00
<?php } ?>
</select>
</td>
</tr>
</tbody>
</table>
</div>
2017-07-26 14:57:52 +03:00
2023-09-09 02:21:15 +03:00
<?php
$content .= ob_get_clean();
2017-07-26 14:57:52 +03:00
2018-03-20 13:24:38 +02:00
return $content;
}
/**
* Converts object to array
*
* @param $obj
*
* @return array
*/
function toArray( $obj ) {
2019-08-08 00:36:33 +03:00
if ( is_object( $obj ) ) {
$obj = (array) $obj;
}
2018-03-20 13:24:38 +02:00
if ( is_array( $obj ) ) {
$new = array();
foreach ( $obj as $key => $val ) {
$new[ $key ] = $this->toArray( $val );
}
} else {
$new = $obj;
}
return $new;
}
/**
* @param $user_id
*
2019-08-08 00:36:33 +03:00
* @return mixed|string
2018-03-20 13:24:38 +02:00
*/
2023-11-03 17:31:18 +02:00
public function get_cached_data( $user_id ) {
2018-03-20 13:24:38 +02:00
$disallow_cache = UM()->options()->get( 'um_profile_object_cache_stop' );
2019-08-08 00:36:33 +03:00
if ( $disallow_cache ) {
2018-03-20 13:24:38 +02:00
return '';
}
if ( is_numeric( $user_id ) && $user_id > 0 ) {
2019-08-08 00:36:33 +03:00
$find_user = get_option( "um_cache_userdata_{$user_id}" );
2018-03-20 13:24:38 +02:00
if ( $find_user ) {
2023-11-03 17:31:18 +02:00
/** This filter is documented in includes/core/class-roles-capabilities.php */
return apply_filters( 'um_user_permissions_filter', $find_user, $user_id );
2018-03-20 13:24:38 +02:00
}
}
return '';
}
/**
* @param $user_id
* @param $profile
*/
function setup_cache( $user_id, $profile ) {
$disallow_cache = UM()->options()->get( 'um_profile_object_cache_stop' );
if ( $disallow_cache ) {
return;
}
update_option( "um_cache_userdata_{$user_id}", $profile, false );
}
/**
* @param $user_id
*/
function remove_cache( $user_id ) {
delete_option( "um_cache_userdata_{$user_id}" );
}
2018-04-05 14:38:20 +03:00
/**
* Remove cache for all users
*/
function remove_cache_all_users() {
global $wpdb;
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'um_cache_userdata_%'" );
}
2018-03-20 13:24:38 +02:00
/**
* This method lets you set a user. For example, to retrieve a profile or anything related to that user.
*
* @usage <?php UM()->user()->set( $user_id, $clean = false ); ?>
*
* @param null|int $user_id Which user to retrieve. A numeric user ID
* @param bool $clean 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
*
* @example The following example makes you set a user and retrieve their display name after that using the user API.
<?php
UM()->user()->set( 12 );
$display_name = UM()->user()->profile['display_name']; // Should print user display name
?>
*
*/
function set( $user_id = null, $clean = false ) {
if ( isset( $this->profile ) ) {
unset( $this->profile );
}
if ( $user_id ) {
$this->id = $user_id;
} elseif ( is_user_logged_in() && $clean == false ) {
$this->id = get_current_user_id();
} else {
$this->id = 0;
}
if ( $this->get_cached_data( $this->id ) ) {
$this->profile = $this->get_cached_data( $this->id );
} else {
if ( $user_id ) {
2023-09-09 02:21:15 +03:00
$this->id = $user_id;
2018-03-20 13:24:38 +02:00
$this->usermeta = get_user_meta( $user_id );
2023-09-09 02:21:15 +03:00
$this->data = get_userdata( $this->id );
2018-03-20 13:24:38 +02:00
} elseif ( is_user_logged_in() && $clean == false ) {
2023-09-09 02:21:15 +03:00
$this->id = get_current_user_id();
$this->usermeta = get_user_meta( $this->id );
$this->data = get_userdata( $this->id );
2018-03-20 13:24:38 +02:00
} else {
2023-09-09 02:21:15 +03:00
$this->id = 0;
2018-03-20 13:24:38 +02:00
$this->usermeta = null;
2023-09-09 02:21:15 +03:00
$this->data = null;
2018-03-20 13:24:38 +02:00
}
// we have a user, populate a profile
if ( $this->id && $this->toArray( $this->data ) ) {
// add user data
$this->data = $this->toArray( $this->data );
2023-09-09 02:21:15 +03:00
foreach ( $this->data as $k => $v ) {
if ( $k == 'roles' ) {
$this->profile['wp_roles'] = implode( ',', $v );
} elseif ( is_array( $v ) ) {
foreach ( $v as $k2 => $v2 ) {
$this->profile[ $k2 ] = $v2;
2018-03-20 13:24:38 +02:00
}
} else {
2023-09-09 02:21:15 +03:00
$this->profile[ $k ] = $v;
2018-03-20 13:24:38 +02:00
}
}
// add account status
2023-09-09 02:21:15 +03:00
if ( ! isset( $this->usermeta['account_status'][0] ) ) {
2018-03-20 13:24:38 +02:00
$this->usermeta['account_status'][0] = 'approved';
}
if ( $this->usermeta['account_status'][0] == 'approved' ) {
2023-09-09 02:21:15 +03:00
$this->usermeta['account_status_name'][0] = __( 'Approved', 'ultimate-member' );
2018-03-20 13:24:38 +02:00
}
if ( $this->usermeta['account_status'][0] == 'awaiting_email_confirmation' ) {
2024-02-16 15:12:21 +02:00
$this->usermeta['account_status_name'][0] = __( 'Awaiting Email Confirmation', 'ultimate-member' );
2018-03-20 13:24:38 +02:00
}
if ( $this->usermeta['account_status'][0] == 'awaiting_admin_review' ) {
2023-09-09 02:21:15 +03:00
$this->usermeta['account_status_name'][0] = __( 'Pending Review', 'ultimate-member' );
2018-03-20 13:24:38 +02:00
}
if ( $this->usermeta['account_status'][0] == 'rejected' ) {
2023-09-09 02:21:15 +03:00
$this->usermeta['account_status_name'][0] = __( 'Membership Rejected', 'ultimate-member' );
2018-03-20 13:24:38 +02:00
}
if ( $this->usermeta['account_status'][0] == 'inactive' ) {
2023-09-09 02:21:15 +03:00
$this->usermeta['account_status_name'][0] = __( 'Membership Inactive', 'ultimate-member' );
2018-03-20 13:24:38 +02:00
}
// add user meta
2019-05-15 12:30:14 +03:00
foreach ( $this->usermeta as $k => $v ) {
2023-09-09 02:21:15 +03:00
if ( $k == 'display_name' ) {
2019-04-15 18:26:26 +03:00
continue;
}
2019-05-15 12:30:14 +03:00
$this->profile[ $k ] = $v[0];
2018-03-20 13:24:38 +02:00
}
// add permissions
2023-09-09 02:21:15 +03:00
$user_role = UM()->roles()->get_priority_user_role( $this->id );
$this->profile['role'] = $user_role;
2018-03-20 13:24:38 +02:00
$this->profile['roles'] = UM()->roles()->get_all_user_roles( $this->id );
$role_meta = UM()->roles()->role_data( $user_role );
2023-11-03 17:31:18 +02:00
/** This filter is documented in includes/core/class-roles-capabilities.php */
2018-03-20 13:24:38 +02:00
$role_meta = apply_filters( 'um_user_permissions_filter', $role_meta, $this->id );
2023-09-09 02:21:15 +03:00
$this->profile = array_merge( $this->profile, (array) $role_meta );
2018-03-20 13:24:38 +02:00
$this->profile['super_admin'] = ( is_super_admin( $this->id ) ) ? 1 : 0;
// clean profile
$this->clean();
// Setup cache
$this->setup_cache( $this->id, $this->profile );
}
}
}
/**
* Reset user data
*
* @param bool $clean
*/
2023-09-09 02:21:15 +03:00
function reset( $clean = false ) {
$this->set( 0, $clean );
2018-03-20 13:24:38 +02:00
}
/**
2023-06-28 11:17:28 +03:00
* Clean user profile before set.
2018-03-20 13:24:38 +02:00
*/
2023-06-28 11:17:28 +03:00
private function clean() {
2019-08-08 00:36:33 +03:00
foreach ( $this->profile as $key => $value ) {
2023-06-28 11:17:28 +03:00
if ( $this->is_metakey_banned( $key ) ) {
unset( $this->profile[ $key ] );
2018-03-20 13:24:38 +02:00
}
}
}
/**
* This method lets you auto sign-in a user to your site.
*
* @usage <?php UM()->user()->auto_login( $user_id, $rememberme = false ); ?>
*
* @param int $user_id Which user ID to sign in automatically
* @param int|bool $rememberme Should be true or false. If you want the user sign in session to use cookies, use true
*
* @example The following example lets you sign in a user automatically by their ID.
<?php UM()->user()->auto_login( 2 ); ?>
*
*
* @example The following example lets you sign in a user automatically by their ID and makes the plugin remember their session.
<?php UM()->user()->auto_login( 10, true ); ?>
*
*/
function auto_login( $user_id, $rememberme = 0 ) {
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id, $rememberme );
2019-08-08 00:36:33 +03:00
$user = get_user_by( 'ID', $user_id );
2018-03-20 13:24:38 +02:00
do_action( 'wp_login', $user->user_login, $user );
}
/**
* Set user's registration details
*
2019-05-08 16:05:27 +03:00
* @param array $submitted
* @param array $args
2023-06-30 21:55:59 +03:00
* @param array $form_data
2018-03-20 13:24:38 +02:00
*/
2023-06-30 21:55:59 +03:00
public function set_registration_details( $submitted, $args, $form_data ) {
2018-03-20 13:24:38 +02:00
if ( isset( $submitted['user_pass'] ) ) {
unset( $submitted['user_pass'] );
}
if ( isset( $submitted['user_password'] ) ) {
unset( $submitted['user_password'] );
}
if ( isset( $submitted['confirm_user_password'] ) ) {
unset( $submitted['confirm_user_password'] );
}
2018-05-14 11:09:27 +03:00
//remove all password field values from submitted details
$password_fields = array();
foreach ( $submitted as $k => $v ) {
2023-06-30 21:55:59 +03:00
if ( 'password' === UM()->fields()->get_field_type( $k ) ) {
2018-05-14 11:09:27 +03:00
$password_fields[] = $k;
$password_fields[] = 'confirm_' . $k;
}
}
foreach ( $password_fields as $pw_field ) {
unset( $submitted[ $pw_field ] );
}
2018-03-20 13:24:38 +02:00
/**
2023-06-30 21:55:59 +03:00
* Filters submitted data before save usermeta "submitted" on registration process.
2018-03-20 13:24:38 +02:00
*
2023-06-30 21:55:59 +03:00
* @param {array} $submitted Form submitted data prepared for submitted usermeta.
* @param {array} $form_submitted_data All submitted data from $_POST. Since 2.6.7.
* @param {array} $form_data Form data. Since 2.6.7.
*
* @return {array} Form submitted data prepared for submitted usermeta.
*
* @since 2.0
* @hook um_before_save_filter_submitted
*
* @example <caption>Change submitted data before save usermeta "submitted" on registration process.</caption>
* function my_before_save_filter_submitted( $submitted, $form_submitted_data, $form_data ) {
2018-03-20 13:24:38 +02:00
* // your code here
* return $submitted;
* }
2023-06-30 21:55:59 +03:00
* add_filter( 'um_before_save_filter_submitted', 'my_before_save_filter_submitted', 10, 3 );
2018-03-20 13:24:38 +02:00
*/
2023-06-30 21:55:59 +03:00
$submitted = apply_filters( 'um_before_save_filter_submitted', $submitted, $args, $form_data );
2018-03-20 13:24:38 +02:00
/**
2023-06-30 21:55:59 +03:00
* Fires before save registration details to the user.
2018-03-20 13:24:38 +02:00
*
2023-06-30 21:55:59 +03:00
* @since 1.3.x
* @hook um_before_save_registration_details
*
* @param {int} $user_id User ID.
* @param {array} $submitted_data $_POST Submission array.
2023-07-02 20:17:18 +03:00
* @param {array} $form_data Form data. Since 2.6.8.
2023-06-30 21:55:59 +03:00
*
* @example <caption>Make any custom action before save registration details to the user.</caption>
2023-07-02 20:17:18 +03:00
* function my_before_save_registration_details( $user_id, $submitted_data, $form_data ) {
2018-03-20 13:24:38 +02:00
* // your code here
* }
2023-07-02 20:17:18 +03:00
* add_action( 'um_before_save_registration_details', 'my_before_save_registration_details', 10, 3 );
2018-03-20 13:24:38 +02:00
*/
2023-07-02 20:17:18 +03:00
do_action( 'um_before_save_registration_details', $this->id, $submitted, $form_data );
2018-03-20 13:24:38 +02:00
update_user_meta( $this->id, 'submitted', $submitted );
$this->update_profile( $submitted );
2023-06-30 21:55:59 +03:00
2018-03-20 13:24:38 +02:00
/**
2023-06-30 21:55:59 +03:00
* Fires after save registration details to the user.
2018-03-20 13:24:38 +02:00
*
2023-06-30 21:55:59 +03:00
* @since 1.3.x
* @hook um_after_save_registration_details
*
* @param {int} $user_id User ID.
* @param {array} $submitted_data $_POST Submission array.
* @param {array} $form_data UM form data. Since 2.6.7
*
* @example <caption>Make any custom action after save registration details to the user.</caption>
* function my_after_save_registration_details( $user_id, $submitted_data, $form_data ) {
2018-03-20 13:24:38 +02:00
* // your code here
* }
2023-06-30 21:55:59 +03:00
* add_action( 'um_after_save_registration_details', 'my_after_save_registration_details', 10, 3 );
2018-03-20 13:24:38 +02:00
*/
2023-06-30 21:55:59 +03:00
do_action( 'um_after_save_registration_details', $this->id, $submitted, $form_data );
2018-03-20 13:24:38 +02:00
}
/**
* Set last login for new registered users
*/
2023-12-01 00:30:37 +02:00
public function set_last_login() {
update_user_meta( $this->id, '_um_last_login', current_time( 'mysql', true ) );
2018-03-20 13:24:38 +02:00
}
/**
* Set user's account status
*
* @param $status
*/
function set_status( $status ) {
/**
* UM hook
*
* @type action
* @title um_when_status_is_set
* @description Action on user status changed
* @input_vars
* [{"var":"$user_id","type":"int","desc":"User ID"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_when_status_is_set', 'function_name', 10, 1 );
* @example
* <?php
* add_action( 'um_when_status_is_set', 'my_when_status_is_set', 10, 1 );
* function my_when_status_is_set( $user_id ) {
* // your code here
* }
* ?>
*/
do_action( 'um_when_status_is_set', um_user( 'ID' ) );
$this->profile['account_status'] = $status;
$this->update_usermeta_info( 'account_status' );
/**
* UM hook
*
* @type action
* @title um_after_user_status_is_changed_hook
* @description Action after user status changed
2019-11-19 18:20:31 +08:00
* @input_vars
* [{"var":"$user_id","type":"int","desc":"User ID"}]
2018-03-20 13:24:38 +02:00
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_after_user_status_is_changed_hook', 'function_name', 10 );
* @example
* <?php
* add_action( 'um_after_user_status_is_changed_hook', 'my_after_user_status_is_changed', 10 );
* function my_after_user_status_is_changed() {
* // your code here
* }
* ?>
*/
2019-11-19 18:20:31 +08:00
do_action( 'um_after_user_status_is_changed_hook', um_user( 'ID' ) );
2018-03-20 13:24:38 +02:00
/**
* UM hook
*
* @type action
* @title um_after_user_status_is_changed
* @description Action after user status changed
* @input_vars
2019-11-19 18:20:31 +08:00
* [{"var":"$status","type":"string","desc":"User Status"},
* {"var":"$user_id","type":"integer","desc":"User ID"}]
2018-03-20 13:24:38 +02:00
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_after_user_status_is_changed', 'function_name', 10, 1 );
* @example
* <?php
* add_action( 'um_after_user_status_is_changed', 'my_after_user_status_is_changed', 10, 1 );
* function my_after_user_status_is_changed( $status ) {
* // your code here
* }
* ?>
*/
2019-11-19 18:20:31 +08:00
do_action( 'um_after_user_status_is_changed', $status, um_user( 'ID' ) );
2018-03-20 13:24:38 +02:00
}
/**
* Set user's hash
*/
function assign_secretkey() {
/**
* UM hook
*
* @type action
* @title um_before_user_hash_is_changed
* @description Action before user hash is changed
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_before_user_hash_is_changed', 'function_name', 10 );
* @example
* <?php
* add_action( 'um_before_user_hash_is_changed', 'my_before_user_hash_is_changed', 10 );
* function my_before_user_hash_is_changed() {
* // your code here
* }
* ?>
*/
do_action( 'um_before_user_hash_is_changed' );
$this->profile['account_secret_hash'] = UM()->validation()->generate();
$this->update_usermeta_info( 'account_secret_hash' );
2021-04-09 15:25:01 +03:00
$expiry_time = UM()->options()->get( 'activation_link_expiry_time' );
if ( ! empty( $expiry_time ) && is_numeric( $expiry_time ) ) {
2024-02-29 15:11:01 +02:00
$this->profile['account_secret_hash_expiry'] = time() + $expiry_time * DAY_IN_SECONDS;
2021-04-09 15:25:01 +03:00
$this->update_usermeta_info( 'account_secret_hash_expiry' );
}
2018-03-20 13:24:38 +02:00
/**
* UM hook
*
* @type action
* @title um_after_user_hash_is_changed
* @description Action after user hash is changed
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_after_user_hash_is_changed', 'function_name', 10 );
* @example
* <?php
* add_action( 'um_after_user_hash_is_changed', 'my_after_user_hash_is_changed', 10 );
* function my_after_user_hash_is_changed() {
* // your code here
* }
* ?>
*/
do_action( 'um_after_user_hash_is_changed' );
}
2021-03-08 16:59:11 +02:00
/**
* @param \WP_User $userdata
*
* @return string|\WP_Error
*/
function maybe_generate_password_reset_key( $userdata ) {
2022-12-13 23:16:33 +02:00
return get_password_reset_key( $userdata );
2021-03-08 16:59:11 +02:00
}
2018-03-20 13:24:38 +02:00
/**
* Password reset email
*/
function password_reset() {
2019-08-08 00:36:33 +03:00
$userdata = get_userdata( um_user( 'ID' ) );
2021-03-08 16:59:11 +02:00
$this->maybe_generate_password_reset_key( $userdata );
2019-05-06 17:22:57 +03:00
add_filter( 'um_template_tags_patterns_hook', array( UM()->password(), 'add_placeholder' ), 10, 1 );
add_filter( 'um_template_tags_replaces_hook', array( UM()->password(), 'add_replace_placeholder' ), 10, 1 );
2022-12-13 16:13:29 +02:00
UM()->mail()->send( $userdata->user_email, 'resetpw_email' );
2018-03-20 13:24:38 +02:00
}
/**
* Password changed email
2022-08-11 21:49:19 +03:00
*
* @param null|int $user_id
2018-03-20 13:24:38 +02:00
*/
2022-08-11 21:49:19 +03:00
function password_changed( $user_id = null ) {
if ( ! empty( $user_id ) ) {
um_fetch_user( $user_id );
}
2019-08-08 00:36:33 +03:00
UM()->mail()->send( um_user( 'user_email' ), 'changedpw_email' );
2022-08-11 21:49:19 +03:00
if ( ! empty( $user_id ) ) {
um_reset_user();
}
2018-03-20 13:24:38 +02:00
}
/**
2024-02-16 15:12:21 +02:00
* This method approves a user membership and sends them an optional welcome/approval email.
2018-03-20 13:24:38 +02:00
*
* @usage <?php UM()->user()->approve(); ?>
*
* @example Approve a pending user and allow him to sign-in to your site.
<?php
um_fetch_user( 352 );
UM()->user()->approve();
?>
*
*/
2024-01-16 14:00:22 +02:00
public function approve( $repeat = true ) {
2023-09-09 02:21:15 +03:00
$user_id = um_user( 'ID' );
2018-09-17 20:44:07 +03:00
2018-10-04 18:46:03 +03:00
if ( ! $repeat ) {
$status = get_user_meta( $user_id, 'account_status', true );
if ( 'approved' === $status ) {
return;
}
2018-09-17 20:44:07 +03:00
}
2018-03-20 13:24:38 +02:00
delete_option( "um_cache_userdata_{$user_id}" );
2024-01-16 14:00:22 +02:00
if ( 'awaiting_admin_review' === um_user( 'account_status' ) ) {
2018-09-16 00:26:32 +03:00
$userdata = get_userdata( $user_id );
2021-03-08 16:59:11 +02:00
$this->maybe_generate_password_reset_key( $userdata );
2019-08-08 00:36:33 +03:00
UM()->mail()->send( um_user( 'user_email' ), 'approved_email' );
2018-03-20 13:24:38 +02:00
} else {
2021-03-08 16:59:11 +02:00
//$userdata = get_userdata( $user_id );
//get_password_reset_key( $userdata );
2024-01-16 14:00:22 +02:00
UM()->mail()->send( um_user( 'user_email' ), 'welcome_email' );
2018-03-20 13:24:38 +02:00
}
2019-08-08 00:36:33 +03:00
$this->set_status( 'approved' );
$this->delete_meta( 'account_secret_hash' );
2021-04-09 15:25:01 +03:00
$this->delete_meta( 'account_secret_hash_expiry' );
2018-03-20 13:24:38 +02:00
/**
* UM hook
*
* @type action
* @title um_after_user_is_approved
* @description Action after user was approved
* @input_vars
* [{"var":"$user_id","type":"int","desc":"User ID"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_after_user_is_approved', 'function_name', 10, 1 );
* @example
* <?php
* add_action( 'um_after_user_is_approved', 'my_after_user_is_approved', 10, 1 );
* function my_after_user_hash_is_changed( $user_id ) {
* // your code here
* }
* ?>
*/
do_action( 'um_after_user_is_approved', um_user( 'ID' ) );
}
/**
* Pending email
*/
function email_pending() {
$this->assign_secretkey();
2019-08-08 00:36:33 +03:00
$this->set_status( 'awaiting_email_confirmation' );
2022-10-10 14:10:32 +03:00
//clear all sessions for email confirmation pending users
$user = \WP_Session_Tokens::get_instance( um_user( 'ID' ) );
$user->destroy_all();
2019-08-08 00:36:33 +03:00
UM()->mail()->send( um_user( 'user_email' ), 'checkmail_email' );
2018-03-20 13:24:38 +02:00
}
/**
2024-02-16 15:12:21 +02:00
* This method puts a user under manual review by administrator and sends them an optional email.
2018-03-20 13:24:38 +02:00
*
* @usage <?php UM()->user()->pending(); ?>
*
* @example An example of putting a user pending manual review
<?php
um_fetch_user( 54 );
UM()->user()->pending();
?>
*
*/
function pending() {
$this->set_status( 'awaiting_admin_review' );
2022-10-10 14:10:32 +03:00
//clear all sessions for awaiting admin confirmation users
$user = \WP_Session_Tokens::get_instance( um_user( 'ID' ) );
$user->destroy_all();
2018-03-20 13:24:38 +02:00
UM()->mail()->send( um_user( 'user_email' ), 'pending_email' );
}
/**
2024-02-16 15:12:21 +02:00
* This method rejects a user membership and sends them an optional email.
2018-03-20 13:24:38 +02:00
*
* @usage <?php UM()->user()->reject(); ?>
*
* @example Reject a user membership example
<?php
um_fetch_user( 114 );
UM()->user()->reject();
?>
*
*/
function reject() {
2019-08-08 00:36:33 +03:00
$this->set_status( 'rejected' );
2022-10-10 14:10:32 +03:00
//clear all sessions for rejected users
$user = \WP_Session_Tokens::get_instance( um_user( 'ID' ) );
$user->destroy_all();
2019-08-08 00:36:33 +03:00
UM()->mail()->send( um_user( 'user_email' ), 'rejected_email' );
2018-03-20 13:24:38 +02:00
}
/**
2024-02-16 15:12:21 +02:00
* This method deactivates a user membership and sends them an optional email.
2018-03-20 13:24:38 +02:00
*
* @usage <?php UM()->user()->deactivate(); ?>
*
* @example Deactivate a user membership with the following example
<?php
um_fetch_user( 32 );
$ultimatemember->user->deactivate();
?>
*
*/
function deactivate() {
$this->set_status( 'inactive' );
2022-10-10 14:10:32 +03:00
//clear all sessions for inactive users
$user = \WP_Session_Tokens::get_instance( um_user( 'ID' ) );
$user->destroy_all();
2018-03-20 13:24:38 +02:00
/**
* UM hook
*
* @type action
* @title um_after_user_is_inactive
* @description Action after user was inactive
* @input_vars
* [{"var":"$user_id","type":"int","desc":"User ID"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_after_user_is_inactive', 'function_name', 10, 1 );
* @example
* <?php
* add_action( 'um_after_user_is_inactive', 'my_after_user_is_inactive', 10, 1 );
* function my_after_user_is_inactive( $user_id ) {
* // your code here
* }
* ?>
*/
do_action( 'um_after_user_is_inactive', um_user( 'ID' ) );
UM()->mail()->send( um_user( 'user_email' ), 'inactive_email' );
}
/**
* Delete user
*
* @param bool $send_mail
*/
function delete( $send_mail = true ) {
2018-05-14 17:55:47 +03:00
$this->send_mail_on_delete = $send_mail;
2018-05-30 18:47:08 +03:00
//don't send email notification to not approved user
if ( 'approved' != um_user( 'account_status' ) ) {
$this->send_mail_on_delete = false;
}
2018-03-20 13:24:38 +02:00
// remove user
if ( is_multisite() ) {
2018-04-02 23:04:39 +03:00
if ( ! function_exists( 'wpmu_delete_user' ) ) {
2023-09-09 02:21:15 +03:00
require_once ABSPATH . 'wp-admin/includes/ms.php';
2018-03-20 13:24:38 +02:00
}
wpmu_delete_user( $this->id );
} else {
2018-04-02 23:04:39 +03:00
if ( ! function_exists( 'wp_delete_user' ) ) {
2023-09-09 02:21:15 +03:00
require_once ABSPATH . 'wp-admin/includes/user.php';
2018-03-20 13:24:38 +02:00
}
wp_delete_user( $this->id );
}
}
/**
* This method gets a user role in slug format. e.g. member
*
* @usage <?php UM()->user()->get_role(); ?>
*
* @return string
*
* @example Do something if the user's role is paid-member
<?php
um_fetch_user( 12 );
if ( UM()->user()->get_role() == 'paid-member' ) {
// Show this to paid customers
} else {
// You are a free member
}
?>
*
*/
function get_role() {
if ( ! empty( $this->profile['role'] ) ) {
return $this->profile['role'];
} else {
2020-01-02 17:21:02 +02:00
if ( ! empty( $this->profile['wp_roles'] ) && $this->profile['wp_roles'] == 'administrator' ) {
2018-03-20 13:24:38 +02:00
return 'admin';
} else {
return 'member';
}
}
}
/**
* Update one key in user meta
*
* @param $key
*/
function update_usermeta_info( $key ) {
// delete the key first just in case
delete_user_meta( $this->id, $key );
2019-08-08 00:36:33 +03:00
update_user_meta( $this->id, $key, $this->profile[ $key ] );
2018-03-20 13:24:38 +02:00
}
/**
* This method can be used to delete user's meta key.
*
* @usage <?php UM()->user()->delete_meta( $key ); ?>
*
* @param string $key The meta field key to remove from user
*
* @example Delete user's age field
<?php
um_fetch_user( 15 );
UM()->user()->delete_meta( 'age' );
?>
*
*/
2019-08-08 00:36:33 +03:00
function delete_meta( $key ) {
2018-03-20 13:24:38 +02:00
delete_user_meta( $this->id, $key );
}
/**
* Get admin actions for individual user
*
* @return array|bool
*/
function get_admin_actions() {
$items = array();
2018-06-03 17:45:21 +03:00
2018-03-20 13:24:38 +02:00
/**
* UM hook
*
* @type filter
* @title um_admin_user_actions_hook
* @description Extend admin actions for each user
* @input_vars
* [{"var":"$actions","type":"array","desc":"Actions for user"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_admin_user_actions_hook', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_admin_user_actions_hook', 'my_admin_user_actions', 10, 1 );
* function my_admin_user_actions( $actions ) {
* // your code here
* return $actions;
* }
* ?>
*/
2019-11-12 15:22:19 +02:00
$actions = apply_filters( 'um_admin_user_actions_hook', array(), um_profile_id() );
if ( empty( $actions ) ) {
return $items;
2018-06-03 17:45:21 +03:00
}
foreach ( $actions as $id => $arr ) {
2023-09-09 02:21:15 +03:00
$url = add_query_arg(
array(
'um_action' => $id,
'uid' => um_profile_id(),
)
);
2018-06-03 17:45:21 +03:00
/*$url = add_query_arg( 'um_action', $id );
$url = add_query_arg( 'uid', um_profile_id(), $url );*/
2023-09-09 02:21:15 +03:00
$items[] = '<a href="' . esc_url( $url ) . '" class="real_url ' . esc_attr( $id ) . '-item">' . esc_html( $arr['label'] ) . '</a>';
2018-03-20 13:24:38 +02:00
}
return $items;
}
2020-12-22 20:13:58 +02:00
/**
* This method checks if the profile indexing is disabled
*
2021-03-03 15:21:18 +02:00
* @param int $user_id
*
* @since 2.1.16
* @usage <?php UM()->user()->is_profile_noindex( $user_id ); ?>
2020-12-22 20:13:58 +02:00
*
* @return boolean Is the profile indexing disabled?
*/
2021-03-03 15:21:18 +02:00
function is_profile_noindex( $user_id ) {
2020-12-22 20:13:58 +02:00
$profile_noindex = false;
2021-03-03 15:21:18 +02:00
if ( ! get_option( 'blog_public' ) ) {
2020-12-22 20:13:58 +02:00
// Option "Search engine visibility" in [wp-admin > Settings > Reading]
$profile_noindex = true;
2021-03-03 15:21:18 +02:00
} elseif ( $this->is_private_profile( $user_id ) ) {
2020-12-22 20:13:58 +02:00
// Setting "Profile Privacy" in [Account > Privacy]
$profile_noindex = true;
2021-03-03 15:21:18 +02:00
} elseif ( get_user_meta( $user_id, 'profile_noindex', true ) === '1' ) {
2020-12-22 20:13:58 +02:00
// Setting "Avoid indexing my profile by search engines in [Account > Privacy]
$profile_noindex = true;
2021-03-03 15:21:18 +02:00
2020-12-22 20:13:58 +02:00
}
2021-03-03 15:21:18 +02:00
if ( ! $profile_noindex ) {
2023-02-24 16:08:16 +02:00
$role = UM()->roles()->get_priority_user_role( $user_id );
2020-12-22 20:13:58 +02:00
$permissions = UM()->roles()->role_data( $role );
2021-03-03 15:21:18 +02:00
2023-02-24 16:08:16 +02:00
if ( isset( $permissions['profile_noindex'] ) && '' !== $permissions['profile_noindex'] ) {
2020-12-22 20:13:58 +02:00
// Setting "Avoid indexing profile by search engines" in [wp-admin > Ultimate Member > User Roles > Edit Role]
2023-02-24 16:08:16 +02:00
$profile_noindex = (bool) $permissions['profile_noindex'];
2020-12-22 20:13:58 +02:00
2023-02-24 16:08:16 +02:00
} else {
2020-12-22 20:13:58 +02:00
// Setting "Avoid indexing profile by search engines" in [wp-admin > Ultimate Member > Settings > General > Users]
2023-02-24 16:08:16 +02:00
$profile_noindex = (bool) UM()->options()->get( 'profile_noindex' );
2020-12-22 20:13:58 +02:00
}
}
2021-03-03 15:21:18 +02:00
return apply_filters( 'um_user_is_profile_noindex', $profile_noindex, $user_id, $this );
2020-12-22 20:13:58 +02:00
}
2018-03-20 13:24:38 +02:00
/**
* This method checks if give user profile is private.
*
* @usage <?php UM()->user()->is_private_profile( $user_id ); ?>
*
* @param int $user_id A user ID must be passed to check if the user profile is private
*
* @return bool
*
* @example This example display a specific user's name If his profile is public
<?php
um_fetch_user( 60 );
$is_private = UM()->user()->is_private_profile( 60 );
if ( ! $is_private ) {
echo 'User is public and his name is ' . um_user('display_name');
}
?>
*
*/
function is_private_profile( $user_id ) {
$privacy = get_user_meta( $user_id, 'profile_privacy', true );
2019-08-08 00:36:33 +03:00
if ( $privacy == __( 'Only me', 'ultimate-member' ) || $privacy == 'Only me' ) {
2018-03-20 13:24:38 +02:00
return true;
}
2020-05-29 17:10:21 +03:00
return $this->is_private_case( $user_id, $privacy );
2018-03-20 13:24:38 +02:00
}
/**
* This method can be used to determine If a certain user is approved or not.
*
* @usage <?php UM()->user()->is_approved( $user_id ); ?>
*
* @param int $user_id The user ID to check approval status for
*
* @return bool
*
* @example Do something If a user's membership is approved
<?php
if ( UM()->user()->is_approved( 55 ) {
// User account is approved
} else {
// User account is not approved
}
?>
*
*/
function is_approved( $user_id ) {
$status = get_user_meta( $user_id, 'account_status', true );
if ( $status == 'approved' || $status == '' ) {
return true;
}
return false;
}
/**
* Is private
*
* @param $user_id
* @param $case
*
2018-07-30 17:33:59 +03:00
* @return bool
2018-03-20 13:24:38 +02:00
*/
function is_private_case( $user_id, $case ) {
$privacy = get_user_meta( $user_id, 'profile_privacy', true );
if ( $privacy == $case ) {
/**
* UM hook
*
* @type filter
* @title um_is_private_filter_hook
* @description Change user privacy
* @input_vars
* [{"var":"$is_private","type":"bool","desc":"Is user private"},
* {"var":"$privacy","type":"bool","desc":"Profile Privacy"},
* {"var":"$user_id","type":"int","desc":"User ID"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_is_private_filter_hook', 'function_name', 10, 3 ); ?>
* @example
* <?php
* add_filter( 'um_is_private_filter_hook', 'my_is_private_filter', 10, 3 );
* function my_is_private_filter( $is_private ) {
* // your code here
* return $is_private;
* }
* ?>
*/
$bool = apply_filters( 'um_is_private_filter_hook', false, $privacy, $user_id );
return $bool;
}
return false;
}
/**
* Update files
*
* @param $changes
2019-10-21 23:39:19 +03:00
*
* @deprecated 2.1.0
2018-03-20 13:24:38 +02:00
*/
function update_files( $changes ) {
2019-10-21 23:39:19 +03:00
um_deprecated_function( 'update_files', '2.1.0', '' );
2018-03-20 13:24:38 +02:00
}
/**
* Update profile
*
2023-07-01 01:52:43 +03:00
* @param array $changes
* @param string $context
2018-03-20 13:24:38 +02:00
*/
2023-07-01 01:52:43 +03:00
public function update_profile( $changes, $context = '' ) {
if ( 'account' !== $context ) {
$this->updating_process = true;
}
$args['ID'] = $this->id;
2018-03-20 13:24:38 +02:00
/**
2023-06-27 10:24:54 +03:00
* Filters the update profile changes data.
2018-03-20 13:24:38 +02:00
*
2023-06-27 10:24:54 +03:00
* @since 1.3.x
* @hook um_before_update_profile
*
* @param {array} $changes User Profile Changes.
* @param {int} $user_id User ID.
*
* @return {array} User Profile Changes.
*
* @example <caption>Remove some_metakey from changes where user ID equals 12.</caption>
2023-06-27 10:25:39 +03:00
* function my_custom_before_update_profile( $changes, $user_id ) {
2023-06-27 10:24:54 +03:00
* if ( 12 === $user_id ) {
* unset( $changes['{some_metakey}'];
* }
2018-03-20 13:24:38 +02:00
* return $changes;
* }
2023-06-27 10:24:54 +03:00
* add_filter( 'um_before_update_profile', 'my_custom_before_update_profile', 10, 2 );
2018-03-20 13:24:38 +02:00
*/
2018-08-10 15:11:48 +03:00
$changes = apply_filters( 'um_before_update_profile', $changes, $args['ID'] );
2018-03-20 13:24:38 +02:00
foreach ( $changes as $key => $value ) {
2023-07-01 12:46:43 +03:00
if ( $this->is_metakey_banned( $key, ( 'account' !== $context ) ? 'submission' : '' ) ) {
2020-10-26 17:21:42 +02:00
continue;
}
2023-06-27 10:24:54 +03:00
if ( ! in_array( $key, $this->update_user_keys, true ) ) {
2019-05-08 16:05:27 +03:00
if ( $value === 0 ) {
2019-02-28 16:11:43 +02:00
update_user_meta( $this->id, $key, '0' );
} else {
update_user_meta( $this->id, $key, $value );
}
2018-03-20 13:24:38 +02:00
} else {
2023-06-27 10:24:54 +03:00
$args[ $key ] = $value;
2018-03-20 13:24:38 +02:00
}
}
2018-05-25 18:22:38 +03:00
2023-06-30 21:55:59 +03:00
$this->updating_process = false;
2018-03-20 13:24:38 +02:00
// update user
if ( count( $args ) > 1 ) {
2023-06-30 21:55:59 +03:00
// If isset roles argument validate role to properly for security reasons
2018-05-25 18:22:38 +03:00
if ( isset( $args['role'] ) ) {
global $wp_roles;
2023-03-28 01:53:50 +03:00
$exclude_roles = array_diff( array_keys( $wp_roles->roles ), UM()->roles()->get_editable_user_roles() );
2023-06-27 10:24:54 +03:00
if ( in_array( $args['role'], $exclude_roles, true ) ) {
2018-05-25 18:22:38 +03:00
unset( $args['role'] );
}
2018-03-20 13:24:38 +02:00
}
wp_update_user( $args );
}
}
/**
* User exists by meta key and value
*
* @param $key
* @param $value
*
* @return bool|int
*/
function user_has_metadata( $key, $value ) {
$value = UM()->validation()->safe_name_in_url( $value );
2023-09-09 02:21:15 +03:00
$ids = get_users(
array(
'fields' => 'ID',
'meta_key' => $key,
'meta_value' => $value,
'meta_compare' => '=',
)
);
2019-08-08 00:36:33 +03:00
if ( ! isset( $ids ) || empty( $ids ) ) {
return false;
}
foreach ( $ids as $k => $id ) {
2023-09-09 02:21:15 +03:00
if ( $id == um_user( 'ID' ) ) {
2019-08-08 00:36:33 +03:00
unset( $ids[ $k ] );
2018-03-20 13:24:38 +02:00
} else {
$duplicates[] = $id;
}
}
2019-08-08 00:36:33 +03:00
if ( ! empty( $duplicates ) ) {
2018-03-20 13:24:38 +02:00
return count( $duplicates );
2019-08-08 00:36:33 +03:00
}
2018-03-20 13:24:38 +02:00
return false;
}
/**
* User exists by name
*
* @param $value
*
* @return bool
*/
2023-09-09 02:21:15 +03:00
public function user_exists_by_name( $value ) {
2018-03-20 13:24:38 +02:00
// Permalink base
$permalink_base = UM()->options()->get( 'permalink_base' );
$raw_value = $value;
2023-09-09 02:21:15 +03:00
$value = UM()->validation()->safe_name_in_url( $value );
$value = um_clean_user_basename( $value );
2018-03-20 13:24:38 +02:00
// Search by Profile Slug
$args = array(
2023-09-09 02:21:15 +03:00
'fields' => array( 'ID' ),
2018-03-20 13:24:38 +02:00
'meta_query' => array(
'relation' => 'OR',
array(
2023-09-09 02:21:15 +03:00
'key' => 'um_user_profile_url_slug_' . $permalink_base,
'value' => strtolower( $raw_value ),
'compare' => '=',
2019-08-08 00:36:33 +03:00
),
),
2018-03-20 13:24:38 +02:00
);
$ids = new \WP_User_Query( $args );
2019-08-08 00:36:33 +03:00
if ( $ids->total_users > 0 ) {
2018-03-20 13:24:38 +02:00
$um_user_query = current( $ids->get_results() );
return $um_user_query->ID;
}
// Search by Display Name or ID
$args = array(
2023-09-09 02:21:15 +03:00
'fields' => array( 'ID' ),
'search' => $value,
'search_columns' => array( 'display_name', 'ID' ),
2018-03-20 13:24:38 +02:00
);
$ids = new \WP_User_Query( $args );
2019-08-08 00:36:33 +03:00
if ( $ids->total_users > 0 ) {
2018-03-20 13:24:38 +02:00
$um_user_query = current( $ids->get_results() );
return $um_user_query->ID;
}
// Search By User Login
2023-09-09 02:21:15 +03:00
$value = str_replace( array( '.', ' ' ), array( '_', '' ), $value );
2018-03-20 13:24:38 +02:00
$args = array(
2023-09-09 02:21:15 +03:00
'fields' => array( 'ID' ),
'search' => $value,
'search_columns' => array(
2018-03-20 13:24:38 +02:00
'user_login',
2023-09-09 02:21:15 +03:00
),
2018-03-20 13:24:38 +02:00
);
$ids = new \WP_User_Query( $args );
2019-08-08 00:36:33 +03:00
if ( $ids->total_users > 0 ) {
2018-03-20 13:24:38 +02:00
$um_user_query = current( $ids->get_results() );
return $um_user_query->ID;
}
return false;
}
/**
* This method checks if a user exists or not in your site based on the user ID.
*
* @usage <?php UM()->user()->user_exists_by_id( $user_id ); ?>
*
* @param int $user_id A user ID must be passed to check if the user exists
*
* @return bool|int
*
* @example Basic Usage
<?php
$boolean = UM()->user()->user_exists_by_id( 15 );
if ( $boolean ) {
// That user exists
}
?>
*
*/
2023-09-09 02:21:15 +03:00
public function user_exists_by_id( $user_id ) {
2020-11-24 12:55:22 +02:00
$aux = get_userdata( absint( $user_id ) );
2019-08-08 00:36:33 +03:00
if ( $aux == false ) {
2018-03-20 13:24:38 +02:00
return false;
} else {
return $user_id;
}
}
2023-09-09 02:21:15 +03:00
/**
* @param string $hash
*
* @return bool|int
*/
public function user_exists_by_hash( $hash ) {
// Permalink base
$permalink_base = UM()->options()->get( 'permalink_base' );
$raw_value = $hash;
// Search by Profile Slug
$args = array(
'fields' => array( 'ID' ),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'um_user_profile_url_slug_' . $permalink_base,
'value' => strtolower( $raw_value ),
'compare' => '=',
),
),
);
$ids = new \WP_User_Query( $args );
if ( $ids->total_users > 0 ) {
$um_user_query = current( $ids->get_results() );
return $um_user_query->ID;
}
return false;
}
2018-03-20 13:24:38 +02:00
2023-09-11 15:41:29 +03:00
/**
* @param string $slug
*
* @return bool|int
*/
public function user_exists_by_custom_meta( $slug ) {
$permalink_base = UM()->options()->get( 'permalink_base' );
$custom_meta = UM()->options()->get( 'permalink_base_custom_meta' );
if ( empty( $custom_meta ) ) {
// Set default permalink base if custom meta is empty.
$permalink_base = 'user_login';
$meta_key = 'um_user_profile_url_slug_' . $permalink_base;
} else {
$meta_key = $custom_meta;
}
$raw_value = $slug;
// Search by Profile Slug
$args = array(
'fields' => array( 'ID' ),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => $meta_key,
'value' => strtolower( $raw_value ),
'compare' => '=',
),
array(
'key' => 'um_user_profile_url_slug_' . $permalink_base,
'value' => strtolower( $raw_value ),
'compare' => '=',
),
),
);
$ids = new \WP_User_Query( $args );
if ( $ids->total_users > 0 ) {
$um_user_query = current( $ids->get_results() );
return $um_user_query->ID;
}
return false;
}
2018-03-20 13:24:38 +02:00
/**
* This method checks if a user exists or not in your site based on the user email as username
*
* @param string $slug A user slug must be passed to check if the user exists
*
* @usage <?php UM()->user()->user_exists_by_email_as_username( $slug ); ?>
*
* @return bool
*
* @example Basic Usage
<?php
$boolean = UM()->user()->user_exists_by_email_as_username( 'calumgmail-com' );
if ( $boolean ) {
// That user exists
}
?>
*/
function user_exists_by_email_as_username( $slug ) {
$user_id = false;
2023-09-09 02:21:15 +03:00
$ids = get_users(
array(
'fields' => 'ID',
'meta_key' => 'um_email_as_username_' . $slug,
)
);
2019-08-08 00:36:33 +03:00
if ( ! empty( $ids[0] ) ) {
2018-03-20 13:24:38 +02:00
$user_id = $ids[0];
}
return $user_id;
}
/**
* Set gravatar hash id
*
* @param $user_id
* @return string
*/
function set_gravatar( $user_id ) {
um_fetch_user( $user_id );
2023-09-09 02:21:15 +03:00
$email_address = um_user( 'user_email' );
2018-03-20 13:24:38 +02:00
$hash_email_address = '';
if ( $email_address ) {
2023-09-09 02:21:15 +03:00
$hash_email_address = md5( $email_address );
2018-03-20 13:24:38 +02:00
$this->profile['synced_gravatar_hashed_id'] = $hash_email_address;
$this->update_usermeta_info( 'synced_gravatar_hashed_id' );
}
return $hash_email_address;
}
2019-05-06 17:22:57 +03:00
/**
* UM Placeholders for activation link in email
*
* @param $placeholders
*
* @return array
*/
function add_activation_placeholder( $placeholders ) {
$placeholders[] = '{account_activation_link}';
return $placeholders;
}
/**
* UM Replace Placeholders for activation link in email
*
* @param $replace_placeholders
*
* @return array
*/
function add_activation_replace_placeholder( $replace_placeholders ) {
$replace_placeholders[] = um_user( 'account_activation_link' );
return $replace_placeholders;
}
2022-06-20 17:29:31 +03:00
/**
* Get pending users (in queue)
*
* @deprecated 2.4.2
*/
function get_pending_users_count() {
_deprecated_function( __METHOD__, '2.4.2', 'UM()->query()->get_pending_users_count()' );
return UM()->query()->get_pending_users_count();
}
/**
* Remove cached queue from Users backend
*
* @deprecated 2.4.2
*/
function remove_cached_queue() {
_deprecated_function( __METHOD__, '2.4.2', '' );
delete_option( 'um_cached_users_queue' );
}
2018-03-20 13:24:38 +02:00
}
2021-06-17 13:40:52 +03:00
}