Skip to content

Instantly share code, notes, and snippets.

@benlumley
Created March 12, 2014 22:18
Show Gist options
  • Save benlumley/9517689 to your computer and use it in GitHub Desktop.
Save benlumley/9517689 to your computer and use it in GitHub Desktop.
Bits from events manager for registering a new user behind the scenes with a generated pw, then emailing them
<?php
/**
* Decides whether to register a user based on a certain booking that is to be added
* @param EM_Booking $EM_Booking
*/
function em_booking_add_registration( $EM_Booking ){
global $EM_Notices;
//Does this user need to be registered first?
$registration = true;
if( ((!is_user_logged_in() && get_option('dbem_bookings_anonymous')) || defined('EM_FORCE_REGISTRATION')) && !get_option('dbem_bookings_registration_disable') ){
//find random username - less options for user, less things go wrong
$username_root = explode('@', wp_kses_data($_REQUEST['user_email']));
$username_root = $username_rand = sanitize_user($username_root[0], true);
while( username_exists($username_rand) ) {
$username_rand = $username_root.rand(1,1000);
}
$_REQUEST['dbem_phone'] = (!empty($_REQUEST['dbem_phone'])) ? wp_kses_data($_REQUEST['dbem_phone']):''; //fix to prevent warnings
$_REQUEST['user_name'] = (!empty($_REQUEST['user_name'])) ? wp_kses_data($_REQUEST['user_name']):''; //fix to prevent warnings
$user_data = array('user_login' => $username_rand, 'user_email'=> $_REQUEST['user_email'], 'user_name'=> $_REQUEST['user_name'], 'dbem_phone'=> $_REQUEST['dbem_phone']);
$id = em_register_new_user($user_data);
if( is_numeric($id) ){
$EM_Person = new EM_Person($id);
$EM_Booking->person_id = $id;
$feedback = get_option('dbem_booking_feedback_new_user');
$EM_Notices->add_confirm( $feedback );
add_action('em_bookings_added', 'em_new_user_notification');
}else{
$registration = false;
if( is_object($id) && get_class($id) == 'WP_Error'){
/* @var $id WP_Error */
if( $id->get_error_code() == 'email_exists' ){
$EM_Notices->add_error( get_option('dbem_booking_feedback_email_exists') );
}else{
$EM_Notices->add_error( $id->get_error_messages() );
}
}else{
$EM_Notices->add_error( get_option('dbem_booking_feedback_reg_error') );
}
}
}elseif( (!is_user_logged_in() || defined('EM_FORCE_REGISTRATION')) && get_option('dbem_bookings_registration_disable') ){
//Validate name, phone and email
if( $EM_Booking->get_person_post() ){
//Save default person to booking
$EM_Booking->person_id = get_option('dbem_bookings_registration_user');
}else{
$registration = false;
}
}elseif( !is_user_logged_in() ){
$registration = false;
$EM_Notices->add_error( get_option('dbem_booking_feedback_log_in') );
}elseif( empty($EM_Booking->person_id) ){ //user must be logged in, so we make this person the current user id
$EM_Booking->person_id = get_current_user_id();
}
return apply_filters('em_booking_add_registration_result', $registration, $EM_Booking, $EM_Notices);
}
/**
* Copied straight from wp-login.php, only change atm is a function renaming.
* Handles registering a new user.
*
* @param array associative array of user values to insert
* @return int|WP_Error Either user's ID or error on failure.
*/
function em_register_new_user( $user_data ) {
$user_data = apply_filters('em_register_new_user_pre',$user_data);
$errors = new WP_Error();
if( !empty($user_data['user_name']) ){
$name = explode(' ', $user_data['user_name']);
$user_data['first_name'] = array_shift($name);
$user_data['last_name'] = implode(' ',$name);
}
$sanitized_user_login = sanitize_user( $user_data['user_login'] );
$user_data['user_login'] = $sanitized_user_login;
$user_email = apply_filters( 'user_registration_email', $user_data['user_email'] );
// Check the username
if ( $sanitized_user_login == '' ) {
$errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.', 'dbem') );
} elseif ( ! validate_username( $user_data['user_login'] ) ) {
$errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.', 'dbem') );
$sanitized_user_login = '';
} elseif ( username_exists( $sanitized_user_login ) ) {
$errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.', 'dbem' ) );
}
// Check the e-mail address
if ( $user_email == '' ) {
$errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.', 'dbem') );
} elseif ( ! is_email( $user_email ) ) {
$errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.', 'dbem') );
$user_email = '';
} elseif ( email_exists( $user_email ) ) {
$errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.', 'dbem' ) );
}
do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
//custom registration filter to prevent things like SI Captcha and other plugins of this kind interfering with EM
$errors = apply_filters( 'em_registration_errors', $errors, $sanitized_user_login, $user_email );
if ( $errors->get_error_code() ) return $errors;
if(empty($user_data['user_pass'])){
$user_data['user_pass'] = wp_generate_password( 12, false);
}
$user_id = wp_insert_user( $user_data );
if( is_numeric($user_id) && !empty($user_data['dbem_phone']) ){
update_user_meta($user_id, 'dbem_phone', $user_data['dbem_phone']);
}
if ( ! $user_id ) {
$errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !', 'dbem'), get_option( 'admin_email' ) ) );
return $errors;
}
update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.
global $em_temp_user_data;
$em_temp_user_data = $user_data; //for later useage
$em_temp_user_data['user_id'] = $user_id;
return apply_filters('em_register_new_user',$user_id);
}
/**
* Notify the blog admin of a new user, normally via email.
*
* @since 2.0
*/
function em_new_user_notification() {
global $em_temp_user_data;
$user_id = $em_temp_user_data['user_id'];
$plaintext_pass = $em_temp_user_data['user_pass'];
//if you want you can disable this email from going out, and will still consider registration as successful.
if( get_option('dbem_email_disable_registration') ){ return true; }
//Copied out of /wp-includes/pluggable.php
$user = new WP_User($user_id);
$user_login = stripslashes($user->user_login);
$user_email = stripslashes($user->user_email);
// The blogname option is escaped with esc_html on the way into the database in sanitize_option
// we want to reverse this for the plain text arena of emails.
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$message = sprintf(__('New user registration on your blog %s:', 'dbem'), $blogname) . "\r\n\r\n";
$message .= sprintf(__('Username: %s', 'dbem'), $user_login) . "\r\n\r\n";
$message .= sprintf(__('E-mail: %s', 'dbem'), $user_email) . "\r\n";
@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration', 'dbem'), $blogname), $message);
if ( empty($plaintext_pass) )
return;
//send email to user
$message = get_option('dbem_bookings_email_registration_body');
if( em_locate_template('emails/new-user.php') ){
ob_start();
em_locate_template('emails/new-user.php', true);
$message = ob_get_clean();
}
$message = str_replace(array('%password%','%username%'), array($plaintext_pass, $user_login), $message);
global $EM_Mailer;
return $EM_Mailer->send(get_option('dbem_bookings_email_registration_subject'), $message, $user_email);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment