230 lines
7.2 KiB
PHP
Executable File
230 lines
7.2 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Contact Form 7 Hooks
|
|
*
|
|
* Handles programmatic email routing for agent contact forms
|
|
* and property inquiry forms.
|
|
*
|
|
* @package HomeProz
|
|
*/
|
|
|
|
// Prevent direct access
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Send additional email to specific agent after form submission
|
|
*
|
|
* This hook fires after CF7 successfully sends the main email.
|
|
* It checks for agent-related hidden fields and sends a copy to
|
|
* the specific agent.
|
|
*
|
|
* @param WPCF7_ContactForm $contact_form The contact form object
|
|
*/
|
|
function homeproz_wpcf7_send_agent_copy($contact_form) {
|
|
$submission = WPCF7_Submission::get_instance();
|
|
|
|
if (!$submission) {
|
|
return;
|
|
}
|
|
|
|
$posted_data = $submission->get_posted_data();
|
|
|
|
// Check if this is an agent contact form (has agent-email field)
|
|
if (!empty($posted_data['agent-email'])) {
|
|
homeproz_send_agent_contact_copy($posted_data, $contact_form);
|
|
}
|
|
|
|
// Check if this is a property inquiry form (has listing-key field)
|
|
if (!empty($posted_data['listing-key'])) {
|
|
homeproz_send_property_inquiry_agent_copy($posted_data, $contact_form);
|
|
}
|
|
}
|
|
add_action('wpcf7_mail_sent', 'homeproz_wpcf7_send_agent_copy');
|
|
|
|
/**
|
|
* Send copy of agent contact form to the specific agent
|
|
*
|
|
* @param array $posted_data Form submission data
|
|
* @param WPCF7_ContactForm $contact_form The contact form object
|
|
*/
|
|
function homeproz_send_agent_contact_copy($posted_data, $contact_form) {
|
|
$agent_email = sanitize_email($posted_data['agent-email']);
|
|
$agent_name = sanitize_text_field($posted_data['agent-name'] ?? 'Agent');
|
|
|
|
// Validate email
|
|
if (!is_email($agent_email)) {
|
|
return;
|
|
}
|
|
|
|
// Build email content
|
|
$sender_name = sanitize_text_field($posted_data['your-name'] ?? $posted_data['name'] ?? 'Website Visitor');
|
|
$sender_email = sanitize_email($posted_data['your-email'] ?? $posted_data['email'] ?? '');
|
|
$sender_phone = sanitize_text_field($posted_data['your-phone'] ?? $posted_data['phone'] ?? '');
|
|
$message = sanitize_textarea_field($posted_data['your-message'] ?? $posted_data['message'] ?? $posted_data['comments'] ?? '');
|
|
$default_message = sanitize_textarea_field($posted_data['default-message'] ?? '');
|
|
|
|
$subject = sprintf('[HomeProz] New Contact Form Message for %s', $agent_name);
|
|
|
|
$body = "Hello {$agent_name},\n\n";
|
|
$body .= "You have received a new message through the HomeProz website contact form.\n\n";
|
|
$body .= "---\n\n";
|
|
|
|
if ($default_message) {
|
|
$body .= "Introduction:\n{$default_message}\n\n";
|
|
}
|
|
|
|
if ($message) {
|
|
$body .= "Additional Message:\n{$message}\n\n";
|
|
}
|
|
|
|
$body .= "---\n\n";
|
|
$body .= "Contact Information:\n";
|
|
$body .= "Name: {$sender_name}\n";
|
|
|
|
if ($sender_email) {
|
|
$body .= "Email: {$sender_email}\n";
|
|
}
|
|
|
|
if ($sender_phone) {
|
|
$body .= "Phone: {$sender_phone}\n";
|
|
}
|
|
|
|
$body .= "\n---\n";
|
|
$body .= "This is an automated copy sent to you as the contacted agent.\n";
|
|
$body .= "The office has also received a copy of this submission.";
|
|
|
|
$headers = array('Content-Type: text/plain; charset=UTF-8');
|
|
|
|
if ($sender_email) {
|
|
$headers[] = 'Reply-To: ' . $sender_name . ' <' . $sender_email . '>';
|
|
}
|
|
|
|
wp_mail($agent_email, $subject, $body, $headers);
|
|
}
|
|
|
|
/**
|
|
* Send copy of property inquiry form to the listing agent
|
|
*
|
|
* For HomeProz listings (is_homeproz = 1), looks up the listing agent
|
|
* and sends them a copy of the inquiry.
|
|
*
|
|
* @param array $posted_data Form submission data
|
|
* @param WPCF7_ContactForm $contact_form The contact form object
|
|
*/
|
|
function homeproz_send_property_inquiry_agent_copy($posted_data, $contact_form) {
|
|
$listing_key = sanitize_text_field($posted_data['listing-key']);
|
|
|
|
if (!$listing_key || !function_exists('mls_get_property')) {
|
|
return;
|
|
}
|
|
|
|
// Get the property from MLS database
|
|
$property = mls_get_property($listing_key);
|
|
|
|
if (!$property) {
|
|
return;
|
|
}
|
|
|
|
// Only send agent copy for HomeProz listings
|
|
if (empty($property->is_homeproz) || !$property->is_homeproz) {
|
|
return;
|
|
}
|
|
|
|
// Try to find the listing agent by MLS ID
|
|
$list_agent_id = $property->list_agent_mls_id ?? '';
|
|
|
|
if (!$list_agent_id) {
|
|
return;
|
|
}
|
|
|
|
// Query for agent with matching MLS ID
|
|
$agents = get_posts(array(
|
|
'post_type' => 'agent',
|
|
'posts_per_page' => 1,
|
|
'post_status' => 'publish',
|
|
'meta_query' => array(
|
|
array(
|
|
'key' => 'agent_mls_id',
|
|
'value' => $list_agent_id,
|
|
'compare' => '=',
|
|
),
|
|
),
|
|
));
|
|
|
|
if (empty($agents)) {
|
|
return;
|
|
}
|
|
|
|
$agent = $agents[0];
|
|
$agent_email = get_field('agent_email', $agent->ID);
|
|
$agent_name = $agent->post_title;
|
|
$agent_disabled = get_field('agent_disabled', $agent->ID);
|
|
|
|
// Don't send to disabled agents
|
|
if ($agent_disabled || !is_email($agent_email)) {
|
|
return;
|
|
}
|
|
|
|
// Build email content
|
|
$sender_name = sanitize_text_field($posted_data['your-name'] ?? $posted_data['name'] ?? 'Website Visitor');
|
|
$sender_email = sanitize_email($posted_data['your-email'] ?? $posted_data['email'] ?? '');
|
|
$sender_phone = sanitize_text_field($posted_data['your-phone'] ?? $posted_data['phone'] ?? '');
|
|
$message = sanitize_textarea_field($posted_data['your-message'] ?? $posted_data['message'] ?? $posted_data['comments'] ?? '');
|
|
$default_message = sanitize_textarea_field($posted_data['default-message'] ?? '');
|
|
$property_address = sanitize_text_field($posted_data['property-address'] ?? '');
|
|
$listing_id = sanitize_text_field($posted_data['listing-id'] ?? '');
|
|
$property_url = esc_url($posted_data['property-url'] ?? '');
|
|
|
|
$subject = sprintf('[HomeProz] Property Inquiry - %s', $property_address ?: 'MLS# ' . $listing_id);
|
|
|
|
$body = "Hello {$agent_name},\n\n";
|
|
$body .= "You have received a new property inquiry for one of your HomeProz listings.\n\n";
|
|
$body .= "---\n\n";
|
|
|
|
$body .= "Property Details:\n";
|
|
if ($property_address) {
|
|
$body .= "Address: {$property_address}\n";
|
|
}
|
|
if ($listing_id) {
|
|
$body .= "MLS#: {$listing_id}\n";
|
|
}
|
|
if ($property_url) {
|
|
$body .= "View Listing: {$property_url}\n";
|
|
}
|
|
$body .= "\n";
|
|
|
|
if ($default_message) {
|
|
$body .= "Inquiry:\n{$default_message}\n\n";
|
|
}
|
|
|
|
if ($message) {
|
|
$body .= "Additional Message:\n{$message}\n\n";
|
|
}
|
|
|
|
$body .= "---\n\n";
|
|
$body .= "Contact Information:\n";
|
|
$body .= "Name: {$sender_name}\n";
|
|
|
|
if ($sender_email) {
|
|
$body .= "Email: {$sender_email}\n";
|
|
}
|
|
|
|
if ($sender_phone) {
|
|
$body .= "Phone: {$sender_phone}\n";
|
|
}
|
|
|
|
$body .= "\n---\n";
|
|
$body .= "This is an automated copy sent to you as the listing agent.\n";
|
|
$body .= "The office has also received a copy of this inquiry.";
|
|
|
|
$headers = array('Content-Type: text/plain; charset=UTF-8');
|
|
|
|
if ($sender_email) {
|
|
$headers[] = 'Reply-To: ' . $sender_name . ' <' . $sender_email . '>';
|
|
}
|
|
|
|
wp_mail($agent_email, $subject, $body, $headers);
|
|
}
|