WYSIJA_help_autonews

The MailPoet Newsletters WYSIJA help autonews class.

Defined (1)

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

/helpers/autonews.php  
  1. class WYSIJA_help_autonews extends WYSIJA_object { 
  2.  
  3. function __construct() { 
  4. parent::__construct(); 
  5.  
  6. function events($key=false, $get=true, $value_set=array()) { 
  7. static $events=array(); 
  8. if($get) { 
  9. if(!$key) { 
  10. return $events; 
  11. }else{ 
  12. if(isset($events[$key])) return $events[$key]; 
  13. return false; 
  14.  
  15. }else{ 
  16. if(isset($events[$key])) return false; 
  17. $events[$key]=$value_set; 
  18.  
  19. function register($key_event, $event=array()) { 
  20. $this->events($key_event, false, $event); 
  21.  
  22. function get($fieldKey) { 
  23. return $this->events($fieldKey); 
  24.  
  25. /** 
  26. * I'm not sure if this function is useful anymore 
  27. * @param type $email 
  28. * @return type 
  29. */ 
  30. function _deprecated_nextSend($email=false) { 
  31. if(!$email) return; 
  32. $model_email=WYSIJA::get('email', 'model'); 
  33.  
  34. if(is_array($email)) { 
  35. $email_data=$model_email->getOne(false, array('email_id'=>$email['email_id'])); 
  36. }else{ 
  37. $email_data=$model_email->getOne(false, array('email_id'=>$email)); 
  38. return $model_email->give_birth($email_data); 
  39.  
  40. /** 
  41. * @param type $email 
  42. * @return type 
  43. */ 
  44. function getNextSend($email) { 
  45. $schedule_at = -1; 
  46.  
  47. //this condition makes sure that our $email is a post notification 
  48. if((int)$email['type'] === 2 && isset($email['params']['autonl']['event']) && $email['params']['autonl']['event'] === 'new-articles') { 
  49.  
  50. $helper_toolbox = WYSIJA::get('toolbox', 'helper'); 
  51. // get current time 
  52. $now = time(); 
  53.  
  54.  
  55. //local time = site's time set by the administrator of the site in WP's settings 
  56. //server time = time return by PHP functions such as time() 
  57. //nextSend is the time scheduled in the future of the local time 
  58. //so if we compare the current time to the next send value we need to offset only one value and that would be the nextSend 
  59. //this way both values are on server time 
  60. if(!isset($email['params']['autonl']['nextSend']) || $now > $helper_toolbox->localtime_to_servertime($email['params']['autonl']['nextSend'])) { 
  61. switch($email['params']['autonl']['when-article']) { 
  62. case 'immediate': 
  63. break; 
  64. case 'daily': 
  65. // get timestamp of when the newsletter is supposed to be sent 
  66. $schedule_at = strtotime($email['params']['autonl']['time']); 
  67.  
  68. // check if the scheduled at time has already passed 
  69. if($helper_toolbox->localtime_to_servertime($schedule_at) < $now) { 
  70. // schedule it for tomorrow 
  71. $schedule_at = strtotime('tomorrow '.$email['params']['autonl']['time']); 
  72. break; 
  73. case 'weekly': 
  74. // get timestamp of when the newsletter is supposed to be sent 
  75. $schedule_at = strtotime(ucfirst($email['params']['autonl']['dayname']).' '.$email['params']['autonl']['time']); 
  76.  
  77. // check if the scheduled at time has already passed 
  78. if($helper_toolbox->localtime_to_servertime($schedule_at) < $now) { 
  79. // schedule it for next week 
  80. $schedule_at = strtotime('next '.ucfirst($email['params']['autonl']['dayname']).' '.$email['params']['autonl']['time']); 
  81. break; 
  82. case 'monthly': 
  83. $time_current_day=date('d', $now); 
  84. $time_current_month=date('m', $now); 
  85. $time_current_year=date('y', $now); 
  86.  
  87. // we increment the next date to next months in two cases 
  88. // 1 - if we're setting the next date using the interface in step 1 or step 3 of the newsletter edition and the current day is greater to the selected day 
  89. if(isset( $_POST['save-reactivate'] ) || isset( $_POST['submit-send'] ) ) { 
  90. //trigger has to be next month 
  91. if($time_current_day > $email['params']['autonl']['daynumber']) { 
  92.  
  93. if((int)$time_current_month === 12) { 
  94. //year +1 
  95. $time_current_month=1; 
  96. $time_current_year++; 
  97. }else{ 
  98. //current year 
  99. $time_current_month++; 
  100. // 2 - if we're setting the next date automatically and the date is already passed 
  101. }else{ 
  102. if($helper_toolbox->localtime_to_servertime($schedule_at) < $now) { 
  103.  
  104. if((int)$time_current_month === 12) { 
  105. //year +1 
  106. $time_current_month=1; 
  107. $time_current_year++; 
  108. }else{ 
  109. //current year 
  110. $time_current_month++; 
  111.  
  112. //3 - otherwise we stay in the same month 
  113.  
  114.  
  115. $schedule_at=strtotime($time_current_month.'/'.$email['params']['autonl']['daynumber'].'/'.$time_current_year.' '.$email['params']['autonl']['time']); 
  116. break; 
  117. case 'monthlyevery': // monthly every X Day of the week 
  118. $current_day = date('d', $now); 
  119. $current_month = date('m', $now); 
  120. $current_year = date('y', $now); 
  121.  
  122. // calculate the timestamp of the Xth day of the week of the current month 
  123. // strtotime('02/01/13 1 Monday 20:00:00') -> this will return the timestamp of the 1st monday of the current month 
  124. $schedule_at = strtotime( 
  125. sprintf('%02d/01/%02d %d %s %s',  
  126. $current_month,  
  127. $current_year,  
  128. $email['params']['autonl']['dayevery'],  
  129. ucfirst($email['params']['autonl']['dayname']),  
  130. $email['params']['autonl']['time'] 
  131. )); 
  132.  
  133. if($helper_toolbox->localtime_to_servertime($schedule_at) < $now) { 
  134. // get first day timestamp of next month 
  135. $first_day_of_next_month = $this->get_first_day_of_month($schedule_at, 1); 
  136.  
  137. // get next month's Xth day of the week 
  138. $schedule_at = strtotime( 
  139. sprintf('%02d/01/%02d %d %s %s',  
  140. date('m', $first_day_of_next_month),  
  141. date('y', $first_day_of_next_month),  
  142. $email['params']['autonl']['dayevery'],  
  143. ucfirst($email['params']['autonl']['dayname']),  
  144. $email['params']['autonl']['time'] 
  145. ); 
  146. break; 
  147. return $schedule_at; 
  148.  
  149. function get_first_day_of_month($time_stamp, $months_to_add = 0) { 
  150. // You can add as many months as you want. mktime will accumulate to the next year. 
  151. $date = getdate($time_stamp); // Covert to Array 
  152. // add number of months 
  153. $date['mon'] = $date['mon'] + (int)$months_to_add; 
  154. // set day to 1 
  155. $date['mday'] = 1; 
  156. // return timestamp 
  157. return mktime($date['hours'], $date['minutes'], $date['seconds'], $date['mon'], $date['mday'], $date['year']); 
  158.  
  159. /** 
  160. * get the time of the n dayname of the month 
  161. * @param type $first_day_of_month 
  162. * @param type $day_name 
  163. * @param type $which_number 
  164. * @param type $time_now 
  165. * @return type 
  166. */ 
  167. function getNextDay($first_day_of_month, $day_name, $which_number, $time_now) { 
  168. $name_first_day = strtolower(date('l', $first_day_of_month)); 
  169.  
  170. if($name_first_day == strtolower($day_name)) $which_number--; 
  171. for($i=0; $i < $which_number;$i++) { 
  172. $first_day_of_month = strtotime('next '.ucfirst($day_name), $first_day_of_month); 
  173. return $first_day_of_month; 
  174.  
  175.  
  176. /** 
  177. * check if there is post notification needing a child email 
  178. */ 
  179. function checkPostNotif() { 
  180. // flag security to make sure that there can't be two checks of the post notif in the same minute 
  181. $current_check = (float)get_option('wysija_check_pn'); 
  182.  
  183. // there is a check that has been starting to run less than 60 seconds ago 
  184. if(microtime(true) < ($current_check+60)) { 
  185. WYSIJA::log('already_running_checkPN', $current_check, 'post_notif'); 
  186. return false; 
  187.  
  188. // flag is down we process our post notification check and set the start time of the current check 
  189. $current_check=microtime(true); 
  190. WYSIJA::update_option('wysija_check_pn', $current_check); 
  191.  
  192. // let's check when do we come here 
  193. WYSIJA::log('check_post_notif_starts', $current_check , 'post_notif'); 
  194.  
  195. $model_email=WYSIJA::get('email', 'model'); 
  196. $model_email->reset(); 
  197. $all_emails=$model_email->get(false, array('type'=>'2', 'status'=>array('1', '3', '99'))); 
  198.  
  199. if($all_emails) { 
  200. $helper_toolbox=WYSIJA::get('toolbox', 'helper'); 
  201. foreach($all_emails as $email) { 
  202. //post notification make a child newsletter when the timing is immediate otherwise let the cron take care of it 
  203. if($email['params']['autonl']['event']=='new-articles' && $email['params']['autonl']['when-article']!='immediate') { 
  204. //check if the next sending is passed if so then we give birth to one child email 
  205. //IMPORTANT WE COMPARE TO THE OFFSET TIME (time set by the administrator) 
  206. //if the nextSend doesn't exist then we set it 
  207. if(!isset($email['params']['autonl']['nextSend'])) { 
  208. WYSIJA::log('check_post_notif_next_send_not_set', $current_check , 'post_notif'); 
  209. }else { 
  210. //if the next send is passed we should trigger it 
  211. $time_now_server=time(); 
  212. if($time_now_server > $helper_toolbox->localtime_to_servertime($email['params']['autonl']['nextSend'])) { 
  213. $how_late=$time_now_server-$helper_toolbox->localtime_to_servertime($email['params']['autonl']['nextSend']); 
  214. //check how late was the previous notification,  
  215. //if it has been more than two hours late then cancel it and change it to the next day 
  216. if(!$this->cancel_late_post_notification($email, $how_late)) { 
  217. WYSIJA::log('check_post_notif_before_give_birth', $current_check, 'post_notif'); 
  218. //it is not cancel so we can give birth toa a child newsletter 
  219. $model_email->give_birth($email); 
  220.  
  221. /** 
  222. * email has a late post notification which needs to be cancelled and postponed 
  223. * @param array $email 
  224. * @param int $how_late 
  225. * @return boolean 
  226. */ 
  227. function cancel_late_post_notification($email, $how_late) { 
  228. $cancel_it=false; 
  229. switch($email['params']['autonl']['when-article']) { 
  230. case 'daily': 
  231. //cancel a daily notification with more than two hours delay 
  232. if($how_late>(2*3600)) { 
  233. $cancel_it=true; 
  234. break; 
  235. case 'weekly': 
  236. //cancel a weekly notification with more than half a day delay 
  237. if($how_late>(12*3600)) { 
  238. $cancel_it=true; 
  239. break; 
  240. case 'monthly': 
  241. //cancel a monthly notification with more than a day delay 
  242. if($how_late>(24*3600)) { 
  243. $cancel_it=true; 
  244. break; 
  245. case 'monthlyevery': 
  246. //cancel a monthly notification with more than a day delay 
  247. if($how_late>(24*3600)) { 
  248. $cancel_it=true; 
  249. break; 
  250. //if the notification is being cancelled then we store the value of that late notification and update the nextSend 
  251. if($cancel_it) { 
  252.  
  253. $late_send=$email['params']['autonl']['nextSend']; 
  254. WYSIJA::log('cancel_late_post_notification_late_send', $late_send, 'post_notif'); 
  255. $next_send=$this->getNextSend($email); 
  256. $email['params']['autonl']['nextSend']=$next_send; 
  257. $email['params']['autonl']['late_send']=$late_send; 
  258.  
  259. $model_email=WYSIJA::get('email', 'model'); 
  260. $model_email->reset(); 
  261. $model_email->update(array('params'=>$email['params']), array('email_id' => $email['email_id'])); 
  262.  
  263. return true; 
  264.  
  265. return false; 
  266.  
  267. /** 
  268. * check if there are any scheduled email not sent yet 
  269. */ 
  270. function checkScheduled() { 
  271. $model_email = WYSIJA::get('email', 'model'); 
  272. $helper_toolbox = WYSIJA::get('toolbox', 'helper'); 
  273. $model_email->reset(); 
  274.  
  275. // select the scheduled emails 
  276. $all_emails = $model_email->get(false, array('type'=>'1', 'status'=>'4')); 
  277.  
  278. if($all_emails) { 
  279.  
  280. foreach($all_emails as $email) { 
  281.  
  282. // check if the email is scheduled 
  283. if(isset($email['params']['schedule']['isscheduled'])) { 
  284.  
  285. $schedule_date = $email['params']['schedule']['day'] . ' ' . $email['params']['schedule']['time']; 
  286. $unix_scheduled_time = strtotime($schedule_date); 
  287.  
  288. // if the scheduled time is passed let's send the email 
  289. // we don't compare to the time recorded but to the offset time which is the time set by the user in his time 
  290. if($helper_toolbox->localtime_to_servertime($unix_scheduled_time) < time()) { 
  291. $model_email->reset(); 
  292. $model_email->send_activate($email); 
  293.  
  294. function refresh_automatic_content($email_ids = array()) { 
  295. // TO OPTIMIZE: add a boolean flag for ALP widget being present or not (this way we filter out "static" auto nl) 
  296. $model_email = WYSIJA::get('email', 'model'); 
  297.  
  298. $conditions = array('type' => 2, 'status' => array(1, 3, 99)); 
  299. if(!empty($email_ids)) { 
  300. $conditions['email_id'] = $email_ids; 
  301. // get only the data needed to update an auto nl so we save some resources 
  302. $data_needed = array('campaign_id', 'email_id', 'params', 'wj_styles', 'wj_data'); 
  303. $emails = $model_email->get( $data_needed, $conditions ); 
  304.  
  305. foreach($emails as $key => $email) { 
  306. if(is_array($email) && isset($email['params']['autonl']['event']) ) { 
  307.  
  308. $wj_data = unserialize(base64_decode($email['wj_data'])); 
  309. $reload_auto_content = false; 
  310. foreach($wj_data['body'] as $block) { 
  311. if(isset($block['type']) && $block['type'] === 'auto-post') { 
  312. $reload_auto_content = true; 
  313. if(!$reload_auto_content) continue; 
  314.  
  315. // we have to regenerate the html rendering of each auto newsletter 
  316. $helper_wj_engine = WYSIJA::get('wj_engine', 'helper'); 
  317. $helper_wj_engine->setStyles($email['wj_styles'], true); 
  318. $helper_wj_engine->setData($email['wj_data'], true); 
  319.  
  320. // update email data 
  321. $values = array( 
  322. 'email_id' => (int)$email['email_id'],  
  323. 'wj_data' => $helper_wj_engine->getEncoded('data'),  
  324. 'body' => $helper_wj_engine->renderEmail($email) 
  325. ); 
  326.  
  327. // make sure the modified_at columns get updated 
  328. $model_email->columns['modified_at']['autoup'] = 1; 
  329.  
  330. // update data in DB 
  331. $model_email->update($values, array('email_id' => (int)$email['email_id'])); 
  332.  
  333. // removes any auto-post block 
  334. function remove_autopost_blocks($data = array()) { 
  335. if(empty($data)) { 
  336. return false; 
  337.  
  338. // decode data 
  339. $wj_data = unserialize(base64_decode($data)); 
  340.  
  341. // init updated_wj_data variable with initial wj_data 
  342. $updated_wj_data = $wj_data; 
  343.  
  344. foreach($wj_data['body'] as $key => $block) { 
  345. // if we detect an auto-post block, we need to remove it 
  346. if(isset($block['type']) && $block['type'] === 'auto-post') { 
  347. // remove block from updated data 
  348. unset($updated_wj_data['body'][$key]); 
  349. // if the wj_data has not changed, return false 
  350. if($updated_wj_data === $wj_data) { 
  351. return false; 
  352. } else { 
  353. // otherwise return encoded version of updated_wj_data 
  354. return base64_encode(serialize($updated_wj_data));