/** * Adds a single registration entry. This is a motley function. * * @param string The status of the registration record. **/ function thatcamp_registrations_add_registration($status = 'pending') { global $wpdb; $table = $wpdb->prefix . "thatcamp_registrations"; $_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; $applicant_info = array(); $applicant_fields = wp_list_pluck(thatcamp_registrations_fields(), 'id'); foreach ($applicant_fields as $field) { $applicant_info[$field] = isset($_POST[$field]) ? $_POST[$field] : null; } $date = isset($_POST['date']) ? $_POST['date'] : null; $applicationText = isset($_POST['application_text']) ? $_POST['application_text'] : 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; // Check for an existing registration $user_exists = false; if (!is_null($user_id) && thatcamp_registrations_get_registration_by_user_id($user_id) || thatcamp_registrations_get_registration_by_applicant_email($applicant_email)) { $user_exists = true; } if ($user_exists) { return 'You have already submitted your registration.'; } else { $reg_id = $wpdb->insert($table, array('applicant_info' => $applicant_info, 'applicant_email' => $applicant_email, 'application_text' => $applicationText, 'status' => $status, 'date' => $date, 'user_id' => $user_id)); thatcamp_registrations_send_applicant_email($applicant_email); thatcamp_registrations_send_admin_notification($wpdb->insert_id); } }
/** * Displays the registration information on the public site. * * @todo - Refactor most of the logic for checking whether to display the * user and registration forms. **/ function display_registration() { $alerts = array(); // Define some alerts if there are errors with the form. if (!empty($_POST)) { // Registration text is required. if (empty($_POST['application_text'])) { $alerts['application_text'] = __('Please tell us why you want to come to THATCamp. What you write here will NOT be publicly displayed.', 'thatcamp-registrations'); } if (!empty($_POST['tcppl-field'])) { $alerts['spammer'] = __("It looks like you filled in the spammer field. No account for you!", 'thatcamp-registrations'); } // User email is required. if (!is_user_logged_in()) { if (empty($_POST['first_name'])) { $alerts['application_text'] = __('You must add a first name.', 'thatcamp-registrations'); } if (empty($_POST['last_name'])) { $alerts['application_text'] = __('You must add a last name.', 'thatcamp-registrations'); } if (empty($_POST['user_email'])) { $alerts['user_email'] = __('You must add an email address.', 'thatcamp-registrations'); } $email = $_POST['user_email']; $the_user = get_user_by('email', $email); $is_an_admin = is_a($the_user, 'WP_User') && user_can($the_user, 'manage_options'); if ($is_an_admin) { $alerts['user_email'] = __('You cannot register the email address of a site administrator.', 'thatcamp-registrations'); } if (empty($_POST['description'])) { $alerts['description'] = __('You must provide a biography', 'thatcamp-registrations'); } } $userEmail = is_user_logged_in() ? $this->current_user->user_email : @$_POST['user_email']; if ($existingApp = thatcamp_registrations_get_registration_by_applicant_email($userEmail)) { $alerts['existing_application'] = __('You have already submitted the form with that email address.', 'thatcamp-registrations'); } } // If user registration is required, and the user isn't logged in. if (thatcamp_registrations_user_required() && !is_user_logged_in()) { echo '<div>You must have a user account to complete the form. Please <a href="<?php echo wp_login_url( get_permalink() ); ?>" title="Login">log in</a>.</div>'; } elseif (is_user_logged_in() && ($existingApp = thatcamp_registrations_get_registration_by_user_id($this->current_user->ID))) { echo '<div>' . __('You have already submitted the form.', 'thatcamp-registrations') . '</div>'; } elseif (!empty($_POST) && empty($alerts)) { thatcamp_registrations_add_registration(); echo '<p>The information you submitted has been saved.</p>'; } else { if (!empty($alerts)) { foreach ($alerts as $alert) { echo '<p style="background:#fc0; padding: 4px;">' . $alert . '</p>'; } } $login_link = add_query_arg('redirect_to', wp_guess_url(), wp_login_url()); // Nudge the user to log in if (!is_user_logged_in()) { echo "<h3>" . __("Already have a THATCamp account?", 'thatcamp-registrations') . "</h3>"; echo "<p>" . sprintf(__("If you've attended a THATCamp in the past, you probably already have an account on thatcamp.org. <a href='%s'>Log in</a> and we'll pre-fill some of your information for you.", 'thatcamp-registrations'), $login_link) . "</p>"; } else { echo "<h3>" . __("Welcome back!", 'thatcamp-registrations') . "</h3>"; echo "<p>" . sprintf(__('You are logged in as <strong>%1$s</strong>, using the the email address <strong>%2$s</strong>', 'thatcamp-registrations'), $this->current_user->display_name, $this->current_user->user_email) . "</p>"; } echo '<form method="post" action="">'; $this->_application_form(); $this->_user_info_form(); if (is_user_logged_in()) { echo '<input type="hidden" name="user_id" value="' . $this->current_user->ID . '" />'; echo '<input type="hidden" name="user_email" value="' . $this->current_user->user_email . '" />'; } echo '<input type="submit" name="thatcamp_registrations_save_registration" value="' . __('Submit Registration', 'thatcamp-registrations') . '" />'; echo '</form>'; } }