protected function send_reg_email()
 {
     global $wpdb;
     if (empty($this->member_info)) {
         return false;
     }
     $member_info = $this->member_info;
     $settings = SwpmSettings::get_instance();
     $subject = $settings->get_value('reg-complete-mail-subject');
     $body = $settings->get_value('reg-complete-mail-body');
     $from_address = $settings->get_value('email-from');
     $login_link = $settings->get_value('login-page-url');
     $headers = 'From: ' . $from_address . "\r\n";
     $member_info['membership_level_name'] = SwpmPermission::get_instance($member_info['membership_level'])->get('alias');
     $member_info['password'] = $member_info['plain_password'];
     $member_info['login_link'] = $login_link;
     $values = array_values($member_info);
     $keys = array_map('swpm_enclose_var', array_keys($member_info));
     $body = html_entity_decode($body);
     $body = str_replace($keys, $values, $body);
     $swpm_user = SwpmMemberUtils::get_user_by_user_name($member_info['user_name']);
     $member_id = $swpm_user->member_id;
     $body = SwpmMiscUtils::replace_dynamic_tags($body, $member_id);
     //Do the standard merge var replacement.
     $email = sanitize_email(filter_input(INPUT_POST, 'email', FILTER_UNSAFE_RAW));
     //Send notification email to the member
     wp_mail(trim($email), $subject, $body, $headers);
     SwpmLog::log_simple_debug('Member notification email sent to: ' . $email, true);
     if ($settings->get_value('enable-admin-notification-after-reg')) {
         //Send notification email to the site admin
         $admin_notification = $settings->get_value('admin-notification-email');
         $admin_notification = empty($admin_notification) ? $from_address : $admin_notification;
         $notify_emails_array = explode(",", $admin_notification);
         $headers = 'From: ' . $from_address . "\r\n";
         $subject = "Notification of New Member Registration";
         $admin_notify_body = $settings->get_value('reg-complete-mail-body-admin');
         if (empty($admin_notify_body)) {
             $admin_notify_body = "A new member has completed the registration.\n\n" . "Username: {user_name}\n" . "Email: {email}\n\n" . "Please login to the admin dashboard to view details of this user.\n\n" . "You can customize this email message from the Email Settings menu of the plugin.\n\n" . "Thank You";
         }
         $admin_notify_body = SwpmMiscUtils::replace_dynamic_tags($admin_notify_body, $member_id);
         //Do the standard merge var replacement.
         foreach ($notify_emails_array as $to_email) {
             $to_email = trim($to_email);
             wp_mail($to_email, $subject, $admin_notify_body, $headers);
             SwpmLog::log_simple_debug('Admin notification email sent to: ' . $to_email, true);
         }
     }
     return true;
 }
 public function reset_password($email)
 {
     $email = sanitize_email($email);
     if (!is_email($email)) {
         $message = '<div class="swpm-reset-pw-error">' . SwpmUtils::_("Email address not valid.") . '</div>';
         $message = array('succeeded' => false, 'message' => $message);
         SwpmTransfer::get_instance()->set('status', $message);
         return;
     }
     global $wpdb;
     $query = 'SELECT member_id,user_name,first_name, last_name FROM ' . $wpdb->prefix . 'swpm_members_tbl ' . ' WHERE email = %s';
     $user = $wpdb->get_row($wpdb->prepare($query, $email));
     if (empty($user)) {
         $message = '<div class="swpm-reset-pw-error">' . SwpmUtils::_("No user found with that email address.") . '</div>';
         $message .= '<div class="swpm-reset-pw-error-email">' . SwpmUtils::_("Email Address: ") . $email . '</div>';
         $message = array('succeeded' => false, 'message' => $message);
         SwpmTransfer::get_instance()->set('status', $message);
         return;
     }
     $settings = SwpmSettings::get_instance();
     $password = wp_generate_password();
     $password_hash = SwpmUtils::encrypt_password(trim($password));
     //should use $saned??;
     $wpdb->update($wpdb->prefix . "swpm_members_tbl", array('password' => $password_hash), array('member_id' => $user->member_id));
     //Update wp user password
     add_filter('send_password_change_email', array(&$this, 'dont_send_password_change_email'), 1, 3);
     //Stop wordpress from sending a reset password email to admin.
     SwpmUtils::update_wp_user($user->user_name, array('plain_password' => $password));
     $body = $settings->get_value('reset-mail-body');
     $subject = $settings->get_value('reset-mail-subject');
     $body = html_entity_decode($body);
     $additional_args = array('password' => $password);
     $body = SwpmMiscUtils::replace_dynamic_tags($body, $user->member_id, $additional_args);
     $from = $settings->get_value('email-from');
     $headers = "From: " . $from . "\r\n";
     wp_mail($email, $subject, $body, $headers);
     SwpmLog::log_simple_debug("Member password has been reset. Password reset email sent to: " . $email, true);
     $message = '<div class="swpm-reset-pw-success">' . SwpmUtils::_("New password has been sent to your email address.") . '</div>';
     $message .= '<div class="swpm-reset-pw-success-email">' . SwpmUtils::_("Email Address: ") . $email . '</div>';
     $message = array('succeeded' => false, 'message' => $message);
     SwpmTransfer::get_instance()->set('status', $message);
 }
function swpm_handle_subsc_signup_stand_alone($ipn_data, $subsc_ref, $unique_ref, $swpm_id = '')
{
    global $wpdb;
    $settings = SwpmSettings::get_instance();
    $members_table_name = $wpdb->prefix . "swpm_members_tbl";
    $membership_level_table = $wpdb->prefix . "swpm_membership_tbl";
    $membership_level = $subsc_ref;
    $subscr_id = $unique_ref;
    swpm_debug_log_subsc("swpm_handle_subsc_signup_stand_alone(). Custom value: " . $ipn_data['custom'] . ", Unique reference: " . $unique_ref, true);
    $custom_vars = parse_str($ipn_data['custom']);
    if (empty($swpm_id)) {
        //Lets try to find an existing user profile for this payment
        $email = $ipn_data['payer_email'];
        $query_db = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$members_table_name} WHERE email = %s", $email), OBJECT);
        if (!$query_db) {
            //try to retrieve the member details based on the unique_ref
            swpm_debug_log_subsc("Could not find any record using the given email address (" . $email . "). Attempting to query database using the unique reference: " . $unique_ref, true);
            if (!empty($unique_ref)) {
                $query_db = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$members_table_name} WHERE subscr_id = %s", $unique_ref), OBJECT);
                if ($query_db) {
                    $swpm_id = $query_db->member_id;
                    swpm_debug_log_subsc("Found a match in the member database using unique reference. Member ID: " . $swpm_id, true);
                } else {
                    swpm_debug_log_subsc("Did not find a match for an existing member profile for the given reference. This must me a new payment from a new member.", true);
                }
            } else {
                swpm_debug_log_subsc("Unique reference is missing in the notification so we have to assume that this is not a payment for an existing member.", true);
            }
        } else {
            $swpm_id = $query_db->member_id;
            swpm_debug_log_subsc("Found a match in the member database. Member ID: " . $swpm_id, true);
        }
    }
    if (!empty($swpm_id)) {
        //This is payment from an existing member/user. Update the existing member account
        swpm_debug_log_subsc("Modifying the existing membership profile... Member ID: " . $swpm_id, true);
        //Upgrade the member account
        $account_state = 'active';
        //This is renewal or upgrade of a previously active account. So the status should be set to active
        $subscription_starts = date("Y-m-d");
        $resultset = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$members_table_name} where member_id=%d", $swpm_id), OBJECT);
        if (!$resultset) {
            swpm_debug_log_subsc("ERROR! Could not find a member account record for the given Member ID: " . $swpm_id, false);
            return;
        }
        $old_membership_level = $resultset->membership_level;
        swpm_debug_log_subsc("Upgrading the current membership level (" . $old_membership_level . ") of this member to the newly paid level (" . $membership_level . ")", true);
        $updatedb = $wpdb->prepare("UPDATE {$members_table_name} SET account_state=%s, membership_level=%d,subscription_starts=%s,subscr_id=%s WHERE member_id=%d", $account_state, $membership_level, $subscription_starts, $subscr_id, $swpm_id);
        $results = $wpdb->query($updatedb);
        do_action('swpm_membership_changed', array('member_id' => $swpm_id, 'from_level' => $old_membership_level, 'to_level' => $membership_level));
        //Set Email details for the account upgrade notification
        $email = $ipn_data['payer_email'];
        $subject = $settings->get_value('upgrade-complete-mail-subject');
        if (empty($subject)) {
            $subject = "Member Account Upgraded";
        }
        $body = $settings->get_value('upgrade-complete-mail-body');
        if (empty($body)) {
            $body = "Your account has been upgraded successfully";
        }
        $from_address = $settings->get_value('email-from');
        $additional_args = array();
        $email_body = SwpmMiscUtils::replace_dynamic_tags($body, $swpm_id, $additional_args);
        $headers = 'From: ' . $from_address . "\r\n";
    } else {
        // create new member account
        $default_account_status = $settings->get_value('default-account-status', 'active');
        $data = array();
        $data['user_name'] = '';
        $data['password'] = '';
        $data['first_name'] = $ipn_data['first_name'];
        $data['last_name'] = $ipn_data['last_name'];
        $data['email'] = $ipn_data['payer_email'];
        $data['membership_level'] = $membership_level;
        $data['subscr_id'] = $unique_ref;
        $data['gender'] = 'not specified';
        swpm_debug_log_subsc("Creating new member account. Membership level ID: " . $membership_level, true);
        $data['address_street'] = $ipn_data['address_street'];
        $data['address_city'] = $ipn_data['address_city'];
        $data['address_state'] = $ipn_data['address_state'];
        $data['address_zipcode'] = isset($ipn_data['address_zip']) ? $ipn_data['address_zip'] : '';
        $data['country'] = isset($ipn_data['address_country']) ? $ipn_data['address_country'] : '';
        $data['member_since'] = $data['subscription_starts'] = $data['last_accessed'] = date("Y-m-d");
        $data['account_state'] = $default_account_status;
        $reg_code = uniqid();
        $md5_code = md5($reg_code);
        $data['reg_code'] = $md5_code;
        $data['referrer'] = $data['extra_info'] = $data['txn_id'] = '';
        $data['subscr_id'] = $subscr_id;
        $data['last_accessed_from_ip'] = isset($user_ip) ? $user_ip : '';
        //Save the users IP address
        $data = array_filter($data);
        //Remove any null values.
        $wpdb->insert($members_table_name, $data);
        //Create the member record
        $results = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$members_table_name} where subscr_id=%s and reg_code=%s", $subscr_id, $md5_code), OBJECT);
        $id = $results->member_id;
        //Alternatively use $wpdb->insert_id;
        if (empty($id)) {
            swpm_debug_log_subsc("Error! Failed to insert a new member record. This request will fail.", false);
            return;
        }
        $separator = '?';
        $url = $settings->get_value('registration-page-url');
        if (strpos($url, '?') !== false) {
            $separator = '&';
        }
        $reg_url = $url . $separator . 'member_id=' . $id . '&code=' . $md5_code;
        swpm_debug_log_subsc("Member signup URL: " . $reg_url, true);
        $subject = $settings->get_value('reg-prompt-complete-mail-subject');
        if (empty($subject)) {
            $subject = "Please complete your registration";
        }
        $body = $settings->get_value('reg-prompt-complete-mail-body');
        if (empty($body)) {
            $body = "Please use the following link to complete your registration. \n {reg_link}";
        }
        $from_address = $settings->get_value('email-from');
        $body = html_entity_decode($body);
        $additional_args = array('reg_link' => $reg_url);
        $email_body = SwpmMiscUtils::replace_dynamic_tags($body, $id, $additional_args);
        $headers = 'From: ' . $from_address . "\r\n";
    }
    wp_mail($email, $subject, $email_body, $headers);
    swpm_debug_log_subsc("Member signup/upgrade completion email successfully sent to: " . $email, true);
}