Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- WRITTEN BY MJDIGITAL: http://www.mjdigital.co.uk/blog/
- CAPTURE AND STORE NOTIFICATIONS - THEN STOP THEM BEING SENT OUT
- UNTIL THEY CAN BE TRIGGERED MANUALLY / VIA ANOTHER PROCESS
- */
- /*
- ADD IN CUSTOM TABLE TO CAPTURE NOTIFICATIONS
- YOU MAY WANT TO REMOVE THIS BIT
- */
- global $wpdb;
- $addTable = $wpdb->query("CREATE TABLE IF NOT EXISTS `mj_form_notifications` (
- `mj_mail_id` int(11) NOT NULL AUTO_INCREMENT,
- `mj_mail_lead` int(10) NOT NULL,
- `mj_mail_data` text NOT NULL,
- `mj_mail_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
- `mj_mail_sent` int(11) NOT NULL DEFAULT '0',
- `mj_mail_send_errors` text,
- PRIMARY KEY (`mj_mail_id`),
- KEY `mj_mail_lead` (`mj_mail_lead`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
- /*
- END TABLE CREATION - AGAIN YOU CAN REMOVE THE ABOVE IF YOU WANT TO
- MANUALLY INSTALL/SETUP YOUR TABLE OR ADD THE DATA TO THE ENTRY
- */
- /*
- ADD HOOKS
- */
- /*
- #############################################################
- HOOK MAIL TO CAPTURE AND DISABLE NOTIFICATIONS
- #############################################################
- */
- add_action("gform_entry_created", "post_submission_handler");
- function post_submission_handler($lead){
- global $wpdb;
- // ENTRY ID
- $leadID = $lead['id'];
- $_POST['lead_id'] = $leadID; // Add the Lead ID to the global post variable so it can easily be retrieved in the filter
- // filter function to capture notification and then stop them going out
- function mj_capture_mail($atts) {
- global $wpdb;
- // recieves a compact array containing: $to, $subject, $message, $headers = '', $attachments = array()
- extract($atts);
- // Get lead ID
- if((isset($_POST['lead_id']))&&(intval($_POST['lead_id']))) {
- // Store emails against lead
- $sql = "INSERT INTO mj_form_notifications (
- mj_mail_lead,
- mj_mail_data
- ) VALUES (
- ".intval($_POST['lead_id']).",
- '".mysql_real_escape_string(serialize($atts))."'
- )";
- // INSERT NOTIFICATIONS
- $insert = $wpdb->query($sql);
- // blank the email to stop it going through this time
- $atts = array('to' => '','subject' => '','message' => '','headers' => '','attachments' => array());
- } else {
- // no lead ID - do nothing to just pass the emails through as normal
- }
- return $atts;
- }
- add_filter('wp_mail','mj_capture_mail',1,1); // Add the above function to filter wp_mail
- }
- /*
- #############################################################
- REMOVE MAIL HOOK TO ENSURE MAIL WORKS AS NORMAL FOR OTHER FUNCTIONS
- #############################################################
- */
- add_action('gform_after_submission', 'post_creation_and_confirmation', 10, 2);
- function post_creation_and_confirmation($entry, $form) {
- remove_filter('wp_mail','mj_capture_mail',1,1);
- }
- /*
- #############################################################
- MANUALLY TRIGGER MAILS TO BE SENT
- #############################################################
- The function below can be called whenever you want to send out
- the stored notifications for a particular lead/entry
- Personally I call this once I get the IPN back from the payment
- gateway telling my site that a payment has gone through successfully
- NOTE: A lead ID is required - this is the entry ID for the
- particular form submission
- */
- function mj_send_notifications($leadID) {
- if((isset($leadID))&&(intval($leadID))) {
- global $wpdb,$ts_mail_errors,$phpmailer;
- // FETCH EMAIL/S
- $emails = $wpdb->get_results("SELECT * FROM mj_form_notifications WHERE mj_mail_lead = ".intval($orderid),ARRAY_A);
- if(count($emails)) {
- $errors = array();
- foreach($emails as $email) {
- $mailData = @unserialize($email['mj_mail_data']);
- $mailID = $email['mj_mail_id'];
- if(count($mailData)) {
- extract($mailData);
- // SEND THE EMAIL VIA wp_mail
- $sendMail = wp_mail( $to, $subject, $message, $headers, $attachments );
- // LOG SEND OR ERROR
- if($sendMail) {
- $wpdb->query("UPDATE mj_form_notifications SET mj_mail_sent = mj_mail_sent+1 WHERE mj_mail_id = ".intval($mailID));
- } else {
- if(!isset($ts_mail_errors)) $ts_mail_errors = array();
- if(isset($phpmailer)) {
- $ts_mail_errors[] = $phpmailer->ErrorInfo;
- $errors[] = $phpmailer->ErrorInfo;
- }
- $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));
- }
- }
- }
- }
- return (count($errors)) ? implode(', ',$errors) : true;
- } else {
- return 'No Lead ID posted';
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement