Advertisement
blackimpala

Formulario Backup

Jan 2nd, 2021
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.60 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.  */
  8.  
  9.  
  10. defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
  11.  
  12.  
  13.  if (isset($_POST['submitted']) && wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')){
  14.  
  15.  
  16.           /* $email_invalid   = "Email Address Invalid.";
  17.           $name_required   = "Name Required.";
  18.           $email_required  = "Email Address Required.";
  19.           $phone_required  = "Phone Required.";
  20.           $text_required   = "Message Text Required.";
  21.           $missing_content = "Please supply all information.";
  22.           $message_unsent  = "Message was not sent. Try Again.";
  23.           $message_sent    = "Thanks! Your message has been sent.";
  24.           $recaptcha_required = "Are you robot?";
  25.         */
  26.  
  27.         global $reg_errors;
  28.         $reg_errors = new WP_Error;
  29.  
  30.           //user posted variables
  31.         $to = get_option('gym_contact_admin_email');
  32.         $name = isset ($_POST['message_name'])? esc_sql(sanitize_text_field($_POST['message_name'])):"";
  33.         $email = isset($_POST['message_email'])? esc_sql(sanitize_text_field(sanitize_email($_POST['message_email']))):"";
  34.         $phone = isset($_POST['message_phone'])? esc_sql(sanitize_text_field($_POST['message_phone'])):"";
  35.         $message = isset($_POST['message_text'])? esc_sql(sanitize_text_field($_POST['message_text'])):"";
  36.  
  37.         //The Name field is required, we check that it is not empty and if not we create an error record
  38.             if ( empty( $name ) ) {
  39.                $reg_errors->add("empty-name", "The field name is required");
  40.              }
  41.  
  42.         //The Email field is required, we check that it is not empty and if not we create an error record
  43.             if ( empty( $email ) ) {
  44.                $reg_errors->add("empty-email", "The field email is required");
  45.              }
  46.  
  47.         //We check that the data is in e-mail format with the WordPress
  48.             if ( !is_email( $email ) ) {
  49.                 $reg_errors->add("invalid-email", "The e-mail is not in a valid format");
  50.               }
  51.        
  52.         //The Message field is required, we check that it is not empty and if not we create an error record
  53.             if ( empty( $message ) ) {
  54.                 $reg_errors->add("empty-message", "The field message is required");
  55.               }
  56.  
  57.  
  58.         //If there are no errors we send the form
  59.                 if (count($reg_errors->get_error_messages()) == 0) {
  60.                   //Recipient
  61.                   $recipient = "destinatario@email.com";
  62.  
  63.                   //Subject of the email
  64.                   $subject = 'Contac Form' . get_bloginfo( 'name' );
  65.  
  66.                   //The email address is that of our blog so by adding this header we can respond to the original sender
  67.                   $headers = "Reply-to: " . $name . " <" . $email . ">\r\n";
  68.  
  69.                   //We mount the body of our e-mail
  70.                   $message = "Name: " . $name . "<br>";
  71.                   $message .= "Email: " . $email . "<br>";
  72.                   $message .= "Phone: " . $phone . "<br>";
  73.                   $message .= "Message: " . nl2br($message) . "<br>";
  74.                                    
  75.                   //Filter to indicate which email should be sent in HTM modeL
  76.                   add_filter('wp_mail_content_type', create_function('', 'return "text/html";'));
  77.                                    
  78.                   //Finally we send the email
  79.                   $sent = wp_mail( $recipient, $subject, $message, $headers, $attachments);
  80.  
  81.                   //If the e-mail is sent correctly we show a message and empty the variables with the data. Otherwise we show an error message
  82.                   if ($sent) {
  83.  
  84.                     $r = array(
  85.                       'name'  => $name,
  86.                       'email' => $email,
  87.                       'phone' => $phone,
  88.                       'message' => $message
  89.                          );
  90.                        wp_send_json_success($r);
  91.                      
  92.                     }else {
  93.                    
  94.                         $r = array('message' => 'Mail Error');
  95.                                     wp_send_json_error($r);
  96.  
  97.                    }
  98.                 }//end if(count)
  99.  
  100.             $r = array('message' => 'Validate Error' );
  101.                        wp_send_json_error($r);
  102.                  
  103.    }//end if(!iiset)
  104.  
  105.  
  106.  
  107.  
  108.  
  109. // On send - works but prob not best practice validate recaptcha https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)
  110.  
  111. /*if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  112.    $captcha = $_POST['g-recaptcha-response'];
  113.  
  114.    //Fields to sent
  115.    $fields = array(
  116.         'secret' => '6Ld61NkUAAAAAI0JuA0dp_RL5_T9EucRdgLX2nVj',
  117.         'response' => '$captcha',
  118.         'remoteip' => $_SERVER['REMOTE_ADDR']
  119.      );
  120.  
  121.    //Start Sesion in CURL or file_get_contents
  122.    $ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
  123.  
  124.    // Configurate CURL options
  125.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  126.    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  127.  
  128.    // Generate array code for URL
  129.    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
  130.  
  131.    // Get response
  132.    $answer = json_decode(curl_exec($ch));
  133.    if ($answer->success) {
  134.      # code here
  135.  
  136.    }
  137.  
  138.  
  139. }
  140. */
  141.  
  142. function gym_contact_create_entry($name, $email, $phone, $message ) {
  143.   global $wpdb;
  144.   $table_name = $wpdb->prefix . 'contact';
  145.  
  146.   $wpdb->insert(
  147.         $table_name,
  148.         array(
  149.             'name' => $name,
  150.             'email' => $email,
  151.             'phone' => $phone,
  152.             'message' => $message,
  153.             'time' => current_time( 'mysql' )
  154.         )
  155.     );
  156. }
  157.  
  158. // WordPress Ajax
  159. add_action( 'wp_ajax_gym_contact_create_entry', 'gym_contact_create_entry' );
  160. add_action( 'wp_ajax_nopriv_my_contact', 'gym_contact_create_entry' );
  161.  
  162.  
  163.  ?>
  164.  
  165.  
  166.  <?php get_header(); ?>  
  167.      
  168.  
  169.  
  170.     <?php
  171.         global $reg_errors;
  172.         $reg_errors = new WP_Error;
  173.  
  174.         if (is_wp_error( $reg_errors )) {
  175.             if (count($reg_errors->get_error_messages()) > 0) {
  176.                 foreach ($reg_errors->get_error_messages() as $error) {?>
  177.                     <div class="row margin-button-small">
  178.                         <div class="col-md-12 alert alert-warning">
  179.                             <button type="button" class="close" data-dismiss="alert" aria-label="Close">
  180.                                 <span aria-hidden="true">&times;</span>
  181.                             </button>
  182.                         </div>
  183.                     </div>
  184.  
  185.                 <?php }
  186.             }
  187.         }
  188.  
  189.      ?>
  190. <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  191.  
  192. <article class="container">
  193.   <div class="row contact">
  194.     <div class="col-md-6">
  195.       <div class="form-area">
  196.             <div class="text-center contact-h"><?php the_title();?></div>
  197.            
  198.             <form id="contact-form" action="<?php the_permalink(); ?>" method="post">
  199.                   <div class="group form-group">
  200.                       <input class="form-control" id="name" type="text" name="message_name" value="<?php if (isset($_POST['message_name'])) { echo esc_attr($_POST['message_name']);} ?>">
  201.                       <span class="highlight"></span>
  202.                       <span class="bar"></span>
  203.                       <label for="name"><?php echo __('Name', 'gymclub'); ?></label>
  204.                   </div><!-- end div group form-group -->
  205.                   <div class="group form-group">
  206.                       <input class="form-control"  id="email" type="email" name="message_email" value="<?php if (isset($_POST['message_email'])) { echo esc_attr($_POST['message_email']);} ?>">
  207.                       <span class="highlight"></span>
  208.                       <span class="bar"></span>
  209.                       <label for="message_email"><?php echo __('Email', 'gymclub'); ?></label>
  210.                   </div><!-- end div group form-group -->
  211.                   <div class="group form-group">
  212.                       <input class="form-control"  id="phone" type="tel" name="message_phone" value="<?php if (isset($_POST['message_phone'])) { echo esc_attr( $_POST['message_phone']); } ?>">
  213.                       <span class="highlight"></span>
  214.                       <span class="bar"></span>
  215.                       <label for="message_phone"><?php echo __('Phone', 'gymclub'); ?></label>
  216.                   </div><!-- end div group form-group -->
  217.                   <div class="group form-group">
  218.                       <div class="text-group">
  219.                           <textarea class="form-control" type="text" name="message_text" rows="4"><?php if (isset($_POST['message_text'])) { echo esc_textarea($_POST['message_text']); } ?></textarea>
  220.                           <label for="message_text" class="input-label"><?php echo __('Message', 'gymclub'); ?></label>
  221.                           <i class="bar"></i>
  222.                       </div><!-- end div text-group -->
  223.                   </div><!-- end div group form-group -->
  224.                   <div class="g-recaptcha" data-sitekey="6Ld61NkUAAAAAJJ60gH6Ku38xJwj8nzKWbYiaecs"></div>
  225.                   <input type="hidden" name="submitted" value="custom_action">
  226.                   <?php wp_nonce_field( 'custom_action_nonce', 'gymclub_nonce_field' ); ?>
  227.                   <button class="btn btn-primary" id="submit" type="submit" id="gymclub-submit" name="submit"><?php echo __('Send', 'gymclub'); ?></button>
  228.             </form><!-- end form -->
  229.       </div><!--end respond -->
  230.    </div><!-- end div -->  
  231.  </div><!-- end div contact -->
  232.  
  233.        <div class="col-md-6" itemscope itemtype="http://schema.org/LocalBusiness">
  234.           <h3><?php echo __('Dates', 'gymclub'); ?></h3>
  235.           <span class="dates_contact" itemprop="name"><i class="fas fa-building"></i><?php echo esc_attr( get_option('gym_contact_name_company') ); ?></span>
  236.           <div  itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
  237.             <span class="dates_contact" itemprop="streetAddress"><i class="fas fa-map-marked-alt"></i><?php echo esc_attr( get_option('gym_contact_address_company') ); ?></span>
  238.              <span class="dates_contact" itemprop="telephone"><i class="fas fa-phone"></i><?php echo esc_attr( get_option('gym_contact_phone_company') ); ?></span>
  239.              <span class="dates_contact" itemprop="email"><i class="far fa-envelope"></i><?php echo esc_attr( get_option('gym_contact_admin_email') ); ?></span>
  240.              <span class="dates_contact" itemprop="postalCode"><i class="fas fa-mail-bulk"></i><?php echo esc_attr( get_option('gym_contact_code_postal_company') ); ?></span>
  241.           </div><!-- end div itemprop -->
  242.        </div><!--end div col-md-6 div itemscope -->
  243.  
  244. </article><!-- end section -->
  245.  
  246.        
  247. <?php get_footer(); ?>
  248.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement