-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtrait-wpum-account.php
More file actions
217 lines (185 loc) · 7.56 KB
/
Copy pathtrait-wpum-account.php
File metadata and controls
217 lines (185 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* Handles the WPUM account shared functions.
*
* @package wp-user-manager
* @copyright Copyright (c) 2022, WP User Manager
* @license https://opensource.org/licenses/GPL-3.0 GNU Public License
*/
/**
* WPUM_Form_Account
*/
trait WPUM_Form_Account {
/**
* @param \WP_User $user
* @param array $values
* @param bool $partial_form
*
* @return int|WP_Error
* @throws Exception
*/
protected function update_account_values( $user, $values, $partial_form = false ) {
// Collect all the data to update the user.
$user_data = array(
'ID' => $user->ID,
);
// Update first name and last name.
if ( isset( $values['account']['user_firstname'] ) ) {
$user_data['first_name'] = wpum_sanitize_text( $values['account']['user_firstname'] );
}
if ( isset( $values['account']['user_lastname'] ) ) {
$user_data['last_name'] = wpum_sanitize_text( $values['account']['user_lastname'] );
}
// Update email address.
if ( isset( $values['account']['user_email'] ) ) {
$user_data['user_email'] = wpum_sanitize_text( $values['account']['user_email'] );
}
// Update nickname.
if ( isset( $values['account']['user_nickname'] ) ) {
$user_data['nickname'] = wpum_sanitize_text( $values['account']['user_nickname'] );
}
// Update website.
if ( isset( $values['account']['user_website'] ) ) {
$user_data['user_url'] = wpum_sanitize_text( $values['account']['user_website'] );
}
// Update description.
if ( isset( $values['account']['user_description'] ) ) {
$user_data['description'] = wpum_sanitize_text( $values['account']['user_description'] );
}
// Update displayed name.
if ( isset( $values['account']['user_displayname'] ) ) {
$user_data['display_name'] = wpum_sanitize_text( $this->parse_displayname( $values['account'], $values['account']['user_displayname'] ) );
}
// Now update the user.
$updated_user_id = wp_update_user( $user_data );
if ( is_wp_error( $updated_user_id ) ) {
throw new Exception( esc_html( $updated_user_id->get_error_message() ) );
}
if ( wpum_get_option( 'custom_avatars' ) ) {
$current_uploaded_avatar = filter_input( INPUT_POST, 'current_user_avatar' );
$currently_uploaded_file = $current_uploaded_avatar ? esc_url_raw( $current_uploaded_avatar ) : false;
$existing_avatar_file_path = get_user_meta( $updated_user_id, '_current_user_avatar_path', true );
// Delete previous avatar if a new one has been uploaded.
if ( $currently_uploaded_file && $existing_avatar_file_path && file_exists( $existing_avatar_file_path ) && isset( $values['account']['user_avatar']['url'] ) && $values['account']['user_avatar']['url'] !== $currently_uploaded_file ) {
wp_delete_file( $existing_avatar_file_path );
}
// If no new avatar uploaded and existing file exists, delete it.
if ( ! $currently_uploaded_file && file_exists( $existing_avatar_file_path ) ) {
wp_delete_file( $existing_avatar_file_path );
carbon_set_user_meta( $updated_user_id, 'current_user_avatar', false );
delete_user_meta( $updated_user_id, '_current_user_avatar_path' );
do_action( 'wpum_user_update_remove_avatar', $user->ID );
}
if ( isset( $values['account']['user_avatar']['url'] ) && $currently_uploaded_file !== $values['account']['user_avatar']['url'] ) {
carbon_set_user_meta( $updated_user_id, 'current_user_avatar', $values['account']['user_avatar']['url'] );
update_user_meta( $updated_user_id, '_current_user_avatar_path', $values['account']['user_avatar']['path'] );
do_action( 'wpum_user_update_change_avatar', $user->ID, $values['account']['user_avatar']['url'] );
}
}
$current_uploaded_cover = filter_input( INPUT_POST, 'current_user_cover' );
$currently_uploaded_cover = $current_uploaded_cover ? esc_url_raw( $current_uploaded_cover ) : false;
$existing_cover_file_path = get_user_meta( $updated_user_id, '_user_cover_path', true );
if ( isset( $values['account']['user_cover']['url'] ) ) {
if ( $currently_uploaded_cover && $existing_cover_file_path && file_exists( $existing_cover_file_path ) && isset( $values['account']['user_cover']['url'] ) && $values['account']['user_cover']['url'] !== $currently_uploaded_cover ) {
wp_delete_file( $existing_cover_file_path );
}
if ( $currently_uploaded_cover !== $values['account']['user_cover']['url'] ) {
carbon_set_user_meta( $updated_user_id, 'user_cover', $values['account']['user_cover']['url'] );
update_user_meta( $updated_user_id, '_user_cover_path', $values['account']['user_cover']['path'] );
do_action( 'wpum_user_update_change_cover', $user->ID, $values['account']['user_cover']['url'] );
}
}
if ( ! $partial_form && ! $currently_uploaded_cover && file_exists( $existing_cover_file_path ) && ! isset( $values['account']['user_cover']['url'] ) ) {
wp_delete_file( $existing_cover_file_path );
carbon_set_user_meta( $updated_user_id, 'user_cover', false );
delete_user_meta( $updated_user_id, '_user_cover_path' );
do_action( 'wpum_user_update_remove_cover', $user->ID );
}
return $updated_user_id;
}
/**
* Retrieve the value of a given field for the currently logged in user.
*
* @param WP_User $user
* @param WPUM_Field $field
*
* @return mixed
*/
protected function get_user_field_value( $user, $field ) {
$value = false;
if ( ! empty( $field->get_primary_id() ) ) {
switch ( $field->get_primary_id() ) {
case 'user_firstname':
$value = esc_html( $user->user_firstname );
break;
case 'user_lastname':
$value = esc_html( $user->user_lastname );
break;
case 'user_email':
$value = esc_html( $user->user_email );
break;
case 'user_nickname':
$value = esc_html( get_user_meta( $user->ID, 'nickname', true ) );
break;
case 'user_website':
$value = esc_html( $user->user_url );
break;
case 'user_description':
$value = esc_textarea( get_user_meta( $user->ID, 'description', true ) );
break;
case 'user_displayname':
$value = $this->get_selected_displayname( $user );
break;
case 'user_avatar':
$value = \WPUM\carbon_get_user_meta( $user->ID, 'current_user_avatar' );
break;
case 'user_cover':
$value = \WPUM\carbon_get_user_meta( $user->ID, 'user_cover' );
break;
}
} elseif ( strpos( $field->get_meta( 'user_meta_key' ), 'wpum_' ) === 0 ) {
$value = \WPUM\carbon_get_user_meta( $user->ID, $field->get_meta( 'user_meta_key' ) );
} else {
$value = esc_html( get_user_meta( $user->ID, $field->get_meta( 'user_meta_key' ), true ) );
}
return apply_filters( 'wpum_custom_field_value', $value, $field, $user->ID );
}
/**
* Retrieve the option currently selected for the display name setting.
*
* @param \WP_User $user
*
* @return string
*/
protected function get_selected_displayname( $user ) {
$selected_name = $user->display_name;
$user_login = $user->user_login;
$nickname = $user->nickname;
$first_name = $user->first_name;
$last_name = $user->last_name;
$firstlast = $user->first_name . ' ' . $user->last_name;
$lastfirst = $user->last_name . ' ' . $user->first_name;
$selected_value = $user_login;
switch ( $selected_name ) {
case $nickname:
$selected_value = 'display_nickname';
break;
case $first_name:
$selected_value = 'display_firstname';
break;
case $last_name:
$selected_value = 'display_lastname';
break;
case $firstlast:
$selected_value = 'display_firstlast';
break;
case $lastfirst:
$selected_value = 'display_lastfirst';
break;
default:
$selected_value = $user_login;
break;
}
return $selected_value;
}
}