/**
  * Create a new user registration for UPME
  *
  * @access public
  * @since 1.0
  * @uses  handle_social_error,upme_new_user_notification,validate_email_confirmation
  *		  automatic_user_login, redirect_registered_users
  * @param object $result Result object created from the response generated from social network
  *		  This object will contain all the user profile data requested by the application
  * @return void 
  */
 public function register_user($result)
 {
     global $upme_register;
     /*  Check for succefull registration or login */
     if ($result->status) {
         if ($result->upme_network_type != 'twitter') {
             $user = get_user_by('email', $result->email);
         } else {
             $user = get_user_by('login', $result->username);
         }
         if (!$user) {
             /* Generate a custom username using the combination of first and last names plus a random
              * number for preventing duplication.
              */
             if ($result->upme_network_type != 'twitter') {
                 $username = strtolower($result->first_name . $result->last_name);
                 if (trim($username) == '') {
                     $username = $result->email;
                 }
                 if (username_exists($username)) {
                     $username = $username . rand(10, 99);
                 }
             } else {
                 $username = $result->username;
             }
             $sanitized_user_login = sanitize_user($username);
             /* Generate password */
             $user_pass = wp_generate_password(12, false);
             /* Create the new user */
             $user_id = wp_create_user($sanitized_user_login, $user_pass, $result->email);
             if (!is_wp_error($user_id)) {
                 update_user_meta($user_id, 'user_email', $result->email);
                 update_user_meta($user_id, 'upme_network_type', $result->upme_network_type);
                 wp_update_user(array('ID' => $user_id, 'display_name' => $result->first_name . ' ' . $result->last_name));
             }
             // Set intial details for users
             $user_info = get_userdata($user_id);
             update_user_meta($user_id, 'first_name', $result->first_name);
             update_user_meta($user_id, 'last_name', $result->last_name);
             update_user_meta($user_id, 'display_name', $result->first_name . ' ' . $result->last_name);
             $activation_status = 'ACTIVE';
             update_user_meta($user_id, 'upme_activation_status', "ACTIVE");
             // Set approval status when user profile approvals are enabled
             $approval_setting_status = $upme_register->validate_user_approval();
             if ($approval_setting_status) {
                 $approval_status = 'INACTIVE';
                 update_user_meta($user_id, 'upme_approval_status', $approval_status);
             } else {
                 $approval_status = 'ACTIVE';
                 update_user_meta($user_id, 'upme_approval_status', $approval_status);
             }
             // Set Profile Status to active by default
             update_user_meta($user_id, 'upme_user_profile_status', 'ACTIVE');
             // Set the password nag when user selected password setting is disabled
             // Set activation status and codes when selected password setting is enabled
             $upme_settings = get_option('upme_options');
             $set_pass = (bool) $upme_settings['set_password'];
             $activation_setting_status = $upme_register->validate_email_confirmation();
             if (!$set_pass) {
                 update_user_option($user_id, 'default_password_nag', true, true);
                 //Set up the Password change nag.
             }
             // if($activation_setting_status){
             //     $activation_status = 'INACTIVE';
             //     update_user_meta($user_id, 'upme_activation_status', $activation_status);
             // }else{
             //     $activation_status = 'ACTIVE';
             //     update_user_meta($user_id, 'upme_activation_status', $activation_status);
             // }
             // $activation_code = wp_generate_password(12, false);
             // update_user_meta($user_id, 'upme_activation_code',$activation_code);
             // Set automatic login based on the setting value in admin
             if ($upme_register->validate_automatic_login()) {
                 wp_set_auth_cookie($user_id, false, is_ssl());
             }
             /* action after Account Creation */
             do_action('upme_user_register', $user_id);
             if (!empty($activation_status) && 'INACTIVE' == $activation_status) {
                 upme_new_user_notification($user_id, $user_pass, $activation_status, $activation_code);
             } else {
                 wp_new_user_notification($user_id, $user_pass);
             }
             upme_update_user_cache($user_id);
             $this->redirect_registered_users($user_id, $activation_status, $approval_status, 'reg');
         } else {
             /* User already registered. Send him for automatic or manual login */
             $this->automatic_user_login($user->ID);
         }
     } else {
         /* Request failed due to an error from social network. Redirect the user to login form
          * with respective error key.
          */
         $type = isset($result->upme_network_type) ? $result->upme_network_type : 'Undefined';
         $error_code = isset($result->error_code) ? $result->error_code : 'Undefined';
         $this->handle_social_error($type, $error_code);
     }
 }
 function create()
 {
     global $upme_roles, $upme_email_templates;
     require_once ABSPATH . 'wp-includes/pluggable.php';
     // Verify whether registration form name is modified
     if (isset($_POST['upme-register-form-name'])) {
         $upme_secret_key = get_option('upme_secret_key');
         $register_form_name = $_POST['upme-register-form-name'];
         $register_form_name_hash = $_POST['upme-hidden-register-form-name-hash'];
         if ($register_form_name_hash != hash('sha256', $register_form_name . $upme_secret_key)) {
             // Invailid form name was defined by manually editing
             $this->errors[] = __('Invalid registration form.', 'upme');
             return;
         }
         $this->registration_form_name = $register_form_name;
     }
     /* UPME action for adding restrictions before registration */
     $before_registration_validation_params = array();
     do_action('upme_before_registration_restrictions', $this->usermeta, $before_registration_validation_params);
     /* END action */
     /* Create profile when there is no error */
     if (!isset($this->errors)) {
         // Set date format from admin settings
         $upme_settings = get_option('upme_options');
         $upme_date_format = (string) isset($upme_settings['date_format']) ? $upme_settings['date_format'] : 'mm/dd/yy';
         /* Create account, update user meta */
         $sanitized_user_login = sanitize_user($_POST['user_login']);
         /* Get password */
         if (isset($_POST['user_pass']) && $_POST['user_pass'] != '') {
             $user_pass = $_POST['user_pass'];
         } else {
             $user_pass = wp_generate_password(12, false);
             /* UPME Filters for before registration head section */
             $registration_generated_password_params = array('meta' => $this->usermeta);
             $user_pass = apply_filters('upme_registration_generated_password', $user_pass, $registration_generated_password_params);
             // End Filters
         }
         /* New user */
         $user_id = wp_create_user($sanitized_user_login, $user_pass, $_POST['user_email']);
         if (!$user_id) {
             /* UPME action for handling failure in new user creation */
             $new_user_registration_fail_params = array();
             do_action('upme_new_user_registration_fail', $user_id, $new_user_registration_fail_params);
             /* END action */
         } else {
             global $upme;
             /* Force custom user role on registration using shortcode attributes */
             if (isset($_POST['upme-hidden-register-form-user-role'])) {
                 $upme_secret_key = get_option('upme_secret_key');
                 $register_user_role = $_POST['upme-hidden-register-form-user-role'];
                 $register_user_role_hash = $_POST['upme-hidden-register-form-user-role-hash'];
                 if ($register_user_role_hash == hash('sha256', $register_user_role . $upme_secret_key) && get_role($register_user_role)) {
                     $user = new WP_User($user_id);
                     $user->set_role($register_user_role);
                 } else {
                     // Invailid user role was defined by manually editing
                     return;
                 }
             } else {
                 /* Allow users to select the role without forcing */
                 $allow_user_role_registration = $upme_settings['select_user_role_in_registration'];
                 // Set new users role specified in the registration page
                 // This will only used when Select User Role in Registration setting is enabled
                 $allowed_user_roles = $upme_roles->upme_allowed_user_roles_registration();
                 $user_role = isset($this->usermeta['user_role']) ? $this->usermeta['user_role'] : '';
                 if (!empty($user_role) && isset($allowed_user_roles[$user_role]) && $allow_user_role_registration) {
                     $user = new WP_User($user_id);
                     $user->set_role($user_role);
                 }
             }
             // Get profile fields
             $profile_fields = get_option('upme_profile_fields');
             // Get list of dattime fields
             $date_time_fields = array();
             foreach ($profile_fields as $key => $field) {
                 extract($field);
                 // Filter date/time custom fields
                 if (isset($profile_fields[$key]['field']) && $profile_fields[$key]['field'] == 'datetime') {
                     array_push($date_time_fields, $profile_fields[$key]['meta']);
                 }
             }
             /* Now update all user meta */
             foreach ($this->usermeta as $key => $value) {
                 // save checkboxes
                 if (is_array($value)) {
                     // checkboxes
                     $value = implode(', ', $value);
                 }
                 if (in_array($key, $date_time_fields)) {
                     if ('' != $value) {
                         $formatted_date = upme_date_format_to_standerd($value, $upme_date_format);
                         $value = $formatted_date;
                     }
                 }
                 /* UPME action for adding custom filtering for each field save registration */
                 $before_registration_field_update_params = array('user_id' => $user_id, 'meta' => $key, 'value' => $value);
                 do_action('upme_before_registration_field_update', $before_registration_field_update_params);
                 /* END action */
                 // Prevent passwords from saving in user meta table
                 if ('user_pass' != $key && 'user_pass_confirm' != $key) {
                     update_user_meta($user_id, $key, esc_attr($value));
                 }
                 /* UPME action for adding custom filtering for each field save registration */
                 $after_registration_field_update_params = array('user_id' => $user_id, 'meta' => $key, 'value' => $value);
                 do_action('upme_after_registration_field_update', $after_registration_field_update_params);
                 /* END action */
                 /* update core fields - email, url, pass */
                 if (in_array($key, array('user_email', 'user_url', 'display_name'))) {
                     wp_update_user(array('ID' => $user_id, $key => esc_attr($value)));
                 }
             }
             // Check user selected passwrod setting for saving the activation details
         }
         // Set approval status when user profile approvals are enabled
         $approval_setting_status = $this->validate_user_approval();
         if ($approval_setting_status) {
             $approval_status = 'INACTIVE';
             update_user_meta($user_id, 'upme_approval_status', $approval_status);
         } else {
             $approval_status = 'ACTIVE';
             update_user_meta($user_id, 'upme_approval_status', $approval_status);
         }
         // Set Profile Status to active by default
         update_user_meta($user_id, 'upme_user_profile_status', 'ACTIVE');
         // Set the password nag when user selected password setting is disabled
         // Set activation status and codes when selected password setting is enabled
         $upme_settings = get_option('upme_options');
         $set_pass = (bool) $upme_settings['set_password'];
         $activation_setting_status = $this->validate_email_confirmation();
         $activation_status = '';
         if (!$set_pass) {
             update_user_option($user_id, 'default_password_nag', true, true);
             //Set up the Password change nag.
         }
         if ($activation_setting_status) {
             $activation_status = 'INACTIVE';
             update_user_meta($user_id, 'upme_activation_status', $activation_status);
         } else {
             $activation_status = 'ACTIVE';
             update_user_meta($user_id, 'upme_activation_status', $activation_status);
         }
         $activation_code = wp_generate_password(12, false);
         update_user_meta($user_id, 'upme_activation_code', $activation_code);
         // Set automatic login based on the setting value in admin
         if ($this->validate_automatic_login()) {
             wp_set_auth_cookie($user_id, false, is_ssl());
         }
         /* action after Account Creation */
         do_action('upme_user_register', $user_id);
         if (!empty($activation_status) && 'INACTIVE' == $activation_status) {
             upme_new_user_notification($user_id, $user_pass, $activation_status, $activation_code);
         } else {
             $user = new WP_User($user_id);
             $username = $user->user_login;
             $current_option = get_option('upme_options');
             $link = get_permalink($current_option['login_page_id']);
             $send_params = array('username' => $username, 'password' => $user_pass, 'login_link' => $link);
             $email_status = $upme_email_templates->upme_send_emails('reg_default_user', '', '', '', $send_params, $user_id);
             $send_params = array('username' => $username, 'email' => $user->user_email);
             $admin_email_status = $upme_email_templates->upme_send_emails('reg_default_admin', '', '', '', $send_params, $user_id);
             if (!$email_status && !$admin_email_status) {
                 wp_new_user_notification($user_id, $user_pass);
             }
         }
     }
 }