/**
  * 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);
 }
 /**
  * Prepare for Member registration.
  *
  * @since  1.0.0
  */
 public function __construct()
 {
     parent::__construct();
     if (MS_Plugin::is_enabled()) {
         do_action('ms_controller_frontend_construct', $this);
         // Process actions like register new account.
         $this->add_action('template_redirect', 'process_actions', 1);
         // Check if the current page is a Membership Page.
         $this->add_action('template_redirect', 'check_for_membership_pages', 2);
         // Propagates SSL cookies when user logs in.
         $this->add_action('wp_login', 'propagate_ssl_cookie', 10, 2);
         // Enqueue scripts.
         $this->add_action('wp_enqueue_scripts', 'enqueue_scripts');
         // Add classes for all memberships the user is registered to.
         $this->add_filter('body_class', 'body_class');
         // Clears the shortcode memory at the beginning of the_content
         $this->add_filter('the_content', 'clear_content_memory', 1);
         // Compact code for output on the front end.
         add_filter('ms_compact_code', array('MS_Helper_Html', 'compact_code'));
         /**
          * This allows WordPress to provide the default register form.
          *
          * Set the filter response to FALSE to stop Membership2 from
          * handling the registration process. WordPress or other plugins can
          * register users in that case.
          *
          * @since  1.0.0
          */
         self::$handle_registration = apply_filters('ms_frontend_handle_registration', true);
         if (self::$handle_registration) {
             // Set the registration URL to the 'Register' Membership Page.
             $this->add_filter('wp_signup_location', 'signup_location', 999);
             $this->add_filter('register_url', 'signup_location', 999);
         }
         // Redirect users to their Account page after login.
         $this->add_filter('login_redirect', 'login_redirect', 10, 3);
     }
 }
 /**
  * Handles register user submit.
  *
  * On validation errors, step back to register form.
  *
  * @since  1.0.0
  */
 public function register_user()
 {
     do_action('ms_controller_frontend_register_user_before', $this);
     if (!$this->verify_nonce()) {
         return;
     }
     try {
         $user = MS_Factory::create('MS_Model_Member');
         // Default WP registration filter
         $fields = apply_filters('signup_user_init', $_REQUEST);
         foreach ($fields as $field => $value) {
             $user->{$field} = $value;
         }
         $user->save();
         // Default WP action hook
         do_action('signup_finished');
         $user->signon_user();
         if (MS_Model_Event::save_event(MS_Model_Event::TYPE_MS_REGISTERED, $user)) {
             if (!defined('MS_DISABLE_WP_NEW_USER_NOTIFICATION')) {
                 wp_new_user_notification($user->id, $user->password);
             }
         }
         do_action('ms_controller_frontend_register_user_complete', $user);
         // Go to membership signup payment form.
         if (empty($_REQUEST['membership_id'])) {
             $redirect = esc_url_raw(add_query_arg(array('step' => self::STEP_CHOOSE_MEMBERSHIP)));
         } else {
             $redirect = esc_url_raw(add_query_arg(array('step' => self::STEP_PAYMENT_TABLE, 'membership_id' => absint($_REQUEST['membership_id']))));
         }
         wp_safe_redirect($redirect);
         exit;
     } catch (Exception $e) {
         self::$register_errors = $e->getMessage();
         // step back
         $this->add_action('the_content', 'register_form', 1);
         do_action('ms_controller_frontend_register_user_error', self::$register_errors);
     }
 }