Advertisement
mjdigital

Gravity Forms Notifications Delay

Nov 26th, 2012
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.56 KB | None | 0 0
  1. <?php
  2. /*
  3.     WRITTEN BY MJDIGITAL: http://www.mjdigital.co.uk/blog/
  4.    
  5.     CAPTURE AND STORE NOTIFICATIONS - THEN STOP THEM BEING SENT OUT
  6.     UNTIL THEY CAN BE TRIGGERED MANUALLY / VIA ANOTHER PROCESS
  7.  
  8. */
  9.  
  10.  
  11. /*
  12.     ADD IN CUSTOM TABLE TO CAPTURE NOTIFICATIONS
  13.     YOU MAY WANT TO REMOVE THIS BIT
  14. */
  15. global $wpdb;
  16. $addTable = $wpdb->query("CREATE TABLE IF NOT EXISTS `mj_form_notifications` (
  17.  `mj_mail_id` int(11) NOT NULL AUTO_INCREMENT,
  18.  `mj_mail_lead` int(10) NOT NULL,
  19.  `mj_mail_data` text NOT NULL,
  20.  `mj_mail_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  21.  `mj_mail_sent` int(11) NOT NULL DEFAULT '0',
  22.  `mj_mail_send_errors` text,
  23.  PRIMARY KEY (`mj_mail_id`),
  24.  KEY `mj_mail_lead` (`mj_mail_lead`)
  25. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
  26. /*
  27.     END TABLE CREATION - AGAIN YOU CAN REMOVE THE ABOVE IF YOU WANT TO
  28.     MANUALLY INSTALL/SETUP YOUR TABLE OR ADD THE DATA TO THE ENTRY
  29. */
  30.  
  31.  
  32. /*
  33.     ADD HOOKS
  34. */
  35.  
  36. /*
  37.     #############################################################
  38.     HOOK MAIL TO CAPTURE AND DISABLE NOTIFICATIONS
  39.     #############################################################
  40. */
  41. add_action("gform_entry_created", "post_submission_handler");
  42. function post_submission_handler($lead){
  43.     global $wpdb;
  44.    
  45.     // ENTRY ID
  46.     $leadID = $lead['id'];
  47.     $_POST['lead_id'] = $leadID; // Add the Lead ID to the global post variable so it can easily be retrieved in the filter
  48.    
  49.     // filter function to capture notification and then stop them going out
  50.     function mj_capture_mail($atts) {
  51.         global $wpdb;
  52.         // recieves a compact array containing: $to, $subject, $message, $headers = '', $attachments = array()
  53.         extract($atts);
  54.        
  55.         // Get lead ID
  56.         if((isset($_POST['lead_id']))&&(intval($_POST['lead_id']))) {
  57.             // Store emails against lead
  58.             $sql = "INSERT INTO mj_form_notifications (
  59.             mj_mail_lead,
  60.             mj_mail_data
  61.             ) VALUES (
  62.             ".intval($_POST['lead_id']).",
  63.             '".mysql_real_escape_string(serialize($atts))."'
  64.             )";
  65.             // INSERT NOTIFICATIONS
  66.             $insert = $wpdb->query($sql);
  67.             // blank the email to stop it going through this time
  68.             $atts = array('to' => '','subject' => '','message' => '','headers' => '','attachments' => array());
  69.         } else {
  70.             // no lead ID - do nothing to just pass the emails through as normal
  71.         }
  72.         return $atts;
  73.     }
  74.     add_filter('wp_mail','mj_capture_mail',1,1); // Add the above function to filter wp_mail
  75. }
  76.  
  77. /*
  78.     #############################################################
  79.     REMOVE MAIL HOOK TO ENSURE MAIL WORKS AS NORMAL FOR OTHER FUNCTIONS
  80.     #############################################################
  81. */
  82. add_action('gform_after_submission', 'post_creation_and_confirmation', 10, 2);
  83. function post_creation_and_confirmation($entry, $form) {
  84.     remove_filter('wp_mail','mj_capture_mail',1,1);
  85. }
  86.  
  87.  
  88. /*
  89.     #############################################################
  90.     MANUALLY TRIGGER MAILS TO BE SENT
  91.     #############################################################
  92.    
  93.     The function below can be called whenever you want to send out
  94.     the stored notifications for a particular lead/entry
  95.    
  96.     Personally I call this once I get the IPN back from the payment
  97.     gateway telling my site that a payment has gone through successfully
  98.    
  99.     NOTE: A lead ID is required - this is the entry ID for the
  100.     particular form submission
  101. */
  102. function mj_send_notifications($leadID) {
  103.     if((isset($leadID))&&(intval($leadID))) {
  104.         global $wpdb,$ts_mail_errors,$phpmailer;   
  105.         // FETCH EMAIL/S
  106.         $emails = $wpdb->get_results("SELECT * FROM mj_form_notifications WHERE mj_mail_lead = ".intval($orderid),ARRAY_A);
  107.         if(count($emails)) {
  108.             $errors = array();
  109.             foreach($emails as $email) {
  110.                 $mailData = @unserialize($email['mj_mail_data']);
  111.                 $mailID = $email['mj_mail_id'];
  112.                 if(count($mailData)) {
  113.                     extract($mailData);
  114.                     // SEND THE EMAIL VIA wp_mail
  115.                     $sendMail = wp_mail( $to, $subject, $message, $headers, $attachments );
  116.                     // LOG SEND OR ERROR
  117.                     if($sendMail) {
  118.                         $wpdb->query("UPDATE mj_form_notifications SET mj_mail_sent = mj_mail_sent+1 WHERE mj_mail_id = ".intval($mailID));
  119.                     } else {
  120.                         if(!isset($ts_mail_errors)) $ts_mail_errors = array();
  121.                         if(isset($phpmailer)) {
  122.                             $ts_mail_errors[] = $phpmailer->ErrorInfo;
  123.                             $errors[] = $phpmailer->ErrorInfo;
  124.                         }
  125.                         $wpdb->query("UPDATE mj_form_notifications SET mj_mail_send_errors = '".mysql_real_escape_string(serialize($ts_mail_errors))."' WHERE mj_mail_id = ".intval($mailID));
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.         return (count($errors)) ? implode(', ',$errors) : true;
  131.     } else {
  132.         return 'No Lead ID posted';
  133.     }
  134. }
  135.  
  136.  
  137. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement