/**
 * Support/Contact form handler - sent from React to admin-ajax
 *
 * @return void
 */
function vip_contact_form_handler()
{
    if (!isset($_POST['body'], $_POST['subject'], $_GET['_wpnonce'])) {
        $return = array('status' => 'error', 'message' => __('Please complete all required fields.', 'vip-dashboard'));
        echo wp_json_encode($return);
        die;
    }
    if (!wp_verify_nonce($_GET['_wpnonce'], 'vip-dashboard')) {
        $return = array('status' => 'error', 'message' => __('Security check failed. Make sure you should be doing this, and try again.', 'vip-dashboard'));
        echo wp_json_encode($return);
        die;
    }
    $vipsupportemailaddy = '*****@*****.**';
    $cc_headers_to_kayako = '';
    $sendemail = true;
    $emailsent = false;
    $current_user = wp_get_current_user();
    $name = !empty($_POST['name']) ? strip_tags(stripslashes($_POST['name'])) : $current_user->display_name;
    $email = !empty($_POST['email']) ? strip_tags(stripslashes($_POST['email'])) : $current_user->user_email;
    if (!is_email($email)) {
        $return = array('status' => 'error', 'message' => __('Please enter a valid email for your ticket.', 'vip-dashboard'));
        echo wp_json_encode($return);
        die;
    }
    $subject = !empty($_POST['subject']) ? strip_tags(stripslashes($_POST['subject'])) : '';
    $group = !empty($_POST['type']) ? strip_tags(stripslashes($_POST['type'])) : 'Technical';
    $priority = !empty($_POST['priority']) ? strip_tags(stripslashes($_POST['priority'])) : 'Medium';
    $ccemail = !empty($_POST['cc']) ? strip_tags(stripslashes($_POST['cc'])) : '';
    $temp_ccemails = explode(',', $ccemail);
    $temp_ccemails = array_filter(array_map('trim', $temp_ccemails));
    $ccemails = array();
    if (!empty($temp_ccemails)) {
        foreach (array_values($temp_ccemails) as $value) {
            if (is_email($value)) {
                $ccemails[] = $value;
            }
        }
    }
    $ccemails = apply_filters('vip_contact_form_cc', $ccemails);
    if (count($ccemails)) {
        $cc_headers_to_kayako .= 'CC: ' . implode(',', $ccemails) . "\r\n";
    }
    if (empty($subject)) {
        $return = array('status' => 'error', 'message' => __('Please enter a descriptive subject for your ticket.', 'vip-dashboard'));
        echo wp_json_encode($return);
        die;
    }
    if ('' === $_POST['body']) {
        $return = array('status' => 'error', 'message' => __('Please enter a detailed description of your issue.', 'vip-dashboard'));
        echo wp_json_encode($return);
        die;
    }
    if ('Emergency' === $priority) {
        $subject = sprintf('[%s] %s', $priority, $subject);
    }
    $content = stripslashes($_POST['body']) . "\n\n--- Ticket Details --- \n";
    if ($priority) {
        $content .= "\nPriority: " . $priority;
    }
    $content .= "\nUser: "******"\nSite Name: " . get_bloginfo('name');
    $content .= "\nSite URLs: " . site_url() . ' | ' . admin_url();
    $content .= "\nTheme: " . get_option('stylesheet') . ' | ' . $theme->get('Name');
    // added for VIPv2.
    $content .= "\nPlatform: VIP Go";
    // send date and time.
    $content .= sprintf("\n\nSent from %s on %s", home_url(), date('c', current_time('timestamp', 1)));
    // Filter from name/email. NOTE - not un-hooking the filter because we die() immediately after wp_mail()
    add_filter('wp_mail_from', function () use($email) {
        return $email;
    });
    add_filter('wp_mail_from_name', function () use($name) {
        return $name;
    });
    $headers = "From: \"{$name}\" <{$email}>\r\n";
    if (wp_mail($vipsupportemailaddy, $subject, $content, $headers . $cc_headers_to_kayako)) {
        $return = array('status' => 'success', 'message' => __('Your support request is on its way, we will be in touch soon.', 'vip-dashboard'));
        echo wp_json_encode($return);
        die;
    } else {
        $manual_link = vip_echo_mailto_vip_hosting(__('Please send in a request manually.', 'vip-dashboard'), false);
        $return = array('status' => 'error', 'message' => sprintf(__('There was an error sending the support request. %1$s', 'vip-dashboard'), $manual_link));
        echo wp_json_encode($return);
        die;
    }
    die;
}
/**
 * Support/Contact form handler - sent from React to admin-ajax
 *
 * @return json
 */
function vip_contact_form_handler()
{
    // check for required fields and nonce
    if (!isset($_POST['body'], $_POST['subject'], $_GET['_wpnonce'])) {
        $return = array('status' => 'error', 'message' => __('Please complete all required fields.', 'vip-dashboard'));
        echo json_encode($return);
        die;
    }
    // check nonce is valid
    if (!wp_verify_nonce($_GET['_wpnonce'], 'vip-dashboard')) {
        $return = array('status' => 'error', 'message' => __('Security check failed. Make sure you should be doing this, and try again.', 'vip-dashboard'));
        echo json_encode($return);
        die;
    }
    // settings
    $vipsupportemailaddy = '*****@*****.**';
    $cc_headers_to_kayako = '';
    // default values
    $sendemail = true;
    // Should we send an e-mail? Tracks errors.
    $emailsent = false;
    // Tracks wp_mail() results
    $new_tmp_name = false;
    // For an attachment
    $current_user = wp_get_current_user();
    // Current user
    // name & email
    $name = !empty($_POST['name']) ? strip_tags(stripslashes($_POST['name'])) : $current_user->display_name;
    $email = !empty($_POST['email']) ? strip_tags(stripslashes($_POST['email'])) : $current_user->user_email;
    // check for valid email
    if (!is_email($email)) {
        $return = array('status' => 'error', 'message' => __('Please enter a valid email for your ticket.', 'vip-dashboard'));
        echo json_encode($return);
        die;
    }
    // subject, group, & priority
    $subject = !empty($_POST['subject']) ? strip_tags(stripslashes($_POST['subject'])) : '';
    $group = !empty($_POST['type']) ? strip_tags(stripslashes($_POST['type'])) : 'Technical';
    $priority = !empty($_POST['priority']) ? strip_tags(stripslashes($_POST['priority'])) : 'Medium';
    // cc
    $ccemail = !empty($_POST['cc']) ? strip_tags(stripslashes($_POST['cc'])) : '';
    $temp_ccemails = explode(',', $ccemail);
    $temp_ccemails = array_filter(array_map('trim', $temp_ccemails));
    $ccemails = array();
    if (!empty($temp_ccemails)) {
        foreach (array_values($temp_ccemails) as $value) {
            if (is_email($value)) {
                $ccemails[] = $value;
            }
        }
    }
    $ccemails = apply_filters('vip_contact_form_cc', $ccemails);
    if (count($ccemails)) {
        $cc_headers_to_kayako .= 'CC: ' . implode(',', $ccemails) . "\r\n";
    }
    // check subject is not empty
    if (empty($subject)) {
        $return = array('status' => 'error', 'message' => __('Please enter a descriptive subject for your ticket.', 'vip-dashboard'));
        echo json_encode($return);
        die;
    }
    // check body is not empty
    if ($_POST['body'] == '') {
        $return = array('status' => 'error', 'message' => __('Please enter a detailed description of your issue.', 'vip-dashboard'));
        echo json_encode($return);
        die;
    }
    if ('Emergency' === $priority) {
        $subject = sprintf('[%s] %s', $priority, $subject);
    }
    $content = stripslashes($_POST['body']) . "\n\n--- Ticket Details --- \n";
    // priority
    if (!empty($_POST['vipsupport-priority'])) {
        $content .= "\nPriority: " . $priority;
    }
    $content .= "\nUser: "******"\nSite Name: " . get_bloginfo('name');
    $content .= "\nSite URLs: " . site_url() . ' | ' . admin_url();
    $content .= "\nTheme: " . get_option('stylesheet') . ' | ' . $theme->get('Name');
    // added for VIPv2
    $content .= "\nPlatform: VIPv2";
    // send date and time
    $content .= sprintf("\n\nSent from %s on %s", home_url(), date('c', current_time('timestamp', 1)));
    // attachments - currently not in use of VIPv2
    $attachments = array();
    if (!empty($_FILES['vipsupport-attachment']) && 4 != $_FILES['vipsupport-attachment']['error']) {
        if (0 != $_FILES['vipsupport-attachment']['error'] || empty($_FILES['vipsupport-attachment']['tmp_name'])) {
            $sendemail = false;
            switch ($_FILES['vipsupport-attachment']['error']) {
                case 1:
                case 2:
                    $max_upload_size = vip_dashboard_contact_form_get_max_upload_size();
                    add_settings_error('vipsupport', 'attachment_error', sprintf('Your uploaded file was too large. Our ticketing system can only accept files up to %s big. Try using <a href="http://www.dropbox.com/">Dropbox</a>, <a href="https://www.yousendit.com/">YouSendIt</a>, or hosting it on a FTP server instead.', $max_upload_size['human']), 'error');
                    break;
                case 3:
                    add_settings_error('vipsupport', 'attachment_error', 'Your uploaded file only partially uploaded. Please try again.', 'error');
                    break;
                default:
                    add_settings_error('vipsupport', 'attachment_error', 'There was an error with the attachment upload.', 'error');
            }
        } else {
            // We need the filename to be correct
            // Don't forget to delete the file manually when done since it's been renamed!
            $new_tmp_name = str_replace(basename($_FILES['vipsupport-attachment']['tmp_name']), $_FILES['vipsupport-attachment']['name'], $_FILES['vipsupport-attachment']['tmp_name']);
            rename($_FILES['vipsupport-attachment']['tmp_name'], $new_tmp_name);
            $attachments = array($new_tmp_name);
        }
    }
    // send the email and unlink upload if required
    $headers = "From: \"{$name}\" <{$email}>\r\n";
    if (wp_mail($vipsupportemailaddy, $subject, $content, $headers . $cc_headers_to_kayako, $attachments)) {
        if ($new_tmp_name) {
            unlink($new_tmp_name);
        }
        $return = array('status' => 'success', 'message' => __('Your support request is on its way, we will be in touch soon.', 'vip-dashboard'));
        echo json_encode($return);
        die;
    } else {
        if ($new_tmp_name) {
            unlink($new_tmp_name);
        }
        $manual_link = vip_echo_mailto_vip_hosting(__('Please send in a request manually.', 'vip-dashboard'), false);
        $return = array('status' => 'error', 'message' => sprintf(__('There was an error sending the support request. %1$s', 'vip-dashboard'), $manual_link));
        echo json_encode($return);
        die;
    }
    die;
}