Exemplo n.º 1
0
function ajax_placester_contact()
{
    if (!empty($_POST)) {
        $error = "";
        $message = "A prospective client wants to get in touch with you. \n\n";
        // Check to make sure that the name field is not empty
        if (trim($_POST['name']) == '' || trim($_POST['name']) == 'Name') {
            $error .= "Your name is required<br/>";
        } else {
            $message .= "Name: " . trim($_POST['name']) . " \n";
        }
        // Check to make sure sure that a valid email address is submitted
        if (trim($_POST['email']) == '' || trim($_POST['email']) == 'Email Address') {
            $error .= "An email address is required<br/>";
        } else {
            if (!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z]{2,4}\$/i", trim($_POST['email']))) {
                $error .= "A valid email address is required<br/>";
            } else {
                $message .= "Email Address: " . trim($_POST['email']) . " \n";
            }
        }
        // Check to make sure that the phone number field is not empty
        if (isset($_POST['phone'])) {
            if (trim($_POST['phone']) == '' || trim($_POST['phone']) == 'Phone Number') {
                $error .= "Your phone number is required<br/>";
            } else {
                $message .= "Phone Number: " . trim($_POST['phone']) . " \n";
            }
        }
        // Check the subject field
        if (isset($_POST['subject'])) {
            if (trim($_POST['subject']) == '') {
                // $message .= "They did not include a subject \n\n ";
                $subject = '';
            } else {
                $message .= "Subject: " . trim($_POST['subject']) . " \n";
                $subject = ': ' . trim($_POST['subject']);
            }
        }
        // Check the departments field
        if (isset($_POST['department'])) {
            if (trim($_POST['department']) == '') {
                // $message .= "They didn't select a department \n\n ";
            } else {
                $message .= "Requested Departments: " . trim($_POST['department']) . " \n";
            }
        }
        // Check the question field
        if (trim($_POST['question']) == '') {
            $message .= "They left no comment nor question. \n\n ";
        } else {
            $message .= "Questions: " . trim($_POST['question']) . " \n";
        }
        if (empty($_POST['id'])) {
            // $message .= "Listing ID: No specific listing \n";
        } else {
            $message .= "Listing ID: " . trim($_POST['id']) . " \n";
        }
        if (trim($_POST['fullAddress']) == '') {
            // $message .= "Listing Address: No specific listing \n";
        } else {
            $message .= "Listing Address: " . $_POST['fullAddress'] . " \n";
        }
        $message .= "\n";
        $message .= "This message was sent from the contact form at: \n" . $_SERVER['HTTP_REFERER'] . " \n";
        if (empty($error)) {
            $api_whoami = PLS_Plugin_API::get_user_details();
            $user_email = @pls_get_option('pls-user-email');
            // Check what email to send the form to...
            if (!empty($user_email)) {
                $email = $user_email;
            } elseif (!empty($api_whoami['user']['email'])) {
                $email = $api_whoami['user']['email'];
            } else {
                $email = $api_whoami['email'];
            }
            if (trim($_POST['send_to_email']) == true) {
                $email = $_POST['send_to_email'];
            }
            $headers = array();
            if (!empty($_POST['cc_value'])) {
                $headers[] = 'Cc: ' . $_POST['cc_value'];
            }
            if (!empty($_POST['bcc_value'])) {
                $headers[] = 'Bcc: ' . $_POST['bcc_value'];
            }
            // Append form title
            if (!empty($_POST['form_title'])) {
                $message .= "This message was sent from the contact form named: \n" . $_POST['form_title'];
            }
            // Append form's custom link
            if (!empty($_POST['custom_link'])) {
                $message .= "The visitor was sent to: \n" . $_POST['custom_link'];
            }
            if (trim($_POST['email_confirmation']) == true) {
                wp_mail($email, 'Email confirmation was sent to ' . $_POST['email'] . ' from ' . home_url(), $message, $headers);
            } elseif ($email) {
                $placester_Mail = wp_mail($email, 'Prospective client from ' . home_url(), $message, PLS_Plugin_API::merge_bcc_forwarding_addresses_for_sending($headers));
            }
            $name = $_POST['name'];
            PLS_Plugin_API::create_person(array('metadata' => array('name' => $name, 'email' => $_POST['email'])));
            // Send a email confirmation
            if (trim($_POST['email_confirmation']) == true) {
                ob_start();
                include get_template_directory() . '/custom/contact-form-email.php';
                $message_to_submitter = ob_get_clean();
                wp_mail($_POST['email'], 'Form Submitted' . $subject, $message_to_submitter);
            }
            // As long as there are no errors we'll allow custom links to override
            // the normal form submission.
            if (!empty($_POST['custom_link'])) {
                return false;
            }
            echo "sent";
        } else {
            echo $error;
        }
        die;
    }
}