Skip to content

Commit

Permalink
Fixes bug that prevented user accounts from being created and associa…
Browse files Browse the repository at this point in the history
…ted with a given blog.
  • Loading branch information
Jeremy Boggs committed Mar 9, 2011
1 parent f79bb63 commit fd0ba14
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 28 deletions.
2 changes: 1 addition & 1 deletion thatcamp-registrations-admin.php
Expand Up @@ -38,7 +38,7 @@ function registrations_display() {
if (isset($_POST['update_status'])) {
thatcamp_registrations_process_registration($_GET['id'], $_POST['status']);
if (isset($_POST['user_account']) && $_POST['user_account'] == 1) {
thatcamp_registrations_process_user(null, $applicant);
thatcamp_registrations_process_user($id);
}
}
}
Expand Down
42 changes: 16 additions & 26 deletions thatcamp-registrations-functions.php
Expand Up @@ -11,7 +11,7 @@ function thatcamp_registrations_add_registration($status = 'pending') {
global $wpdb;
$table = $wpdb->prefix . "thatcamp_registrations";

$_POST = stripslashes_deep($_POST);
$_POST = stripslashes_deep($_POST);

// The user_id is set to the posted user ID, or null.
$user_id = isset($_POST['user_id']) ? $_POST['user_id'] : null;
Expand All @@ -34,24 +34,16 @@ function thatcamp_registrations_add_registration($status = 'pending') {
$applicant_info[$field] = isset($_POST[$field]) ? $_POST[$field] : null;
}

// If the user_id is null, we don't have an authenticated user. So, we'll use the applicant_info
if ( $user_id == null ) {
// If there isn't a user_id, but a user exists with the posted email. Sneaky!
if ( $user_id = email_exists($_POST['applicant_email'])) {
thatcamp_registrations_process_user($user_id, $applicant_info);
}
}

$date = isset($_POST['date']) ? $_POST['date'] : null;
$applicationText = isset($_POST['application_text']) ? $_POST['application_text'] : null;
$bootcampSession = isset($_POST['bootcamp_session']) ? $_POST['bootcamp_session'] : null;

// Lets serialize the applicant_info before putting it in the database.
$applicant_info = maybe_serialize($applicant_info);
$applicant_email = isset($_POST['user_email']) ? $_POST['user_email'] : null;

if (($registration = thatcamp_registrations_get_registration_by_user_id($user_id) )
|| ($registration = thatcamp_registrations_get_registration_by_applicant_email($applicant_email)) ) {
if ( $registration = thatcamp_registrations_get_registration_by_user_id($user_id)
|| $registration = thatcamp_registrations_get_registration_by_applicant_email($applicant_email) ) {
return 'You have already submitted an application.';
} else {
$wpdb->insert(
Expand All @@ -66,10 +58,6 @@ function thatcamp_registrations_add_registration($status = 'pending') {
)
);
thatcamp_registrations_send_applicant_email($applicant_email);

// Get and return the application ID
$applicationId = $wpdb->insert_id;
return $applicationId;
}
}

Expand Down Expand Up @@ -121,7 +109,7 @@ function thatcamp_registrations_process_registrations($ids = array(), $status) {
);
if ($status == 'approved' && thatcamp_registrations_create_user_accounts()) {
foreach ($ids as $id) {
thatcamp_registrations_process_user(null, array(), $id);
thatcamp_registrations_process_user($id);
}
}
}
Expand Down Expand Up @@ -151,26 +139,28 @@ function thatcamp_registrations_process_registration($id, $status) {
* @param $registrationId
* @return integer The User ID
**/
function thatcamp_registrations_process_user($userId = null, $userInfo = array(), $registrationId = null, $role = 'author') {
function thatcamp_registrations_process_user($registrationId = null, $role = 'author') {
global $wpdb;

/**
* If the Registration ID is set, it means we already have a registration
* record! Booyah. We'll use the user_id and application_info colums from
* that record to process the user.
*/

if ($registration = thatcamp_registrations_get_registration_by_id($registrationId)) {
$userId = $registration->user_id;

$userInfo = maybe_unserialize($registration->applicant_info);
} else {
// If we pass a User ID, we're probably dealing with an existing user.
if ($userId && !is_user_member_of_blog($userId)) {
$userId = $registration->user_id ? $registration->user_id : email_exists($registration->applicant_email);

// If we have a valid a User ID, we're dealing with an existing user.
if ($userId) {
add_existing_user_to_blog(array('user_id' => $userId, 'role' => $role));
} else if ($userId = email_exists($userInfo->user_email)) {
thatcamp_registrations_update_user_data($userId, $userInfo);
} else { // We're probably dealing with a new user. Lets create one and associate it to our blog.
}
// We're probably dealing with a new user. Lets create one and associate it to our blog.
else {
$randomPassword = wp_generate_password( 12, false );
$userEmail = $userInfo->user_email;
$userEmail = $registration->applicant_email;
$userId = wp_create_user( $userEmail, $randomPassword, $userEmail );
add_user_to_blog($wpdb->blogid, $userId, $role);
thatcamp_registrations_update_user_data($userId, $userInfo);
Expand Down
2 changes: 1 addition & 1 deletion thatcamp-registrations-public-registration.php
Expand Up @@ -75,7 +75,7 @@ function display_registration() {
$this->_application_form();

// If user login is not required, display the user info form.
if ( !thatcamp_registrations_user_required() ) {
if ( !thatcamp_registrations_user_required() && !is_user_logged_in()) {
$this->_user_info_form();
} elseif (is_user_logged_in()) {
echo '<input type="hidden" name="user_id" value="'. $this->current_user->ID .'" />';
Expand Down

0 comments on commit fd0ba14

Please sign in to comment.