pack' ) ); } if ( username_exists( $username ) ) { $username .= '-' . zeroise( wp_rand( 0, 9999 ), 4 ); } $data['username'] = $username; $data['password'] = wp_generate_password( apply_filters( 'pp_login_form_password_length', 12 ), true, false ); $user_id = wp_insert_user( array( 'user_login' => $data['username'], 'user_pass' => $data['password'], 'user_email' => $email, 'first_name' => isset( $data['first_name'] ) ? $data['first_name'] : '', 'last_name' => isset( $data['last_name'] ) ? $data['last_name'] : '', ) ); if ( is_wp_error( $user_id ) ) { wp_send_json_error( $user_id->get_error_message() ); } update_user_meta( $user_id, 'pp_login_form_provider', $data['provider'] ); $this->do_social_login( $data, true ); } } public function process_lost_password() { if ( ! isset( $_POST['pp-lf-lost-password-nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['pp-lf-lost-password-nonce'] ), 'lost_password' ) ) { wp_send_json_error( __( 'Invalid data.', 'powerpack' ) ); } $success = $this->retrieve_password(); if ( ! $success ) { wp_send_json_error( $this->form_error ); } wp_send_json_success( array( 'redirect_url' => $this->get_redirect_url(), ) ); } private function retrieve_password() { $login = isset( $_POST['user_login'] ) ? sanitize_user( wp_unslash( $_POST['user_login'] ) ) : ''; // phpcs:ignore input var ok, CSRF ok. if ( empty( $login ) ) { $this->form_error = __( 'Enter a username or email address.', 'powerpack' ); return false; } else { // Check on username first, as customers can use emails as usernames. $user_data = get_user_by( 'login', $login ); } // If no user found, check if it login is email and lookup user based on email. if ( ! $user_data && is_email( $login ) ) { $user_data = get_user_by( 'email', $login ); } $errors = new \WP_Error(); do_action( 'lostpassword_post', $errors ); if ( $errors->get_error_code() ) { $this->form_error = $errors->get_error_message(); return false; } if ( ! $user_data ) { $this->form_error = __( 'Invalid username or email.', 'powerpack' ); return false; } if ( is_multisite() && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) { $this->form_error = __( 'Invalid username or email.', 'powerpack' ); return false; } // Redefining user_login ensures we return the right case in the email. $user_login = $user_data->user_login; do_action( 'retrieve_password', $user_login ); $allow = apply_filters( 'allow_password_reset', true, $user_data->ID ); if ( ! $allow ) { $this->form_error = __( 'Password reset is not allowed for this user', 'powerpack' ); return false; } elseif ( is_wp_error( $allow ) ) { $this->form_error = $errors->get_error_message(); return false; } // Get password reset key (function introduced in WordPress 4.4). $key = get_password_reset_key( $user_data ); $page_url = esc_url_raw( $_POST['page_url'] ); $reset_url = add_query_arg( array( 'reset_pass' => 1, 'key' => $key, 'id' => $user_data->ID, ), $page_url ); // Send email notification. $email_sent = $this->send_activation_email( $user_data, $reset_url ); if ( $email_sent ) { $this->form_error = esc_html__( 'An error occurred sending email. Please try again.', 'powerpack' ); } return $email_sent; } private function send_activation_email( $user, $reset_url ) { $email = $user->data->user_email; $blogname = esc_html( wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) ); $admin_email = get_option( 'admin_email' ); $subject = sprintf( esc_html__( 'Password Reset Request for %s', 'powerpack' ), $blogname ); $content = ''; /* translators: %s: Username */ $content .= '

' . sprintf( esc_html__( 'Hi %s,', 'powerpack' ), esc_html( $user->data->user_login ) ) . '

'; /* translators: %s: Site name */ $content .= '

' . sprintf( esc_html__( 'Someone has requested a new password for the following account on %s:', 'powerpack' ), $blogname ) . '

'; /* translators: %s Username */ $content .= '

' . sprintf( esc_html__( 'Username: %s', 'powerpack' ), esc_html( $user->data->user_login ) ) . '

'; $content .= esc_html__( 'If you didn\'t make this request, just ignore this email. If you\'d like to proceed:', 'powerpack' ); $content .= '

'; $content .= ''; $content .= esc_html__( 'Click here to reset your password', 'powerpack' ); $content .= ''; $content .= '

'; // translators: %s: email_from_name $headers = sprintf( 'From: %s <%s>' . "\r\n", $blogname, get_option( 'admin_email' ) ); // translators: %s: email_reply_to $headers .= sprintf( 'Reply-To: %s' . "\r\n", $admin_email ); $headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n"; // Send email to user. $email_sent = wp_mail( $email, $subject, $content, $headers ); return $email_sent; } public function process_reset_password() { if ( ! isset( $_POST['pp-lf-reset-password-nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['pp-lf-reset-password-nonce'] ), 'reset_password' ) ) { wp_send_json_error( __( 'Invalid data.', 'powerpack' ) ); } $posted_fields = array( 'password_1', 'password_2', 'reset_key', 'reset_login' ); foreach ( $posted_fields as $field ) { if ( ! isset( $_POST[ $field ] ) ) { wp_send_json_error( __( 'Invalid data.', 'powerpack' ) ); } if ( in_array( $field, array( 'password_1', 'password_2' ) ) ) { //phpcs:ignore // Don't unslash password fields $posted_fields[ $field ] = $_POST[ $field ]; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash } else { $posted_fields[ $field ] = wp_unslash( $_POST[ $field ] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized } } if ( empty( $posted_fields['password_1'] ) ) { $this->form_error = __( 'Please enter your password.', 'powerpack' ); } elseif ( $posted_fields['password_1'] !== $posted_fields['password_2'] ) { $this->form_error = __( 'Passwords do not match.', 'powerpack' ); } $user = $this->check_password_reset_key( $posted_fields['reset_key'], $posted_fields['reset_login'] ); if ( is_object( $user ) && empty( $this->form_error ) ) { $errors = new \WP_Error(); do_action( 'validate_password_reset', $errors, $user ); if ( is_wp_error( $errors ) && $errors->get_error_messages() ) { foreach ( $errors->get_error_messages() as $error ) { $this->form_error .= $error . "\r\n"; } } $this->reset_password( $user, $posted_fields['password_1'] ); do_action( 'pp_login_form_user_reset_password', $user ); wp_send_json_success(); } if ( ! empty( $this->form_error ) ) { wp_send_json_error( $this->form_error ); } wp_send_json_error( __( 'Unknown error', 'powerpack' ) ); //phpcs:ignore } public function check_password_reset_key( $key, $login ) { // Check for the password reset key. // Get user data or an error message in case of invalid or expired key. $user = check_password_reset_key( $key, $login ); if ( is_wp_error( $user ) ) { $this->form_error = __( 'This key is invalid or has already been used. Please reset your password again if needed.', 'powerpack' ); return false; } return $user; } /** * Handles resetting the user's password. * * @param object $user The user. * @param string $new_pass New password for the user in plaintext. */ private function reset_password( $user, $new_pass ) { do_action( 'password_reset', $user, $new_pass ); wp_set_password( $new_pass, $user->ID ); $this->set_reset_password_cookie(); if ( ! apply_filters( 'pp_login_form_disable_password_change_notification', false ) ) { wp_password_change_notification( $user ); } } /** * Set or unset the cookie. * * @param string $value Cookie value. */ private function set_reset_password_cookie( $value = '' ) { $rp_cookie = 'wp-resetpass-' . COOKIEHASH; $rp_path = isset( $_POST['page_url'] ) ? current( explode( '?', wp_unslash( $_POST['page_url'] ) ) ) : ''; // WPCS: input var ok, sanitization ok. if ( $value ) { setcookie( $rp_cookie, $value, 0, $rp_path, COOKIE_DOMAIN, is_ssl(), true ); } else { setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true ); } } private function get_redirect_url() { if ( ! empty( $_POST['redirect'] ) ) { $redirect = wp_unslash( $_POST['redirect'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized } elseif ( $this->get_raw_referer() ) { $redirect = $this->get_raw_referer(); } else { $redirect = wp_unslash( $_POST['page_url'] ); } return wp_validate_redirect( $redirect, wp_unslash( $_POST['page_url'] ) ); } /** * Get raw referer. * * @since 1.5.0 * @access private */ private function get_raw_referer() { if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) { // phpcs:ignore input var ok, CSRF ok. return wp_unslash( $_REQUEST['_wp_http_referer'] ); // phpcs:ignore input var ok, CSRF ok, sanitization ok. } elseif ( ! empty( $_SERVER['HTTP_REFERER'] ) ) { // phpcs:ignore input var ok, CSRF ok. return wp_unslash( $_SERVER['HTTP_REFERER'] ); // phpcs:ignore input var ok, CSRF ok, sanitization ok. } return false; } public function get_error_message() { return $this->form_error; } }