author | Anthony Ly <anthonyly.com@gmail.com> |
Mon, 19 Nov 2012 18:26:13 +0100 | |
changeset 194 | 32102edaa81b |
parent 136 | bde1974c263b |
child 204 | 09a1c134465b |
permissions | -rw-r--r-- |
136 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress user administration API. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Creates a new user from the "Users" form using $_POST information. |
|
11 |
* |
|
12 |
* @since 2.0 |
|
13 |
* |
|
14 |
* @return null|WP_Error|int Null when adding user, WP_Error or User ID integer when no parameters. |
|
15 |
*/ |
|
16 |
function add_user() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
17 |
return edit_user(); |
136 | 18 |
} |
19 |
||
20 |
/** |
|
21 |
* Edit user settings based on contents of $_POST |
|
22 |
* |
|
23 |
* Used on user-edit.php and profile.php to manage and process user options, passwords etc. |
|
24 |
* |
|
25 |
* @since 2.0 |
|
26 |
* |
|
27 |
* @param int $user_id Optional. User ID. |
|
28 |
* @return int user id of the updated user |
|
29 |
*/ |
|
30 |
function edit_user( $user_id = 0 ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
31 |
global $wp_roles, $wpdb; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
32 |
$user = new stdClass; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
33 |
if ( $user_id ) { |
136 | 34 |
$update = true; |
35 |
$user->ID = (int) $user_id; |
|
36 |
$userdata = get_userdata( $user_id ); |
|
37 |
$user->user_login = $wpdb->escape( $userdata->user_login ); |
|
38 |
} else { |
|
39 |
$update = false; |
|
40 |
} |
|
41 |
||
42 |
if ( !$update && isset( $_POST['user_login'] ) ) |
|
43 |
$user->user_login = sanitize_user($_POST['user_login'], true); |
|
44 |
||
45 |
$pass1 = $pass2 = ''; |
|
46 |
if ( isset( $_POST['pass1'] )) |
|
47 |
$pass1 = $_POST['pass1']; |
|
48 |
if ( isset( $_POST['pass2'] )) |
|
49 |
$pass2 = $_POST['pass2']; |
|
50 |
||
51 |
if ( isset( $_POST['role'] ) && current_user_can( 'edit_users' ) ) { |
|
52 |
$new_role = sanitize_text_field( $_POST['role'] ); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
53 |
$potential_role = isset($wp_roles->role_objects[$new_role]) ? $wp_roles->role_objects[$new_role] : false; |
136 | 54 |
// Don't let anyone with 'edit_users' (admins) edit their own role to something without it. |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
55 |
// Multisite super admins can freely edit their blog roles -- they possess all caps. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
56 |
if ( ( is_multisite() && current_user_can( 'manage_sites' ) ) || $user_id != get_current_user_id() || ($potential_role && $potential_role->has_cap( 'edit_users' ) ) ) |
136 | 57 |
$user->role = $new_role; |
58 |
||
59 |
// If the new role isn't editable by the logged-in user die with error |
|
60 |
$editable_roles = get_editable_roles(); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
61 |
if ( ! empty( $new_role ) && empty( $editable_roles[$new_role] ) ) |
136 | 62 |
wp_die(__('You can’t give users that role.')); |
63 |
} |
|
64 |
||
65 |
if ( isset( $_POST['email'] )) |
|
66 |
$user->user_email = sanitize_text_field( $_POST['email'] ); |
|
67 |
if ( isset( $_POST['url'] ) ) { |
|
68 |
if ( empty ( $_POST['url'] ) || $_POST['url'] == 'http://' ) { |
|
69 |
$user->user_url = ''; |
|
70 |
} else { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
71 |
$user->user_url = esc_url_raw( $_POST['url'] ); |
136 | 72 |
$user->user_url = preg_match('/^(https?|ftps?|mailto|news|irc|gopher|nntp|feed|telnet):/is', $user->user_url) ? $user->user_url : 'http://'.$user->user_url; |
73 |
} |
|
74 |
} |
|
75 |
if ( isset( $_POST['first_name'] ) ) |
|
76 |
$user->first_name = sanitize_text_field( $_POST['first_name'] ); |
|
77 |
if ( isset( $_POST['last_name'] ) ) |
|
78 |
$user->last_name = sanitize_text_field( $_POST['last_name'] ); |
|
79 |
if ( isset( $_POST['nickname'] ) ) |
|
80 |
$user->nickname = sanitize_text_field( $_POST['nickname'] ); |
|
81 |
if ( isset( $_POST['display_name'] ) ) |
|
82 |
$user->display_name = sanitize_text_field( $_POST['display_name'] ); |
|
83 |
||
84 |
if ( isset( $_POST['description'] ) ) |
|
85 |
$user->description = trim( $_POST['description'] ); |
|
86 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
87 |
foreach ( _wp_get_user_contactmethods( $user ) as $method => $name ) { |
136 | 88 |
if ( isset( $_POST[$method] )) |
89 |
$user->$method = sanitize_text_field( $_POST[$method] ); |
|
90 |
} |
|
91 |
||
92 |
if ( $update ) { |
|
93 |
$user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' == $_POST['rich_editing'] ? 'false' : 'true'; |
|
94 |
$user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh'; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
95 |
$user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false'; |
136 | 96 |
} |
97 |
||
98 |
$user->comment_shortcuts = isset( $_POST['comment_shortcuts'] ) && 'true' == $_POST['comment_shortcuts'] ? 'true' : ''; |
|
99 |
||
100 |
$user->use_ssl = 0; |
|
101 |
if ( !empty($_POST['use_ssl']) ) |
|
102 |
$user->use_ssl = 1; |
|
103 |
||
104 |
$errors = new WP_Error(); |
|
105 |
||
106 |
/* checking that username has been typed */ |
|
107 |
if ( $user->user_login == '' ) |
|
108 |
$errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' )); |
|
109 |
||
110 |
/* checking the password has been typed twice */ |
|
111 |
do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 )); |
|
112 |
||
113 |
if ( $update ) { |
|
114 |
if ( empty($pass1) && !empty($pass2) ) |
|
115 |
$errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass1' ) ); |
|
116 |
elseif ( !empty($pass1) && empty($pass2) ) |
|
117 |
$errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass2' ) ); |
|
118 |
} else { |
|
119 |
if ( empty($pass1) ) |
|
120 |
$errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password.' ), array( 'form-field' => 'pass1' ) ); |
|
121 |
elseif ( empty($pass2) ) |
|
122 |
$errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter your password twice.' ), array( 'form-field' => 'pass2' ) ); |
|
123 |
} |
|
124 |
||
125 |
/* Check for "\" in password */ |
|
126 |
if ( false !== strpos( stripslashes($pass1), "\\" ) ) |
|
127 |
$errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) ); |
|
128 |
||
129 |
/* checking the password has been typed twice the same */ |
|
130 |
if ( $pass1 != $pass2 ) |
|
131 |
$errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in the two password fields.' ), array( 'form-field' => 'pass1' ) ); |
|
132 |
||
133 |
if ( !empty( $pass1 ) ) |
|
134 |
$user->user_pass = $pass1; |
|
135 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
136 |
if ( !$update && isset( $_POST['user_login'] ) && !validate_username( $_POST['user_login'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
137 |
$errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' )); |
136 | 138 |
|
139 |
if ( !$update && username_exists( $user->user_login ) ) |
|
140 |
$errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' )); |
|
141 |
||
142 |
/* checking e-mail address */ |
|
143 |
if ( empty( $user->user_email ) ) { |
|
144 |
$errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ), array( 'form-field' => 'email' ) ); |
|
145 |
} elseif ( !is_email( $user->user_email ) ) { |
|
146 |
$errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The e-mail address isn’t correct.' ), array( 'form-field' => 'email' ) ); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
147 |
} elseif ( ( $owner_id = email_exists($user->user_email) ) && ( !$update || ( $owner_id != $user->ID ) ) ) { |
136 | 148 |
$errors->add( 'email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'), array( 'form-field' => 'email' ) ); |
149 |
} |
|
150 |
||
151 |
// Allow plugins to return their own errors. |
|
152 |
do_action_ref_array('user_profile_update_errors', array ( &$errors, $update, &$user ) ); |
|
153 |
||
154 |
if ( $errors->get_error_codes() ) |
|
155 |
return $errors; |
|
156 |
||
157 |
if ( $update ) { |
|
158 |
$user_id = wp_update_user( get_object_vars( $user ) ); |
|
159 |
} else { |
|
160 |
$user_id = wp_insert_user( get_object_vars( $user ) ); |
|
161 |
wp_new_user_notification( $user_id, isset($_POST['send_password']) ? $pass1 : '' ); |
|
162 |
} |
|
163 |
return $user_id; |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* Fetch a filtered list of user roles that the current user is |
|
168 |
* allowed to edit. |
|
169 |
* |
|
170 |
* Simple function who's main purpose is to allow filtering of the |
|
171 |
* list of roles in the $wp_roles object so that plugins can remove |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
172 |
* inappropriate ones depending on the situation or user making edits. |
136 | 173 |
* Specifically because without filtering anyone with the edit_users |
174 |
* capability can edit others to be administrators, even if they are |
|
175 |
* only editors or authors. This filter allows admins to delegate |
|
176 |
* user management. |
|
177 |
* |
|
178 |
* @since 2.8 |
|
179 |
* |
|
180 |
* @return unknown |
|
181 |
*/ |
|
182 |
function get_editable_roles() { |
|
183 |
global $wp_roles; |
|
184 |
||
185 |
$all_roles = $wp_roles->roles; |
|
186 |
$editable_roles = apply_filters('editable_roles', $all_roles); |
|
187 |
||
188 |
return $editable_roles; |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* Retrieve user data and filter it. |
|
193 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
194 |
* @since 2.0.5 |
136 | 195 |
* |
196 |
* @param int $user_id User ID. |
|
197 |
* @return object WP_User object with user data. |
|
198 |
*/ |
|
199 |
function get_user_to_edit( $user_id ) { |
|
200 |
$user = new WP_User( $user_id ); |
|
201 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
202 |
$user->filter = 'edit'; |
136 | 203 |
|
204 |
return $user; |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* Retrieve the user's drafts. |
|
209 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
210 |
* @since 2.0.0 |
136 | 211 |
* |
212 |
* @param int $user_id User ID. |
|
213 |
* @return array |
|
214 |
*/ |
|
215 |
function get_users_drafts( $user_id ) { |
|
216 |
global $wpdb; |
|
217 |
$query = $wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = %d ORDER BY post_modified DESC", $user_id); |
|
218 |
$query = apply_filters('get_users_drafts', $query); |
|
219 |
return $wpdb->get_results( $query ); |
|
220 |
} |
|
221 |
||
222 |
/** |
|
223 |
* Remove user and optionally reassign posts and links to another user. |
|
224 |
* |
|
225 |
* If the $reassign parameter is not assigned to an User ID, then all posts will |
|
226 |
* be deleted of that user. The action 'delete_user' that is passed the User ID |
|
227 |
* being deleted will be run after the posts are either reassigned or deleted. |
|
228 |
* The user meta will also be deleted that are for that User ID. |
|
229 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
230 |
* @since 2.0.0 |
136 | 231 |
* |
232 |
* @param int $id User ID. |
|
233 |
* @param int $reassign Optional. Reassign posts and links to new User ID. |
|
234 |
* @return bool True when finished. |
|
235 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
236 |
function wp_delete_user( $id, $reassign = 'novalue' ) { |
136 | 237 |
global $wpdb; |
238 |
||
239 |
$id = (int) $id; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
240 |
$user = new WP_User( $id ); |
136 | 241 |
|
242 |
// allow for transaction statement |
|
243 |
do_action('delete_user', $id); |
|
244 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
245 |
if ( 'novalue' === $reassign || null === $reassign ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
246 |
$post_types_to_delete = array(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
247 |
foreach ( get_post_types( array(), 'objects' ) as $post_type ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
248 |
if ( $post_type->delete_with_user ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
249 |
$post_types_to_delete[] = $post_type->name; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
250 |
} elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
251 |
$post_types_to_delete[] = $post_type->name; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
252 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
253 |
} |
136 | 254 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
255 |
$post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
256 |
$post_types_to_delete = implode( "', '", $post_types_to_delete ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
257 |
$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d AND post_type IN ('$post_types_to_delete')", $id ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
258 |
if ( $post_ids ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
259 |
foreach ( $post_ids as $post_id ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
260 |
wp_delete_post( $post_id ); |
136 | 261 |
} |
262 |
||
263 |
// Clean links |
|
264 |
$link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) ); |
|
265 |
||
266 |
if ( $link_ids ) { |
|
267 |
foreach ( $link_ids as $link_id ) |
|
268 |
wp_delete_link($link_id); |
|
269 |
} |
|
270 |
} else { |
|
271 |
$reassign = (int) $reassign; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
272 |
$wpdb->update( $wpdb->posts, array('post_author' => $reassign), array('post_author' => $id) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
273 |
$wpdb->update( $wpdb->links, array('link_owner' => $reassign), array('link_owner' => $id) ); |
136 | 274 |
} |
275 |
||
276 |
// FINALLY, delete user |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
277 |
if ( is_multisite() ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
278 |
remove_user_from_blog( $id, get_current_blog_id() ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
279 |
} else { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
280 |
$meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
281 |
foreach ( $meta as $mid ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
282 |
delete_metadata_by_mid( 'user', $mid ); |
136 | 283 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
284 |
$wpdb->delete( $wpdb->users, array( 'ID' => $id ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
285 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
286 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
287 |
clean_user_cache( $user ); |
136 | 288 |
|
289 |
// allow for commit transaction |
|
290 |
do_action('deleted_user', $id); |
|
291 |
||
292 |
return true; |
|
293 |
} |
|
294 |
||
295 |
/** |
|
296 |
* Remove all capabilities from user. |
|
297 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
298 |
* @since 2.1.0 |
136 | 299 |
* |
300 |
* @param int $id User ID. |
|
301 |
*/ |
|
302 |
function wp_revoke_user($id) { |
|
303 |
$id = (int) $id; |
|
304 |
||
305 |
$user = new WP_User($id); |
|
306 |
$user->remove_all_caps(); |
|
307 |
} |
|
308 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
309 |
add_action('admin_init', 'default_password_nag_handler'); |
136 | 310 |
/** |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
311 |
* @since 2.8.0 |
136 | 312 |
*/ |
313 |
function default_password_nag_handler($errors = false) { |
|
314 |
global $user_ID; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
315 |
if ( ! get_user_option('default_password_nag') ) //Short circuit it. |
136 | 316 |
return; |
317 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
318 |
//get_user_setting = JS saved UI setting. else no-js-fallback code. |
136 | 319 |
if ( 'hide' == get_user_setting('default_password_nag') || isset($_GET['default_password_nag']) && '0' == $_GET['default_password_nag'] ) { |
320 |
delete_user_setting('default_password_nag'); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
321 |
update_user_option($user_ID, 'default_password_nag', false, true); |
136 | 322 |
} |
323 |
} |
|
324 |
||
325 |
add_action('profile_update', 'default_password_nag_edit_user', 10, 2); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
326 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
327 |
* @since 2.8.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
328 |
*/ |
136 | 329 |
function default_password_nag_edit_user($user_ID, $old_data) { |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
330 |
if ( ! get_user_option('default_password_nag', $user_ID) ) //Short circuit it. |
136 | 331 |
return; |
332 |
||
333 |
$new_data = get_userdata($user_ID); |
|
334 |
||
335 |
if ( $new_data->user_pass != $old_data->user_pass ) { //Remove the nag if the password has been changed. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
336 |
delete_user_setting('default_password_nag', $user_ID); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
337 |
update_user_option($user_ID, 'default_password_nag', false, true); |
136 | 338 |
} |
339 |
} |
|
340 |
||
341 |
add_action('admin_notices', 'default_password_nag'); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
342 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
343 |
* @since 2.8.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
344 |
*/ |
136 | 345 |
function default_password_nag() { |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
346 |
global $pagenow; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
347 |
if ( 'profile.php' == $pagenow || ! get_user_option('default_password_nag') ) //Short circuit it. |
136 | 348 |
return; |
349 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
350 |
echo '<div class="error default-password-nag">'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
351 |
echo '<p>'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
352 |
echo '<strong>' . __('Notice:') . '</strong> '; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
353 |
_e('You’re using the auto-generated password for your account. Would you like to change it to something easier to remember?'); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
354 |
echo '</p><p>'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
355 |
printf( '<a href="%s">' . __('Yes, take me to my profile page') . '</a> | ', admin_url('profile.php') . '#password' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
356 |
printf( '<a href="%s" id="default-password-nag-no">' . __('No thanks, do not remind me again') . '</a>', '?default_password_nag=0' ); |
136 | 357 |
echo '</p></div>'; |
358 |
} |