Codice PHP:
* Form Processing.
*
* Here is where we validate the POST'ed data and
* format it for sending in an e-mail.
*
* We then send the e-mail and notify the user.
--------------------------------------------------*/
$emailSent = false;
if ( ( count( $_POST ) > 2 ) && isset( $_POST['submitted'] ) ) {
$fields_to_skip = array( 'checking', 'submitted', 'sendCopy' );
$default_fields = array( 'contactName' => '', 'contactEmail' => '', 'contactMessage' => '' );
$error_responses = array(
'contactName' => __( 'Perpiacere inserisci il tuo nome', 'woothemes' ),
'contactEmail' => __( 'Perpiacere inserisci la tua email (e controlla che sia corretta)', 'woothemes' ),
'contactMessage' => __( 'Perpiacere inserisci il tuo messaggio', 'woothemes' )
);
$posted_data = $_POST;
// Check if we're using the default fields.
if ( $show_default_fields != 'no' ) {
// Check for errors.
foreach ( array_keys( $default_fields ) as $d ) {
if ( !isset ( $_POST[$d] ) || $_POST[$d] == '' || ( $d == 'contactEmail' && ! is_email( $_POST[$d] ) ) ) {
$error_messages[$d] = esc_html( $error_responses[$d] );
} // End IF Statement
} // End FOREACH Loop
} else {
$default_fields = array( 'contactName' => get_bloginfo( 'name' ), 'contactEmail' => get_bloginfo( 'admin_email' ), 'contactMessage' => '' );
}
// If we have errors, don't do anything. Otherwise, run the processing code.
if ( count( $error_messages ) ) {} else {
// Setup e-mail variables.
$message_fromname = $default_fields['contactName'];
$message_fromemail = strtolower( $default_fields['contactEmail'] );
$message_subject = $subject;
$message_body = $default_fields['contactMessage'] . "\n\r\n\r";
// Filter out skipped fields and assign default fields.
foreach ( $posted_data as $k => $v ) {
if ( in_array( $k, $fields_to_skip ) ) {
unset( $posted_data[$k] );
} // End IF Statement
if ( in_array( $k, array_keys( $default_fields ) ) ) {
$default_fields[$k] = $v;
unset( $posted_data[$k] );
} // End IF Statement
} // End FOREACH Loop
// Okay, so now we're left with only the dynamic fields. Assign to a fresh variable.
$dynamic_fields = $posted_data;
// Format the default fields into the $message_body.
foreach ( $default_fields as $k => $v ) {
if ( $v == '' ) {} else {
$message_body .= str_replace( 'contact', '', $k ) . ': ' . $v . "\n\r";
} // End IF Statement
} // End FOREACH Loop
// Format the dynamic fields into the $message_body.
foreach ( $dynamic_fields as $k => $v ) {
if ( $v == '' ) {} else {
$value = '';
if ( substr( $k, 0, 7 ) == 'select_' || substr( $k, 0, 6 ) == 'radio_' ) {
$message_body .= $formatted_dynamic_atts[$k]['label'] . ': ' . $formatted_dynamic_atts[$k]['options'][$v] . "\n\r";
} else {
$message_body .= $formatted_dynamic_atts[$k]['label'] . ': ' . $v . "\n\r";
} // End IF Statement
} // End IF Statement
} // End FOREACH Loop
// Send the e-mail.
$headers = __( 'From: ', 'woothemes') . $default_fields['contactName'] . ' <' . $default_fields['contactEmail'] . '>' . "\r\n" . __( 'Reply-To: ', 'woothemes' ) . $default_fields['contactEmail'];
$emailSent = wp_mail($email, $subject, $message_body, $headers);
// Send a copy of the e-mail to the sender, if specified.
if ( isset( $_POST['sendCopy'] ) && $_POST['sendCopy'] == 'true' ) {
$headers = __( 'From: ', 'woothemes') . $default_fields['contactName'] . ' <' . $default_fields['contactEmail'] . '>' . "\r\n" . __( 'Reply-To: ', 'woothemes' ) . $default_fields['contactEmail'];
$emailSent = wp_mail($default_fields['contactEmail'], $subject, $message_body, $headers);
} // End IF Statement
} // End IF Statement ( count( $error_messages ) )
} // End IF Statement
/* Generate the form HTML.
--------------------------------------------------*/
$html .= '<div class="post contact-form">' . "\n";
/* Display message HTML if necessary.
--------------------------------------------------*/
// Success messages
if( isset( $emailSent ) && $emailSent == true ) {
$html .= do_shortcode( '[box type="tick"]' . __( 'La tua email e\' stata inviata con successo.', 'woothemes' ) . '[/box]' );
$html .= '<span class="has_sent hide"></span>' . "\n";
}
// Error messages
if( count( $error_messages ) ) {
$html .= do_shortcode( '[box type="alert"]' . __( 'Ci sono stati degli errori nell\'invio del modulo', 'woothemes' ) . '[/box]' );
}
// No e-mail address supplied.
if( $email == '' ) {
$html .= do_shortcode( '[box type="alert"]' . __( 'L\'Email non risulta essere stata inserita correttamente. Perpiacere inseriscila.', 'woothemes' ) . '[/box]' );
}
if ( $email == '' ) {} else {
$html .= '<form action="" id="contactForm" method="post">' . "\n";
$html .= '<fieldset class="forms">' . "\n";
/* Parse the "static" form fields.
--------------------------------------------------*/
if ( $show_default_fields != 'no' ) {
$contactName = '';
if( isset( $_POST['contactName'] ) ) { $contactName = $_POST['contactName']; }
$contactEmail = '';
if( isset( $_POST['contactEmail'] ) ) { $contactEmail = $_POST['contactEmail']; }
$contactMessage = '';
if( isset( $_POST['contactMessage'] ) ) { $contactMessage = stripslashes( $_POST['contactMessage'] ); }
$html .= '<p><label for="contactName">' . __( 'Nome', 'woothemes' ) . '</label>' . "\n";
$html .= '<input type="text" name="contactName" id="contactName" value="' . esc_attr( $contactName ) . '" class="txt requiredField" />' . "\n";
if( array_key_exists( 'contactName', $error_messages ) ) {
$html .= '<span class="error">' . esc_html( $error_messages['contactName'] ) . '</span>' . "\n";
}
$html .= '</p>' . "\n";
$html .= '<p><label for="contactEmail">' . __( 'Email', 'woothemes' ) . '</label>' . "\n";
$html .= '<input type="text" name="contactEmail" id="contactEmail" value="' . esc_attr( $contactEmail ) . '" class="txt requiredField email" />' . "\n";
if( array_key_exists( 'contactEmail', $error_messages ) ) {
$html .= '<span class="error">' . esc_html( $error_messages['contactEmail'] ) . '</span>' . "\n";
}
$html .= '</p>' . "\n";
$html .= '<p class="textarea"><label for="contactMessage">' . __( 'Messaggio', 'woothemes' ) . '</label>' . "\n";
$html .= '<textarea name="contactMessage" id="contactMessage" rows="20" cols="30" class="textarea requiredField">' . esc_textarea( $contactMessage ) . '</textarea>' . "\n";
if( array_key_exists( 'contactMessage', $error_messages ) ) {
$html .= '<span class="error">' . esc_html( $error_messages['contactMessage'] ) . '</span>' . "\n";
}
$html .= '</p>' . "\n";
} // End static fields check
/* Parse dynamic fields into HTML.
--------------------------------------------------*/
if ( count( $formatted_dynamic_atts ) ) {
foreach ( $formatted_dynamic_atts as $k => $v ) {
/* Parse the radio buttons.
--------------------------------------------------*/
if ( substr( $k, 0, 6 ) == 'radio_' ) {
/* Generate Select Box Field HTML.
----------------------------------------------*/
${$k} = $v['default_value'];
if ( isset( $_POST[$k] ) ) { ${$k} = trim( strip_tags( $_POST[$k] ) ); } // End IF Statement
$html .= '<p><label for="' . esc_attr( $k ) . '">' . esc_html( $v['label'] ) . '</label>' . "\n";
$html .= '<span class="woo-radio-container fl">' . "\n";
foreach ( $v['options'] as $value => $label ) {
$html .= '<input type="radio" name="' . esc_attr( $k ) . '" class="radio-button woo-input-radio" value="' . esc_attr( $value ) . '"' . checked( $value, ${$k}, false ) . ' /> ' . esc_html( $label ) . '<br />' . "\n";
}
$html .= '</span><!--/.woo-radio-container-->' . "\n";
}
/* Parse the checkbox inputs.
--------------------------------------------------*/
if ( substr( $k, 0, 9 ) == 'checkbox_' ) {
/* Generate Checkbox Input Field HTML.
----------------------------------------------*/
${$k} = $v['value'];
if ( isset( $_POST[$k] ) ) { ${$k} = trim( strip_tags( $_POST[$k] ) ); } // End IF Statement
$checked = 0;
if ( array_key_exists( 'checked', $v ) && $v['checked'] == 'yes' ) { $checked = ${$k}; }
$html .= '<p class="inline">' . "\n";
$html .= '<input type="checkbox" value="' . esc_attr( ${$k} ) . '" name="' . esc_attr( $k ) . '" id="' . esc_attr( $k ) . '" class="checkbox input-checkbox woo-input-checkbox"' . checked( $checked, ${$k}, false ) . ' />' . "\n";
$html .= '<label for="' . esc_attr( $k ) . '">' . esc_html( $v['label'] ) . '</label></p>' . "\n";
}
/* Parse the text inputs.
--------------------------------------------------*/