20) {
for ($i = 1; $i < count($wp_cron_calls); $i++) {
$diff = $wp_cron_calls[$i] - $wp_cron_calls[$i - 1];
$total += $diff;
if ($wp_cron_calls_min == 0 || $wp_cron_calls_min > $diff) {
$wp_cron_calls_min = $diff;
}
if ($wp_cron_calls_max < $diff) {
$wp_cron_calls_max = $diff;
}
}
$wp_cron_calls_avg = (int) ($total / (count($wp_cron_calls) - 1));
}
if ($controls->is_action('delete_logs')) {
$files = glob(WP_CONTENT_DIR . '/logs/newsletter/*.txt');
foreach ($files as $file) {
if (is_file($file))
unlink($file);
}
$secret = NewsletterModule::get_token(8);
update_option('newsletter_logger_secret', $secret);
$controls->messages = 'Logs deleted';
}
if ($controls->is_action('reschedule')) {
wp_clear_scheduled_hook('newsletter');
wp_schedule_event(time() + 30, 'newsletter', 'newsletter');
$controls->add_message_done();
}
if ($controls->is_action('trigger')) {
Newsletter::instance()->hook_newsletter();
$controls->messages = 'Triggered';
}
if ($controls->is_action('conversion')) {
$this->logger->info('Maybe convert to utf8mb4');
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
if (function_exists('maybe_convert_table_to_utf8mb4')) {
$r = maybe_convert_table_to_utf8mb4(NEWSLETTER_EMAILS_TABLE);
if (!$r) {
$controls->errors .= 'It was not possible to run the conversion for the table ' . NEWSLETTER_EMAILS_TABLE . ' - ';
$controls->errors .= $wpdb->last_error . '
';
}
$r = maybe_convert_table_to_utf8mb4(NEWSLETTER_USERS_TABLE);
if (!$r) {
$controls->errors .= 'It was not possible to run the conversion for the table ' . NEWSLETTER_EMAILS_TABLE . ' - ';
$controls->errors .= $wpdb->last_error . '
';
}
$controls->messages = 'Done.';
} else {
$controls->errors = 'Table conversion function not available';
}
Newsletter::instance()->hook_newsletter();
$controls->messages = 'Triggered';
}
if ($controls->is_action('test')) {
if (!NewsletterModule::is_email($controls->data['test_email'])) {
$controls->errors = 'The test email address is not set or is not correct.';
}
if (empty($controls->errors)) {
$options = $controls->data;
if ($controls->data['test_email'] == $this->options['sender_email']) {
$controls->messages .= 'Warning: you are using as test email the same address configured as sender in main configuration. Test can fail because of that.
';
}
$message = NewsletterMailerAddon::get_test_message($controls->data['test_email'], 'Newsletter test email at ' . date(DATE_ISO8601));
$r = $this->deliver($message);
if (!is_wp_error($r)) {
$options['mail'] = 1;
$controls->messages .= 'SUCCESS
';
$controls->messages .= 'Anyway if the message does not appear the mailbox (check even the spam folder) you can ';
$controls->messages .= 'read more here.';
} else {
$options['mail'] = 0;
$options['mail_error'] = $r->get_error_message();
$controls->errors .= 'FAILED (' . $r->get_error_message() . ')
';
if (!empty($this->options['return_path'])) {
$controls->errors .= '- Try to remove the return path on main settings.
';
}
$controls->errors .= '' . __('Read more', 'newsletter') . '.';
$parts = explode('@', $this->options['sender_email']);
$sitename = strtolower($_SERVER['SERVER_NAME']);
if (substr($sitename, 0, 4) == 'www.') {
$sitename = substr($sitename, 4);
}
if (strtolower($sitename) != strtolower($parts[1])) {
$controls->errors .= '- Try to set on main setting a sender address with the same domain of your blog: ' . $sitename . ' (you are using ' . $this->options['sender_email'] . ')
';
}
}
$this->save_options($options, 'status');
}
}
if ($controls->is_action( 'stats_email_column_upgrade') ) {
$this->query( "alter table " . NEWSLETTER_STATS_TABLE . " drop index email_id" );
$this->query( "alter table " . NEWSLETTER_STATS_TABLE . " drop index user_id" );
$this->query( "alter table `" . NEWSLETTER_STATS_TABLE . "` modify column `email_id` int(11) not null default 0" );
$this->query( "create index email_id on " . NEWSLETTER_STATS_TABLE . " (email_id)" );
$this->query( "create index user_id on " . NEWSLETTER_STATS_TABLE . " (user_id)" );
$controls->add_message_done();
update_option('newsletter_stats_email_column_upgraded', true);
}
$options = $this->get_options('status');
// Compute the number of newsletters ongoing and other stats
$emails = $wpdb->get_results("select * from " . NEWSLETTER_EMAILS_TABLE . " where status='sending' and send_on<" . time() . " order by id asc");
$total = 0;
$queued = 0;
foreach ($emails as $email) {
$total += $email->total;
$queued += $email->total - $email->sent;
}
$speed = Newsletter::$instance->options['scheduler_max'];
function tnp_status_print_flag($condition) {
switch ($condition) {
case 0: echo ' KO';
break;
case 1: echo 'OK';
break;
case 2: echo 'MAYBE';
break;
}
}
?>