Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created December 19, 2018 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/5c02b56fb7f9d28b01d8afc5f574dc4b to your computer and use it in GitHub Desktop.
Save wpmudev-sls/5c02b56fb7f9d28b01d8afc5f574dc4b to your computer and use it in GitHub Desktop.
[CoursePress] - User Signup with ajax. Changes the `course_signup` shortcode to register users with ajax
<?php
/**
* Plugin Name: [CoursePress] - User Signup with ajax
* Plugin URI: https://premium.wpmudev.org/
* Description: Changes the `course_signup` shortcode to register users with ajax
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_CP_UserSignup_Ajax' ) ) {
class WPMUDEV_CP_UserSignup_Ajax {
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_CP_UserSignup_Ajax();
}
return self::$_instance;
}
private function __construct() {
if ( ! class_exists( 'CoursePress_UserLogin' ) ) {
return;
}
add_action( 'coursepress_after_signup_form', array( $this, 'plant_inputs' ) );
add_action( 'wp_footer', array( $this, 'footer_scripts' ) );
add_action( 'wp_ajax__cp_ajax_signup', array( $this, 'cp_ajax_signup' ) );
add_action( 'wp_ajax_nopriv__cp_ajax_signup', array( $this, 'cp_ajax_signup' ) );
}
public function cp_ajax_signup() {
check_ajax_referer( '_cp_ajax_signup', '_cp_ajax_signup' );
$_POST['student-settings-submit'] = '1';
wp_send_json( $this->process_registration_form() );
}
public function process_registration_form() {
if ( isset( $_POST['student-settings-submit'] ) && isset( $_POST['_wpnonce'] )
&& wp_verify_nonce( $_POST['_wpnonce'], 'student_signup' ) ) {
check_admin_referer( 'student_signup' );
/**
* Trigger before validating registration form
**/
do_action( 'coursepress_before_signup_validation' );
$min_password_length = CoursePress_Helper_Utility::get_minimum_password_length();
$username = $_POST['username'];
$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$email = $_POST['email'];
$passwd = $_POST['password'];
$passwd2 = $_POST['password_confirmation'];
$redirect_url = $_POST['redirect_url'];
$found_errors = 0;
$form_message = '';
$form_message_class = '';
if ( $username && $firstname && $lastname && $email && $passwd && $passwd2 ) {
if ( username_exists( $username ) ) {
$form_message = __( 'Username already exists. Please choose another one.', 'coursepress' );
$found_errors++;
} elseif ( ! validate_username( $username ) ) {
$form_message = __( 'Invalid username!', 'coursepress' );
$found_errors++;
} elseif ( ! is_email( $email ) ) {
$form_message = __( 'E-mail address is not valid.', 'coursepress' );
$found_errors++;
} elseif ( email_exists( $email ) ) {
$form_message = __( 'Sorry, that email address is already used!', 'coursepress' );
$found_errors++;
} elseif ( $passwd != $passwd2 ) {
$form_message = __( 'Passwords don\'t match', 'coursepress' );
$found_errors++;
} elseif ( ! CoursePress_Helper_Utility::is_password_strong() ) {
if ( CoursePress_Helper_Utility::is_password_strength_meter_enabled() ) {
$form_message = __( 'Your password is too weak.', 'coursepress' );
} else {
$form_message = sprintf( __( 'Your password must be at least %d characters long and have at least one letter and one number in it.', 'coursepress' ), $min_password_length );
}
$found_errors++;
} elseif ( isset( $_POST['tos_agree'] ) && ! cp_is_true( $_POST['tos_agree'] ) ) {
$form_message = __( 'You must agree to the Terms of Service in order to signup.', 'coursepress' );
$found_errors++;
}
} else {
$form_message = __( 'All fields are required.', 'coursepress' );
$found_errors++;
}
if ( $found_errors > 0 ) {
$form_message_class = 'red';
return array(
'success' => false,
'message' => $form_message,
'class' => $form_message_class
);
} else {
// Register new user
$student_data = array(
'default_role' => get_option( 'default_role', 'subscriber' ),
'user_login' => $username,
'user_email' => $email,
'first_name' => $firstname,
'last_name' => $lastname,
'user_pass' => $passwd,
'password_txt' => $passwd,
);
$student_data = CoursePress_Helper_Utility::sanitize_recursive( $student_data );
$student_id = wp_insert_user( $student_data );
if ( ! empty( $student_id ) ) {
// Send registration email
CoursePress_Data_Student::send_registration( $student_id, $student_data );
$creds = array(
'user_login' => $username,
'user_password' => $passwd,
'remember' => true,
);
$user = wp_signon( $creds, false );
if ( is_wp_error( $user ) ) {
return array(
'success' => false,
'message' => $user->get_error_message(),
'class' => 'red'
);
}
if ( ! empty( $_POST['course_id'] ) ) {
$url = get_permalink( (int) $_POST['course_id'] );
//wp_safe_redirect( $url );
return array(
'success' => true,
'redirect_url' => $url
);
} else {
if ( ! empty( $redirect_url ) ) {
$url = esc_url_raw( apply_filters( 'coursepress_redirect_after_signup_redirect_url', $redirect_url ) );
} else {
$url = esc_url_raw( apply_filters( 'coursepress_redirect_after_signup_url', CoursePress_Core::get_slug( 'student_dashboard', true ) ) );
}
return array(
'success' => true,
'redirect_url' => $url
);
}
} else {
return array(
'success' => false,
'message' => $user->get_error_message(),
'class' => 'red'
);
}
}
}
return array(
'success' => false,
'message' => 'Something went wrong'
);
}
public function footer_scripts() {
global $post;
if ( ! $post instanceof WP_Post || ! has_shortcode( $post->post_content, 'course_signup' ) ) {
return;
}
?>
<script type="text/javascript">
(function($){
$( document ).ready(function(){
$( '#student-settings .submit-link input' ).on( 'click', function(e){
e.preventDefault();
var form = $( '#student-settings' ),
data = form.serialize(),
ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
$.post( ajaxurl, data, function( response ) {
if ( ! response.success ) {
let error_msg = response.message,
error_class = response.class,
el = $( '.coursepress-form-signup p[class^="form-info-"]' );
el.removeAttr('class').addClass( 'form-info-' + error_class ).html( error_msg );
}
else {
if ( typeof response.redirect_url !== "undefined" ){
window.location = response.redirect_url;
}
else {
location.reload();
}
}
});
});
});
})(jQuery);
</script>
<?php
}
public function plant_inputs() {
echo '<input type="hidden" name="action" value="_cp_ajax_signup">';
wp_nonce_field( '_cp_ajax_signup', '_cp_ajax_signup' );
}
}
if ( ! function_exists( 'wpmudev_cp_usersignup_ajax' ) ) {
function wpmudev_cp_usersignup_ajax() {
return WPMUDEV_CP_UserSignup_Ajax::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_cp_usersignup_ajax', 10 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment