/**
  * Create new WP user.
  *
  * @since  1.0.0
  * @internal
  * @throws Exception
  */
 private function create_new_user()
 {
     // Check if the WordPress settings allow user registration.
     if (!MS_Model_Member::can_register()) {
         throw new Exception(__('Registration is currently not allowed.', 'membership2'), 1);
         return;
     }
     if (is_user_logged_in()) {
         throw new Exception(__('You cannot register a new account, because you are already logged in.', 'membership2'), 1);
         return;
     }
     $validation_errors = new WP_Error();
     $required = array('username' => __('Username', 'membership2'), 'email' => __('Email address', 'membership2'), 'password' => __('Password', 'membership2'), 'password2' => __('Password confirmation', 'membership2'));
     /**
      * Filter the required field list to customize the fields that are
      * mandatory.
      *
      * @since 1.0.1.0
      * @var   array
      */
     $required = apply_filters('ms_model_member_create_user_required_fields', $required);
     foreach ($required as $field => $message) {
         if (empty($this->{$field}) && empty($_POST[$field])) {
             $validation_errors->add($field, sprintf(__('Please ensure that the <span class="ms-bold">%s</span> information is completed.', 'membership2'), $message));
         }
     }
     if ($this->password != $this->password2) {
         $validation_errors->add('passmatch', __('Please ensure the passwords match.', 'membership2'));
     }
     if (!validate_username($this->username)) {
         $validation_errors->add('usernamenotvalid', __('The username is not valid, sorry.', 'membership2'));
     }
     if (username_exists($this->username)) {
         $validation_errors->add('usernameexists', __('That username is already taken, sorry.', 'membership2'));
     }
     if (!is_email($this->email)) {
         $validation_errors->add('emailnotvalid', __('The email address is not valid, sorry.', 'membership2'));
     }
     if (email_exists($this->email)) {
         $validation_errors->add('emailexists', __('That email address is already taken, sorry.', 'membership2'));
     }
     // Check the multisite Email-Domain limitation for new registrations.
     if (is_multisite()) {
         $illegal_names = get_site_option('illegal_names');
         $limited_domains = get_site_option('limited_email_domains');
         $banned_domains = get_site_option('banned_email_domains');
         $email_domain = substr(strrchr($this->email, '@'), 1);
         if ($illegal_names && is_array($illegal_names)) {
             if (in_array($this->username, $illegal_names)) {
                 $validation_errors->add('illegalname', __('The username is not valid, sorry.', 'membership2'));
             }
         }
         if ($limited_domains && is_array($limited_domains)) {
             if (!in_array($email_domain, $limited_domains)) {
                 $validation_errors->add('emaildomain', __('That email domain is not allowed for registration, sorry.', 'membership2'));
             }
         }
         if ($banned_domains && is_array($banned_domains)) {
             if (in_array($email_domain, $banned_domains)) {
                 $validation_errors->add('emaildomain', __('That email domain is not allowed for registration, sorry.', 'membership2'));
             }
         }
     }
     $validation_errors = apply_filters('ms_model_membership_create_new_user_validation_errors', $validation_errors);
     // Compatibility with WangGuard
     $_POST['user_email'] = $this->email;
     $user_data = array('user_name' => $this->username, 'orig_username' => $this->username, 'user_email' => $this->email, 'errors' => $validation_errors);
     $user_data = apply_filters('wpmu_validate_user_signup', $user_data);
     if (is_wp_error($user_data)) {
         /*
          * Some plugins incorrectly return a WP_Error object as result of
          * the wpmu_validate_user_signup filter.
          */
         $validation_errors = $user_data;
     } else {
         $validation_errors = $user_data['errors'];
     }
     $errors = $validation_errors->get_error_messages();
     if (!empty($errors)) {
         throw new Exception(implode('<br/>', $errors));
     } else {
         if (!$this->password) {
             /**
              * For some reason the user did not provide a password in the
              * registration form. We help out here by creating a password
              * for the little bugger and send him a password-reset email.
              *
              * So: Generate a STRONG password for the new user.
              *
              * Important: This password should be sent to the user via the
              * Email template "User Account Created"
              */
             $this->password = wp_generate_password(24);
             $this->password2 = $this->password;
         }
         $user_id = wp_create_user($this->username, $this->password, $this->email);
         if (is_wp_error($user_id)) {
             $validation_errors->add('userid', $user_id->get_error_message());
             throw new Exception(implode('<br/>', $validation_errors->get_error_messages()));
         }
         $this->id = $user_id;
     }
     do_action('ms_model_member_create_new_user', $this);
 }
 /**
  * Show register user form.
  *
  * Related Filter Hooks:
  * - the_content
  *
  * @since  1.0.0
  *
  * @param string $content The page content to filter.
  * @return string The filtered content.
  */
 public function register_form($content)
 {
     // Check if the WordPress settings allow user registration.
     if (!MS_Model_Member::can_register()) {
         return __('Registration is currently not allowed.', MS_TEXT_DOMAIN);
     }
     // Do not parse the form when building the excerpt
     global $wp_current_filter;
     if (in_array('get_the_excerpt', $wp_current_filter)) {
         return '';
     }
     /**
      * Add-ons or other plugins can use this filter to define a completely
      * different registration form. If this filter returns any content, then
      * the default form will not be generated
      *
      * @since  1.0.0
      * @var string
      */
     $custom_code = apply_filters('ms_frontend_custom_registration_form', '', $this->register_errors, $this);
     if ($custom_code) {
         $content = $custom_code;
     } else {
         remove_filter('the_content', 'wpautop');
         $did_form = MS_Helper_Shortcode::has_shortcode(MS_Helper_Shortcode::SCODE_REGISTER_USER, $content);
         if (!$did_form) {
             $scode = sprintf('[%s errors="%s"]', MS_Helper_Shortcode::SCODE_REGISTER_USER, str_replace('"', "'", $this->register_errors));
             $reg_form = do_shortcode($scode);
             if (!MS_Model_Member::is_logged_in()) {
                 $content = $reg_form;
             } else {
                 $content .= $reg_form;
             }
         }
     }
     return apply_filters('ms_controller_frontend_register_form_content', $content, $this);
 }
 /**
  * Returns the HTML code.
  *
  * @since  1.0.0
  * @return string
  */
 public function to_html()
 {
     $res_html = '';
     $res_form = '';
     $html = '';
     $valid_forms = array('login', 'logout', 'reset', 'lost');
     extract($this->data);
     if (!isset($form) || !in_array($form, $valid_forms)) {
         if (MS_Model_Member::is_logged_in()) {
             $form = 'logout';
         } elseif (isset($action) && 'resetpass' === $action) {
             $form = 'reset';
         } elseif ('lostpass' == $_GET['show']) {
             $form = 'lost';
         } else {
             $form = 'login';
         }
         $this->data['form'] = $form;
     }
     /**
      * Provide a customized login form.
      *
      * Possible filters to provide a customized login form:
      * - 'ms_shortcode_custom_form-login'
      * - 'ms_shortcode_custom_form-logout'
      * - 'ms_shortcode_custom_form-reset'
      * - 'ms_shortcode_custom_form-lost'
      *
      * @since  1.0.0
      */
     $html = apply_filters('ms_shortcode_custom_form-' . $form, '', $this->data);
     if (!empty($html)) {
         return $html;
     } else {
         $html = '';
     }
     if ('logout' === $form) {
         return $this->logout_form();
     } elseif ('reset' === $form) {
         return $this->reset_form();
     } else {
         if (empty($redirect_login)) {
             $redirect_login = MS_Helper_Utility::get_current_url();
         }
         // Build the Login Form.
         $res_form .= $prefix;
         $res_form .= $this->login_form($redirect_login);
         $res_form .= $this->lostpass_form();
         // Wrap form in optional wrappers.
         if (!empty($wrapwith)) {
             $res_form .= sprintf('<%s class="%s">', esc_attr($wrapwith), esc_attr($wrapwithclass));
             $res_form = sprintf('<%1$s class="%2$s">%3$s</%1$s>', esc_attr($wrapwith), esc_attr($wrapwithclass), $res_form);
         }
         if (!empty($item)) {
             $res_form = sprintf('<%1$s class="%2$s">%3$s</%1$s>', esc_attr($item), esc_attr($itemclass), $res_form);
         }
         if (!empty($holder)) {
             $res_form = sprintf('<%1$s class="%2$s">%3$s</%1$s>', esc_attr($holder), esc_attr($holderclass), $res_form);
         }
         // Complete the HTML output.
         if ($header) {
             $html .= $this->login_header_html();
         }
         $html .= $res_form;
         if ($register && !MS_Model_Member::is_logged_in()) {
             if (MS_Model_Member::can_register()) {
                 $link = sprintf('<div class="registerhere">Don&apos;t have an account?' . ' ' . '<a href="%1$s" class="register">%2$s</a></div>', MS_Controller_Frontend::get_registration_url('register'), __('Sign up here.', 'membership2'));
                 /**
                  * Filter documented in wp-includes/general-template.php
                  */
                 $html .= apply_filters('register', $link);
             }
         }
         // Load the ajax script that handles the Ajax login functions.
         wp_enqueue_script('ms-ajax-login');
         lib3()->ui->data('ms_ajax_login', array('ajaxurl' => admin_url('admin-ajax.php'), 'loadingmessage' => __('Please wait...', 'membership2'), 'errormessage' => __('Request failed, please try again.', 'membership2')));
     }
     // Remove linebreaks to bypass the "wpautop" filter.
     $html = str_replace(array("\r\n", "\r", "\n"), '', $html);
     $html = '<div class="ms-membership-form-wrapper">' . $html . '</div>';
     $html = apply_filters('ms_compact_code', $html);
     /*
      * Possible filters to provide a customized login form:
      * - 'ms_shortcode_form-login'
      * - 'ms_shortcode_form-logout'
      * - 'ms_shortcode_form-reset'
      * - 'ms_shortcode_form-lost'
      */
     return apply_filters('ms_shortcode_form-' . $form, $html, $this->data);
 }