Advertisement
blackimpala

Validation Fields Custom Form Plugin

Jul 2nd, 2021
1,703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.76 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Contact form functionality
  4.  * Used within the loop
  5.  * @link https://developer.wordpress.org/reference/functions/wp_nonce_field/
  6.  * @link https://codex.wordpress.org/Class_Reference/WP_Error
  7.  * @link https://developer.wordpress.org/reference/functions/wp_is_mobile/
  8.  * @link https://developer.wordpress.org/reference/functions/wp_kses_data/
  9.  * @link https://developer.wordpress.org/reference/functions/wp_remote_retrieve_response_code/
  10.  * @link https://developer.wordpress.org/reference/functions/wp_get_referer/
  11.  */
  12.  
  13.  
  14. defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
  15.  
  16.  get_header();
  17.  
  18. class Gymclub_contact_form {
  19.  
  20.  
  21.     private $reg_errors = array();
  22.  
  23.     public function __construct() {
  24.               // add form template
  25.               add_action( 'init', array( $this, 'load_form' ) );
  26.  
  27.             }
  28.  
  29.    
  30.     public function load_form($obj) {
  31.  
  32.      ob_start();
  33.  
  34.      get_template_part( 'templates/contac_form');
  35.  
  36.         //include_once plugin_dir_url(__FILE__) . '/templates/contact_form.php';
  37.  
  38.  
  39.      return ob_get_clean();
  40.  
  41.      }
  42.  
  43.     public function validate_form( $name, $email, $phone, $message, $response ) {
  44.  
  45.               $not_human  = "Human verification incorrect.";
  46.               $failed_connect = "An error has occurred while validating the recaptcha";
  47.               $email_invalid   = "Email Address Invalid.";
  48.               $name_error   = "Name should be at least 4 characters";
  49.               $missing_content = "Please supply all information.";
  50.  
  51.  
  52.               $response = wp_safe_remote_post( "https://www.google.com/recaptcha/api/siteverify", array(
  53.                 'method' => 'POST',
  54.                 'timeout' => 45,
  55.                 'redirection' => 5,
  56.                 'httpversion' => '1.0',
  57.                 'blocking' => true,
  58.                 'headers' => array(),
  59.                 'body' => array(
  60.                   'secret' => "6Ld61NkUAAAAAI0JuA0dp_RL5_T9EucRdgLX2nVj",
  61.                   'response' => esc_attr($_POST['g-recaptcha-response'])),
  62.                 'cookies' => array()
  63.                 )
  64.               );
  65.  
  66.  
  67.           // If any field is left empty, add the error message to the error array
  68.             if ( empty($name) || empty($email) || empty($phone) || empty($message) || empty($response) ) {
  69.                 $this->reg_errors->add( esc_html__( $missing_content, 'gymclub') );
  70.             }
  71.  
  72.             // if the name field isn't alphabetic, add the error message
  73.             if ( strlen($name) < 4 ) {
  74.                 $this->reg_errors->add( esc_html__( $name_error, 'gymclub') );
  75.             }
  76.  
  77.             // Check if the email is valid
  78.             if ( !is_email($email) ) {
  79.                 $this->reg_errors->add( esc_html__( $email_invalid, 'gymclub') );
  80.             }
  81.  
  82.             //Check if we have some kind of error in the connection with google
  83.             if (is_wp_error( $response )) {
  84.                 $this->reg_errors->add( esc_html__( $failed_connect, 'gymclub' ));
  85.             } else{
  86.               //If we have successfully connected to google, we check if the answer is true or false
  87.                $response = json_decode($response['body']);
  88.             }if ($response->success == false) {
  89.                 $this->reg_errors->add( esc_html__( $not_human, 'gymclub' ));
  90.             }
  91.     }
  92.  
  93.     public function send_email( $name, $email, $phone, $message, $response ) {
  94.  
  95.             $message_sent    = "Thanks! Your message has been sent.";
  96.  
  97.  
  98.          if (count( $this->reg_errors )  < 1 ) {
  99.  
  100.  
  101.                   $name = isset ($_POST['message_name'])? esc_sql(sanitize_text_field($_POST['message_name'])):"";
  102.                   $email = isset($_POST['message_email'])? esc_sql(sanitize_text_field(sanitize_email($_POST['message_email']))):"";
  103.                   $phone = isset($_POST['message_phone'])? esc_sql(sanitize_text_field($_POST['message_phone'])):"";
  104.                   $message = isset($_POST['message_text'])? esc_sql(sanitize_text_field($_POST['message_text'])):"";
  105.  
  106.                   $to = get_option('gym_contact_admin_email');
  107.  
  108.                   $headers[] = 'From: '. $name . ' <' . $email . '>';
  109.  
  110.                   if ( wp_mail($to, $subject, $message, $headers)) { ?>
  111.                       <div class="row margin-button-small">
  112.                           <div class="col-md-12 alert alert-success">
  113.                               <button type="button" class="close" data-dismiss="alert" aria-label="close">
  114.                                   <span aria-hidden="true">&times;</span>
  115.                               </button>
  116.                               <p class="message"><?php echo __( $message_sent, 'gymclub'); ?></p>
  117.                           </div>
  118.                      </div> <!-- end row -->
  119.                   <?php}
  120.  
  121.  
  122.               }
  123.  
  124.     }
  125.  
  126.   public function process_functions() {
  127.  
  128.        if (isset($_POST['submit']) && isset($_POST['gymclub_nonce_field'])) {
  129.  
  130.               return;
  131.  
  132.            } //end isset
  133.  
  134.        if (wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')) {
  135.  
  136.               return;
  137.  
  138.            }// end verify nonce
  139.  
  140.          $url = wp_get_referer();
  141.  
  142.          $this->validate_form($_POST['message_name'], $_POST['message_email'], $_POST[''], $_POST['message_phone'], $_POST['message_text']), ($_POST['g-recaptcha-response']);
  143.  
  144.           if (is_wp_error( $this->reg_errors) && count( $this->reg_errors->get_error_messages() ) > 0 ) {
  145.                    foreach ( $this->reg_errors->get_error_messages() as $error ) {?>
  146.                        <div class="row margin-button-small">
  147.                           <div class="col-md-12 alert alert-success">
  148.                               <button type="button" class="close" data-dismiss="alert" aria-label="close">
  149.                                   <span aria-hidden="true">&times;</span>
  150.                               </button>
  151.                               <p class="message"><?php echo __( $error, 'gymclub'); ?></p>
  152.                           </div>
  153.                        </div> <!-- end row -->
  154.                   <?php }  //end reg_errors
  155.  
  156.             } // end is_wp_error
  157.  
  158.             wp_safe_redirect( $url );
  159.             exit();
  160.  
  161.         } //end public function
  162.  
  163.  
  164.  
  165. }
  166.  
  167.  
  168. // WordPress Ajax
  169. add_action( 'wp_ajax_gym_contact_create_entry', 'gym_contact_create_entry' );
  170. add_action( 'wp_ajax_nopriv_my_contact', 'gym_contact_create_entry' );
  171.  
  172.  
  173. // Ajax insert data contact entry
  174. function gym_contact_create_entry($name, $email, $phone, $message ) {
  175.   global $wpdb;
  176.   $table_name = $wpdb->prefix . 'contact';
  177.  
  178.   $wpdb->insert(
  179.         $table_name,
  180.         array(
  181.             'name' => $name,
  182.             'email' => $email,
  183.             'phone' => $phone,
  184.             'message' => $message,
  185.             'time' => current_time( 'mysql' )
  186.         )
  187.     );
  188. }
  189.  
  190.  
  191.  ?>
  192.  
  193. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement