'', 'name' => '', 'email' => '', 'message' => '', 'subject' => '', 'all_fields' => '', ); $vars = wp_parse_args( $vars, $defaults ); foreach ( $vars as $key => $value ) { $text = str_replace( '{' . $key . '}', $value, $text ); } return $text; } /** * Throttle public submissions per client IP. * * Without a limit the public form, and in particular the auto reply, could be * used to send any number of messages from this site to any address. * * @return bool True when this client has reached the limit. */ function acscf_submission_rate_limited() { if ( current_user_can( acscf_manage_capability() ) ) { return false; } $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; /** * Filters how many submissions one IP address may send in ten minutes. * * @param int $limit Maximum submissions. 0 disables the limit. * @param string $ip Client IP address. */ $limit = (int) apply_filters( 'acscf_submission_rate_limit', 5, $ip ); if ( $limit < 1 || '' === $ip ) { return false; } $key = 'acscf_rate_' . md5( $ip ); $count = (int) get_transient( $key ); if ( $count >= $limit ) { return true; } set_transient( $key, $count + 1, 10 * MINUTE_IN_SECONDS ); return false; } /** * Handle a form submission sent through admin-ajax. * * @return void */ function acscf_submit_form() { if ( ! isset( $_POST['form_id'] ) || ! check_ajax_referer( 'acscf_frontend_nonce', false, false ) ) { wp_send_json_error( array( 'message' => __( 'Your session has expired. Please reload the page.', 'acs-contact-form' ) ) ); } $form_id = isset( $_POST['form_id'] ) ? absint( wp_unslash( $_POST['form_id'] ) ) : 0; $post = get_post( $form_id ); if ( ! $post || 'acscf_form' !== $post->post_type ) { wp_send_json_error( array( 'message' => __( 'Invalid form request.', 'acs-contact-form' ) ) ); } // Same rule as the shortcode: only published forms accept submissions from the public. if ( 'publish' !== $post->post_status && ! current_user_can( 'edit_post', $form_id ) ) { wp_send_json_error( array( 'message' => __( 'Invalid form request.', 'acs-contact-form' ) ) ); } if ( acscf_submission_rate_limited() ) { wp_send_json_error( array( 'message' => __( 'The request failed. Please wait a moment and try again.', 'acs-contact-form' ) ) ); } $settings = acscf_get_settings(); $honeypot_value = isset( $_POST['acscf_hp_email'] ) ? sanitize_text_field( wp_unslash( $_POST['acscf_hp_email'] ) ) : ''; $is_honeypot = ! acscf_validate_honeypot( $honeypot_value ); $save_entries = '1' === $settings['save_entries']; // Settings of this form. $standard_fields = get_post_meta( $form_id, '_acscf_standard_fields', true ); if ( empty( $standard_fields ) ) { $standard_fields = acscf_get_default_standard_fields(); } $custom_fields = get_post_meta( $form_id, '_acscf_custom_fields', true ); if ( ! is_array( $custom_fields ) ) { $custom_fields = array(); } $email_settings = acscf_normalize_email_settings( get_post_meta( $form_id, '_acscf_email_settings', true ) ); $file_settings = get_post_meta( $form_id, '_acscf_file_settings', true ); if ( empty( $file_settings ) ) { $file_settings = acscf_get_default_file_settings(); } $field_order = acscf_get_field_order( $form_id, $standard_fields, $custom_fields, $file_settings ); if ( empty( $field_order ) ) { $field_order = acscf_build_default_field_order( $standard_fields, $custom_fields, $file_settings ); } // Raw input. $posted_sf = array(); $posted_cf_data = array(); if ( isset( $_POST['acscf_sf'] ) && is_array( $_POST['acscf_sf'] ) ) { $posted_sf = map_deep( wp_unslash( $_POST['acscf_sf'] ), 'sanitize_textarea_field' ); } if ( isset( $_POST['acscf_data'] ) && is_array( $_POST['acscf_data'] ) ) { $posted_cf_data = map_deep( wp_unslash( $_POST['acscf_data'] ), 'sanitize_textarea_field' ); } $sanitized_sf_data = array(); $entry_fields = array(); $all_fields_text = ''; foreach ( $field_order as $order_item ) { if ( 0 === strpos( $order_item, 'standard:' ) ) { $key = substr( $order_item, strlen( 'standard:' ) ); if ( ! isset( $standard_fields[ $key ] ) || '1' !== $standard_fields[ $key ]['enabled'] ) { continue; } $field = $standard_fields[ $key ]; $value = isset( $posted_sf[ $key ] ) && is_scalar( $posted_sf[ $key ] ) ? (string) $posted_sf[ $key ] : ''; if ( 'message' === $key ) { $clean = sanitize_textarea_field( $value ); } elseif ( 'email' === $key ) { $clean = sanitize_email( $value ); if ( '1' === $field['required'] && ! is_email( $clean ) ) { /* translators: %s: field label. */ wp_send_json_error( array( 'message' => sprintf( __( '%s is not valid.', 'acs-contact-form' ), $field['label'] ) ) ); } } else { $clean = sanitize_text_field( $value ); } if ( '1' === $field['required'] && '' === $clean ) { /* translators: %s: field label. */ wp_send_json_error( array( 'message' => sprintf( __( '%s is required.', 'acs-contact-form' ), $field['label'] ) ) ); } $sanitized_sf_data[ $key ] = $clean; $entry_fields[] = array( 'label' => $field['label'], 'value' => $clean, ); $all_fields_text .= '■ ' . $field['label'] . "\n" . $clean . "\n\n"; } elseif ( 0 === strpos( $order_item, 'custom:' ) ) { $index = (int) substr( $order_item, strlen( 'custom:' ) ); if ( ! isset( $custom_fields[ $index ] ) ) { continue; } $custom_field = $custom_fields[ $index ]; $label = sanitize_text_field( $custom_field['label'] ); $value = isset( $posted_cf_data[ $index ] ) ? $posted_cf_data[ $index ] : ''; if ( is_array( $value ) ) { $value_text = implode( ', ', array_map( 'sanitize_text_field', $value ) ); } else { $value_text = 'textarea' === $custom_field['type'] ? sanitize_textarea_field( $value ) : sanitize_text_field( $value ); } if ( isset( $custom_field['required'] ) && '1' === $custom_field['required'] && '' === $value_text ) { /* translators: %s: field label. */ wp_send_json_error( array( 'message' => sprintf( __( '%s is required.', 'acs-contact-form' ), $label ) ) ); } $entry_fields[] = array( 'label' => $label, 'value' => $value_text, ); $all_fields_text .= '■ ' . $label . "\n" . $value_text . "\n\n"; } } // Attachments. $attachments = array(); if ( ! empty( $file_settings['enabled'] ) && '1' === $file_settings['enabled'] ) { $raw_files = isset( $_FILES['acscf_files'] ) ? map_deep( wp_unslash( $_FILES['acscf_files'] ), 'sanitize_text_field' ) : array(); $upload_result = acscf_handle_uploads( $form_id, $file_settings, $raw_files ); if ( is_wp_error( $upload_result ) ) { wp_send_json_error( array( 'message' => $upload_result->get_error_message() ) ); } $attachments = $upload_result; } $form_name = $post->post_title; $sender_name = isset( $sanitized_sf_data['name'] ) ? $sanitized_sf_data['name'] : ''; $sender_email = isset( $sanitized_sf_data['email'] ) ? $sanitized_sf_data['email'] : ''; $sender_message = isset( $sanitized_sf_data['message'] ) ? $sanitized_sf_data['message'] : ''; $sender_subject = isset( $sanitized_sf_data['subject'] ) ? $sanitized_sf_data['subject'] : ''; // Classification. $category = acscf_classify( array( 'subject' => $sender_subject, 'body' => $all_fields_text, 'email' => $sender_email, 'honeypot' => $is_honeypot, ) ); $notify = acscf_should_notify( $category ); // Storage. $entry_id = 0; if ( $save_entries ) { $entry_id = acscf_store_entry( array( 'form_id' => $form_id, 'form_name' => $form_name, 'name' => $sender_name, 'email' => $sender_email, 'subject' => $sender_subject, 'message' => $sender_message, 'fields' => $entry_fields, 'attachments' => $attachments, 'category' => $category, 'referer' => wp_get_referer() ? wp_get_referer() : '', 'ip' => '1' === $settings['store_ip'] ? acscf_get_remote_ip() : '', ) ); } $mail_sent = false; if ( $notify ) { $replace_vars = array( 'form_name' => $form_name, 'name' => $sender_name, 'email' => $sender_email, 'message' => $sender_message, 'subject' => $sender_subject, 'all_fields' => trim( $all_fields_text ), ); $to = ! empty( $email_settings['to'] ) ? $email_settings['to'] : get_option( 'admin_email' ); $admin_subject = acscf_replace_mail_placeholders( isset( $email_settings['subject'] ) ? $email_settings['subject'] : '', $replace_vars ); if ( '' === trim( $admin_subject ) ) { /* translators: %s: form name. */ $admin_subject = sprintf( __( '[%s] New inquiry', 'acs-contact-form' ), $form_name ); } $admin_body = __( 'A new inquiry was submitted with the following details.', 'acs-contact-form' ) . "\n\n"; $admin_body .= $all_fields_text; $admin_body .= "\n---\n"; /* translators: %s: category label. */ $admin_body .= sprintf( __( 'Category: %s', 'acs-contact-form' ), acscf_category_label( $category ) ) . "\n"; /* translators: %s: source URL. */ $admin_body .= sprintf( __( 'Submitted from: %s', 'acs-contact-form' ), wp_get_referer() ? wp_get_referer() : '-' ) . "\n"; if ( '1' === $settings['store_ip'] ) { /* translators: %s: IP address. */ $admin_body .= sprintf( __( 'IP address: %s', 'acs-contact-form' ), acscf_get_remote_ip() ) . "\n"; } if ( $entry_id ) { /* translators: %s: admin URL of the stored submission. */ $admin_body .= sprintf( __( 'Admin screen: %s', 'acs-contact-form' ), acscf_entry_admin_url( $entry_id ) ) . "\n"; } $admin_headers = array(); if ( is_email( $sender_email ) ) { // wp_mail() splits Reply-To on commas, so characters that could add another // address or break the "Name
" form are dropped from the name. $reply_to_name = trim( str_replace( array( ',', ';', '<', '>', '"', "\r", "\n" ), '', $sender_name ) ); $admin_headers[] = 'Reply-To: ' . ( '' !== $reply_to_name ? $reply_to_name . ' ' : '' ) . '<' . $sender_email . '>'; } $mail_files = array(); foreach ( $attachments as $attachment ) { if ( ! empty( $attachment['file'] ) ) { $mail_files[] = $attachment['file']; } } $mail_sent = wp_mail( $to, $admin_subject, $admin_body, $admin_headers, $mail_files ); if ( isset( $email_settings['auto_reply'] ) && '1' === $email_settings['auto_reply'] && is_email( $sender_email ) ) { $reply_subject = acscf_replace_mail_placeholders( isset( $email_settings['auto_reply_subject'] ) ? $email_settings['auto_reply_subject'] : '', $replace_vars ); $reply_body = acscf_replace_mail_placeholders( isset( $email_settings['auto_reply_body'] ) ? $email_settings['auto_reply_body'] : '', $replace_vars ); $reply_headers = array( 'From: ' . get_bloginfo( 'name' ) . ' <' . $to . '>' ); wp_mail( $sender_email, $reply_subject, $reply_body, $reply_headers, array() ); } if ( $entry_id ) { update_post_meta( $entry_id, '_acscf_notified', $mail_sent ? '1' : '0' ); } } elseif ( $entry_id ) { update_post_meta( $entry_id, '_acscf_notified', 'suppressed' ); } // Nothing is kept when storage is switched off. if ( ! $save_entries ) { acscf_delete_stored_files( $attachments ); } if ( $entry_id && $notify ) { acscf_maybe_auto_draft( $entry_id ); } /** * Fires after a submission has been fully processed. * * @param int $entry_id Stored entry ID, or 0. * @param int $form_id Form post ID. * @param string $category Category slug. * @param bool $mail_sent Whether the admin notification was sent. */ do_action( 'acscf_after_submission', $entry_id, $form_id, $category, $mail_sent ); if ( $is_honeypot ) { // Behave exactly as a successful submission so that the bot learns nothing. wp_send_json_success( array( 'message' => __( 'Your message has been sent.', 'acs-contact-form' ) ) ); } if ( $mail_sent || $entry_id || ! $notify ) { wp_send_json_success( array( 'message' => __( 'Your message has been sent. Thank you for getting in touch.', 'acs-contact-form' ) ) ); } wp_send_json_error( array( 'message' => __( 'The email could not be sent. Please check your server configuration.', 'acs-contact-form' ) ) ); } add_action( 'wp_ajax_acscf_submit_form', 'acscf_submit_form' ); add_action( 'wp_ajax_nopriv_acscf_submit_form', 'acscf_submit_form' ); /** * Remote address of the current request. * * @return string */ function acscf_get_remote_ip() { if ( ! isset( $_SERVER['REMOTE_ADDR'] ) ) { return ''; } $ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); return (string) filter_var( $ip, FILTER_VALIDATE_IP ); } /** * Send a test mail from the form edit screen. * * @return void */ function acscf_send_test_email() { if ( ! check_ajax_referer( 'acscf_send_test_email', 'nonce', false ) ) { wp_send_json_error( array( 'message' => __( 'Your session has expired. Please reload the page.', 'acs-contact-form' ) ) ); } $form_id = isset( $_POST['form_id'] ) ? absint( wp_unslash( $_POST['form_id'] ) ) : 0; if ( ! $form_id || ! current_user_can( 'edit_post', $form_id ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to send a test message from this form.', 'acs-contact-form' ) ) ); } $post = get_post( $form_id ); if ( ! $post || 'acscf_form' !== $post->post_type ) { wp_send_json_error( array( 'message' => __( 'Form not found.', 'acs-contact-form' ) ) ); } $test_email = isset( $_POST['test_email'] ) ? sanitize_email( wp_unslash( $_POST['test_email'] ) ) : ''; if ( ! is_email( $test_email ) ) { wp_send_json_error( array( 'message' => __( 'Please check the test recipient email address.', 'acs-contact-form' ) ) ); } $email_settings = acscf_normalize_email_settings( get_post_meta( $form_id, '_acscf_email_settings', true ) ); $form_name = get_the_title( $form_id ); $sample_name = __( 'John Doe', 'acs-contact-form' ); $sample_subject = __( 'Test message', 'acs-contact-form' ); $sample_message = __( 'This is a test message from ACS Contact Form. It was not submitted from a public page.', 'acs-contact-form' ); $sample_fields = '■ ' . __( 'Name', 'acs-contact-form' ) . "\n" . $sample_name . "\n\n" . '■ ' . __( 'Email address', 'acs-contact-form' ) . "\n" . $test_email . "\n\n" . '■ ' . __( 'Subject', 'acs-contact-form' ) . "\n" . $sample_subject . "\n\n" . '■ ' . __( 'Message', 'acs-contact-form' ) . "\n" . $sample_message; $template_vars = array( 'form_name' => $form_name, 'name' => $sample_name, 'email' => $test_email, 'message' => $sample_message, 'subject' => $sample_subject, 'all_fields' => $sample_fields, ); $subject = acscf_replace_mail_placeholders( isset( $email_settings['subject'] ) ? $email_settings['subject'] : '', $template_vars ); if ( '' === trim( $subject ) ) { /* translators: %s: form name. */ $subject = sprintf( __( '[%s] Test message', 'acs-contact-form' ), $form_name ); } $body = __( 'This is a test message from ACS Contact Form.', 'acs-contact-form' ) . "\n"; $body .= __( 'It was not submitted from a public page.', 'acs-contact-form' ) . "\n\n"; $body .= $sample_fields; $body .= "\n\n---\n"; /* translators: %d: form ID. */ $body .= sprintf( __( 'Form ID: %d', 'acs-contact-form' ), $form_id ) . "\n"; /* translators: %s: date and time. */ $body .= sprintf( __( 'Sent: %s', 'acs-contact-form' ), current_time( 'mysql' ) ); $headers = array( 'Reply-To: ' . $sample_name . ' <' . $test_email . '>' ); $sent = wp_mail( $test_email, '[' . __( 'Test message', 'acs-contact-form' ) . '] ' . $subject, $body, $headers ); if ( $sent ) { wp_send_json_success( array( 'message' => __( 'The test email was sent. Check your inbox and your spam folder.', 'acs-contact-form' ) ) ); } wp_send_json_error( array( 'message' => __( 'The test email could not be sent. Check the mail configuration on your server.', 'acs-contact-form' ) ) ); } add_action( 'wp_ajax_acscf_send_test_email', 'acscf_send_test_email' );