Skip to content
Snippets Groups Projects
Commit 633d5122 authored by bgm's avatar bgm Committed by bgm
Browse files

wordpress#82 Add support for creating WordPress accounts from the contact record

Also removes the possibility to set a user-defined password for WordPress users.
parent 8257f280
No related branches found
No related tags found
No related merge requests found
......@@ -64,14 +64,20 @@ class CRM_Contact_Form_Task_Useradd extends CRM_Core_Form {
* Build the form object.
*/
public function buildQuickForm() {
$config = CRM_Core_Config::singleton();
$element = $this->add('text', 'name', ts('Full Name'), ['class' => 'huge']);
$element->freeze();
$this->add('text', 'cms_name', ts('Username'), ['class' => 'huge']);
$this->addRule('cms_name', 'Username is required', 'required');
$this->add('password', 'cms_pass', ts('Password'), ['class' => 'huge']);
$this->add('password', 'cms_confirm_pass', ts('Confirm Password'), ['class' => 'huge']);
$this->addRule('cms_pass', 'Password is required', 'required');
$this->addRule(['cms_pass', 'cms_confirm_pass'], 'ERROR: Password mismatch', 'compare');
if (!$config->userSystem->isUserRegistrationPermitted()) {
$this->add('password', 'cms_pass', ts('Password'), ['class' => 'huge']);
$this->add('password', 'cms_confirm_pass', ts('Confirm Password'), ['class' => 'huge']);
$this->addRule('cms_pass', 'Password is required', 'required');
$this->addRule(['cms_pass', 'cms_confirm_pass'], 'ERROR: Password mismatch', 'compare');
}
$this->add('text', 'email', ts('Email:'), ['class' => 'huge'])->freeze();
$this->addRule('email', 'Email is required', 'required');
$this->add('hidden', 'contactID');
......
......@@ -765,7 +765,6 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
public function createUser(&$params, $mail) {
$user_data = [
'ID' => '',
'user_pass' => $params['cms_pass'],
'user_login' => $params['cms_name'],
'user_email' => $params[$mail],
'nickname' => $params['cms_name'],
......@@ -783,15 +782,22 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
}
}
$this->hooks_core_remove();
$uid = wp_insert_user($user_data);
$creds = [];
$creds['user_login'] = $params['cms_name'];
$creds['user_password'] = $params['cms_pass'];
$creds['remember'] = TRUE;
$user = wp_signon($creds, FALSE);
wp_new_user_notification($uid, $user_data['user_pass']);
// Call wp_signon if we aren't already logged in
// For example, we might be creating a new user from the Contact record.
if (!current_user_can('create_users')) {
wp_signon($creds, FALSE);
}
do_action('register_new_user', $uid);
$this->hooks_core_add();
return $uid;
}
......@@ -870,7 +876,7 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
* @inheritDoc
*/
public function isPasswordUserGenerated() {
return TRUE;
return FALSE;
}
/**
......@@ -990,6 +996,13 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
}
}
/**
* @inheritDoc
*/
public function checkPermissionAddUser() {
return current_user_can('create_users');
}
/**
* @inheritDoc
*/
......@@ -1252,4 +1265,38 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
return ['ufAccessURL' => CRM_Utils_System::url('civicrm/admin/access/wp-permissions', 'reset=1')];
}
/**
* Remove CiviCRM's callbacks.
*
* These may cause recursive updates when creating or editing a WordPress
* user. This doesn't seem to have been necessary in the past, but seems
* to be causing trouble when newer versions of BuddyPress and CiviCRM are
* active.
*
* Based on the civicrm-wp-profile-sync plugin by Christian Wach.
*
* @see self::hooks_core_add()
*/
public function hooks_core_remove() {
$civicrm = civi_wp();
// Remove current CiviCRM plugin filters.
remove_action('user_register', [$civicrm->users, 'update_user']);
remove_action('profile_update', [$civicrm->users, 'update_user']);
}
/**
* Add back CiviCRM's callbacks.
* This method undoes the removal of the callbacks above.
*
* @see self::hooks_core_remove()
*/
public function hooks_core_add() {
$civicrm = civi_wp();
// Re-add current CiviCRM plugin filters.
add_action('user_register', [$civicrm->users, 'update_user']);
add_action('profile_update', [$civicrm->users, 'update_user']);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment