Website Summary Files Commits Branches @commit @diff @stats
path: root / wp-content / plugins / dx-users / register-user-class.php
blob: bbadf3cd4e3bdd5ed52727919d605bb541e93a00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
class RegisterUser {
    function init() {
        add_action( 'admin_post_nopriv_process_register', array( $this, 'process_register_data' ));
    }

    function process_register_data() {
        if( username_exists( sanitize_text_field( $_POST['password'] ) ) ) {
            wp_die( 'User already exists!' );
        }

        $user_data = array(
            'user_pass'    => sanitize_text_field( $_POST['password'] ),
            'user_login'   => sanitize_text_field( $_POST['email'] ),
            'user_email'   => sanitize_text_field( $_POST['email'] ),
            'display_name' => sanitize_text_field( $_POST['name'] ),
        );

        if( ! is_email( $user_data['user_email'] ) ) {
            wp_die( 'Invaid email address!' );
        }

        $user_id = wp_insert_user( $user_data );

        if( is_wp_error( $user_id ) ) {
            wp_die( $user_id->get_error_message() );
        }

        wp_redirect( home_url() ); exit;
    }
}