Skip to content

Instantly share code, notes, and snippets.

@webdevid
Created August 12, 2014 19:37
Show Gist options
  • Save webdevid/c431e4608e9fbe116bb1 to your computer and use it in GitHub Desktop.
Save webdevid/c431e4608e9fbe116bb1 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Roman Ožana <[email protected]>
*/
class CountEmail {
/**
* @return MailFrom
*/
public static function init() {
return new self;
}
public function __construct() {
add_action('admin_menu', [$this, 'adminMenu']);
add_filter('wp_mail', [$this, 'wp_mail']);
}
public function adminMenu() {
add_submenu_page(
'index.php', 'Email stats', 'Email stats', 'manage_categories', __CLASS__, [$this, 'countEmail']
);
}
/**
* Page with results
*/
public function countEmail() {
if (!current_user_can('manage_categories')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
$counter = self::getCounter();
$days = [['Day', 'Send emails']];
ksort($counter->day);
foreach (array_slice($counter->day, -14, null, true) as $day => $count) {
$date = \DateTime::createFromFormat('z', $day)->format('j.n.');
$days[] = [ $date, $count];
}
$months = [['Month', 'Send emails']];
ksort($counter->month);
foreach ($counter->month as $month => $count) {
$date = \DateTime::createFromFormat('n', $month)->format('U');
$months[] = [date_i18n('F', $date), $count];
}
require __DIR__ . '/CountEmail.phtml';
}
/**
* Return or update counter
*
* @param bool $update
* @param null $time
* @return mixed|void
*/
public static function getCounter($update = false, $time = null) {
list($day, $week, $month, $year) = array_map('intval', explode('|', date('z|W|m|Y', $time ? : time())));
$option = 'count_email_' . $year;
$counter = get_option($option, (object)['day' => [], 'week' => [], 'month' => [], 'total' => 0]);
if ($update) {
$counter->day[$day] = isset($counter->day[$day]) ? $counter->day[$day] + 1 : 1;
$counter->week[$week] = isset($counter->week[$week]) ? $counter->week[$week] + 1 : 1;
$counter->month[$month] = isset($counter->month[$month]) ? $counter->month[$month] + 1 : 1;
$counter->total++;
update_option($option, $counter); // save counter
}
return $counter;
}
/**
* Update counter on email send
*
* @param $email
* @return mixed
*/
public static function wp_mail($email) {
self::getCounter(true);
return $email;
}
}
<style type="text/css">
.graph {
width: 100%;
max-width: 1024px;
height: 500px;
margin: 10px 0;
border: 1px solid #ddd;
background: #fff
}
</style>
<div class="wrap">
<h1>Send emails</h1>
<p>Total send <strong><?= $counter->total ?></strong> emails this year</p>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages: ["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
<? if (count($days) > 1) { ?>
var days = google.visualization.arrayToDataTable(<?= json_encode($days)?>);
var chart = new google.visualization.ColumnChart(document.getElementById('day_chart_div'));
chart.draw(days, {});
<? } ?>
<? if (count($months) > 1) { ?>
var months = google.visualization.arrayToDataTable(<?= json_encode($months)?>);
var chart2 = new google.visualization.ColumnChart(document.getElementById('moth_chart_div'));
chart2.draw(months, {});
<? } ?>
}
</script>
<h2>Send email per month</h2>
<div id="moth_chart_div" class="graph"></div>
<h2>Send emails in last 14 days</h2>
<div id="day_chart_div" class="graph"></div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment