<?php

global $unsublink2;
global $woocommerce;
$emailusers = unserialize($emails->sendmail_to);
if (in_array($user_id, $emailusers)) {
    foreach ($emailusers as $myuser) {
        $user = get_userdata($myuser);
        if (get_user_meta($myuser, 'unsub_value', true) != 'yes') {
            $user_wmpl_lang = get_user_meta($myuser, 'rs_wpml_lang', true);
            if (empty($user_wmpl_lang)) {
                $user_wmpl_lang = 'en';
            }
            $to = $user->user_email;
            $subject = RSWPMLSupport::fp_rs_get_wpml_text('rs_template_' . $emails->id . '_subject', $user_wmpl_lang, $emails->subject);
            $firstname = $user->user_firstname;
            $lastname = $user->user_lastname;
            $url_to_click = site_url();
            $userpoint = RSPointExpiry::total_available_points_in_site($myuser);
            $roundofftype = get_option('rs_round_off_type') == '1' ? '2' : '0';
            $points_in_value = display_total_currency_value($user_id);
            $userpoint = round_off_type($userpoint);
            $minimumuserpoints = $emails->minimum_userpoints;
            $mailsendredeempoint = $emails->redeemingpoints;
            $wpnonce = wp_create_nonce('rs_unsubscribe_' . $myuser);
            $unsublink = esc_url_raw(add_query_arg(array('userid' => $myuser, 'unsub' => 'yes', 'nonce' => $wpnonce), site_url()));
            if ($mailsendredeempoint == '') {
                $mailsendredeempoint = 0;
            }
            if ($minimumuserpoints == '') {
                $minimumuserpoints = 0;
            } else {
                $minimumuserpoints = $emails->minimum_userpoints;
            }
            if ($userpoint != 0 && $userpoint >= $minimumuserpoints) {
                $getredeemedpoints = rs_common_function_to_get_redeem_points_for_order($order_id);

                if ($getredeemedpoints >= $mailsendredeempoint) {

                    $message = RSWPMLSupport::fp_rs_get_wpml_text('rs_template_' . $emails->id . '_message', $user_wmpl_lang, $emails->message);
                    $message = str_replace('{rssitelink}', $url_to_click, $message);
                    $message = str_replace('{rsfirstname}', $firstname, $message);
                    $message = str_replace('{rslastname}', $lastname, $message);
                    $message = str_replace('{rspoints}', $userpoint, $message);
                    if (strpos($message, '{rs_points_in_currency}') !== false) {
                        $message = str_replace('{rs_points_in_currency}', $points_in_value, $message);
                    }
                    $levelname = RSMemberFunction::current_level_name($myuser, 'email');
                    if (strpos($message, '[rs_my_current_earning_level_name]') !== false) {
                        $message = str_replace('[rs_my_current_earning_level_name]', $levelname, $message);
                    }
                    $nextlevelname = RSMemberFunction::next_earning_level_points($myuser, 'email');
                    if (strpos($message, '[rs_next_earning_level_points]') !== false) {
                        $message = str_replace('[rs_next_earning_level_points]', $nextlevelname, $message);
                    }
                    $message = $message;
                    $message = do_shortcode($message); //shortcode feature

                    $unsublink1 = get_option('rs_unsubscribe_link_for_email');
                    $unsublink2 = str_replace('{rssitelinkwithid}', $unsublink, $unsublink1);

                    add_filter('woocommerce_email_footer_text', 'footer_link');

                    ob_start();
                    wc_get_template('emails/email-header.php', array('email_heading' => $subject));
                    echo $message;
                    wc_get_template('emails/email-footer.php');
                    $woo_temp_msg = ob_get_clean();
                    $headers = "MIME-Version: 1.0\r\n";
                    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
                    if ($emails->sender_opt == 'local') {
                        FPRewardSystem::$rs_from_email_address = $emails->from_email;
                        FPRewardSystem::$rs_from_name = $emails->from_name;
                    }
                    add_filter('woocommerce_email_from_address', 'rs_alter_from_email_of_woocommerce', 10, 2);
                    add_filter('woocommerce_email_from_name', 'rs_alter_from_name_of_woocommerce', 10, 2);
                    if ('2' == get_option('rs_select_mail_function')) {
                        if ((float) $woocommerce->version <= (float) ('2.2.0')) {
                            if (wp_mail($to, $subject, $woo_temp_msg, $headers = '')) {
                                
                            }
                        } else {
                            $mailer = WC()->mailer();
                            $mailer->send($to, $subject, $woo_temp_msg, $headers);
                        }
                    } elseif ('1' == get_option('rs_select_mail_function')) {
                        if (mail($to, $subject, $woo_temp_msg, $headers)) {
                            
                        }
                    } else {
                        if ((float) $woocommerce->version <= (float) ('2.2.0')) {
                            if (wp_mail($to, $subject, $woo_temp_msg, $headers = '')) {
                                
                            }
                        } else {
                            $mailer = WC()->mailer();
                            $mailer->send($to, $subject, $woo_temp_msg, $headers);
                        }
                    }
                    remove_filter('woocommerce_email_from_address', 'rs_alter_from_email_of_woocommerce', 10, 2);
                    remove_filter('woocommerce_email_from_name', 'rs_alter_from_name_of_woocommerce', 10, 2);
                    FPRewardSystem::$rs_from_email_address = false;
                    FPRewardSystem::$rs_from_name = false;
                }
            }
        }
    }
}
?>
