function wysija_exec_process($process = 'queue') { $scheduled_times = WYSIJA::get_cron_schedule($process); if (isset($scheduled_times['running']) && $scheduled_times['running'] && $scheduled_times['running'] + 900 > time()) { echo 'already running : ' . $process . '<br/>'; return; } //set schedule as running WYSIJA::set_cron_schedule($process, 0, time()); //execute schedule switch ($process) { case 'queue': WYSIJA::croned_queue($process); $hPremium =& WYSIJA::get('premium', 'helper', false, WYSIJANLP); if (is_object($hPremium)) { $hPremium->splitVersion_croned_queue_process(); } break; case 'bounce': $hPremium =& WYSIJA::get('premium', 'helper', false, WYSIJANLP); if (is_object($hPremium)) { $hPremium->croned_bounce(); } break; case 'daily': WYSIJA::croned_daily(); break; case 'weekly': if (is_object($hPremium)) { $hPremium->croned_weekly(); } break; case 'monthly': WYSIJA::croned_monthly(); break; } //set next_schedule WYSIJA::set_cron_schedule($process); if ($report) { echo 'processed : ' . $process . '<br/>'; } }
/** * check that there is no passed schedules that need to be executed now * @return void */ public static function cron_check() { $cron_schedules = WYSIJA::get_cron_schedule('all'); if (empty($cron_schedules)) { return; } else { $processes = WYSIJA::get_cron_frequencies(); $updatedsched = false; foreach ($cron_schedules as $proc => &$params) { $running = 0; if (isset($params['running'])) { $running = $params['running']; } //if the process has timedout we reschedule the next execution if ($running && time() > $running + 900) { //WYSIJA::setInfo('error','modifying next schedule for '.$proc); $next_schedule = time() + $processes[$proc]; $params = array('next_schedule' => $next_schedule, 'prev_schedule' => $running, 'running' => false); $updatedsched = true; } } if ($updatedsched) { //WYSIJA::setInfo('error','updating scheds'); WYSIJA::update_option('wysija_schedules', $cron_schedules, 'yes'); } } $timenow = time(); $processesToRun = array(); foreach ($cron_schedules as $process => $scheduled_times) { if ((!$scheduled_times['running'] || (int) $scheduled_times['running'] + 900 < $timenow) && $scheduled_times['next_schedule'] < $timenow) { $processesToRun[] = $process; } } $model_config =& WYSIJA::get('config', 'model'); if (!empty($processesToRun) && $model_config->getValue('cron_page_hit_trigger')) { //call the cron url $cron_url = site_url('wp-cron.php') . '?' . WYSIJA_CRON . '&action=wysija_cron&process=' . implode(',', $processesToRun) . '&silent=1'; //TODO we should use the http class there $hHTTP =& WYSIJA::get('http', 'helper'); $hHTTP->request_timeout($cron_url); } }
/** * run process if it's not detected as already running * @param type $process * @return type */ function run_scheduled_task($process = 'queue') { //first let's make sure that the process asked to be run is not already running $scheduled_times = WYSIJA::get_cron_schedule($process); $processes = WYSIJA::get_cron_frequencies(); $process_frequency = $processes[$process]; // check if the scheduled task is already being processed, // we consider it timed out once the started running time plus the frequency has been passed if (!empty($scheduled_times['running']) && $scheduled_times['running'] + $process_frequency > time()) { if ($this->report) { echo 'already running : ' . $process . '<br/>'; } return; } // set schedule as running WYSIJA::set_cron_schedule($process, 0, time()); // execute schedule switch ($process) { case 'queue': // check if there are any scheduled newsletters ready for action WYSIJA::check_scheduled_newsletters(); // if premium is activated we execute the premium cron process if (defined('WYSIJANLP')) { $helper_premium = WYSIJA::get('premium', 'helper', false, WYSIJANLP); $helper_premium->croned_queue_process(); } else { // run the standard queue process no scheduled tasks will be check since it has already been checked above WYSIJA::croned_queue(false); } break; case 'bounce': $helper_premium = WYSIJA::get('premium', 'helper', false, WYSIJANLP); $model_config = WYSIJA::get('config', 'model'); // if premium is activated we launch the premium function if (is_multisite()) { $multisite_prefix = 'ms_'; } // we don't process the bounce automatically unless the option is ticked if (defined('WYSIJANLP') && $model_config->getValue($multisite_prefix . 'bounce_process_auto')) { $helper_premium->croned_bounce(); } else { $process .= ' (bounce handling not activated)'; } break; case 'daily': WYSIJA::croned_daily(); break; case 'weekly': if (defined('WYSIJANLP')) { $helper_premium = WYSIJA::get('premium', 'helper', false, WYSIJANLP); $helper_premium->croned_weekly(); } WYSIJA::croned_weekly(); break; case 'monthly': WYSIJA::croned_monthly(); break; } // set next_schedule details WYSIJA::set_cron_schedule($process); if ($this->report) { echo 'processed : ' . $process . '<br/>'; } }
function defaultDisplay() { $this->data['base_url'] = $this->base_url . '?' . http_build_query($_REQUEST); // saved filter $this->title = __('Newsletters', WYSIJA); $this->viewShow = $this->action = 'main'; $this->js[] = 'wysija-admin-list'; $this->jsTrans["selecmiss"] = __('Please select a newsletter.', WYSIJA); $this->jsTrans['suredelete'] = __('Delete this newsletter for ever?', WYSIJA); $this->jsTrans['suredelete_bulk'] = __('Delete these newsletters for ever?', WYSIJA); $this->jsTrans['processqueue'] = __('Sending batch of emails...', WYSIJA); $this->jsTrans['viewnews'] = __('View newsletter', WYSIJA); $this->jsTrans['confirmpauseedit'] = __('The newsletter will be deactivated, you will need to reactivate it once you\'re over editing it. Do you want to proceed?', WYSIJA); //get the filters if (isset($_REQUEST['search']) && $_REQUEST['search']) { $this->filters['like'] = array(); foreach ($this->searchable as $field) { $this->filters['like'][$field] = $_REQUEST['search']; } } if (isset($_REQUEST['filter-list']) && $_REQUEST['filter-list']) { $this->filters['equal'] = array('C.list_id' => $_REQUEST['filter-list']); } if (isset($_REQUEST['filter-date']) && $_REQUEST['filter-date']) { $this->filters['greater_eq'] = array('created_at' => $_REQUEST['filter-date']); $this->filters['less_eq'] = array('created_at' => strtotime('+1 month', $_REQUEST['filter-date'])); } $this->filters['is'] = array('type' => 'IS NOT NULL'); if (isset($_REQUEST['link_filter']) && $_REQUEST['link_filter']) { $linkfilters = explode('-', $_REQUEST['link_filter']); if (count($linkfilters) > 1) { $this->_addLinkFilter($linkfilters[1], $linkfilters[0]); } else { $this->_addLinkFilter($_REQUEST['link_filter']); } } $this->modelObj->noCheck = true; $this->modelObj->reset(); if ($this->filters) { $this->modelObj->setConditions($this->filters); } // Count emails by status and type $emails_by_status = $this->count_emails_by_status(); $emails_by_type = $this->count_emails_by_type(); $counts = array_merge($emails_by_status, $emails_by_type); $counts['all'] = array_sum($emails_by_status); // collect data $this->data['campaigns'] = $this->get_campaigns(); $this->data['datemin'] = $this->get_oldest_compaign(); $lists = $this->get_lists(); // $lists is in use later within this scope $this->data['lists'] = $lists; // for paging $this->modelObj->countRows = $counts['all']; if ($this->filters) { $count_emails = $this->count_emails(); if (!empty($count_emails)) { $this->modelObj->countRows = $count_emails; } } // count queue $email_ids = array(); foreach ($this->data['campaigns'] as $emailcamp) { if (in_array($emailcamp['status'], array(1, 3, 99))) { $email_ids[] = $emailcamp['email_id']; } } $model_queue = WYSIJA::get('queue', 'model'); $model_queue->setConditions(array("email_id" => $email_ids)); $model_queue->groupBy('email_id'); $queue = $model_queue->count(); if ($queue) { $this->viewObj->queuedemails = $queue; } $this->modelObj->reset(); //make a loop from the first created to now and increment an array of months $now = time(); $this->data['dates'] = array(); if ((int) $this->data['datemin']['datemin'] > 1) { setlocale(LC_TIME, 'en_US'); $formtlettres = "1 " . date('F', $this->data['datemin']['datemin']) . ' ' . date("Y", $this->data['datemin']['datemin']); $month_start = strtotime($formtlettres); if ($month_start > 0) { for ($i = $month_start; $i < $now; $i = strtotime('+1 month', $i)) { $this->data['dates'][$i] = date_i18n('F Y', $i); //date('F Y',$i); } } } $campaign_ids_sent = $campaign_ids = array(); foreach ($this->data['campaigns'] as &$campaign) { $campaign_ids[] = $campaign['campaign_id']; $model_email = WYSIJA::get('email', 'model'); $model_email->getParams($campaign); if (in_array((int) $campaign['status'], array(-1, 1, 2, 3, 99))) { $campaign_ids_sent[] = $campaign['campaign_id']; } } // 3 - campaign_list request & count request for queue */ if ($campaign_ids) { $model_campaign_list = WYSIJA::get('campaign_list', 'model'); $userlists = $model_campaign_list->get(array('list_id', 'campaign_id'), array('campaign_id' => $campaign_ids)); if ($campaign_ids_sent) { $model_campaign_list = WYSIJA::get("email_user_stat", "model"); $statstotal = $model_campaign_list->getResults("SELECT COUNT(A.user_id) as count,B.email_id FROM `[wysija]queue` as A\n\t\t\t\t\t JOIN `[wysija]email` as B on A.email_id=B.email_id\n\t\t\t\t\t\tWHERE B.campaign_id IN (" . implode(",", $campaign_ids_sent) . ") group by B.email_id"); $senttotalgroupedby = $model_campaign_list->getResults("SELECT COUNT(A.user_id) as count,B.campaign_id,B.email_id,B.type,B.status,A.status as statususer FROM `[wysija]" . $model_campaign_list->table_name . "` as A\n\t\t\t\t\t JOIN `[wysija]email` as B on A.email_id=B.email_id\n\t\t\t\t\t\tWHERE B.campaign_id IN (" . implode(",", $campaign_ids_sent) . ") group by A.status,B.email_id"); //,A.status $updateEmail = array(); $columnnamestatus = array(0 => "number_sent", 1 => "number_opened", 2 => "number_clicked", 3 => "number_unsub", -1 => "number_bounce"); foreach ($senttotalgroupedby as $sentbystatus) { if ($sentbystatus['statususer'] != "-2") { $updateEmail[$sentbystatus['email_id']][$columnnamestatus[$sentbystatus['statususer']]] = $sentbystatus['count']; } if (isset($senttotal[$sentbystatus['email_id']])) { $senttotal[$sentbystatus['email_id']]['count'] = (int) $senttotal[$sentbystatus['email_id']]['count'] + (int) $sentbystatus['count']; } else { unset($sentbystatus['statususer']); $senttotal[$sentbystatus['email_id']] = $sentbystatus; } } $model_email = WYSIJA::get('email', 'model'); foreach ($updateEmail as $emailid => $update) { foreach ($columnnamestatus as $v) { if (!isset($update[$v])) { $update[$v] = 0; } } $model_email->update($update, array('email_id' => $emailid)); $model_email->reset(); } $model_config = WYSIJA::get('config', 'model'); $running = false; $is_multisite = is_multisite(); if ($is_multisite && $model_config->getValue('sending_method') == 'network') { $sending_emails_each = $model_config->getValue('ms_sending_emails_each'); } else { $sending_emails_each = $model_config->getValue('sending_emails_each'); } if ($model_config->getValue('cron_manual')) { $formsHelp = WYSIJA::get('forms', 'helper'); $queue_frequency = $formsHelp->eachValuesSec[$sending_emails_each]; $queue_scheduled = WYSIJA::get_cron_schedule('queue'); $next_scheduled_queue = $queue_scheduled['next_schedule']; $running = $queue_scheduled['running']; if ($running) { $helper_toolbox = WYSIJA::get('toolbox', 'helper'); $running = time() - $running; $running = $helper_toolbox->duration_string($running, true, 4); } } else { $schedules = wp_get_schedules(); $queue_frequency = $schedules[wp_get_schedule('wysija_cron_queue')]['interval']; $next_scheduled_queue = wp_next_scheduled('wysija_cron_queue'); } $status_sent_complete = array(); if (isset($senttotal) && $senttotal) { foreach ($senttotal as $sentot) { if ($sentot) { $this->data['sent'][$sentot['email_id']]['total'] = $sentot['count']; $this->data['sent'][$sentot['email_id']]['to'] = $sentot['count']; } else { $this->data['sent'][$sentot['email_id']]['total'] = $this->data['sent'][$sentot['email_id']]['to'] = 0; } $this->data['sent'][$sentot['email_id']]['status'] = $sentot['status']; $this->data['sent'][$sentot['email_id']]['type'] = $sentot['type']; $this->data['sent'][$sentot['email_id']]['left'] = (int) $this->data['sent'][$sentot['email_id']]['total'] - (int) $this->data['sent'][$sentot['email_id']]['to']; } } foreach ($statstotal as $sentot) { if (!isset($this->data['sent'][$sentot['email_id']])) { $this->data['sent'][$sentot['email_id']]['total'] = 0; $this->data['sent'][$sentot['email_id']]['to'] = 0; } $this->data['sent'][$sentot['email_id']]['total'] = $this->data['sent'][$sentot['email_id']]['total'] + $sentot['count']; $this->data['sent'][$sentot['email_id']]['left'] = (int) $this->data['sent'][$sentot['email_id']]['total'] - (int) $this->data['sent'][$sentot['email_id']]['to']; } if ($is_multisite && $model_config->getValue('sending_method') == 'network') { $sending_emails_number = $model_config->getValue('ms_sending_emails_number'); } else { $sending_emails_number = $model_config->getValue('sending_emails_number'); } if (isset($this->data['sent'])) { foreach ($this->data['sent'] as $key => &$camp) { if ($this->data['sent'][$key]['left'] > 0) { $cronsneeded = ceil($this->data['sent'][$key]['left'] / $sending_emails_number); $this->data['sent'][$key]['remaining_time'] = $cronsneeded * $queue_frequency; $this->data['sent'][$key]['running_for'] = $running; $this->data['sent'][$key]['next_batch'] = $next_scheduled_queue - time(); $this->data['sent'][$key]['remaining_time'] = $this->data['sent'][$key]['remaining_time'] - $queue_frequency + $this->data['sent'][$key]['next_batch']; } else { if (in_array($this->data['sent'][$key]['status'], array(1, 3, 99)) && $this->data['sent'][$key]['type'] == 1) { $status_sent_complete[] = $key; } } } } // status update to sent for the one that are sent if (count($status_sent_complete) > 0) { $model_email = WYSIJA::get('email', 'model'); $model_email->noCheck = true; $model_email->reset(); $model_email->update(array('status' => 2), array('equal' => array('email_id' => $status_sent_complete))); } } } $this->data['counts'] = array_reverse($counts); // regrouping all the data in the same array foreach ($this->data['campaigns'] as &$campaign) { // default key while we don't have the data //TODO add data for stats about emails opened clicked etc $campaign["emails"] = 0; $campaign["opened"] = 0; $campaign["clicked"] = 0; if ($userlists) { foreach ($userlists as $key => $userlist) { if ($campaign["campaign_id"] == $userlist["campaign_id"] && isset($lists[$userlist["list_id"]])) { if (!isset($campaign["lists"])) { $campaign["lists"] = $lists[$userlist["list_id"]]["name"]; } else { $campaign["lists"] .= ", " . $lists[$userlist["list_id"]]["name"]; } } } } if (isset($campaign["lists"]) && !$campaign["lists"]) { unset($campaign["lists"]); } if ((isset($campaign['params']['schedule']['isscheduled']) || $campaign['type'] == 2 && isset($campaign['params']['autonl']['event']) && in_array($campaign['params']['autonl']['event'], array('new-articles'))) && $campaign['status'] != 2 && !isset($campaign["lists"]) || $campaign['type'] == 2 && isset($campaign['params']['autonl']['event']) && in_array($campaign['params']['autonl']['event'], array('subs-2-nl')) && $campaign['status'] != 2 && (!isset($campaign['params']['autonl']['subscribetolist']) || !isset($lists[$campaign['params']['autonl']['subscribetolist']]))) { $campaign['classRow'] = " listmissing "; $campaign['msgListEdit'] = '<strong>' . __('The list has been deleted.', WYSIJA) . '</strong>'; $campaign['msgSendSuspended'] = '<strong>' . __('Sending suspended.', WYSIJA) . '</strong>'; } } $this->dataAutoNl(); if (!$this->data['campaigns']) { $this->notice(__('There are no newsletters.', WYSIJA)); } }
/** * check that there is no passed schedules that need to be executed now * @return void */ public static function cron_check() { $cron_schedules = WYSIJA::get_cron_schedule('all'); if (empty($cron_schedules)) { return; } else { $processes = WYSIJA::get_cron_frequencies(); $updated_sched = false; foreach ($cron_schedules as $schedule => &$params) { $running = 0; $time_now = time(); if (isset($params['running'])) { $running = $params['running']; } //if the process has timedout we reschedule the next execution if ($running && $time_now > $running + $processes[$schedule]) { //WYSIJA::setInfo('error','modifying next schedule for '.$proc); $process_frequency = $processes[$schedule]; $next_schedule = $running + $process_frequency; // if the next schedule is already behind, we give it 30 seconds before it can trigger again if ($next_schedule < $time_now) { $next_schedule = $time_now + 30; } $params = array('next_schedule' => $next_schedule, 'prev_schedule' => $running, 'running' => false); $updated_sched = true; } } if ($updated_sched) { //WYSIJA::setInfo('error','updating scheds'); WYSIJA::update_option('wysija_schedules', $cron_schedules, 'yes'); } } $time_now = time(); $processesToRun = array(); foreach ($cron_schedules as $schedule => $scheduled_times) { if (strpos($schedule, '(bounce handling not activated)') !== false) { continue; } if (!isset($processes[$schedule])) { continue; } $process_frequency = $processes[$schedule]; if ((!$scheduled_times['running'] || (int) $scheduled_times['running'] + $process_frequency < $time_now) && $scheduled_times['next_schedule'] < $time_now) { $processesToRun[] = $schedule; } } $model_config = WYSIJA::get('config', 'model'); $page_view_trigger = (int) $model_config->getValue('cron_page_hit_trigger'); if (!empty($processesToRun) && $page_view_trigger === 1) { //call the cron url // do not call that more than once per 5 minutes attempt at reducing the CPU load for some users // http://wordpress.org/support/topic/wysija-newsletters-slowing-down-my-site-1 $last_cron_time_plus_5min = (int) get_option('wysija_last_php_cron_call') + 5 * 60; if ($last_cron_time_plus_5min < time()) { $cron_url = site_url('wp-cron.php') . '?' . WYSIJA_CRON . '&action=wysija_cron&process=' . implode(',', $processesToRun) . '&silent=1'; $cron_request = apply_filters('cron_request', array('url' => $cron_url, 'args' => array('timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters('https_local_ssl_verify', true)))); wp_remote_post($cron_url, $cron_request['args']); WYSIJA::update_option('wysija_last_php_cron_call', time()); } } }
function defaultDisplay() { $this->data['base_url'] = $this->base_url . '?' . http_build_query($_REQUEST); // saved filter $this->title = __('Newsletters', WYSIJA); $this->viewShow = $this->action = 'main'; $this->js[] = 'wysija-admin-list'; $this->jsTrans["selecmiss"] = __('Please select a newsletter.', WYSIJA); $this->jsTrans['suredelete'] = __('Delete this newsletter for ever?', WYSIJA); $this->jsTrans['processqueue'] = __('Sending batch of emails...', WYSIJA); $this->jsTrans['viewnews'] = __('View newsletter', WYSIJA); $this->jsTrans['confirmpauseedit'] = __('The newsletter will be deactivated, you will need to reactivate it once you\'re over editing it. Do you want to proceed?', WYSIJA); //get the filters if (isset($_REQUEST['search']) && $_REQUEST['search']) { $this->filters['like'] = array(); foreach ($this->searchable as $field) { $this->filters['like'][$field] = $_REQUEST['search']; } } if (isset($_REQUEST['filter-list']) && $_REQUEST['filter-list']) { $this->filters['equal'] = array('C.list_id' => $_REQUEST['filter-list']); } if (isset($_REQUEST['filter-date']) && $_REQUEST['filter-date']) { $this->filters['greater_eq'] = array('created_at' => $_REQUEST['filter-date']); $this->filters['less_eq'] = array('created_at' => strtotime('+1 month', $_REQUEST['filter-date'])); } $this->filters['is'] = array('type' => 'IS NOT NULL'); if (isset($_REQUEST['link_filter']) && $_REQUEST['link_filter']) { $linkfilters = explode('-', $_REQUEST['link_filter']); if (count($linkfilters) > 1) { $this->_addLinkFilter($linkfilters[1], $linkfilters[0]); } else { $this->_addLinkFilter($_REQUEST['link_filter']); } } $this->modelObj->noCheck = true; // 0 - counting request $queryCmmonStart = "SELECT count(distinct A.campaign_id) as campaigns FROM `[wysija]" . $this->modelObj->table_name . "` as A"; $queryCmmonStart .= ' LEFT JOIN `[wysija]email` as B on A.campaign_id=B.campaign_id'; $queryCmmonStart .= ' LEFT JOIN `[wysija]campaign_list` as C on A.campaign_id=C.campaign_id'; // all the counts query $query = 'SELECT count(email_id) as campaigns, status FROM `[wysija]email` WHERE campaign_id > 0 GROUP BY status'; $countss = $this->modelObj->query('get_res', $query, ARRAY_A); $counts = array(); $total = 0; foreach ($countss as $count) { switch ($count['status']) { case '0': $type = 'draft'; break; case '1': case '3': case '99': $type = 'sending'; break; case '2': $type = 'sent'; break; case '-1': $type = 'paused'; break; case '4': $type = 'scheduled'; break; } $total = $total + $count['campaigns']; $counts['status-' . $type] = $count['campaigns']; } $query = 'SELECT count(email_id) as campaigns, type FROM `[wysija]email` WHERE campaign_id > 0 GROUP BY type'; $countss = $this->modelObj->query('get_res', $query, ARRAY_A); foreach ($countss as $count) { switch ($count['type']) { case '1': $type = 'regular'; break; case '2': $type = 'autonl'; break; } $counts['type-' . $type] = $count['campaigns']; } $counts['all'] = $total; $this->modelObj->reset(); if ($this->filters) { $this->modelObj->setConditions($this->filters); } // 1 - campaign request $query = 'SELECT A.campaign_id, A.name as campaign_name, B.subject as name, A.description, B.params , B.type , B.number_opened,B.number_clicked,B.number_unsub,B.status,B.status,B.created_at,B.modified_at,B.sent_at,B.email_id FROM `[wysija]' . $this->modelObj->table_name . '` as A'; $query .= ' LEFT JOIN `[wysija]email` as B on A.campaign_id=B.campaign_id'; $query .= ' LEFT JOIN `[wysija]campaign_list` as C on A.campaign_id=C.campaign_id'; $queryFinal = $this->modelObj->makeWhere(); //campaign created the longest ago $query2 = 'SELECT MIN(B.created_at) as datemin FROM `[wysija]' . $this->modelObj->table_name . '` as A'; $query2 .= ' LEFT JOIN `[wysija]email` as B on A.campaign_id=B.campaign_id'; $query2 .= ' LEFT JOIN `[wysija]campaign_list` as C on A.campaign_id=C.campaign_id'; $queryFinal2 = $this->modelObj->makeWhere(); //without filter we already have the total number of campaigns if ($this->filters) { $this->modelObj->countRows = $this->modelObj->count($queryCmmonStart . $queryFinal, 'campaigns'); } else { $this->modelObj->countRows = $counts['all']; } $orderby = ' ORDER BY '; if (isset($_REQUEST['orderby'])) { if (!is_string($_REQUEST['orderby']) or preg_match('|[^a-z0-9#_.-]|i', $_REQUEST['orderby']) !== 0) { $_REQUEST['orderby'] = ''; } if (!in_array(strtoupper($_REQUEST['ordert']), array('DESC', 'ASC'))) { $_REQUEST['ordert'] = 'DESC'; } $orderby .= $_REQUEST['orderby'] . ' ' . $_REQUEST['ordert']; } else { $orderby = ' ORDER BY '; $orderby .= 'FIELD(B.status, 99,3,1,0,2), '; $orderby .= 'B.status desc, '; $orderby .= 'B.modified_at desc, '; $orderby .= 'B.sent_at desc, '; $orderby .= 'B.type desc, '; $orderby .= 'A.' . $this->modelObj->getPk() . ' desc'; } //$this->data['campaigns']=$this->modelObj->getResults($query.$queryFinal." GROUP BY A.campaign_id".$orderby.$this->modelObj->setLimit()); $this->data['campaigns'] = $this->modelObj->getResults($query . $queryFinal . ' GROUP BY B.email_id' . $orderby . $this->modelObj->setLimit()); //dbg($this->data['campaigns']); $emailids = array(); foreach ($this->data['campaigns'] as $emailcamp) { if (in_array($emailcamp['status'], array(1, 3, 99))) { $emailids[] = $emailcamp['email_id']; } } $modelQ = WYSIJA::get('queue', 'model'); $modelQ->setConditions(array("email_id" => $emailids)); $modelQ->groupBy('email_id'); $queue = $modelQ->count(); if ($queue) { $this->viewObj->queuedemails = $queue; } $this->modelObj->reset(); $this->data['datemin'] = $this->modelObj->query('get_row', $query2 . $queryFinal2); $this->modelObj->reset(); //make a loop from the first created to now and increment an array of months $now = time(); $this->data['dates'] = array(); if ((int) $this->data['datemin']['datemin'] > 1) { setlocale(LC_TIME, 'en_US'); $firstdayof = getdate($this->data['datemin']['datemin']); $formtlettres = "1 " . date("F", $this->data['datemin']['datemin']) . " " . date("Y", $this->data['datemin']['datemin']); $monthstart = strtotime($formtlettres); if ($monthstart > 0) { for ($i = $monthstart; $i < $now; $i = strtotime('+1 month', $i)) { $this->data['dates'][$i] = date_i18n('F Y', $i); //date('F Y',$i); } } } //make the data object for the listing view*/ $modelList = WYSIJA::get('list', 'model'); // 2 - list request */ $query = 'SELECT A.list_id, A.name,A.is_enabled, count( B.campaign_id ) AS users FROM `[wysija]' . $modelList->table_name . '` as A'; $query .= ' LEFT JOIN `[wysija]campaign_list` as B on A.list_id = B.list_id'; $query .= ' GROUP BY A.list_id'; $listsDB = $modelList->getResults($query); $lists = array(); foreach ($listsDB as $listobj) { $lists[$listobj["list_id"]] = $listobj; } $listsDB = null; $campaign_ids_sent = $campaign_ids = array(); foreach ($this->data['campaigns'] as &$campaign) { $campaign_ids[] = $campaign['campaign_id']; $modelEmail = WYSIJA::get('email', 'model'); $modelEmail->getParams($campaign); if (in_array((int) $campaign['status'], array(-1, 1, 2, 3, 99))) { $campaign_ids_sent[] = $campaign['campaign_id']; } } // 3 - campaign_list request & count request for queue */ if ($campaign_ids) { $modeluList = WYSIJA::get('campaign_list', 'model'); $userlists = $modeluList->get(array('list_id', 'campaign_id'), array('campaign_id' => $campaign_ids)); if ($campaign_ids_sent) { $modeluList = WYSIJA::get("email_user_stat", "model"); $statstotal = $modeluList->getResults("SELECT COUNT(A.user_id) as count,B.email_id FROM `[wysija]queue` as A\r\n JOIN `[wysija]email` as B on A.email_id=B.email_id\r\n WHERE B.campaign_id IN (" . implode(",", $campaign_ids_sent) . ") group by B.email_id"); $senttotalgroupedby = $modeluList->getResults("SELECT COUNT(A.user_id) as count,B.campaign_id,B.email_id,B.type,B.status,A.status as statususer FROM `[wysija]" . $modeluList->table_name . "` as A\r\n JOIN `[wysija]email` as B on A.email_id=B.email_id\r\n WHERE B.campaign_id IN (" . implode(",", $campaign_ids_sent) . ") group by A.status,B.email_id"); //,A.status $updateEmail = array(); $columnnamestatus = array(0 => "number_sent", 1 => "number_opened", 2 => "number_clicked", 3 => "number_unsub", -1 => "number_bounce"); foreach ($senttotalgroupedby as $sentbystatus) { if ($sentbystatus['statususer'] != "-2") { $updateEmail[$sentbystatus['email_id']][$columnnamestatus[$sentbystatus['statususer']]] = $sentbystatus['count']; } if (isset($senttotal[$sentbystatus['email_id']])) { $senttotal[$sentbystatus['email_id']]['count'] = (int) $senttotal[$sentbystatus['email_id']]['count'] + (int) $sentbystatus['count']; } else { unset($sentbystatus['statususer']); $senttotal[$sentbystatus['email_id']] = $sentbystatus; } } $modelEmail = WYSIJA::get('email', 'model'); foreach ($updateEmail as $emailid => $update) { foreach ($columnnamestatus as $v) { if (!isset($update[$v])) { $update[$v] = 0; } } $modelEmail->update($update, array('email_id' => $emailid)); $modelEmail->reset(); } /**/ $modelC = WYSIJA::get('config', 'model'); $running = false; $is_multisite = is_multisite(); //$is_multisite=true;//PROD comment that line if ($is_multisite && $modelC->getValue('sending_method') == 'network') { $sending_emails_each = $modelC->getValue('ms_sending_emails_each'); } else { $sending_emails_each = $modelC->getValue('sending_emails_each'); } if ($modelC->getValue('cron_manual')) { $formsHelp = WYSIJA::get('forms', 'helper'); $queue_frequency = $formsHelp->eachValuesSec[$sending_emails_each]; $queue_scheduled = WYSIJA::get_cron_schedule('queue'); $next_scheduled_queue = $queue_scheduled['next_schedule']; $running = $queue_scheduled['running']; if ($running) { $helperToolbox = WYSIJA::get('toolbox', 'helper'); $running = time() - $running; $running = $helperToolbox->duration($running, true, 4); } } else { $schedules = wp_get_schedules(); $queue_frequency = $schedules[wp_get_schedule('wysija_cron_queue')]['interval']; $next_scheduled_queue = wp_next_scheduled('wysija_cron_queue'); } $status_sent_complete = array(); if (isset($senttotal) && $senttotal) { foreach ($senttotal as $sentot) { if ($sentot) { $this->data['sent'][$sentot['email_id']]['total'] = $sentot['count']; $this->data['sent'][$sentot['email_id']]['to'] = $sentot['count']; } else { $this->data['sent'][$sentot['email_id']]['total'] = $this->data['sent'][$sentot['email_id']]['to'] = 0; } $this->data['sent'][$sentot['email_id']]['status'] = $sentot['status']; $this->data['sent'][$sentot['email_id']]['type'] = $sentot['type']; $this->data['sent'][$sentot['email_id']]['left'] = (int) $this->data['sent'][$sentot['email_id']]['total'] - (int) $this->data['sent'][$sentot['email_id']]['to']; } } foreach ($statstotal as $sentot) { if (!isset($this->data['sent'][$sentot['email_id']])) { $this->data['sent'][$sentot['email_id']]['total'] = 0; $this->data['sent'][$sentot['email_id']]['to'] = 0; } $this->data['sent'][$sentot['email_id']]['total'] = $this->data['sent'][$sentot['email_id']]['total'] + $sentot['count']; $this->data['sent'][$sentot['email_id']]['left'] = (int) $this->data['sent'][$sentot['email_id']]['total'] - (int) $this->data['sent'][$sentot['email_id']]['to']; } $is_multisite = is_multisite(); //$is_multisite=true;//PROD comment that line if ($is_multisite && $modelC->getValue('sending_method') == 'network') { $sending_emails_number = $modelC->getValue('ms_sending_emails_number'); } else { $sending_emails_number = $modelC->getValue('sending_emails_number'); } if (isset($this->data['sent'])) { foreach ($this->data['sent'] as $key => $camp) { if ($this->data['sent'][$key]['left'] > 0) { $cronsneeded = ceil($this->data['sent'][$key]['left'] / $sending_emails_number); $this->data['sent'][$key]['remaining_time'] = $cronsneeded * $queue_frequency; $this->data['sent'][$key]['running_for'] = $running; $this->data['sent'][$key]['next_batch'] = $next_scheduled_queue - time(); $this->data['sent'][$key]['remaining_time'] = $this->data['sent'][$key]['remaining_time'] - $queue_frequency + $this->data['sent'][$key]['next_batch']; } else { if (in_array($this->data['sent'][$key]['status'], array(1, 3, 99)) && $this->data['sent'][$key]['type'] == 1) { $status_sent_complete[] = $key; } } } } /* status update to sent for the one that are sent*/ if (count($status_sent_complete) > 0) { $modelEmail = WYSIJA::get('email', 'model'); $modelEmail->noCheck = true; $modelEmail->reset(); $modelEmail->update(array('status' => 2), array('equal' => array('email_id' => $status_sent_complete))); } } } $this->data['lists'] = $lists; $this->data['counts'] = array_reverse($counts); /* regrouping all the data in the same array */ foreach ($this->data['campaigns'] as $keysus => &$campaign) { /* default key while we don't have the data*/ //TODO add data for stats about emails opened clicked etc $campaign["emails"] = 0; $campaign["opened"] = 0; $campaign["clicked"] = 0; if ($userlists) { foreach ($userlists as $key => $userlist) { if ($campaign["campaign_id"] == $userlist["campaign_id"] && isset($lists[$userlist["list_id"]])) { if (!isset($campaign["lists"])) { $campaign["lists"] = $lists[$userlist["list_id"]]["name"]; } else { $campaign["lists"] .= ", " . $lists[$userlist["list_id"]]["name"]; } } } } if (isset($campaign["lists"]) && !$campaign["lists"]) { unset($campaign["lists"]); } if ((isset($campaign['params']['schedule']['isscheduled']) || $campaign['type'] == 2 && isset($campaign['params']['autonl']['event']) && in_array($campaign['params']['autonl']['event'], array('new-articles'))) && $campaign['status'] != 2 && !isset($campaign["lists"]) || $campaign['type'] == 2 && isset($campaign['params']['autonl']['event']) && in_array($campaign['params']['autonl']['event'], array('subs-2-nl')) && $campaign['status'] != 2 && (!isset($campaign['params']['autonl']['subscribetolist']) || !isset($lists[$campaign['params']['autonl']['subscribetolist']]))) { $campaign['classRow'] = " listmissing "; $campaign['msgListEdit'] = '<strong>' . __('The list has been deleted.', WYSIJA) . '</strong>'; $campaign['msgSendSuspended'] = '<strong>' . __('Sending suspended.', WYSIJA) . '</strong>'; } } $this->dataAutoNl(); if (!$this->data['campaigns']) { $this->notice(__('There are no newsletters.', WYSIJA)); } }
function run_scheduled_task($process = 'queue') { $scheduled_times = WYSIJA::get_cron_schedule($process); if (isset($scheduled_times['running']) && $scheduled_times['running'] && $scheduled_times['running'] + 900 > time()) { if ($this->report) { echo 'already running : ' . $process . '<br/>'; } return; } WYSIJA::set_cron_schedule($process, 0, time()); switch ($process) { case 'queue': WYSIJA::croned_queue($process); if (defined('WYSIJANLP')) { $hPremium =& WYSIJA::get('premium', 'helper', false, WYSIJANLP); $hPremium->croned_queue_process(); } break; case 'bounce': if (defined('WYSIJANLP')) { $hPremium =& WYSIJA::get('premium', 'helper', false, WYSIJANLP); $hPremium->croned_bounce(); } break; case 'daily': WYSIJA::croned_daily(); break; case 'weekly': if (defined('WYSIJANLP')) { $hPremium =& WYSIJA::get('premium', 'helper', false, WYSIJANLP); $hPremium->croned_weekly(); } WYSIJA::croned_weekly(); break; case 'monthly': WYSIJA::croned_monthly(); break; } WYSIJA::set_cron_schedule($process); if ($this->report) { echo 'processed : ' . $process . '<br/>'; } }
/** * check that there is no passed schedules that need to be executed now * @return void */ public static function cron_check() { $cron_schedules = WYSIJA::get_cron_schedule('all'); if (empty($cron_schedules)) { return; } else { $processes = WYSIJA::get_cron_frequencies(); $updated_sched = false; foreach ($cron_schedules as $schedule => &$params) { $running = 0; $time_now = time(); if (isset($params['running'])) { $running = $params['running']; } //if the process has timedout we reschedule the next execution if ($running && $time_now > $running + $processes[$schedule]) { //WYSIJA::setInfo('error','modifying next schedule for '.$proc); $process_frequency = $processes[$schedule]; $next_schedule = $running + $process_frequency; // if the next schedule is already behind, we give it 30 seconds before it can trigger again if ($next_schedule < $time_now) { $next_schedule = $time_now + 30; } $params = array('next_schedule' => $next_schedule, 'prev_schedule' => $running, 'running' => false); $updated_sched = true; } } if ($updated_sched) { //WYSIJA::setInfo('error','updating scheds'); WYSIJA::update_option('wysija_schedules', $cron_schedules, 'yes'); } } $time_now = time(); $processesToRun = array(); foreach ($cron_schedules as $schedule => $scheduled_times) { $process_frequency = $processes[$schedule]; if ((!$scheduled_times['running'] || (int) $scheduled_times['running'] + $process_frequency < $time_now) && $scheduled_times['next_schedule'] < $time_now) { $processesToRun[] = $schedule; } } $model_config = WYSIJA::get('config', 'model'); $page_view_trigger = (int) $model_config->getValue('cron_page_hit_trigger'); if (!empty($processesToRun) && $page_view_trigger === 1) { //call the cron url $cron_url = site_url('wp-cron.php') . '?' . WYSIJA_CRON . '&action=wysija_cron&process=' . implode(',', $processesToRun) . '&silent=1'; $cron_request = apply_filters('cron_request', array('url' => $cron_url, 'args' => array('timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters('https_local_ssl_verify', true)))); wp_remote_post($cron_url, $cron_request['args']); } }