コード例 #1
0
function wpr_processqueue()
{
    global $wpdb;
    $last_cron_status = get_option("_wpr_queue_delivery_status");
    /*
    When the cron is running the _wpr_queue_delivery_status
    is set to the timestamp at which the cron processing was started.
    
    Before shutting down the _wpr_queue_delivery_status is
    set to 'stopped'.
    
    This cron will run only if the _wpr_queue_delivery_status option
    is set to "stopped" or is empty.
    */
    $timeOfStart = time();
    $timeMaximumExecutionTimeAgo = $timeOfStart - WPR_MAX_NEWSLETTER_PROCESS_EXECUTION_TIME;
    if (!empty($last_cron_status) && $last_cron_status != "stopped") {
        $last_cron_status = intval($last_cron_status);
        if ($last_cron_status != 0 && $last_cron_status > $timeMaximumExecutionTimeAgo) {
            return;
        }
    }
    update_option("_wpr_queue_delivery_status", $timeOfStart);
    $timeOfStart = time();
    $timeMaximumExecutionTimeAgo = $timeOfStart - WPR_MAX_AUTORESPONDER_PROCESS_EXECUTION_TIME;
    if (!empty($last_cron_status) && $last_cron_status != "stopped") {
        $last_cron_status = intval($last_cron_status);
        if ($last_cron_status != 0 && $last_cron_status > $timeMaximumExecutionTimeAgo) {
            return;
        }
    }
    $hourlyLimit = getNumberOfEmailsToDeliver();
    $hourlyLimit = (int) $hourlyLimit;
    $limitClause = $hourlyLimit == 0 ? "" : " limit " . $hourlyLimit;
    $query = $wpdb->escape("SELECT * FROM " . $wpdb->prefix . "wpr_queue where sent=0 {$limitClause} ");
    $results = $wpdb->get_results($query);
    foreach ($results as $mail) {
        $mail = (array) $mail;
        try {
            dispatchEmail($mail);
        } catch (Swift_RfcComplianceException $exception) {
            //disable all subscribers with that email.
            $email = $mail['to'];
            $query = "UPDATE " . $wpdb->prefix . "wpr_subscribers set active=3, confirmed=0 where email='{$email}'";
            $wpdb->query($query);
        }
        $query = "UPDATE " . $wpdb->prefix . "wpr_queue set sent=1 where id=" . $mail['id'];
        $wpdb->query($query);
        $current_cron_status = get_option("_wpr_queue_delivery_status");
        if ($current_cron_status != $timeOfStart) {
            return;
        }
    }
    update_option("_wpr_queue_delivery_status", "stopped");
}
コード例 #2
0
function _wpr_process_queue()
{
    global $wpdb;
    /*ENSURING THERE IS ONLY ONE INSTANCE THAT RUNS FOR A MAXIMUM OF ONE HOUR START HERE*/
    set_time_limit(3600);
    $last_cron_status = get_option("_wpr_queue_delivery_status");
    /*
    When the cron is running the _wpr_queue_delivery_status
    is set to the timestamp at which the cron processing was started.
    
    Before shutting down the _wpr_queue_delivery_status is
    set to 'stopped'.
    
    This cron will run only if the _wpr_queue_delivery_status option
    is set to "stopped" or is empty.
    */
    $timeOfStart = time();
    $timeMaximumExecutionTimeAgo = $timeOfStart - WPR_MAX_QUEUE_DELIVERY_EXECUTION_TIME;
    if (!empty($last_cron_status) && $last_cron_status != "stopped") {
        $last_cron_status = intval($last_cron_status);
        if ($last_cron_status != 0 && $last_cron_status > $timeMaximumExecutionTimeAgo) {
            return;
        }
    }
    update_option("_wpr_queue_delivery_status", $timeOfStart);
    $numberOfEmailsToDeliver = getNumberOfEmailsToDeliver();
    $queueBatchSize = WPR_MAX_QUEUE_EMAILS_SENT_PER_ITERATION;
    $numberOfIterations = ceil($numberOfEmailsToDeliver / $queueBatchSize);
    //queue batch size of number of emails to deliver whichever is lesser should be fetched from db.
    if ($numberOfEmailsToDeliver < $queueBatchSize) {
        $queueBatchSize = $numberOfEmailsToDeliver;
    }
    for ($iter = 0; $iter < $numberOfIterations; $iter++) {
        $limitClause = sprintf(" LIMIT %d", $queueBatchSize);
        $query = sprintf("SELECT q.* FROM `%swpr_queue` q, %swpr_subscribers s WHERE s.id=q.sid AND q.`sent`=0 AND s.confirmed=1 AND s.active=1 %s ", $wpdb->prefix, $wpdb->prefix, $limitClause);
        $results = $wpdb->get_results($query);
        foreach ($results as $mail) {
            $mail = (array) $mail;
            //
            $checkWhetherUnsent = sprintf("SELECT COUNT(*) num FROM {$wpdb->prefix}wpr_queue WHERE `id`=%d AND `sent`=0", $mail['id']);
            $whetherUnsentRes = $wpdb->get_results($checkWhetherUnsent);
            $number = $whetherUnsentRes[0]->num;
            if ($number == 0) {
                continue;
            }
            $setEmailAsSentQuery = sprintf("UPDATE `%swpr_queue` SET `sent`=1 WHERE `id`=%d", $wpdb->prefix, $mail['id']);
            $wpdb->query($setEmailAsSentQuery);
            try {
                dispatchEmail($mail);
            } catch (Swift_RfcComplianceException $exception) {
                //disable all subscribers with that email.
                //TODO: Move this to a separate function.
                $email = $mail['to'];
                $setTheEmailAsFailedQuery = $wpdb->prepare("UPDATE `{$wpdb->prefix}wpr_subscribers` SET `active`=3, `confirmed`=0 WHERE `email`=%s", $email);
                $wpdb->query($setTheEmailAsFailedQuery);
                //set aall other emails in queue which have this email to 3.
                //TODO: #DevDoc sent=3 means invalid email.
                $markAllEmailsOfThisEmailUnprocessable = $wpdb->prepare("UPDATE `{$wpdb->prefix}wpr_queue` SET `sent`=3 WHERE `sent`=0 AND `email`=%s", $email);
                $wpdb->query($markAllEmailsOfThisEmailUnprocessable);
            }
            $timeThisInstant = time();
            $timeSinceStart = $timeThisInstant - $timeOfStart;
            if ($timeSinceStart > WPR_MAX_QUEUE_DELIVERY_EXECUTION_TIME) {
                update_option("_wpr_queue_delivery_status", "stopped");
                return;
            }
        }
    }
    update_option("_wpr_queue_delivery_status", "stopped");
}