| 1 |
<?php |
| 2 |
/** |
| 3 |
* Email functions. |
| 4 |
* |
| 5 |
* Helper wrappers around wp_mail() used by the theme, plus the AJAX handler |
| 6 |
* that backs the front-end contact form (both the classic and Elementor |
| 7 |
* contact widgets). Provides the default "From"/"Reply-To" identity and a |
| 8 |
* single send routine that switches between plain-text and HTML headers. |
| 9 |
* |
| 10 |
* @package wpstream-theme |
| 11 |
*/ |
| 12 |
|
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
// Only define once; guards against redeclaration when the theme/plugin overlap. |
| 20 |
if ( ! function_exists( 'wpstream_theme_return_sending_email' ) ) { |
| 21 |
/** |
| 22 |
* Build the default "From" identity used for outgoing theme emails. |
| 23 |
* |
| 24 |
* @return string A formatted "Name <email>" sender string. |
| 25 |
*/ |
| 26 |
function wpstream_theme_return_sending_email() { |
| 27 |
// Placeholder sender address; intended to be customised per install. |
| 28 |
$from_email = 'noreply@changeme.net'; |
| 29 |
// Placeholder display name shown as the sender. |
| 30 |
$name_email = 'changeME'; |
| 31 |
|
| 32 |
// Assemble the RFC-style "Name <email>" header value. |
| 33 |
return $name_email . ' <' . $from_email . '>'; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
if ( ! function_exists( 'wpstream_theme_send_emails' ) ) { |
| 39 |
/** |
| 40 |
* Send emails. |
| 41 |
* |
| 42 |
* @param string $user_email The email address of the recipient. |
| 43 |
* @param string $subject The subject of the email. |
| 44 |
* @param string $message The message content of the email. |
| 45 |
* @param string $email_type The type of the email (text or HTML). |
| 46 |
* @param string $reply_to The reply-to email address. |
| 47 |
* @param string|array $extra_headers Extra headers to include in the email. |
| 48 |
*/ |
| 49 |
function wpstream_theme_send_emails( $user_email, $subject, $message, $email_type, $reply_to = '', $extra_headers = '' ) { |
| 50 |
// When no explicit reply-to is passed, reply-to defaults to the sender identity. |
| 51 |
if ( '' === $reply_to ) { |
| 52 |
$reply_to = wpstream_theme_return_sending_email(); |
| 53 |
} |
| 54 |
|
| 55 |
// Default headers: HTML body with UTF-8 encoding. |
| 56 |
$headers = 'From: ' . wpstream_theme_return_sending_email() . "\r\n" . |
| 57 |
'Reply-To:' . $reply_to . "\r\n" . |
| 58 |
'Content-Type: text/html; charset="UTF-8"' . "\r\n" . |
| 59 |
'Content-Transfer-Encoding: 8bit' . "\r\n" . |
| 60 |
'MIME-Version: 1.0' . "\r\n" . |
| 61 |
'X-Mailer: PHP/' . phpversion(); |
| 62 |
|
| 63 |
// Override the Content-Type with a plain-text variant when requested. |
| 64 |
if ( 'text' === $email_type ) { |
| 65 |
$headers = 'From: ' . wpstream_theme_return_sending_email() . "\r\n" . |
| 66 |
'Reply-To:' . $reply_to . "\r\n" . |
| 67 |
'Content-Type: text/plain ; charset="UTF-8"' . "\r\n" . |
| 68 |
'Content-Transfer-Encoding: 8bit' . "\r\n" . |
| 69 |
'MIME-Version: 1.0' . "\r\n" . |
| 70 |
'X-Mailer: PHP/' . phpversion(); |
| 71 |
} |
| 72 |
|
| 73 |
// Append any caller-supplied extra headers to the base header block. |
| 74 |
$headers = $headers . $extra_headers; |
| 75 |
|
| 76 |
// Dispatch the email; stripslashes undoes any slashes added by WP on input. |
| 77 |
$sent = wp_mail( |
| 78 |
$user_email, |
| 79 |
stripslashes( $subject ), |
| 80 |
stripslashes( $message ), |
| 81 |
$headers |
| 82 |
); |
| 83 |
|
| 84 |
// Log a failure so delivery problems are visible in the error log. |
| 85 |
if ( ! $sent ) { |
| 86 |
error_log( 'Failed to send email to ' . $user_email ); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
* |
| 93 |
* Ajax adv search contact function |
| 94 |
* |
| 95 |
*/ |
| 96 |
|
| 97 |
|
| 98 |
if ( ! function_exists( 'wpstream_theme_post_text' ) ) { |
| 99 |
/** |
| 100 |
* Read a POST field as trimmed text. |
| 101 |
* |
| 102 |
* Missing fields and non-scalar payloads (name[]=x style) both come back |
| 103 |
* as '', so callers can validate "empty" without type juggling warnings. |
| 104 |
* |
| 105 |
* @param string $key POST field name. |
| 106 |
* @return string Trimmed scalar value, or '' when absent/non-scalar. |
| 107 |
*/ |
| 108 |
function wpstream_theme_post_text( $key ) { |
| 109 |
return ( isset( $_POST[ $key ] ) && is_scalar( $_POST[ $key ] ) ) ? trim( (string) $_POST[ $key ] ) : ''; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// Register the contact-form AJAX handler for both logged-out and logged-in users. |
| 114 |
add_action( 'wp_ajax_nopriv_wpstream_ajax_contact_function', 'wpstream_ajax_contact_function' ); |
| 115 |
add_action( 'wp_ajax_wpstream_ajax_contact_function', 'wpstream_ajax_contact_function' ); |
| 116 |
|
| 117 |
// Guard against redeclaration before defining the handler. |
| 118 |
if( !function_exists('wpstream_ajax_contact_function') ): |
| 119 |
|
| 120 |
/** |
| 121 |
* AJAX handler for the front-end contact form. |
| 122 |
* |
| 123 |
* Validates the submitted fields, builds a plain message body, and emails it |
| 124 |
* to the site admin. Handles both the classic contact form and the Elementor |
| 125 |
* contact-form builder (which sends a pre-composed comment only). |
| 126 |
* Echoes a JSON payload describing success/failure and exits. |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
function wpstream_ajax_contact_function(){ |
| 131 |
|
| 132 |
|
| 133 |
// check for POST vars |
| 134 |
// Accumulator for the outgoing message body. |
| 135 |
$message = ''; |
| 136 |
// Error flag (declared but not actively used below). |
| 137 |
$hasError = false; |
| 138 |
// Empty allowlist means wp_kses strips all HTML from user input. |
| 139 |
$allowed_html = array(); |
| 140 |
// Scratch output buffer (declared but not actively used below). |
| 141 |
$to_print = ''; |
| 142 |
// Sanitized message body; stays '' when the Elementor branch gets |
| 143 |
// no comment field ($name/$email are always set before use). |
| 144 |
$comment = ''; |
| 145 |
// Reject the request unless the contact-form nonce is valid. |
| 146 |
if ( !wp_verify_nonce( $_POST['nonce'] ?? '', 'ajax-property-contact')) { |
| 147 |
exit("No naughty business please"); |
| 148 |
} |
| 149 |
|
| 150 |
// Flag: 1 when the submission comes from the Elementor contact widget. |
| 151 |
$is_elementor_contact_builder = intval($_POST['is_elementor'] ?? 0); |
| 152 |
|
| 153 |
// Classic contact form path: validate name/email/comment individually |
| 154 |
// (each read defaults to '' so a missing field is just an empty one). |
| 155 |
if($is_elementor_contact_builder==0){ |
| 156 |
// Validate the name field. |
| 157 |
$posted_name = wpstream_theme_post_text('name'); |
| 158 |
// Empty name (or the untouched placeholder) is treated as invalid. |
| 159 |
if( $posted_name =='' || $posted_name ==esc_html__( 'Your Name','hello-wpstream') ){ |
| 160 |
echo json_encode(array('sent'=>false, 'response'=>esc_html__( 'The name field is empty !','hello-wpstream') )); |
| 161 |
exit(); |
| 162 |
}else { |
| 163 |
// Sanitize the name (strips all HTML). |
| 164 |
$name = wp_kses( $posted_name,$allowed_html ); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
//Check email |
| 169 |
$posted_email = wpstream_theme_post_text('email'); |
| 170 |
// Empty email is invalid. |
| 171 |
if( $posted_email ==''){ |
| 172 |
echo json_encode(array('sent'=>false, 'response'=>esc_html__( 'The email field is empty','hello-wpstream' ) ) ); |
| 173 |
exit(); |
| 174 |
// Reject malformed email addresses. |
| 175 |
} else if( filter_var($posted_email,FILTER_VALIDATE_EMAIL) === false) { |
| 176 |
echo json_encode(array('sent'=>false, 'response'=>esc_html__( 'The email doesn\'t look right !','hello-wpstream') ) ); |
| 177 |
exit(); |
| 178 |
} else { |
| 179 |
// Sanitize the accepted email address. |
| 180 |
$email = wp_kses( $posted_email,$allowed_html ); |
| 181 |
} |
| 182 |
|
| 183 |
//Check comments |
| 184 |
$posted_comment = wpstream_theme_post_text('comment'); |
| 185 |
// Empty message (or the untouched placeholder) is invalid. |
| 186 |
if( $posted_comment =='' || $posted_comment ==esc_html__( 'Your Message','hello-wpstream')){ |
| 187 |
echo json_encode(array('sent'=>false, 'response'=>esc_html__( 'Your message is empty !','hello-wpstream') ) ); |
| 188 |
exit(); |
| 189 |
}else { |
| 190 |
// Sanitize the message body. |
| 191 |
$comment = wp_kses($posted_comment ,$allowed_html ); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
// Prepend the collected name and email to the message body. |
| 196 |
$message .= esc_html__('Client Name','hello-wpstream').": " . $name . PHP_EOL; |
| 197 |
$message .= esc_html__('Email','hello-wpstream').": " . $email . PHP_EOL; |
| 198 |
// Optional website field, when supplied. |
| 199 |
$posted_website = wpstream_theme_post_text('website'); |
| 200 |
if( $posted_website !== '' ){ |
| 201 |
$website = wp_kses( $posted_website,$allowed_html ); |
| 202 |
$message .= esc_html__('Website','hello-wpstream').": " . $website . PHP_EOL; |
| 203 |
} |
| 204 |
|
| 205 |
}else{ |
| 206 |
// Elementor contact-builder path: use its pre-composed comment only. |
| 207 |
$posted_comment = wpstream_theme_post_text('comment'); |
| 208 |
if ( $posted_comment !== '' ) { |
| 209 |
$comment = wp_kses($posted_comment ,$allowed_html ); |
| 210 |
// Convert literal "/n" tokens into real line breaks. |
| 211 |
$comment = str_replace('/n',PHP_EOL,$comment); |
| 212 |
|
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
// Default subject line records the site the form was submitted from. |
| 218 |
$subject =esc_html__( 'Contact form from ','hello-wpstream') . esc_url( home_url('/') ) ; |
| 219 |
// Deliver to the site's configured admin email. |
| 220 |
$receiver_email = esc_html(get_option('admin_email') ); |
| 221 |
// Append the message body and a provenance note. |
| 222 |
$message .= esc_html__('Message','hello-wpstream').": ".PHP_EOL." " . $comment. PHP_EOL; |
| 223 |
$message .= esc_html__('Message sent from contact page','hello-wpstream'). PHP_EOL; |
| 224 |
|
| 225 |
|
| 226 |
// Derive the host for a fallback From header (note: unused by the send call below). |
| 227 |
$site_web_url = parse_url(home_url(), PHP_URL_HOST); |
| 228 |
$headers = 'From: No Reply <noreply@' . $site_web_url . '>' . "\r\n"; |
| 229 |
|
| 230 |
// Elementor widgets may supply their own subject line. |
| 231 |
if(isset($_POST['elementor_email_subject'])){ |
| 232 |
$subject = sanitize_text_field( $_POST['elementor_email_subject']); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
// Send the composed contact email to the site admin. |
| 239 |
wpstream_theme_send_emails( $receiver_email, $subject, $message, '' ); |
| 240 |
|
| 241 |
|
| 242 |
// Report success back to the AJAX caller and stop execution. |
| 243 |
echo json_encode(array('sent'=>true,'data'=>true, 'response'=>esc_html__( 'The message was sent !','hello-wpstream') ) ); |
| 244 |
die(); |
| 245 |
} |
| 246 |
|
| 247 |
endif; // end wpstream_theme_ajax_agent_contact_form |
| 248 |
|
| 249 |
|