2019-08-08 00:36:33 +03:00
< ? php if ( ! defined ( 'ABSPATH' ) ) exit ;
2014-12-15 22:38:07 +02:00
2017-08-11 14:21:42 +03:00
/**
* Validate for errors in account form
2018-03-19 16:31:49 +02:00
*
* @param $args
2017-08-11 14:21:42 +03:00
*/
function um_submit_account_errors_hook ( $args ) {
2017-06-06 13:26:11 +08:00
2019-03-11 16:01:11 +02:00
if ( ! isset ( $_POST [ 'um_account_submit' ] ) ) {
2017-08-11 14:21:42 +03:00
return ;
2019-03-11 16:01:11 +02:00
}
if ( ! wp_verify_nonce ( $_POST [ 'um_account_nonce_' . $_POST [ '_um_account_tab' ] ], 'um_update_account_' . $_POST [ '_um_account_tab' ] ) ) {
UM () -> form () -> add_error ( 'um_account_security' , __ ( 'Are you hacking? Please try again!' , 'ultimate-member' ) );
}
2017-07-26 14:57:52 +03:00
2017-08-11 14:21:42 +03:00
$user = get_user_by ( 'login' , um_user ( 'user_login' ) );
2017-07-26 14:57:52 +03:00
2017-08-11 14:21:42 +03:00
if ( isset ( $_POST [ '_um_account_tab' ] ) ) {
switch ( $_POST [ '_um_account_tab' ] ) {
case 'delete' : {
// delete account
if ( strlen ( trim ( $_POST [ 'single_user_password' ] ) ) == 0 ) {
2019-03-12 09:13:40 +02:00
UM () -> form () -> add_error ( 'single_user_password' , __ ( 'You must enter your password' , 'ultimate-member' ) );
2017-08-11 14:21:42 +03:00
} else {
2018-09-17 16:53:40 +03:00
if ( ! wp_check_password ( $_POST [ 'single_user_password' ], $user -> data -> user_pass , $user -> data -> ID ) ) {
2019-03-12 09:13:40 +02:00
UM () -> form () -> add_error ( 'single_user_password' , __ ( 'This is not your password' , 'ultimate-member' ) );
2016-05-26 11:23:43 +08:00
}
}
2017-02-09 20:52:47 +08:00
2017-08-11 14:21:42 +03:00
UM () -> account () -> current_tab = 'delete' ;
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
break ;
2014-12-15 22:38:07 +02:00
}
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
case 'password' : {
// change password
if ( ( isset ( $_POST [ 'current_user_password' ] ) && $_POST [ 'current_user_password' ] != '' ) ||
( isset ( $_POST [ 'user_password' ] ) && $_POST [ 'user_password' ] != '' ) ||
( isset ( $_POST [ 'confirm_user_password' ] ) && $_POST [ 'confirm_user_password' ] != '' ) ) {
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
if ( $_POST [ 'current_user_password' ] == '' || ! wp_check_password ( $_POST [ 'current_user_password' ], $user -> data -> user_pass , $user -> data -> ID ) ) {
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
UM () -> form () -> add_error ( 'current_user_password' , __ ( 'This is not your password' , 'ultimate-member' ) );
UM () -> account () -> current_tab = 'password' ;
} else { // correct password
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
if ( $_POST [ 'user_password' ] != $_POST [ 'confirm_user_password' ] && $_POST [ 'user_password' ] ) {
UM () -> form () -> add_error ( 'user_password' , __ ( 'Your new password does not match' , 'ultimate-member' ) );
UM () -> account () -> current_tab = 'password' ;
}
2016-01-24 15:13:13 -08:00
2017-12-11 09:53:38 +02:00
if ( UM () -> options () -> get ( 'account_require_strongpass' ) ) {
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
if ( strlen ( utf8_decode ( $_POST [ 'user_password' ] ) ) < 8 ) {
UM () -> form () -> add_error ( 'user_password' , __ ( 'Your password must contain at least 8 characters' , 'ultimate-member' ) );
}
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
if ( strlen ( utf8_decode ( $_POST [ 'user_password' ] ) ) > 30 ) {
UM () -> form () -> add_error ( 'user_password' , __ ( 'Your password must contain less than 30 characters' , 'ultimate-member' ) );
}
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
if ( ! UM () -> validation () -> strong_pass ( $_POST [ 'user_password' ] ) ) {
UM () -> form () -> add_error ( 'user_password' , __ ( 'Your password must contain at least one lowercase letter, one capital letter and one number' , 'ultimate-member' ) );
}
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
}
2016-01-24 15:13:13 -08:00
2016-07-03 16:49:50 +08:00
}
2016-06-23 13:45:26 +08:00
}
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
break ;
2015-04-07 20:10:23 +02:00
}
2016-09-07 21:20:51 +08:00
2017-08-11 14:21:42 +03:00
case 'account' :
case 'general' : {
// errors on general tab
2016-01-24 15:13:13 -08:00
2017-12-11 09:53:38 +02:00
$account_name_require = UM () -> options () -> get ( 'account_name_require' );
2016-06-02 21:24:14 +08:00
2017-08-11 14:21:42 +03:00
if ( ! empty ( $_POST [ 'user_login' ] ) && ! validate_username ( $_POST [ 'user_login' ] ) ) {
UM () -> form () -> add_error ( 'user_login' , __ ( 'Your username is invalid' , 'ultimate-member' ) );
return ;
}
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
if ( isset ( $_POST [ 'first_name' ] ) && ( strlen ( trim ( $_POST [ 'first_name' ] ) ) == 0 && $account_name_require ) ) {
UM () -> form () -> add_error ( 'first_name' , __ ( 'You must provide your first name' , 'ultimate-member' ) );
}
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
if ( isset ( $_POST [ 'last_name' ] ) && ( strlen ( trim ( $_POST [ 'last_name' ] ) ) == 0 && $account_name_require ) ) {
UM () -> form () -> add_error ( 'last_name' , __ ( 'You must provide your last name' , 'ultimate-member' ) );
}
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
if ( isset ( $_POST [ 'user_email' ] ) ) {
2019-03-12 09:13:40 +02:00
if ( strlen ( trim ( $_POST [ 'user_email' ] ) ) == 0 ) {
2017-08-11 14:21:42 +03:00
UM () -> form () -> add_error ( 'user_email' , __ ( 'You must provide your e-mail' , 'ultimate-member' ) );
2019-03-12 09:13:40 +02:00
}
2016-01-24 15:13:13 -08:00
2019-03-12 09:13:40 +02:00
if ( ! is_email ( $_POST [ 'user_email' ] ) ) {
2017-08-11 14:21:42 +03:00
UM () -> form () -> add_error ( 'user_email' , __ ( 'Please provide a valid e-mail' , 'ultimate-member' ) );
2019-03-12 09:13:40 +02:00
}
2016-01-24 15:13:13 -08:00
2019-03-12 09:13:40 +02:00
if ( email_exists ( $_POST [ 'user_email' ] ) && email_exists ( $_POST [ 'user_email' ] ) != get_current_user_id () ) {
2017-08-11 14:21:42 +03:00
UM () -> form () -> add_error ( 'user_email' , __ ( 'Email already linked to another account' , 'ultimate-member' ) );
2019-03-12 09:13:40 +02:00
}
}
// check account password
if ( UM () -> options () -> get ( 'account_general_password' ) ) {
if ( strlen ( trim ( $_POST [ 'single_user_password' ] ) ) == 0 ) {
UM () -> form () -> add_error ( 'single_user_password' , __ ( 'You must enter your password' , 'ultimate-member' ) );
} else {
if ( ! wp_check_password ( $_POST [ 'single_user_password' ], $user -> data -> user_pass , $user -> data -> ID ) ) {
UM () -> form () -> add_error ( 'single_user_password' , __ ( 'This is not your password' , 'ultimate-member' ) );
}
}
2014-12-15 22:38:07 +02:00
}
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
break ;
2016-07-03 16:49:50 +08:00
}
2017-08-11 14:21:42 +03:00
default :
2018-03-05 16:35:51 +02:00
/**
* UM hook
*
* @type action
* @title um_submit_account_{$tab}_tab_errors_hook
* @description On submit account current $tab validation
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_submit_account_{$tab}_tab_errors_hook', 'function_name', 10 );
* @example
* <?php
* add_action( 'um_submit_account_{$tab}_tab_errors_hook', 'my_submit_account_tab_errors', 10 );
* function my_submit_account_tab_errors() {
* // your code here
* }
* ?>
*/
2017-08-11 14:21:42 +03:00
do_action ( 'um_submit_account_' . $_POST [ '_um_account_tab' ] . '_tab_errors_hook' );
break ;
2014-12-15 22:38:07 +02:00
}
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
UM () -> account () -> current_tab = $_POST [ '_um_account_tab' ];
2014-12-15 22:38:07 +02:00
}
2016-01-24 15:13:13 -08:00
2017-08-11 14:21:42 +03:00
}
2018-03-19 16:31:49 +02:00
add_action ( 'um_submit_account_errors_hook' , 'um_submit_account_errors_hook' );
2016-06-23 13:45:26 +08:00
2016-01-24 15:13:13 -08:00
2018-03-19 16:31:49 +02:00
/**
* Submit account page changes
*
* @param $args
*/
function um_submit_account_details ( $args ) {
$tab = ( get_query_var ( 'um_tab' ) ) ? get_query_var ( 'um_tab' ) : 'general' ;
2016-01-24 15:13:13 -08:00
2018-03-19 16:31:49 +02:00
$current_tab = isset ( $_POST [ '_um_account_tab' ] ) ? $_POST [ '_um_account_tab' ] : '' ;
2016-01-24 15:13:13 -08:00
2019-11-21 14:08:15 +08:00
$user_id = um_user ( 'ID' );
2018-03-19 16:31:49 +02:00
//change password account's tab
if ( 'password' == $current_tab && $_POST [ 'user_password' ] && $_POST [ 'confirm_user_password' ] ) {
2016-01-24 15:13:13 -08:00
2018-03-19 16:31:49 +02:00
$changes [ 'user_pass' ] = $_POST [ 'user_password' ];
2016-01-24 15:13:13 -08:00
2019-11-21 14:08:15 +08:00
$args [ 'user_id' ] = $user_id ;
2016-01-24 15:13:13 -08:00
2018-09-16 00:26:32 +03:00
UM () -> user () -> password_changed ();
2019-05-29 18:51:22 +03:00
add_filter ( 'send_password_change_email' , '__return_false' );
2016-01-24 15:13:13 -08:00
2018-09-16 00:26:32 +03:00
//clear all sessions with old passwords
2019-11-21 14:08:15 +08:00
$user = WP_Session_Tokens :: get_instance ( $user_id );
2018-09-16 00:26:32 +03:00
$user -> destroy_all ();
2019-11-21 14:08:15 +08:00
wp_set_password ( $changes [ 'user_pass' ], $user_id );
2019-09-26 16:52:11 +03:00
2018-03-19 16:31:49 +02:00
wp_signon ( array ( 'user_login' => um_user ( 'user_login' ), 'user_password' => $changes [ 'user_pass' ] ) );
}
2015-03-07 13:07:49 +02:00
2016-01-24 15:13:13 -08:00
2018-03-19 16:31:49 +02:00
// delete account
$user = get_user_by ( 'login' , um_user ( 'user_login' ) );
2019-09-26 16:52:11 +03:00
if ( 'delete' == $current_tab && isset ( $_POST [ 'single_user_password' ] ) &&
wp_check_password ( $_POST [ 'single_user_password' ], $user -> data -> user_pass , $user -> data -> ID ) ) {
2018-03-19 16:31:49 +02:00
if ( current_user_can ( 'delete_users' ) || um_user ( 'can_delete_profile' ) ) {
2018-04-09 00:45:43 +03:00
UM () -> user () -> delete ();
if ( um_user ( 'after_delete' ) && um_user ( 'after_delete' ) == 'redirect_home' ) {
um_redirect_home ();
} elseif ( um_user ( 'delete_redirect_url' ) ) {
/**
* UM hook
*
* @type filter
* @title um_delete_account_redirect_url
* @description Change redirect URL after delete account
* @input_vars
* [{"var":"$url","type":"string","desc":"Redirect URL"},
* {"var":"$id","type":"int","desc":"User ID"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_delete_account_redirect_url', 'function_name', 10, 2 ); ?>
* @example
* <?php
* add_filter( 'um_delete_account_redirect_url', 'my_delete_account_redirect_url', 10, 2 );
* function my_delete_account_redirect_url( $url, $id ) {
* // your code here
* return $url;
* }
* ?>
*/
2019-11-21 14:08:15 +08:00
$redirect_url = apply_filters ( 'um_delete_account_redirect_url' , um_user ( 'delete_redirect_url' ), $user_id );
2018-04-09 00:45:43 +03:00
exit ( wp_redirect ( $redirect_url ) );
} else {
um_redirect_home ();
2017-08-11 14:21:42 +03:00
}
2015-01-03 15:31:15 +02:00
}
2018-03-19 16:31:49 +02:00
}
2019-10-21 13:31:59 +03:00
$arr_fields = array ();
2019-09-26 16:52:11 +03:00
if ( UM () -> account () -> is_secure_enabled () ) {
2019-11-21 14:08:15 +08:00
$account_fields = get_user_meta ( $user_id , 'um_account_secure_fields' , true );
2019-09-26 16:52:11 +03:00
/**
* UM hook
*
* @type filter
* @title um_secure_account_fields
* @description Change secure account fields
* @input_vars
* [{"var":"$fields","type":"array","desc":"Secure account fields"},
* {"var":"$user_id","type":"int","desc":"User ID"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_secure_account_fields', 'function_name', 10, 2 ); ?>
* @example
* <?php
* add_filter( 'um_secure_account_fields', 'my_secure_account_fields', 10, 2 );
* function my_secure_account_fields( $fields, $user_id ) {
* // your code here
* return $fields;
* }
* ?>
*/
2019-11-21 14:08:15 +08:00
$secure_fields = apply_filters ( 'um_secure_account_fields' , $account_fields , $user_id );
2019-09-26 16:52:11 +03:00
2019-11-07 17:53:18 +02:00
if ( isset ( $secure_fields [ $current_tab ] ) && is_array ( $secure_fields [ $current_tab ] ) ) {
2019-09-26 16:52:11 +03:00
$arr_fields = array_merge ( $arr_fields , $secure_fields [ $current_tab ] );
2015-01-03 15:31:15 +02:00
}
2018-03-19 16:31:49 +02:00
}
2015-03-07 13:07:49 +02:00
2018-03-19 16:31:49 +02:00
$changes = array ();
foreach ( $_POST as $k => $v ) {
2019-09-26 16:52:11 +03:00
if ( ! in_array ( $k , $arr_fields ) ) {
2018-03-19 16:31:49 +02:00
continue ;
2019-08-08 00:36:33 +03:00
}
2018-02-12 15:37:10 +02:00
2018-03-19 16:31:49 +02:00
$changes [ $k ] = $v ;
}
2015-03-07 13:07:49 +02:00
2019-08-08 00:36:33 +03:00
if ( isset ( $changes [ 'hide_in_members' ] ) && ( $changes [ 'hide_in_members' ] == __ ( 'No' , 'ultimate-member' ) || $changes [ 'hide_in_members' ] == 'No' ) ) {
2019-11-21 14:08:15 +08:00
delete_user_meta ( $user_id , 'hide_in_members' );
2018-03-19 16:31:49 +02:00
unset ( $changes [ 'hide_in_members' ] );
2015-02-10 02:05:27 +02:00
}
2016-01-24 15:13:13 -08:00
2018-03-19 16:31:49 +02:00
/**
* UM hook
*
* @type filter
* @title um_account_pre_updating_profile_array
* @description Change update profile data before saving
* @input_vars
* [{"var":"$changes","type":"array","desc":"Profile changes array"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_account_pre_updating_profile_array', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_account_pre_updating_profile_array', 'my_account_pre_updating_profile', 10, 1 );
* function my_account_pre_updating_profile( $changes ) {
* // your code here
* return $changes;
* }
* ?>
*/
$changes = apply_filters ( 'um_account_pre_updating_profile_array' , $changes );
/**
* UM hook
*
* @type action
* @title um_account_pre_update_profile
* @description Fired on account page, just before updating profile
* @input_vars
* [{"var":"$changes","type":"array","desc":"Submitted data"},
* {"var":"$user_id","type":"int","desc":"User ID"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_account_pre_update_profile', 'function_name', 10, 2 );
* @example
* <?php
* add_action( 'um_account_pre_update_profile', 'my_account_pre_update_profile', 10, 2 );
* function my_account_pre_update_profile( $changes, $user_id ) {
* // your code here
* }
* ?>
*/
2019-11-21 14:08:15 +08:00
do_action ( 'um_account_pre_update_profile' , $changes , $user_id );
2016-01-24 15:13:13 -08:00
2018-03-19 16:31:49 +02:00
UM () -> user () -> update_profile ( $changes );
2016-01-24 15:13:13 -08:00
2019-09-26 16:52:11 +03:00
if ( UM () -> account () -> is_secure_enabled () ) {
2019-11-21 14:08:15 +08:00
update_user_meta ( $user_id , 'um_account_secure_fields' , array () );
2019-09-26 16:52:11 +03:00
}
2017-02-18 11:28:52 +08:00
/**
2018-03-19 16:31:49 +02:00
* UM hook
*
* @type action
* @title um_post_account_update
* @description Fired on account page, after updating profile
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_post_account_update', 'function_name', 10 );
* @example
* <?php
* add_action( 'um_post_account_update', 'my_post_account_update', 10 );
* function my_account_pre_update_profile() {
* // your code here
* }
* ?>
*/
do_action ( 'um_post_account_update' );
/**
* UM hook
*
* @type action
* @title um_after_user_account_updated
* @description Fired on account page, after updating profile
* @input_vars
* [{"var":"$user_id","type":"int","desc":"User ID"},
* {"var":"$changes","type":"array","desc":"Submitted data"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_after_user_account_updated', 'function_name', 10, 2 );
* @example
* <?php
* add_action( 'um_after_user_account_updated', 'my_after_user_account_updated', 10, 2 );
* function my_after_user_account_updated( $user_id, $changes ) {
* // your code here
* }
* ?>
2017-02-18 11:28:52 +08:00
*/
2019-11-21 14:08:15 +08:00
do_action ( 'um_after_user_account_updated' , $user_id , $changes );
2016-01-24 15:13:13 -08:00
2018-03-19 16:31:49 +02:00
$url = '' ;
if ( um_is_core_page ( 'account' ) ) {
2016-01-24 15:13:13 -08:00
2018-03-19 16:31:49 +02:00
$url = UM () -> account () -> tab_link ( $tab );
2016-01-24 15:13:13 -08:00
2018-03-19 16:31:49 +02:00
$url = add_query_arg ( 'updated' , 'account' , $url );
2016-01-24 15:13:13 -08:00
2018-03-19 16:31:49 +02:00
if ( function_exists ( 'icl_get_current_language' ) ) {
if ( icl_get_current_language () != icl_get_default_language () ) {
$url = UM () -> permalinks () -> get_current_url ( true );
$url = add_query_arg ( 'updated' , 'account' , $url );
2017-08-11 14:21:42 +03:00
2018-03-19 16:31:49 +02:00
um_js_redirect ( $url );
}
}
2014-12-15 22:38:07 +02:00
}
2016-01-24 15:13:13 -08:00
2018-03-19 16:31:49 +02:00
um_js_redirect ( $url );
}
add_action ( 'um_submit_account_details' , 'um_submit_account_details' );
2017-08-11 14:21:42 +03:00
2018-03-19 16:31:49 +02:00
/**
* Hidden inputs for account form
*
* @param $args
*/
function um_account_page_hidden_fields ( $args ) {
?>
<input type="hidden" name="_um_account" id="_um_account" value="1" />
2019-08-08 00:36:33 +03:00
<input type="hidden" name="_um_account_tab" id="_um_account_tab" value="<?php echo esc_attr( UM()->account()->current_tab ); ?>" />
2018-03-19 16:31:49 +02:00
<?php
}
add_action( 'um_account_page_hidden_fields', 'um_account_page_hidden_fields' );
/**
* Before delete account tab content
*/
function um_before_account_delete() {
2018-04-18 22:22:49 +08:00
echo wpautop( htmlspecialchars( UM()->options()->get( 'delete_account_text' ) ) );
2018-03-19 16:31:49 +02:00
}
add_action( 'um_before_account_delete', 'um_before_account_delete' );
/**
* Before notifications account tab content
2019-07-16 14:24:12 +03:00
*
* @param array $args
2019-09-26 16:52:11 +03:00
*
* @throws Exception
2018-03-19 16:31:49 +02:00
*/
2019-07-10 13:02:04 +03:00
function um_before_account_notifications( $args = array() ) {
$output = UM()->account()->get_tab_fields( 'notifications', $args );
2019-07-16 14:24:12 +03:00
if ( substr_count( $output, '_enable_new_' ) ) { ?>
2019-11-28 12:51:44 +02:00
<p><?php _e( 'Select what email notifications do you want to receive', 'ultimate-member' ); ?></p>
2019-07-16 14:24:12 +03:00
<?php }
2019-07-10 13:02:04 +03:00
}
2018-03-19 16:31:49 +02:00
add_action( 'um_before_account_notifications', 'um_before_account_notifications' );
2014-12-15 22:38:07 +02:00
2017-02-09 20:52:47 +08:00
2018-02-12 16:27:34 +02:00
/**
* Update Profile URL
*
* @param $user_id
* @param $changed
*/
function um_after_user_account_updated_permalink( $user_id, $changed ) {
2018-02-18 13:27:46 +02:00
UM()->user()->generate_profile_slug( $user_id );
2018-02-12 16:27:34 +02:00
}
2018-04-01 15:45:25 +03:00
add_action( 'um_after_user_account_updated', 'um_after_user_account_updated_permalink', 10, 2 );
/**
* Update Account Email Notification
*
* @param $user_id
* @param $changed
*/
function um_account_updated_notification( $user_id, $changed ) {
um_fetch_user( $user_id );
UM()->mail()->send( um_user( 'user_email' ), 'changedaccount_email' );
}
add_action( 'um_after_user_account_updated', 'um_account_updated_notification', 20, 2 );
/**
* Disable WP native email notification when change email on user account
*
* @param $user_id
* @param $changed
*/
function um_disable_native_email_notificatiion( $changed, $user_id ) {
add_filter( 'send_email_change_email', '__return_false' );
}
add_action( 'um_account_pre_update_profile', 'um_disable_native_email_notificatiion', 10, 2 );