Advertisement
tribulant

function cron_hook()

Sep 5th, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.96 KB | None | 0 0
  1. function cron_hook() {
  2.             global $wpdb, $Db, $Email, $History, $Subscriber, $SubscribersList, $Queue;
  3.             $emailssent = 0;
  4.        
  5.             //update scheduling
  6.             $this -> scheduling();
  7.             $this -> autoresponders_hook();
  8.        
  9.             //ensure that scheduling has been turned on
  10.             if ($this -> get_option('scheduling') == "Y") {
  11.                 $emailsperinterval = $this -> get_option('emailsperinterval');
  12.                
  13.                 $emailsquery = "SELECT * FROM " . $wpdb -> prefix . $Queue -> table . " WHERE `senddate` < '" . date("Y-m-d H:i:s", time()) . "' LIMIT " . $emailsperinterval . "";
  14.                
  15.                 //retrieve all the queue emails for this execution
  16.                 if ($emails = $wpdb -> get_results($emailsquery)) {
  17.                 //if ($emails = $Queue -> find_all(false, false, false, $emailsperinterval)) {             
  18.                     //send an email notification to the administrator if specified
  19.                     if ($this -> get_option('schedulenotify') == "Y" && $this -> get_option('scheduling') == "Y") {
  20.                         $subscriber_id = $Subscriber -> admin_subscriber_id($_POST['mailinglists']);
  21.                         $subscriber = $Subscriber -> get($subscriber_id, false);
  22.                         $subject = $this -> et_subject('schedule', $subscriber);
  23.                         $fullbody = $this -> et_message('schedule', $subscriber);  
  24.                         $message = $this -> render_email(false, array('subscriber' => $subscriber, 'mailinglist' => $mailinglist), false, $this -> htmltf($subscriber -> format), true, $this -> default_theme_id(), false, $fullbody);
  25.                         $this -> execute_mail($subscriber, $subject, $message, false, false, false, false);
  26.                     }
  27.                    
  28.                     $subscriber = false;
  29.                     $subscriberids = array();
  30.                     $subscriberemails = array();
  31.                     $emailssent = 0;
  32.                
  33.                     foreach ($emails as $email) {
  34.                         $this -> remove_server_limits();
  35.                        
  36.                         $historyquery = "SELECT `post_id` FROM `" . $wpdb -> prefix . $History -> table . "` WHERE `id` = '" . $email -> history_id . "'";
  37.                         if ($history_post_id = $wpdb -> get_var($historyquery)) {
  38.                             if ($getpost = get_post($history_post_id)) {
  39.                                 global $post;
  40.                                 $post = $getpost;
  41.                             }
  42.                         }
  43.                        
  44.                         if ($subscriber = $Subscriber -> get($email -> subscriber_id, false)) {
  45.                             $subscriber -> mailinglist_id = $email -> mailinglist_id;
  46.                             $subscriber -> mailinglists = $email -> mailinglists;
  47.                             $eunique = md5($subscriber -> id . $subscriber -> mailinglist_id . $email -> history_id . date("YmdH", time()));
  48.                            
  49.                             $checkemailquery = "SELECT `id` FROM `" . $wpdb -> prefix . $Email -> table . "` WHERE `eunique` = '" . $eunique . "' AND `history_id` = '" . $email -> history_id . "'";                          
  50.                             if (!$wpdb -> get_var($checkemailquery)) {
  51.                                 if (empty($subscriberids) || (!empty($subscriberids) && !in_array($subscriber -> id, $subscriberids))) {
  52.                                     $subscriberids[] = $subscriber -> id;
  53.                                    
  54.                                     if ((empty($subscriberemails[$email -> history_id])) || (!empty($subscriberemails[$email -> history_id]) && !in_array($subscriber -> email, $subscriberemails[$email -> history_id]))) {                       
  55.                                         $subscriberemails[$email -> history_id][] = $subscriber -> email;
  56.                                        
  57.                                         $Db -> model = $Email -> model;
  58.                                         $message = $this -> render_email('send', array('message' => $email -> message, 'subject' => $email -> subject, 'subscriber' => $subscriber, 'history_id' => $email -> history_id, 'post_id' => $email -> post_id, 'eunique' => $eunique), false, $this -> htmltf($subscriber -> format), true, $email -> theme_id);
  59.                                        
  60.                                         if ($this -> execute_mail($subscriber, $email -> subject, $message, $email -> attachments, $email -> history_id, $eunique)) {                              
  61.                                             $Queue -> delete($email -> id);
  62.                                             $emailssent++;
  63.                                         }
  64.                                     }
  65.                                 }
  66.                             }
  67.                         } else {
  68.                             $Queue -> delete($email -> id);
  69.                         }
  70.                        
  71.                         $subscriber = array();
  72.                     }
  73.                 }
  74.             }
  75.                
  76.             //update the "lastcron" setting
  77.             $this -> update_option('lastcron', time());
  78.             echo $emailssent . " " . __('emails have been sent out', $this -> plugin_name);
  79.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement