WYSIJA_help_cron

The MailPoet Newsletters WYSIJA help cron class.

Defined (1)

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

/helpers/cron.php  
  1. class WYSIJA_help_cron extends WYSIJA_object{ 
  2.  
  3. var $report=false; 
  4.  
  5. function __construct() { 
  6. parent::__construct(); 
  7.  
  8. /** 
  9. * the cron tasks are being run for a certain number of processes (all queue, bounce etc..) 
  10. * @return void 
  11. */ 
  12. function run() { 
  13. @ini_set('max_execution_time', 0); 
  14. $model_config = WYSIJA::get('config', 'model'); 
  15. $running = false; 
  16. if(!$model_config->getValue('cron_manual')) { 
  17. return; 
  18. // get the param from where you want 
  19. $report = $process = false; 
  20. if(isset($_REQUEST['process']) && $_REQUEST['process']) { 
  21. $process = $_REQUEST['process']; 
  22. }elseif(!isset($_SERVER['REQUEST_URI']) && isset($_SERVER['SHELL']) && isset($_SERVER['argv'][2]) && $_SERVER['argv'][2]) { 
  23. $process = $_SERVER['argv'][2]; 
  24.  
  25. if(isset($_REQUEST['report']) && $_REQUEST['report']) { 
  26. $this->report = $_REQUEST['report']; 
  27. }elseif(!isset($_SERVER['REQUEST_URI']) && isset($_SERVER['SHELL']) && isset($_SERVER['argv'][3]) && $_SERVER['argv'][3]) { 
  28. $this->report = $_SERVER['argv'][3]; 
  29.  
  30. if( !empty($process) ) { 
  31. //include the needed parts of wp plus wysija 
  32. if(isset($_REQUEST[WYSIJA_CRON]) || ( isset($_SERVER['argv'][1]) && $_SERVER['argv'][1]==WYSIJA_CRON )) echo ''; 
  33. else{ 
  34. wp_die("<h2>" . 'Invalid token' . "</h2>", "MailPoet CRON error", array( 
  35. 'response' => 404,  
  36. 'back_link' => false 
  37. )); 
  38. $cron_schedules = get_option('wysija_schedules'); 
  39.  
  40. $processes = array(); 
  41. if(strpos($process, ', ')!==false) { 
  42. $processes = explode(', ', $process); 
  43. }else $processes[] = $process; 
  44.  
  45. $allProcesses = array('queue', 'bounce', 'daily', 'weekly', 'monthly'); 
  46. foreach($processes as $scheduleprocess) { 
  47. if($scheduleprocess!='all') { 
  48. if( in_array( $scheduleprocess, $allProcesses ) ) { 
  49. $this->check_scheduled_task($cron_schedules, $scheduleprocess); 
  50. }else{ 
  51. wp_die("<h2>" . 'Invalid process' . "</h2>", "MailPoet CRON error", array( 
  52. 'response' => 404,  
  53. 'back_link' => false 
  54. )); 
  55. }else{ 
  56. foreach($allProcesses as $processNK) { 
  57. $this->check_scheduled_task($cron_schedules, $processNK); 
  58. if($this->report) echo 'processed : All<br/>'; 
  59. if(!isset($_REQUEST['silent'])) echo 'MailPoet\'s cron is ready. Simply setup a CRON job on your server (cpanel or other) to trigger this page.'; 
  60. exit; 
  61. }else{ 
  62. wp_die("<h2>" . 'Missing process' . "</h2>", "MailPoet CRON error", array( 
  63. 'response' => 404,  
  64. 'back_link' => false 
  65. )); 
  66. if(!isset($_REQUEST['silent'])) echo '"MailPoet\'s cron is ready. Simply setup a CRON job on your server (cpanel or other) to trigger this page.' ; 
  67. if($process) exit; 
  68.  
  69. /** 
  70. * check that one scheduled task is ready to be executed 
  71. * @param type $cron_schedules list of recorded cron schedules 
  72. * @param type $processNK what to process queue, bounce etc... 
  73. */ 
  74. function check_scheduled_task($cron_schedules, $processNK) { 
  75. $helper_toolbox = WYSIJA::get('toolbox', 'helper'); 
  76. $time_passed = $time_left = 0; 
  77. $run_scheduled = true; 
  78. $extra_text = $multisite_prefix = ''; 
  79. // this is to display a different message whether we're dealing with bounce or not. 
  80. if($processNK == 'bounce') { 
  81. $model_config = WYSIJA::get( 'config' , 'model' ); 
  82. // if premium is activated we launch the premium function 
  83. $multisite_prefix = ''; 
  84. if(is_multisite()) { 
  85. $multisite_prefix = 'ms_'; 
  86.  
  87. // we don't process the bounce automatically unless the option is ticked 
  88. if(!(defined('WYSIJANLP') && $model_config->getValue( $multisite_prefix . 'bounce_process_auto' )) ) { 
  89. $extra_text = ' (bounce handling not activated)'; 
  90. $run_scheduled=false; 
  91.  
  92.  
  93. // calculate the time passed processing a scheduled task 
  94. if(!empty($cron_schedules[$processNK]['running'])) { 
  95. $time_passed = time()- $cron_schedules[$processNK]['running']; 
  96. $time_passed = $helper_toolbox->duration_string($time_passed, true, 2, 5); 
  97. }else{ 
  98. $time_left = $cron_schedules[$processNK]['next_schedule'] - time(); 
  99. $time_left = $helper_toolbox->duration_string($time_left, true, 2, 5); 
  100.  
  101. if($run_scheduled && $cron_schedules[$processNK]['next_schedule'] < time() && !$cron_schedules[$processNK]['running']) { 
  102. if($this->report) echo 'exec process '.$processNK.'<br/>'; 
  103. $this->run_scheduled_task($processNK); 
  104. }else{ 
  105. if($this->report) { 
  106. if($time_passed) $text_time = ' running since : '.$time_passed; 
  107. else $text_time = ' next run : '.$time_left; 
  108. if(!empty($extra_text)) $text_time = $extra_text; 
  109. echo 'skip process <strong>'.$processNK.'</strong>'.$text_time.'<br/>'; 
  110.  
  111. /** 
  112. * run process if it's not detected as already running 
  113. * @param type $process 
  114. * @return type 
  115. */ 
  116. function run_scheduled_task($process = 'queue') { 
  117. //first let's make sure that the process asked to be run is not already running 
  118. $scheduled_times = WYSIJA::get_cron_schedule($process); 
  119. $processes = WYSIJA::get_cron_frequencies(); 
  120. $process_frequency = $processes[$process]; 
  121.  
  122. // check if the scheduled task is already being processed,  
  123. // we consider it timed out once the started running time plus the frequency has been passed 
  124. if(!empty($scheduled_times['running']) && ($scheduled_times['running'] + $process_frequency) > time()) { 
  125. if($this->report) echo 'already running : '.$process.'<br/>'; 
  126. return; 
  127.  
  128. // set schedule as running 
  129. WYSIJA::set_cron_schedule($process, 0, time()); 
  130.  
  131. // execute schedule 
  132. switch($process) { 
  133. case 'queue': 
  134. // check if there are any scheduled newsletters ready for action 
  135. WYSIJA::check_scheduled_newsletters(); 
  136.  
  137. // if premium is activated we execute the premium cron process 
  138. if(defined('WYSIJANLP')) { 
  139. $helper_premium = WYSIJA::get('premium', 'helper', false, WYSIJANLP); 
  140. $helper_premium->croned_queue_process(); 
  141. }else{ 
  142. // run the standard queue process no scheduled tasks will be check since it has already been checked above 
  143. WYSIJA::croned_queue(false); 
  144. break; 
  145. case 'bounce': 
  146. $helper_premium = WYSIJA::get('premium', 'helper', false, WYSIJANLP); 
  147. $model_config = WYSIJA::get( 'config' , 'model' ); 
  148. // if premium is activated we launch the premium function 
  149. if(is_multisite()) { 
  150. $multisite_prefix='ms_'; 
  151.  
  152. // we don't process the bounce automatically unless the option is ticked 
  153. if(defined('WYSIJANLP') && $model_config->getValue( $multisite_prefix . 'bounce_process_auto' )) { 
  154. $helper_premium->croned_bounce(); 
  155. }else{ 
  156. $process .= ' (bounce handling not activated)'; 
  157.  
  158. break; 
  159. case 'daily': 
  160. WYSIJA::croned_daily(); 
  161. break; 
  162. case 'weekly': 
  163. if(defined('WYSIJANLP')) { 
  164. $helper_premium = WYSIJA::get('premium', 'helper', false, WYSIJANLP); 
  165. $helper_premium->croned_weekly(); 
  166. WYSIJA::croned_weekly(); 
  167. break; 
  168. case 'monthly': 
  169. WYSIJA::croned_monthly(); 
  170. break; 
  171. // set next_schedule details 
  172. WYSIJA::set_cron_schedule($process); 
  173. if($this->report) echo 'processed : '.$process.'<br/>';