<?php
/**
 * Template Name: Register
 *
 * Allow users to register from any page on your site.
 * Note that this template is under heavy development and may change drastically in the future.
 * @link http://themehybrid.com/themes/hybrid/page-templates/register
 *
 * @package Hybrid
 * @subpackage Template
 */

/* Load registration file. */
require_once( ABSPATH . WPINC . '/registration.php' );

/* Check if users can register. */
$registration = get_option( 'users_can_register' );

/* If user registered, input info. */
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action']) && $_POST['action'] == 'adduser' ) :

	$userdata = array(
		'user_pass' => $_POST['pass1'],
		'user_login' => $_POST['user-name'],
		'user_url' => $_POST['url'],
		'user_email' => $_POST['email'],
		'first_name' => $_POST['first-name'],
		'last_name' => $_POST['last-name'],
		'description' => $_POST['description'],
		'role' => get_option( 'default_role' ),
	);

	if ( ($_POST['user-name'] ) && ( $_POST['email'] ) && ( $_POST['pass1'] ) && ( $_POST['pass1'] == $_POST['pass2'] ) ) :
		$new_user = wp_insert_user( $userdata );
	else :
		if ( !$_POST['user-name'] )
			$error = __('A username is required for registration.', 'hybrid');
		elseif ( !$_POST['email'] )
			$error = __('You must enter an email address.', 'hybrid');
		elseif ( !$_POST['pass1'] )
			$error = __('You must enter a password.', 'hybrid');
		elseif ( !$_POST['pass2'] )
			$error = __('You must enter your password twice.', 'hybrid');
		elseif ( $_POST['pass1'] !== $_POST['pass2'] )
			$error = __('You must enter the same password twice.', 'hybrid');
		else
			$error = __('User registration failed.  Please try again.', 'hybrid');
	endif;

endif;

get_header(); ?>

	<div class="hfeed content">

		<?php hybrid_before_content(); // Before content hook ?>

		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

			<div id="post-<?php the_ID(); ?>" class="<?php hybrid_entry_class(); ?>">

				<?php hybrid_before_entry(); // Before entry hook ?>

				<div class="entry-content entry">

					<?php the_content(); ?>

					<?php if ( is_user_logged_in() && !current_user_can( 'create_users' ) ) : ?>

						<p class="log-in-out alert">
						<?php printf( __('You are logged in as <a href="%1$s" title="%2$s">%2$s</a>.  You don\'t need another account.', 'hybrid'), get_author_posts_url( $curauth->ID ), $user_identity ); ?> <a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="<?php _e('Log out of this account', 'hybrid'); ?>"><?php _e('Logout &raquo;', 'hybrid'); ?></a>
						</p><!-- .log-in-out .alert -->

					<?php elseif ( $new_user ) : ?>

						<p class="alert">
						<?php
							if ( current_user_can( 'create_users' ) )
								printf( __('A user account for %1$s has been created.', 'hybrid'), $_POST['user-name'] );
							else 
								printf( __('Thank you for registering, %1$s.', 'hybrid'), $_POST['user-name'] );
						?>
						</p><!-- .alert -->

					<?php else : ?>

						<?php if ( $error ) : ?>
							<p class="error">
								<?php echo $error; ?>
							</p><!-- .error -->
						<?php endif; ?>

						<?php if ( current_user_can( 'create_users' ) && $registration ) : ?>
							<p class="alert">
								<?php _e('Users can register themselves or you can manually create users here.', 'hybrid'); ?>
							</p><!-- .alert -->
						<?php elseif ( current_user_can( 'create_users' ) ) : ?>
							<p class="alert">
								<?php _e('Users cannot currently register themselves, but you can manually create users here.', 'hybrid'); ?>
							</p><!-- .alert -->
						<?php endif; ?>

						<?php if ( $registration || current_user_can( 'create_users' ) ) : ?>

						<form method="post" id="adduser" action="http://<?php echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>">

						<p class="form-required">
							<?php _e('Required fields are marked <span class="required">*</span>', 'hybrid'); ?>
						</p><!-- .form-required -->

						<p class="form-username">
							<label for="user-name"><?php _e('Username *', 'hybrid'); ?></label>
							<input class="text-input" name="user-name" type="text" id="user-name" value="<?php if ( $error ) echo wp_specialchars( $_POST['user-name'], 1 ); ?>" />
						</p><!-- .form-username -->

						<p class="form-username">
							<label for="first-name"><?php _e('First Name', 'hybrid'); ?></label>
							<input class="text-input" name="first-name" type="text" id="first-name" value="<?php if ( $error ) echo wp_specialchars( $_POST['first-name'], 1 ); ?>" />
						</p><!-- .form-username -->

						<p class="form-username">
							<label for="last-name"><?php _e('Last Name', 'hybrid'); ?></label>
							<input class="text-input" name="last-name" type="text" id="last-name" value="<?php if ( $error ) echo wp_specialchars( $_POST['last-name'], 1 ); ?>" />
						</p><!-- .form-username -->

						<p class="form-email">
							<label for="email"><?php _e('E-mail *', 'hybrid'); ?></label>
							<input class="text-input" name="email" type="text" id="email" value="<?php if ( $error ) echo wp_specialchars( $_POST['email'], 1 ); ?>" />
						</p><!-- .form-email -->

						<p class="form-url">
							<label for="url"><?php _e('Website', 'hybrid'); ?></label>
							<input class="text-input" name="url" type="text" id="url" value="<?php if ( $error ) echo wp_specialchars($_POST['url'], 1 ); ?>" />
						</p><!-- .form-email -->

						<?php if ( apply_filters( 'show_password_fields', true ) ) : ?>

							<p class="form-password">
								<label for="pass1"><?php _e('Password *', 'hybrid'); ?> </label>
								<input class="text-input" name="pass1" type="password" id="pass1" />
							</p><!-- .form-password -->

							<p class="form-password">
								<label for="pass2"><?php _e('Repeat Password *', 'hybrid'); ?></label>
								<input class="text-input" name="pass2" type="password" id="pass2" />
							</p><!-- .form-password -->

						<?php endif; ?>

						<p class="form-textarea">
							<label for="description"><?php _e('Biographical Information', 'hybrid') ?></label>
							<textarea name="description" id="description" rows="3" cols="50"><?php if( $error ) echo wp_specialchars( $_POST['description'], 1 ); ?></textarea>
						</p><!-- .form-textarea -->

						<p class="form-submit">
							<?php echo $referer; ?>
							<input name="adduser" type="submit" id="addusersub" class="submit button" value="<?php if ( current_user_can( 'create_users' ) ) _e('Add User', 'hybrid'); else _e('Register', 'hybrid'); ?>" />
							<?php wp_nonce_field( 'add-user' ) ?>
							<input name="action" type="hidden" id="action" value="adduser" />
						</p><!-- .form-submit -->

						</form><!-- #adduser -->

						<?php endif; ?>

					<?php endif; ?>

					<?php wp_link_pages( array( 'before' => '<p class="pages">' . __('Pages:', 'hybrid'), 'after' => '</p>' ) ); ?>

				</div><!-- .entry-content -->

				<?php hybrid_after_entry(); // After entry hook ?>

			</div><!-- .hentry .post -->

			<?php endwhile; ?>

			<?php hybrid_after_page(); // After page hook ?>

			<?php comments_template( '', true ); ?>

		<?php else: ?>

			<p class="no-data">
				<?php _e('Sorry, no page matched your criteria.', 'hybrid'); ?>
			</p><!-- .no-data -->

		<?php endif; ?>

		<?php hybrid_after_content(); // After content hook ?>

	</div><!-- .content .hfeed -->

<?php get_footer(); ?>