Make WordPress Core

Opened 15 years ago

Closed 14 years ago

#18749 closed enhancement (fixed)

Remove "the hackiest hack that ever did hack" from add_user()

Reported by: duck_'s profile duck_ Owned by:
Milestone: 3.4 Priority: normal
Severity: normal Version:
Component: Users Keywords: has-patch 2nd-opinion
Focuses: Cc:

Description

"The hackiest hack that ever did hack" was introduced in [3677] as part of #2624 to "[allow] adding new users of any defined role straight from users.php (i.e. without having to create a user of the default role and the promoting to the desired level)".

This seems unnecessary now as wp_insert_user() does this anyway:

if ( isset($role) )
    $user->set_role($role);
elseif ( !$update )
    $user->set_role(get_option('default_role'));

$role being set in edit_user() from $_POST['role'].

Back at revision 3677 this was a different story:

if ($update && !empty($role)) {
    $user = new WP_User($user_id);
    $user->set_role($role);
}
	
if ( !$update ) {
    $user = new WP_User($user_id);
    $user->set_role(get_settings('default_role'));
}

New users would always have the default role set on insert.

The add-user ajax action isn't actually used any more, but I tested the hack removal by using Chromium's JavaScript console to send AJAX requests.

At the very least the documentation should be updated to reflect the actual reasoning for the code rather than just "seems that [it] is for backwards compatibility".

Issue found during testing

The add-user admin-ajax case doesn't set the current screen so several notices are thrown by the list table code:

Notice: Trying to get property of non-object in wp-admin/includes/class-wp-users-list-table.php on line 17
Notice: Trying to get property of non-object in wp-admin/includes/class-wp-list-table.php on line 89
Notice: Trying to get property of non-object in wp-admin/includes/template.php on line 238
Notice: Trying to get property of non-object in wp-admin/includes/template.php on line 239
Notice: Trying to get property of non-object in wp-admin/includes/template.php on line 239
Notice: Trying to get property of non-object in wp-admin/includes/template.php on line 242
Notice: Trying to get property of non-object in wp-admin/includes/template.php on line 257
Notice: Trying to get property of non-object in wp-admin/includes/class-wp-list-table.php on line 602

Attachments (2)

18749.diff (1.6 KB) - added by duck_ 15 years ago.
18749.use-edit_user.diff (1.1 KB) - added by duck_ 14 years ago.

Download all attachments as: .zip

Change History (10)

@duck_
15 years ago

#1 @duck_
15 years ago

See #9891 and [11442] for the change to wp_insert_user().

#2 @scribu
15 years ago

Mmm... hacky.

How about also deprecating add_user()?

#3 @duck_
14 years ago

  • Milestone changed from Awaiting Review to 3.4

#4 @duck_
14 years ago

[19686]

Ryan suggested switching to edit_user() exclusively in core, but leaving add_user() as is for now, see patch.

#5 follow-up: @nacin
14 years ago

I agree.

Grain of salt, but I prefer ! $user_id = edit_user() to ! ( $user_id = edit_user() ), the latter looks like a situation where you're trying to invert what's inside. The former looks like a deliberate assignment and a check against the value of the assignment.

#6 in reply to: ↑ 5 @duck_
14 years ago

  • Resolution set to fixed
  • Status changed from new to closed

Replying to nacin:

Grain of salt, but I prefer ! $user_id = edit_user() to ! ( $user_id = edit_user() ), the latter looks like a situation where you're trying to invert what's inside. The former looks like a deliberate assignment and a check against the value of the assignment.

FWIW I was thinking that somebody might read ! $user_id = edit_user() as "$user_id doesn't equal edit_user()", but parens removed and left as is.

[19689]

#7 follow-up: @scribu
14 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

How about just doing the assignment separately:

$user_id = edit_user();
if ( !$user_id ) {

}

#8 in reply to: ↑ 7 @duck_
14 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

It's fine, but separate assignment is best general.

Note: See TracTickets for help on using tickets.