WYSIJA_help_notifications

The MailPoet Newsletters WYSIJA help notifications class.

Defined (1)

The class is defined in the following location(s).

/helpers/notifications.php  
  1. class WYSIJA_help_notifications extends WYSIJA_object{ 
  2.  
  3. /** 
  4. * is this time to send the report yet? 
  5. * @return boolean 
  6. */ 
  7. private function _can_send_daily_report() { 
  8. $model_config = WYSIJA::get('config' , 'model'); 
  9. $last_daily_report = $model_config->getValue('last_daily_report'); 
  10.  
  11. // make sure the last report was not sent within the last 23 hours 
  12. if(time() - $last_daily_report < (3600*23)) return false; 
  13.  
  14. // if the scheduled time is passed let's send the email 
  15. // we don't compare to the time recorded but to the offset time which is the time set by the user in his time 
  16. $helper_toolbox = WYSIJA::get('toolbox', 'helper'); 
  17. $unix_scheduled_time = strtotime('Today, 23:00:00'); 
  18.  
  19. if($helper_toolbox->localtime_to_servertime($unix_scheduled_time) < time()) { 
  20. return true; 
  21. return false; 
  22.  
  23.  
  24. /** 
  25. * send an email summary of the email activity 
  26. * @return type 
  27. */ 
  28. public function send_daily_report() { 
  29. //make sure tody's report hasn't been sent yet 
  30. if(!$this->_can_send_daily_report()) return false; 
  31.  
  32. //know everything that happened in the last 24 hours 
  33. $one_day_ago = time()-(3600*24); 
  34.  
  35. $model_email_user_stat = WYSIJA::get('email_user_stat', 'model'); 
  36. $query='SELECT COUNT('.$model_email_user_stat->getPk().') as count, status FROM `[wysija]'.$model_email_user_stat->table_name.'` 
  37. WHERE sent_at>'.$one_day_ago.' 
  38. GROUP BY status'; 
  39. $status_count = $model_email_user_stat->query('get_res', $query); 
  40.  
  41. $query = 'SELECT B.user_id, B.email FROM `[wysija]'.$model_email_user_stat->table_name.'` as A JOIN `[wysija]user` as B on A.user_id=B.user_id 
  42. WHERE A.sent_at>'.$one_day_ago." AND A.status='-1'"; 
  43. $details = $model_email_user_stat->query('get_res', $query); 
  44.  
  45. $total = 0; 
  46. foreach($status_count as &$count) { 
  47. switch($count['status']) { 
  48. case '-2': 
  49. $count['status']=__('undelivered', WYSIJA); 
  50. break; 
  51. case '-1': 
  52. $count['status']=__('bounced', WYSIJA); 
  53. break; 
  54. case '0': 
  55. $count['status']=__('unopened', WYSIJA); 
  56. break; 
  57. case '1': 
  58. $count['status']=__('opened', WYSIJA); 
  59. break; 
  60. case '2': 
  61. $count['status']=__('clicked', WYSIJA); 
  62. break; 
  63. case '3': 
  64. $count['status']=__('unsubscribed', WYSIJA); 
  65. break; 
  66. $total = $total + $count['count']; 
  67.  
  68. if((int)$total<=0) return; 
  69.  
  70. $html = '<h2>'.__('Today\'s statistics', WYSIJA).'</h2>'; 
  71. $html .= '<h3>'.sprintf(__('Today you have sent %1$s emails', WYSIJA), $total); 
  72. foreach($status_count as $counting) { 
  73. $html .= sprintf(__(', %1$s of which were %2$s', WYSIJA), $counting['count'], $counting['status']); 
  74. $html .= '.</h3>'; 
  75. if(count($details)>0) { 
  76. $html .= '<h2>'.sprintf(__('Here is the list of bounced emails.', WYSIJA), $total).'</h2>'; 
  77.  
  78. foreach($details as $email) { 
  79. $html .= '<p>'.$email['email'].'</p>'; 
  80. $html .= '<p>'.__('Cheers, your MailPoet Newsletter Plugin', WYSIJA).'</p>'; 
  81.  
  82. $model_config = WYSIJA::get('config', 'model'); 
  83. $helper_mailer = WYSIJA::get('mailer', 'helper'); 
  84. $helper_mailer->testemail=true; 
  85.  
  86. $emails_notifieds = $model_config->getValue('emails_notified'); 
  87. $emails_notifieds = explode(', ' , $emails_notifieds); 
  88.  
  89.  
  90. foreach($emails_notifieds as $receiver) { 
  91. $helper_mailer->sendSimple( trim($receiver) , __('Your daily newsletter stats', WYSIJA), $html); 
  92.  
  93. // keep track of the time we sent the daily report to make sure we don't send it many times per day but just once 
  94. $model_config->save( array('last_daily_report' => time() )); 
  95.  
  96.