Advertisement
Guest User

Untitled

a guest
Jul 12th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 35.45 KB | None | 0 0
  1. <?php
  2.  
  3. class Custom_Members
  4. {
  5.  
  6.     public function __construct()
  7.     {
  8.         function app_output_buffer()
  9.         {
  10.             ob_start();
  11.         } // soi_output_buffer
  12.         add_action('init', 'app_output_buffer');
  13.  
  14.         // LOGIN & LOGOUT
  15.         add_shortcode('custom-login-form', array($this, 'render_login_form'));
  16.         add_action('login_form_login', array($this, 'redirect_to_custom_login'));
  17.         add_filter('authenticate', array($this, 'maybe_redirect_at_authenticate'), 101, 3);
  18.         add_action('wp_logout', array($this, 'redirect_after_logout'));
  19.         add_filter('login_redirect', array($this, 'redirect_after_login'), 10, 3);
  20.  
  21.         // REGISTER
  22.         add_shortcode('custom-register-form', array($this, 'render_register_form'));
  23.         add_action('login_form_register', array($this, 'redirect_to_custom_register'));
  24.         add_action('login_form_register', array($this, 'do_register_user'));
  25.  
  26.         // LOST
  27.         add_action('login_form_lostpassword', array($this, 'redirect_to_custom_lostpassword'));
  28.         add_shortcode('custom-password-lost-form', array($this, 'render_password_lost_form'));
  29.         add_action('login_form_lostpassword', array($this, 'do_password_lost'));
  30.         add_filter('retrieve_password_message', array($this, 'replace_retrieve_password_message'), 10, 4);
  31.         add_filter('retrieve_password_title', function( $title )
  32.         {
  33.             return 'Password Reset';
  34.         }, 10, 4);
  35.  
  36.         add_filter( 'wp_mail_content_type', function( $content_type ) {
  37.             return 'text/html';
  38.         });
  39.  
  40.         // RESET
  41.         add_action('login_form_rp', array($this, 'redirect_to_custom_password_reset'));
  42.         add_action('login_form_resetpass', array($this, 'redirect_to_custom_password_reset'));
  43.         add_shortcode('custom-password-reset-form', array($this, 'render_password_reset_form'));
  44.         add_action('login_form_rp', array($this, 'do_password_reset'));
  45.         add_action('login_form_resetpass', array($this, 'do_password_reset'));
  46.  
  47.         // CHANGE PASSWORD
  48.         add_shortcode('custom-password-change-form', array($this, 'render_change_password_form'));
  49.         add_action('custom_change_password_post', array($this, 'custom_change_password_post'));
  50.  
  51.         add_action('init', array($this, 'init'));
  52.  
  53.     }
  54.  
  55.     public function init()
  56.     {
  57.         add_filter('password_hint', array($this, 'password_hint'));
  58.         add_filter('wp_mail_from_name', array($this, 'custom_wp_mail_from_name'));
  59.         add_filter('auth_cookie_expiration', array($this, 'keep_me_logged_in_for_30_days'));
  60.     }
  61.  
  62.  
  63.     public function custom_wp_mail_from_name($original_email_from)
  64.     {
  65.         return get_option('blogname');
  66.     }
  67.  
  68.     function keep_me_logged_in_for_30_days($expirein)
  69.     {
  70.         return 2592000;
  71.     }
  72.  
  73.     /**
  74.      * A shortcode for rendering the login form.
  75.      *
  76.      * @param  array $attributes Shortcode attributes.
  77.      * @param  string $content The text content for shortcode. Not used.
  78.      *
  79.      * @return string  The shortcode output
  80.      */
  81.     public function render_login_form($attributes, $content = null)
  82.     {
  83.         // Parse shortcode attributes
  84.         $default_attributes = array('show_title' => true);
  85.         $attributes = shortcode_atts($default_attributes, $attributes);
  86.         $show_title = $attributes['show_title'];
  87.  
  88.         if (is_user_logged_in()) {
  89.             //return __('You are already signed in.', 'personalize-login');
  90.             wp_redirect(home_url('community'));
  91.         }
  92.  
  93.         // Pass the redirect parameter to the WordPress login functionality: by default,
  94.         // don't specify a redirect, but if a valid redirect URL has been passed as
  95.         // request parameter, use it.
  96.         $attributes['redirect'] = '';
  97.         if (isset($_REQUEST['redirect_to'])) {
  98.             $attributes['redirect'] = wp_validate_redirect($_REQUEST['redirect_to'], $attributes['redirect']);
  99.         }
  100.  
  101.         // Error messages
  102.         $errors = array();
  103.         if (isset($_REQUEST['login'])) {
  104.             $error_codes = explode(',', $_REQUEST['login']);
  105.  
  106.             foreach ($error_codes as $code) {
  107.                 $errors [] = $this->get_error_message($code);
  108.             }
  109.         }
  110.         $attributes['errors'] = $errors;
  111.  
  112.         // Check if the user just registered
  113.         $attributes['registered'] = isset($_REQUEST['registered']);
  114.  
  115.         // Check if the user just requested a new password
  116.         $attributes['lost_password_sent'] = isset($_REQUEST['checkemail']) && $_REQUEST['checkemail'] == 'confirm';
  117.  
  118.         $attributes['logged_out'] = isset($_REQUEST['logged_out']) && $_REQUEST['logged_out'] == true;
  119.  
  120.         // Check if user just updated password
  121.         $attributes['password_updated'] = isset($_REQUEST['password']) && $_REQUEST['password'] == 'changed';
  122.  
  123.         // Render the login form using an external template
  124.         return $this->get_template_html('login_form', $attributes);
  125.     }
  126.  
  127.     /**
  128.      * Renders the contents of the given template to a string and returns it.
  129.      *
  130.      * @param string $template_name The name of the template to render (without .php)
  131.      * @param array $attributes The PHP variables for the template
  132.      *
  133.      * @return string               The contents of the template.
  134.      */
  135.     private function get_template_html($template_name, $attributes = null)
  136.     {
  137.         if (!$attributes) {
  138.             $attributes = array();
  139.         }
  140.  
  141.         ob_start();
  142.  
  143.         do_action('personalize_login_before_' . $template_name);
  144.  
  145.         require('custom_members_tpl/' . $template_name . '.php');
  146.  
  147.         do_action('personalize_login_after_' . $template_name);
  148.  
  149.         $html = ob_get_contents();
  150.         ob_end_clean();
  151.  
  152.         return $html;
  153.     }
  154.  
  155.     /**
  156.      * Redirect the user to the custom login page instead of wp-login.php.
  157.      */
  158.     function redirect_to_custom_login()
  159.     {
  160.         if ($_SERVER['REQUEST_METHOD'] == 'GET') {
  161.             $redirect_to = isset($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] : null;
  162.  
  163.             if (is_user_logged_in()) {
  164.                 $this->redirect_logged_in_user($redirect_to);
  165.                 exit;
  166.             }
  167.  
  168.             // The rest are redirected to the login page
  169.             $login_url = home_url('login');
  170.             if (!empty($redirect_to)) {
  171.                 $login_url = add_query_arg('redirect_to', $redirect_to, $login_url);
  172.             }
  173.  
  174.             wp_redirect($login_url);
  175.             exit;
  176.         }
  177.     }
  178.  
  179.     /**
  180.      * Redirects the user to the correct page depending on whether he / she
  181.      * is an admin or not.
  182.      *
  183.      * @param string $redirect_to An optional redirect_to URL for admin users
  184.      */
  185.     private function redirect_logged_in_user($redirect_to = null)
  186.     {
  187.         $user = wp_get_current_user();
  188.         if (user_can($user, 'manage_options')) {
  189.             if ($redirect_to) {
  190.                 wp_safe_redirect($redirect_to);
  191.             } else {
  192.                 wp_redirect(admin_url());
  193.             }
  194.         } else {
  195.             wp_redirect(home_url('community'));
  196.         }
  197.     }
  198.  
  199.     /**
  200.      * Redirect the user after authentication if there were any errors.
  201.      *
  202.      * @param Wp_User|Wp_Error $user The signed in user, or the errors that have occurred during login.
  203.      * @param string $username The user name used to log in.
  204.      * @param string $password The password used to log in.
  205.      *
  206.      * @return Wp_User|Wp_Error The logged in user, or error information if there were errors.
  207.      */
  208.     function maybe_redirect_at_authenticate($user, $username, $password)
  209.     {
  210.         // Check if the earlier authenticate filter (most likely,
  211.         // the default WordPress authentication) functions have found errors
  212.         if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  213.             if (is_wp_error($user)) {
  214.                 $error_codes = join(',', $user->get_error_codes());
  215.  
  216.                 $login_url = home_url('login');
  217.                 $login_url = add_query_arg('login', $error_codes, $login_url);
  218.  
  219.                 wp_redirect($login_url);
  220.                 exit;
  221.             }
  222.         }
  223.  
  224.         return $user;
  225.     }
  226.  
  227.     /**
  228.      * Finds and returns a matching error message for the given error code.
  229.      *
  230.      * @param string $error_code The error code to look up.
  231.      *
  232.      * @return string               An error message.
  233.      */
  234.     private function get_error_message($error_code)
  235.     {
  236.         switch ($error_code) {
  237.             case 'empty_username':
  238.             case 'empty_password':
  239.                 return __('Please enter your email and a password', 'personalize-login');
  240.  
  241.             case 'invalid_username':
  242.                 return __(
  243.                     "Sorry, we didn't find any user with this email address",
  244.                     'personalize-login'
  245.                 );
  246.  
  247.             case 'incorrect_password':
  248.                 $err = __(
  249.                     "The password you entered is incorrect. <a href='%s'>Forgot your password</a>?",
  250.                     'personalize-login'
  251.                 );
  252.                 return sprintf($err, wp_lostpassword_url());
  253.  
  254.             case 'user_login':
  255.                 return __('Invalid username', 'personalize-login');
  256.  
  257.             case 'email':
  258.                 return __('Invalid email address', 'personalize-login');
  259.  
  260.             case 'email_exists':
  261.                 return __('An account exists with this email address.', 'personalize-login');
  262.  
  263.             case 'user_login_exists':
  264.                 return __('An account exists with this Username.', 'personalize-login');
  265.  
  266.             case 'closed':
  267.                 return __('Registering new users is currently not allowed.', 'personalize-login');
  268.  
  269.             case 'empty_username':
  270.                 return __('Please enter your email address to continue.', 'personalize-login');
  271.  
  272.             case 'invalid_email':
  273.             case 'invalidcombo':
  274.                 return __('There are no users registered with this email address.', 'personalize-login');
  275.  
  276.             case 'expiredkey':
  277.             case 'invalidkey':
  278.                 return __('The password reset link you used is not valid anymore.', 'personalize-login');
  279.  
  280.             case 'password_reset_mismatch':
  281.                 return __("The two passwords entered don't match.", 'personalize-login');
  282.  
  283.             case 'password_change_mismatch':
  284.                 return __("The new password and the confirmation link you entered don't match.", 'personalize-login');
  285.  
  286.             case 'password_reset_empty':
  287.                 return __("Sorry, we don't accept empty passwords.", 'personalize-login');
  288.  
  289.             case 'short_password':
  290.                 return __("Sorry, Your password must be at least 6 characters.", 'personalize-login');
  291.  
  292.             case 'all_fields_required':
  293.                 return __("Please fill all fields.", 'personalize-login');
  294.  
  295.             case 'wrong_old_password':
  296.                 return __("Wrong current password, please enter the current password correctly!", 'personalize-login');
  297.  
  298.             default:
  299.                 break;
  300.         }
  301.  
  302.         return __('An unknown error occurred. Please try again later.', 'personalize-login');
  303.     }
  304.  
  305.     /**
  306.      * Redirect to custom login page after the user has been logged out.
  307.      */
  308.     public function redirect_after_logout()
  309.     {
  310.         if (!isset($_SESSION['local_logout'])) {
  311.             $redirect_url = home_url('login?logged_out=true');
  312.             wp_safe_redirect($redirect_url);
  313.             exit;
  314.         } else {
  315.             unset($_SESSION['local_logout']);
  316.         }
  317.     }
  318.  
  319.     /**
  320.      * Returns the URL to which the user should be redirected after the (successful) login.
  321.      *
  322.      * @param string $redirect_to The redirect destination URL.
  323.      * @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
  324.      * @param WP_User|WP_Error $user WP_User object if login was successful, WP_Error object otherwise.
  325.      *
  326.      * @return string Redirect URL
  327.      */
  328.     public function redirect_after_login($redirect_to, $requested_redirect_to, $user)
  329.     {
  330.         $redirect_url = home_url();
  331.  
  332.         if (!isset($user->ID)) {
  333.             return $redirect_url;
  334.         }
  335.  
  336.         if (user_can($user, 'manage_options')) {
  337.             // Use the redirect_to parameter if one is set, otherwise redirect to admin dashboard.
  338.             if ($requested_redirect_to == '') {
  339.                 $redirect_url = admin_url();
  340.             } else {
  341.                 $redirect_url = $requested_redirect_to;
  342.             }
  343.         } else {
  344.             // Non-admin users always go to their account page after login
  345.             $redirect_url = home_url('community');
  346.         }
  347.  
  348.         return wp_validate_redirect($redirect_url, home_url());
  349.     }
  350.  
  351.  
  352.     /**
  353.      * A shortcode for rendering the new user registration form.
  354.      *
  355.      * @param  array $attributes Shortcode attributes.
  356.      * @param  string $content The text content for shortcode. Not used.
  357.      *
  358.      * @return string  The shortcode output
  359.      */
  360.     public function render_register_form($attributes, $content = null)
  361.     {
  362.         // Parse shortcode attributes
  363.         $default_attributes = array('show_title' => true);
  364.         $attributes = shortcode_atts($default_attributes, $attributes);
  365.  
  366.         // Retrieve possible errors from request parameters
  367.         $attributes['errors'] = array();
  368.         if (isset($_REQUEST['register-errors'])) {
  369.             $error_codes = explode(',', $_REQUEST['register-errors']);
  370.  
  371.             foreach ($error_codes as $error_code) {
  372.                 $attributes['errors'] [] = $this->get_error_message($error_code);
  373.             }
  374.         }
  375.  
  376.         if (is_user_logged_in()) {
  377.             //return __('You are already signed in.', 'personalize-login');
  378.             wp_redirect(home_url('community'));
  379.         } elseif (!get_option('users_can_register')) {
  380.             return __('Registering new users is currently not allowed.', 'personalize-login');
  381.         } else {
  382.             return $this->get_template_html('register_form', $attributes);
  383.         }
  384.     }
  385.  
  386.     /**
  387.      * Redirects the user to the custom registration page instead
  388.      * of wp-login.php?action=register.
  389.      */
  390.     public function redirect_to_custom_register()
  391.     {
  392.         if ('GET' == $_SERVER['REQUEST_METHOD']) {
  393.             if (is_user_logged_in()) {
  394.                 $this->redirect_logged_in_user();
  395.             } else {
  396.                 wp_redirect(home_url('register'));
  397.             }
  398.             exit;
  399.         }
  400.     }
  401.  
  402.     /**
  403.      * Validates and then completes the new user signup process if all went well.
  404.      *
  405.      * @param string $user_login The new user's user_login address
  406.      * @param string $email The new user's email address
  407.      * @param string $first_name The new user's first name
  408.      * @param string $last_name The new user's last name
  409.      * @param string $user_pass The new user's password
  410.      *
  411.      * @return int|WP_Error         The id of the user that was created, or error if failed.
  412.      */
  413.     private function register_user($user_login, $email, $first_name, $last_name)
  414.     {
  415.         $errors = new WP_Error();
  416.  
  417.         // Email address is used as both username and email. It is also the only
  418.         // parameter we need to validate
  419.  
  420.         $_SESSION['custom_email'] = $_POST['email'];
  421.         $_SESSION['custom_first_name'] = $_POST['first_name'];
  422.         $_SESSION['custom_last_name'] = $_POST['last_name'];
  423.  
  424.         if (!isset($user_login) || !isset($email) || !isset($first_name) || !isset($_POST['last_name']) ||
  425.             empty($user_login) || empty($email) || empty($first_name) || empty($last_name)
  426.         ) {
  427.             $errors->add('all_fields_required', $this->get_error_message('all_fields_required'));
  428.             return $errors;
  429.         }
  430.  
  431.         if (!isset($user_login) || strlen($user_login) < 3 || !validate_username($user_login)) {
  432.             $errors->add('user_login', $this->get_error_message('user_login'));
  433.             return $errors;
  434.         }
  435.  
  436.         if (!is_email($email)) {
  437.             $errors->add('email', $this->get_error_message('email'));
  438.             return $errors;
  439.         }
  440.  
  441.         if (username_exists($user_login)) {
  442.             $errors->add('user_login_exists', $this->get_error_message('email_exists'));
  443.             return $errors;
  444.         }
  445.  
  446.         if (email_exists($email)) {
  447.             $errors->add('email_exists', $this->get_error_message('email_exists'));
  448.             return $errors;
  449.         }
  450.  
  451.         unset($_SESSION['custom_user_login']);
  452.         unset($_SESSION['custom_email']);
  453.         unset($_SESSION['custom_first_name']);
  454.         unset($_SESSION['custom_last_name']);
  455.  
  456.  
  457.         // Generate the password so that the subscriber will have to check email...
  458.         $password = wp_generate_password(12, false);
  459.  
  460.         $user_data = array(
  461.             'user_login' => $user_login,
  462.             'user_email' => $email,
  463.             'user_pass' => $password,
  464.             'first_name' => $first_name,
  465.             'last_name' => $last_name,
  466.             'nickname' => $first_name,
  467.         );
  468.  
  469.         $user_id = wp_insert_user($user_data);
  470.  
  471.  
  472.         wp_new_user_notification($user_id, null, true);
  473.         return $user_id;
  474.     }
  475.  
  476.     /**
  477.      * Handles the registration of a new user.
  478.      *
  479.      * Used through the action hook "login_form_register" activated on wp-login.php
  480.      * when accessed through the registration action.
  481.      */
  482.     public function do_register_user()
  483.     {
  484.         if ('POST' == $_SERVER['REQUEST_METHOD']) {
  485.             $redirect_url = home_url('register');
  486.  
  487.             if (!get_option('users_can_register')) {
  488.                 // Registration closed, display error
  489.                 $redirect_url = add_query_arg('register-errors', 'closed', $redirect_url);
  490.             } else {
  491.                 $user_login = sanitize_user($_POST['user_login']);
  492.                 $email = sanitize_email($_POST['email']);
  493.                 $first_name = sanitize_text_field($_POST['first_name']);
  494.                 $last_name = sanitize_text_field($_POST['last_name']);
  495.  
  496.  
  497.                 $result = $this->register_user($user_login, $email, $first_name, $last_name);
  498.  
  499.                 if (is_wp_error($result)) {
  500.                     // Parse errors into a string and append as parameter to redirect
  501.                     $errors = join(',', $result->get_error_codes());
  502.                     $redirect_url = add_query_arg('register-errors', $errors, $redirect_url);
  503.                 } else {
  504.                     // Success, redirect to login page.
  505.                     $redirect_url = home_url('login');
  506.                     $redirect_url = add_query_arg('registered', $email, $redirect_url);
  507.                 }
  508.             }
  509.  
  510.             wp_redirect($redirect_url);
  511.             exit;
  512.         }
  513.     }
  514.  
  515.  
  516.     /**
  517.      * Redirects the user to the custom "Forgot your password?" page instead of
  518.      * wp-login.php?action=lostpassword.
  519.      */
  520.     public function redirect_to_custom_lostpassword()
  521.     {
  522.         if ('GET' == $_SERVER['REQUEST_METHOD']) {
  523.             if (is_user_logged_in()) {
  524.                 $this->redirect_logged_in_user();
  525.                 exit;
  526.             }
  527.  
  528.             wp_redirect(home_url('lost-password'));
  529.             exit;
  530.         }
  531.     }
  532.  
  533.     /**
  534.      * A shortcode for rendering the form used to initiate the password reset.
  535.      *
  536.      * @param  array $attributes Shortcode attributes.
  537.      * @param  string $content The text content for shortcode. Not used.
  538.      *
  539.      * @return string  The shortcode output
  540.      */
  541.     public function render_password_lost_form($attributes, $content = null)
  542.     {
  543.         // Parse shortcode attributes
  544.         $default_attributes = array('show_title' => true);
  545.         $attributes = shortcode_atts($default_attributes, $attributes);
  546.  
  547.         // Retrieve possible errors from request parameters
  548.         $attributes['errors'] = array();
  549.         if (isset($_REQUEST['errors'])) {
  550.             $error_codes = explode(',', $_REQUEST['errors']);
  551.  
  552.             foreach ($error_codes as $error_code) {
  553.                 $attributes['errors'] [] = $this->get_error_message($error_code);
  554.             }
  555.         }
  556.  
  557.         if (is_user_logged_in()) {
  558.             return __('You are already signed in.', 'personalize-login');
  559.         } else {
  560.             return $this->get_template_html('password_lost_form', $attributes);
  561.         }
  562.     }
  563.  
  564.     /**
  565.      * Initiates password reset.
  566.      */
  567.     public function do_password_lost()
  568.     {
  569.         if ('POST' == $_SERVER['REQUEST_METHOD']) {
  570.             $errors = retrieve_password();
  571.             if (is_wp_error($errors)) {
  572.                 // Errors found
  573.                 $redirect_url = home_url('lost-password');
  574.                 $redirect_url = add_query_arg('errors', join(',', $errors->get_error_codes()), $redirect_url);
  575.             } else {
  576.                 // Email sent
  577.                 $redirect_url = home_url('login');
  578.                 $redirect_url = add_query_arg('checkemail', 'confirm', $redirect_url);
  579.             }
  580.  
  581.             wp_redirect($redirect_url);
  582.             exit;
  583.         }
  584.     }
  585.  
  586.     /**
  587.      * Returns the message body for the password reset mail.
  588.      * Called through the retrieve_password_message filter.
  589.      *
  590.      * @param string $message Default mail message.
  591.      * @param string $key The activation key.
  592.      * @param string $user_login The username for the user.
  593.      * @param WP_User $user_data WP_User object.
  594.      *
  595.      * @return string   The mail message to send.
  596.      */
  597.     public function replace_retrieve_password_message($message, $key, $user_login, $user_data)
  598.     {
  599.         global $wlg_notify;
  600.         $link = site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login');
  601.         return $wlg_notify->retrieve_password($user_data->user_email,$user_login,$link);
  602.     }
  603.  
  604.     /**
  605.      * Redirects to the custom password reset page, or the login page
  606.      * if there are errors.
  607.      */
  608.     public function redirect_to_custom_password_reset()
  609.     {
  610.         if ('GET' == $_SERVER['REQUEST_METHOD']) {
  611.             // Verify key / login combo
  612.             $user = check_password_reset_key($_REQUEST['key'], $_REQUEST['login']);
  613.             if (!$user || is_wp_error($user)) {
  614.                 if ($user && $user->get_error_code() === 'expired_key') {
  615.                     wp_redirect(home_url('login?login=expiredkey'));
  616.                 } else {
  617.                     wp_redirect(home_url('login?login=invalidkey'));
  618.                 }
  619.                 exit;
  620.             }
  621.  
  622.             $redirect_url = home_url('password-reset');
  623.             $redirect_url = add_query_arg('login', esc_attr($_REQUEST['login']), $redirect_url);
  624.             $redirect_url = add_query_arg('key', esc_attr($_REQUEST['key']), $redirect_url);
  625.  
  626.             wp_redirect($redirect_url);
  627.             exit;
  628.         }
  629.     }
  630.  
  631.     /**
  632.      * A shortcode for rendering the form used to reset a user's password.
  633.      *
  634.      * @param  array $attributes Shortcode attributes.
  635.      * @param  string $content The text content for shortcode. Not used.
  636.      *
  637.      * @return string  The shortcode output
  638.      */
  639.     public function render_password_reset_form($attributes, $content = null)
  640.     {
  641.         // Parse shortcode attributes
  642.         $default_attributes = array('show_title' => true);
  643.         $attributes = shortcode_atts($default_attributes, $attributes);
  644.  
  645.         if (is_user_logged_in()) {
  646.             wp_redirect(home_url('community'));
  647.             exit;
  648.         } else {
  649.             if (isset($_REQUEST['login']) && isset($_REQUEST['key'])) {
  650.                 $attributes['login'] = $_REQUEST['login'];
  651.                 $attributes['key'] = $_REQUEST['key'];
  652.  
  653.                 if (isset($_REQUEST['wlgrp_type']) && $_REQUEST['wlgrp_type'] == "new") {
  654.                     if (!isset($_SESSION['wlg_new_member'])) {
  655.                         $_SESSION['wlg_new_member'] = true;
  656.                     }
  657.                 }
  658.  
  659.                 // Error messages
  660.                 $errors = array();
  661.                 if (isset($_REQUEST['error'])) {
  662.                     $error_codes = explode(',', $_REQUEST['error']);
  663.  
  664.                     foreach ($error_codes as $code) {
  665.                         $errors [] = $this->get_error_message($code);
  666.                     }
  667.                 }
  668.                 $attributes['errors'] = $errors;
  669.  
  670.                 return $this->get_template_html('password_reset_form', $attributes);
  671.             } else {
  672.                 return __('Invalid password reset link.', 'personalize-login');
  673.             }
  674.         }
  675.     }
  676.  
  677.     /**
  678.      * Resets the user's password if the password reset form was submitted.
  679.      */
  680.     public function do_password_reset()
  681.     {
  682.         if ('POST' == $_SERVER['REQUEST_METHOD']) {
  683.             $rp_key = $_REQUEST['rp_key'];
  684.             $rp_login = $_REQUEST['rp_login'];
  685.  
  686.             $user = check_password_reset_key($rp_key, $rp_login);
  687.  
  688.             if (!$user || is_wp_error($user)) {
  689.                 if ($user && $user->get_error_code() === 'expired_key') {
  690.                     wp_redirect(home_url('login?login=expiredkey'));
  691.                 } else {
  692.                     wp_redirect(home_url('login?login=invalidkey'));
  693.                 }
  694.                 exit;
  695.             }
  696.  
  697.             if (isset($_POST['pass1'])) {
  698.                 if ($_POST['pass1'] != $_POST['pass2']) {
  699.                     // Passwords don't match
  700.                     $redirect_url = home_url('password-reset');
  701.  
  702.                     $redirect_url = add_query_arg('key', $rp_key, $redirect_url);
  703.                     $redirect_url = add_query_arg('login', $rp_login, $redirect_url);
  704.                     $redirect_url = add_query_arg('error', 'password_reset_mismatch', $redirect_url);
  705.  
  706.                     wp_redirect($redirect_url);
  707.                     exit;
  708.                 }
  709.  
  710.                 if (empty($_POST['pass1'])) {
  711.                     // Password is empty
  712.                     $redirect_url = home_url('password-reset');
  713.  
  714.                     $redirect_url = add_query_arg('key', $rp_key, $redirect_url);
  715.                     $redirect_url = add_query_arg('login', $rp_login, $redirect_url);
  716.                     $redirect_url = add_query_arg('error', 'password_reset_empty', $redirect_url);
  717.  
  718.                     wp_redirect($redirect_url);
  719.                     exit;
  720.                 }
  721.  
  722.                 if (5 >= strlen($_POST['pass1'])) {
  723.                     // Password is empty
  724.                     $redirect_url = home_url('password-reset');
  725.  
  726.                     $redirect_url = add_query_arg('key', $rp_key, $redirect_url);
  727.                     $redirect_url = add_query_arg('login', $rp_login, $redirect_url);
  728.                     $redirect_url = add_query_arg('error', 'short_password', $redirect_url);
  729.  
  730.                     wp_redirect($redirect_url);
  731.                     exit;
  732.                 }
  733.  
  734.                 // Parameter checks OK, reset password
  735.                 reset_password($user, $_POST['pass1']);
  736.  
  737.                 if(isset($_SESSION['wlg_new_member'])) {
  738.                     global $wlg_notify;
  739.                     $wlg_notify->welcome($user->user_email,$user->user_login);
  740.                     unset($_SESSION['wlg_new_member']);
  741.                     $_SESSION['show_tour'] = 1;
  742.                 }
  743.  
  744.                 $user_id = $user->ID;
  745.                 wp_set_current_user($user_id, $user->user_login);
  746.                 wp_set_auth_cookie($user_id);
  747.                 do_action('wp_login', $user->user_login);
  748.                 wp_safe_redirect(home_url(''));
  749.                 exit();
  750.  
  751.             } else {
  752.                 echo "Invalid request.";
  753.             }
  754.  
  755.             exit;
  756.         }
  757.     }
  758.  
  759.  
  760.     /**
  761.      * A shortcode for rendering the form used to reset a user's password.
  762.      *
  763.      * @param  array $attributes Shortcode attributes.
  764.      * @param  string $content The text content for shortcode. Not used.
  765.      *
  766.      * @return string  The shortcode output
  767.      */
  768.     public function render_change_password_form($attributes, $content = null)
  769.     {
  770.         // Parse shortcode attributes
  771.         $default_attributes = array('show_title' => true);
  772.         $attributes = shortcode_atts($default_attributes, $attributes);
  773.  
  774.         if (!is_user_logged_in()) {
  775.             wp_redirect(home_url('login'));
  776.             exit;
  777.         } else {
  778.  
  779.             // Error messages
  780.             $errors = array();
  781.             if (isset($_REQUEST['error'])) {
  782.                 $error_codes = explode(',', $_REQUEST['error']);
  783.  
  784.                 foreach ($error_codes as $code) {
  785.                     $errors [] = $this->get_error_message($code);
  786.                 }
  787.             }
  788.             if (isset($_REQUEST['password_change_status'])) {
  789.                 $attributes['status'] = $_REQUEST['password_change_status'] == 'success' ? "Password Changes Successfully!" : "Something wrong!";
  790.             }
  791.             $attributes['errors'] = $errors;
  792.             $attributes['action_url'] = home_url('change-password');
  793.  
  794.             return $this->get_template_html('change_password_form', $attributes);
  795.  
  796.         }
  797.     }
  798.  
  799.     public function custom_change_password_post()
  800.     {
  801.         if ('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['change-password-button'])) {
  802.             global $wpdb, $wp_hasher;
  803.             $user_id = get_current_user_id();
  804.             $user = get_userdata($user_id);
  805.             $redirect_url = home_url('change-password');
  806.  
  807.             if (!$user || is_wp_error($user)) {
  808.                 wp_redirect(home_url('login'));
  809.                 exit;
  810.             }
  811.  
  812.             if (!isset($_POST['pass_old']) || !isset($_POST['pass1']) || !isset($_POST['pass2']) ||
  813.                 empty($_POST['pass_old']) || empty($_POST['pass1']) || empty($_POST['pass2'])
  814.             ) {
  815.                 $redirect_url = add_query_arg('error', 'all_fields_required', $redirect_url);
  816.                 wp_redirect($redirect_url);
  817.                 exit;
  818.             }
  819.             $old = $_POST['pass_old'];
  820.             $new1 = $_POST['pass1'];
  821.             $new2 = $_POST['pass2'];
  822.             /*
  823.             var_dump(array(
  824.                 'a' => $user->user_pass,
  825.                 'b' => wp_hash_password($old)
  826.             ));
  827.             */
  828.             $wp_hasher = new PasswordHash(8, TRUE);
  829.  
  830.             if (!$wp_hasher->CheckPassword($old, $user->user_pass)) {
  831.                 $redirect_url = add_query_arg('error', 'wrong_old_password', $redirect_url);
  832.                 wp_redirect($redirect_url);
  833.                 exit;
  834.             }
  835.  
  836.             if ($new1 != $new2) {
  837.                 $redirect_url = add_query_arg('error', 'password_change_mismatch', $redirect_url);
  838.                 wp_redirect($redirect_url);
  839.                 exit;
  840.             }
  841.  
  842.             if (strlen($new1) < 6) {
  843.                 $redirect_url = add_query_arg('error', 'short_password', $redirect_url);
  844.                 wp_redirect($redirect_url);
  845.                 exit;
  846.             }
  847.  
  848.             // Parameter checks OK, reset password
  849.  
  850.             $redirect_url = add_query_arg('password_change_status', 'success', $redirect_url);
  851.  
  852.             $this->change_password($user, $_POST['pass1'], $redirect_url);
  853.  
  854.             /*
  855.             if (isset($_POST['pass1'])) {
  856.                 if ($_POST['pass1'] != $_POST['pass2']) {
  857.                     // Passwords don't match
  858.                     $redirect_url = home_url('password-reset');
  859.  
  860.                     $redirect_url = add_query_arg('key', $rp_key, $redirect_url);
  861.                     $redirect_url = add_query_arg('login', $rp_login, $redirect_url);
  862.                     $redirect_url = add_query_arg('error', 'password_reset_mismatch', $redirect_url);
  863.  
  864.                     wp_redirect($redirect_url);
  865.                     exit;
  866.                 }
  867.  
  868.                 if (empty($_POST['pass1'])) {
  869.                     // Password is empty
  870.                     $redirect_url = home_url('password-reset');
  871.  
  872.                     $redirect_url = add_query_arg('key', $rp_key, $redirect_url);
  873.                     $redirect_url = add_query_arg('login', $rp_login, $redirect_url);
  874.                     $redirect_url = add_query_arg('error', 'password_reset_empty', $redirect_url);
  875.  
  876.                     wp_redirect($redirect_url);
  877.                     exit;
  878.                 }
  879.  
  880.                 // Parameter checks OK, reset password
  881.                 reset_password($user, $_POST['pass1']);
  882.                 wp_redirect(home_url('login?password=changed'));
  883.             } else {
  884.                 echo "Invalid request.";
  885.             }*/
  886.  
  887.             //exit;
  888.         }
  889.  
  890.     }
  891.  
  892.     private function change_password($user, $new_password, $redirect_url)
  893.     {
  894.         global $wpdb;
  895.         $update = $wpdb->query($wpdb->prepare("UPDATE {$wpdb->users} SET `user_pass` = %s WHERE `ID` = %d", array(wp_hash_password($new_password), $user->ID)));
  896.  
  897.         if (!is_wp_error($update)) {
  898.             wp_cache_delete($user->ID, 'users');
  899.             wp_cache_delete($user->user_login, 'userlogins');
  900.             $_SESSION['local_logout'] = true;
  901.             wp_logout();
  902.             if (wp_signon(array('user_login' => $user->user_login, 'user_password' => $new_password), false)):
  903.                 wp_redirect($redirect_url);
  904.             endif;
  905.         }
  906.     }
  907.  
  908.     public function password_hint($hint)
  909.     {
  910.         return __("Hint: The password should be at least six characters long.
  911.        To make it stronger, use upper and lower case letters, numbers, and symbols like ! \" ? $ % ^ & ).", 'personalize-login');
  912.     }
  913. }
  914.  
  915.  
  916. if (!function_exists('wp_new_user_notification')) :
  917.     function wp_new_user_notification($user_id, $deprecated = null, $notify = '')
  918.     {
  919.         if ($deprecated !== null) {
  920.             _deprecated_argument(__FUNCTION__, '4.3.1');
  921.         }
  922.  
  923.         global $wpdb, $wp_hasher;
  924.         $user = get_userdata($user_id);
  925.  
  926.         // The blogname option is escaped with esc_html on the way into the database in sanitize_option
  927.         // we want to reverse this for the plain text arena of emails.
  928.         $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  929.  
  930.         $message = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
  931.         $message .= sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n";
  932.         $message .= sprintf(__('E-mail: %s'), $user->user_email) . "\r\n";
  933.  
  934.         @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);
  935.  
  936.         if ('admin' === $notify || empty($notify)) {
  937.             return;
  938.         }
  939.  
  940.         // Generate something random for a password reset key.
  941.         $key = wp_generate_password(20, false);
  942.  
  943.         /** This action is documented in wp-login.php */
  944.         do_action('retrieve_password_key', $user->user_login, $key);
  945.  
  946.         // Now insert the key, hashed, into the DB.
  947.         if (empty($wp_hasher)) {
  948.             require_once ABSPATH . WPINC . '/class-phpass.php';
  949.             $wp_hasher = new PasswordHash(8, true);
  950.         }
  951.         $hashed = time() . ':' . $wp_hasher->HashPassword($key);
  952.         $wpdb->update($wpdb->users, array('user_activation_key' => $hashed), array('user_login' => $user->user_login));
  953.  
  954.         $link = home_url("password-reset/?action=rp&wlgrp_type=new&key=$key&login=" . rawurlencode($user->user_login));
  955.  
  956.         global $wlg_notify;
  957.         $wlg_notify->confirm($user->user_email, $user->user_login, $link);
  958.     }
  959. endif;
  960.  
  961. $custom_members = new Custom_Members();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement