Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Contact form functionality
- * Used within the loop
- * @link https://developer.wordpress.org/reference/functions/wp_nonce_field/
- * @link https://codex.wordpress.org/Class_Reference/WP_Error
- * @link https://developer.wordpress.org/reference/functions/wp_is_mobile/
- * @link https://developer.wordpress.org/reference/functions/wp_kses_data/
- * @link https://developer.wordpress.org/reference/functions/wp_remote_retrieve_response_code/
- * @link https://developer.wordpress.org/reference/functions/wp_get_referer/
- */
- defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
- get_header();
- class Gymclub_contact_form {
- private $reg_errors = array();
- public function __construct() {
- // add form template
- add_action( 'init', array( $this, 'load_form' ) );
- }
- public function load_form($obj) {
- ob_start();
- get_template_part( 'templates/contac_form');
- //include_once plugin_dir_url(__FILE__) . '/templates/contact_form.php';
- return ob_get_clean();
- }
- public function validate_form( $name, $email, $phone, $message, $response ) {
- $not_human = "Human verification incorrect.";
- $failed_connect = "An error has occurred while validating the recaptcha";
- $email_invalid = "Email Address Invalid.";
- $name_error = "Name should be at least 4 characters";
- $missing_content = "Please supply all information.";
- $response = wp_safe_remote_post( "https://www.google.com/recaptcha/api/siteverify", array(
- 'method' => 'POST',
- 'timeout' => 45,
- 'redirection' => 5,
- 'httpversion' => '1.0',
- 'blocking' => true,
- 'headers' => array(),
- 'body' => array(
- 'secret' => "6Ld61NkUAAAAAI0JuA0dp_RL5_T9EucRdgLX2nVj",
- 'response' => esc_attr($_POST['g-recaptcha-response'])),
- 'cookies' => array()
- )
- );
- // If any field is left empty, add the error message to the error array
- if ( empty($name) || empty($email) || empty($phone) || empty($message) || empty($response) ) {
- $this->reg_errors->add( esc_html__( $missing_content, 'gymclub') );
- }
- // if the name field isn't alphabetic, add the error message
- if ( strlen($name) < 4 ) {
- $this->reg_errors->add( esc_html__( $name_error, 'gymclub') );
- }
- // Check if the email is valid
- if ( !is_email($email) ) {
- $this->reg_errors->add( esc_html__( $email_invalid, 'gymclub') );
- }
- //Check if we have some kind of error in the connection with google
- if (is_wp_error( $response )) {
- $this->reg_errors->add( esc_html__( $failed_connect, 'gymclub' ));
- } else{
- //If we have successfully connected to google, we check if the answer is true or false
- $response = json_decode($response['body']);
- }if ($response->success == false) {
- $this->reg_errors->add( esc_html__( $not_human, 'gymclub' ));
- }
- }
- public function send_email( $name, $email, $phone, $message, $response ) {
- $message_sent = "Thanks! Your message has been sent.";
- if (count( $this->reg_errors ) < 1 ) {
- $name = isset ($_POST['message_name'])? esc_sql(sanitize_text_field($_POST['message_name'])):"";
- $email = isset($_POST['message_email'])? esc_sql(sanitize_text_field(sanitize_email($_POST['message_email']))):"";
- $phone = isset($_POST['message_phone'])? esc_sql(sanitize_text_field($_POST['message_phone'])):"";
- $message = isset($_POST['message_text'])? esc_sql(sanitize_text_field($_POST['message_text'])):"";
- $to = get_option('gym_contact_admin_email');
- $headers[] = 'From: '. $name . ' <' . $email . '>';
- if ( wp_mail($to, $subject, $message, $headers)) { ?>
- <div class="row margin-button-small">
- <div class="col-md-12 alert alert-success">
- <button type="button" class="close" data-dismiss="alert" aria-label="close">
- <span aria-hidden="true">×</span>
- </button>
- <p class="message"><?php echo __( $message_sent, 'gymclub'); ?></p>
- </div>
- </div> <!-- end row -->
- <?php}
- }
- }
- public function process_functions() {
- if (isset($_POST['submit']) && isset($_POST['gymclub_nonce_field'])) {
- return;
- } //end isset
- if (wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')) {
- return;
- }// end verify nonce
- $url = wp_get_referer();
- $this->validate_form($_POST['message_name'], $_POST['message_email'], $_POST[''], $_POST['message_phone'], $_POST['message_text']), ($_POST['g-recaptcha-response']);
- if (is_wp_error( $this->reg_errors) && count( $this->reg_errors->get_error_messages() ) > 0 ) {
- foreach ( $this->reg_errors->get_error_messages() as $error ) {?>
- <div class="row margin-button-small">
- <div class="col-md-12 alert alert-success">
- <button type="button" class="close" data-dismiss="alert" aria-label="close">
- <span aria-hidden="true">×</span>
- </button>
- <p class="message"><?php echo __( $error, 'gymclub'); ?></p>
- </div>
- </div> <!-- end row -->
- <?php } //end reg_errors
- } // end is_wp_error
- wp_safe_redirect( $url );
- exit();
- } //end public function
- }
- // WordPress Ajax
- add_action( 'wp_ajax_gym_contact_create_entry', 'gym_contact_create_entry' );
- add_action( 'wp_ajax_nopriv_my_contact', 'gym_contact_create_entry' );
- // Ajax insert data contact entry
- function gym_contact_create_entry($name, $email, $phone, $message ) {
- global $wpdb;
- $table_name = $wpdb->prefix . 'contact';
- $wpdb->insert(
- $table_name,
- array(
- 'name' => $name,
- 'email' => $email,
- 'phone' => $phone,
- 'message' => $message,
- 'time' => current_time( 'mysql' )
- )
- );
- }
- ?>
- <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement