Created
June 23, 2013 15:03
-
-
Save josesayago/5845333 to your computer and use it in GitHub Desktop.
GlotPress custom route for Register page, gp-includes/routes/register.php.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Registration Class | |
* | |
* Allow users to create new accounts | |
*/ | |
class GP_Route_Register extends GP_Route_Main { | |
// GET variables | |
function register_get() { | |
// If user is already logged in | |
if ( GP::$user->logged_in() ) { | |
// Send him to homepage | |
$this->redirect( gp_url( '/' ) ); | |
return; | |
} | |
// Load template | |
gp_tmpl_load( 'register', array() ); | |
} | |
// POST variables | |
function register_post() { | |
global $wp_users_object, $wp_auth_object; | |
require_once( GP_PLUGINS_PATH . '/recaptcha/recaptchalib.php'); | |
// Replace $privatekey with your own | |
// Get it from https://www.google.com/recaptcha/admin/ | |
$privatekey = "your private key"; | |
$resp = recaptcha_check_answer ($privatekey, | |
$_SERVER["REMOTE_ADDR"], | |
$_POST["recaptcha_challenge_field"], | |
$_POST["recaptcha_response_field"]); | |
if ( !$resp->is_valid ) { | |
// What happens when the CAPTCHA was entered incorrectly | |
$this->errors[] = __("Yay! are you a bot? Please enter the CAPTCHA code"); | |
$this->redirect( gp_url_register() ); | |
return; | |
} | |
// Check for full name | |
if( empty( $_POST['user_nicename'] ) ) { | |
$this->errors[] = __("Please tell us your full name"); | |
$this->redirect( gp_url_register() ); | |
return; | |
} else { | |
$_POST['user_nicename'] = htmlspecialchars( $_POST['user_nicename'] ); | |
} | |
// Check for username | |
if( empty( $_POST['user_login'] ) ) { | |
$this->errors[] = __("Please choose a username"); | |
$this->redirect( gp_url_register() ); | |
return; | |
} | |
// Check for email | |
if( empty( $_POST['user_email'] ) ) { | |
$this->errors[] = __("We need your email address"); | |
$this->redirect( gp_url_register() ); | |
return; | |
} | |
// Check for password | |
if( empty( $_POST['user_pass'] ) ) { | |
$this->errors[] = __("Please set a password for your account"); | |
$this->redirect( gp_url_register() ); | |
return; | |
} | |
// Get username by login | |
$user = GP::$user->by_login( strtolower( htmlspecialchars( $_POST['user_login'] ) ) ); | |
// Get user by email | |
$user_email = GP::$user->by_email( strtolower( htmlspecialchars( $_POST['user_email'] ) ) ); | |
// If username exists | |
if ( $user || is_wp_error($user) ) { | |
// Ask for a new one | |
$this->errors[] = __("Sorry that username is already taken, please pick a new one."); | |
$this->redirect( gp_url_register() ); | |
return; | |
} | |
// If email exists | |
if( $user_email || is_wp_error($email) ) { | |
// Ask for a new one | |
$this->errors[] = __("That email is already registered, please log in."); | |
$this->redirect( gp_url_login() ); | |
return; | |
} | |
$args = array(); | |
$args['user_login'] = htmlspecialchars( $_POST['user_login'] ); | |
$args['user_pass'] = htmlspecialchars( $_POST['user_pass'] ); | |
$args['user_nicename'] = htmlspecialchars( $_POST['user_nicename'] ) ; | |
$args['display_name'] = htmlspecialchars( $_POST['user_nicename'] ) ; | |
$args['user_email'] = htmlspecialchars( $_POST['user_email'] ); | |
$new_user = GP::$user->create( $args ); | |
if ( !$new_user ) { | |
$this->errors[] = __("Sorry that didn't work, please try again"); | |
$this->redirect( gp_url_register() ); | |
return; | |
} else { | |
$this->errors[] = __("Your account has been created, please log in."); | |
$this->redirect( gp_url_login() ); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment