strtolower( $form['title'] ), '{site}' => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), ); foreach ( $fields as $key => $value ) { $map[ '{' . $key . '}' ] = is_scalar( $value ) ? (string) $value : ''; } $out = strtr( $template, $map ); return trim( preg_replace( '/\{[a-z0-9_]+\}/i', '', $out ) ); } /** * Display value for a field (select options show their label). * * @param array $field Field definition. * @param string $value Stored value. */ function stackmime_forms_display_value( $field, $value ) { if ( 'consent' === $field['type'] ) { return '1' === (string) $value ? __( 'Yes', 'stackmime-forms' ) : ''; } if ( in_array( $field['type'], array( 'select', 'radio' ), true ) && isset( $field['options'][ $value ] ) ) { return (string) $field['options'][ $value ]; } if ( 'choices' === $field['type'] && '' !== (string) $value ) { $labels = array(); foreach ( explode( ',', (string) $value ) as $k ) { $labels[] = isset( $field['options'][ $k ] ) ? (string) $field['options'][ $k ] : $k; } return implode( ', ', $labels ); } if ( 'date' === $field['type'] && $value ) { $ts = strtotime( $value ); return $ts ? wp_date( get_option( 'date_format' ), $ts ) : $value; } return (string) $value; } /** * Send the business notification for an entry. Records the result on the entry. * * @param int $entry_id Entry ID. * @return bool Sent. */ function stackmime_forms_send_notification( $entry_id ) { $entry = stackmime_forms_entry( $entry_id ); $form = $entry ? stackmime_forms_get( $entry->form_id ) : null; if ( ! $entry || ! $form ) { return false; } $settings = stackmime_forms_settings(); $to = array_filter( array_map( 'sanitize_email', array_map( 'trim', explode( ',', $form['notify_to'] ? $form['notify_to'] : $settings['notify_to'] ) ) ), 'is_email' ); if ( ! $to ) { stackmime_forms_update( $entry_id, array( 'email_status' => 'failed', 'email_error' => __( 'No notification address is set (StackMime Forms → Settings).', 'stackmime-forms' ), ) ); return false; } $fields = $entry->fields; $contact = stackmime_forms_entry_contact( $form, $fields ); if ( ! isset( $fields['name'] ) ) { $fields['name'] = $contact['name']; } // {entry} = the entry number, so each notification has its own subject. Gmail otherwise merges repeat // subjects (e.g. two "New message from Sam") into one thread, and a second inbox may seem to miss it. $subject = stackmime_forms_fill( $form['subject'], array_merge( $fields, array( 'entry' => (string) (int) $entry_id ) ), $form ); $rows = ''; foreach ( $form['fields'] as $key => $field ) { if ( stackmime_forms_is_display_type( $field['type'] ) ) { continue; } $value = stackmime_forms_display_value( $field, $fields[ $key ] ?? '' ); if ( '' === $value ) { continue; } if ( 'email' === $field['type'] ) { $value_html = '' . esc_html( $value ) . ''; } elseif ( 'tel' === $field['type'] ) { $value_html = '' . esc_html( $value ) . ''; } else { $value_html = nl2br( esc_html( $value ) ); } $rows .= '' . esc_html( $field['label'] ) . '' . $value_html . ''; } $admin_link = admin_url( 'admin.php?page=stackmime-forms&entry=' . (int) $entry_id ); $reply_note = '' !== $contact['email'] ? __( 'Reply to this email to answer directly.', 'stackmime-forms' ) : ( '' !== $contact['phone'] ? __( 'No email given. Please call back.', 'stackmime-forms' ) : '' ); $body = '
' . '

' . esc_html( $subject ) . '

' . '

' . esc_html( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ) . ( $reply_note ? ' · ' . $reply_note : '' ) ) . '

' . '' . $rows . '
' /* translators: %s: page address. */ . ( $entry->page_url ? '

' . esc_html( sprintf( __( 'Sent from: %s', 'stackmime-forms' ), $entry->page_url ) ) . '

' : '' ) . '

' . esc_html__( 'View this entry on the website', 'stackmime-forms' ) . '

' . '
'; $headers = array( 'Content-Type: text/html; charset=UTF-8' ); if ( '' !== $contact['email'] ) { $name = str_replace( array( "\r", "\n", '"', '<', '>', ',' ), '', $contact['name'] ); $headers[] = 'Reply-To: ' . ( $name ? $name . ' ' : '' ) . '<' . $contact['email'] . '>'; } $error = ''; $catch = function ( $e ) use ( &$error ) { $error = $e->get_error_message(); }; add_action( 'wp_mail_failed', $catch ); $sent = wp_mail( $to, $subject, $body, $headers ); remove_action( 'wp_mail_failed', $catch ); stackmime_forms_update( $entry_id, array( 'email_status' => $sent ? 'sent' : 'failed', 'email_error' => $sent ? '' : ( $error ? $error : __( 'The mail server did not accept the message.', 'stackmime-forms' ) ), ) ); return $sent; } /** * Send the customer a confirmation, if switched on and they gave an email. * * @param int $entry_id Entry ID. */ function stackmime_forms_send_autoreply( $entry_id ) { $entry = stackmime_forms_entry( $entry_id ); $form = $entry ? stackmime_forms_get( $entry->form_id ) : null; if ( ! $entry || ! $form ) { return; } $settings = stackmime_forms_settings(); $enabled = null === $form['autoreply'] ? (bool) $settings['autoreply'] : (bool) $form['autoreply']; $contact = stackmime_forms_entry_contact( $form, $entry->fields ); $email = $contact['email']; if ( ! $enabled || ! is_email( $email ) ) { return; } $fields = $entry->fields; if ( ! isset( $fields['name'] ) ) { $fields['name'] = $contact['name']; } if ( ! empty( $fields['name'] ) ) { $fields['name'] = trim( strtok( $fields['name'], ' ' ) ); // First name reads friendlier. } $fields['entry'] = (string) (int) $entry_id; $subject = '' !== (string) $form['autoreply_subject'] ? $form['autoreply_subject'] : $settings['autoreply_subject']; $body = '' !== (string) $form['autoreply_body'] ? $form['autoreply_body'] : $settings['autoreply_body']; wp_mail( $email, stackmime_forms_fill( $subject, $fields, $form ), stackmime_forms_fill( $body, $fields, $form ) ); }