index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /* @var $wpdb wpdb */
  3. if (!defined('ABSPATH'))
  4. exit;
  5. require_once NEWSLETTER_INCLUDES_DIR . '/controls.php';
  6. $module = NewsletterStatistics::instance();
  7. $controls = new NewsletterControls();
  8. wp_enqueue_script('tnp-chart');
  9. if ($controls->is_action('country')) {
  10. $module->country();
  11. $controls->messages = $module->country_result;
  12. }
  13. if ($controls->is_action('import')) {
  14. $wpdb->query("insert ignore into " . $wpdb->prefix . "newsletter_sent (user_id, email_id, time) select user_id, email_id, UNIX_TIMESTAMP(created) from " . NEWSLETTER_STATS_TABLE);
  15. $controls->messages = 'Done!';
  16. }
  17. $types = $wpdb->get_results("select distinct type from " . NEWSLETTER_EMAILS_TABLE);
  18. $type_options = array();
  19. foreach ($types as $type) {
  20. if ($type->type == 'followup')
  21. continue;
  22. if ($type->type == 'message') {
  23. $type_options[$type->type] = 'Standard Newsletter';
  24. } else if ($type->type == 'feed') {
  25. $type_options[$type->type] = 'Feed by Mail';
  26. } else if (strpos($type->type, 'automated') === 0) {
  27. list($a, $id) = explode('_', $type->type);
  28. $type_options[$type->type] = 'Automated Channel ' . $id;
  29. } else {
  30. $type_options[$type->type] = $type->type;
  31. }
  32. }
  33. if (empty($controls->data['type'])) {
  34. $emails = $wpdb->get_results("select send_on, id, subject, total, status, type, track, sent, subject from " . NEWSLETTER_EMAILS_TABLE . " where status='sent' order by send_on desc limit 20");
  35. } else {
  36. $emails = $wpdb->get_results($wpdb->prepare("select send_on, id, subject, total, type from " . NEWSLETTER_EMAILS_TABLE . " where status='sent' and type=%s order by send_on desc limit 20", $controls->data['type']));
  37. }
  38. $overview_labels = array();
  39. $overview_titles = array();
  40. $overview_open_rate = array();
  41. $overview_click_rate = array();
  42. $total_sent = 0;
  43. $open_count_total = 0;
  44. $click_count_total = 0;
  45. foreach ($emails as $email) {
  46. $entry = array();
  47. // Skip newsletters which has no sent records
  48. $total = $module->get_total_count($email);
  49. if (empty($total)) {
  50. continue;
  51. }
  52. $total_sent += $total;
  53. //$entry[0] = $email->subject . ' [' . date('Y-m-d', $email->send_on) . ', ' . $email->type . ']';
  54. $entry[0] = date('Y-m-d', $email->send_on);
  55. $open_count = $module->get_open_count($email);
  56. $open_count_total += $open_count;
  57. $entry[1] = $open_count / $total * 100;
  58. $entry[1] = round($entry[1], 2);
  59. $entry[2] = $email->subject; // . ' (' . percent($open_count, $email->sent) . ')';
  60. $click_count = $module->get_click_count($email);
  61. $click_count_total += $click_count;
  62. $entry[3] = $click_count / $total * 100;
  63. $entry[3] = round($entry[3], 2);
  64. $overview_labels[] = strftime('%a, %e %b', $email->send_on);
  65. $overview_open_rate[] = $entry[1];
  66. $overview_click_rate[] = $entry[3];
  67. $overview_titles[] = $entry[2];
  68. }
  69. $overview_labels = array_reverse($overview_labels);
  70. $overview_open_rate = array_reverse($overview_open_rate);
  71. $overview_click_rate = array_reverse($overview_click_rate);
  72. ?>
  73. <div class="wrap" id="tnp-wrap">
  74. <?php include NEWSLETTER_DIR . '/tnp-header.php' ?>
  75. <div id="tnp-heading">
  76. <h2><?php _e('Global Newsletter Statistics', 'newsletter') ?></h2>
  77. </div>
  78. <div id="tnp-body" class="tnp-statistics">
  79. <form method="post" action="">
  80. <?php $controls->init(); ?>
  81. <?php if (empty($emails)) { ?>
  82. <img src="https://cdn.thenewsletterplugin.com/tnp-reports-dummy-image.png" style="max-width: 100%">
  83. <?php } else { ?>
  84. <div class="row">
  85. <div class="tnp-statistics-info-box">
  86. <p class="tnp-legend">Select Newsletter category:</p>
  87. <?php $controls->select('type', $type_options, 'All') ?>
  88. <?php $controls->button('update', __('Update Charts', 'newsletter')) ?>
  89. </div>
  90. </div>
  91. <br>
  92. <div class="row">
  93. <div class="col-md-6">
  94. <div class="tnp-widget">
  95. <h3>Overview (Last 20 Newsletters)</h3>
  96. <div class="inside">
  97. <p class="tnp-events-legend">Subscribers interactions distribution over time,<br>starting from the sending day.</p>
  98. <div id="tnp-events-chart">
  99. <canvas id="tnp-events-chart-canvas"></canvas>
  100. </div>
  101. <script type="text/javascript">
  102. var events_data = {
  103. labels: <?php echo json_encode($overview_labels) ?>,
  104. datasets: [
  105. {
  106. label: "Open",
  107. fill: false,
  108. strokeColor: "#27AE60",
  109. backgroundColor: "#27AE60",
  110. borderColor: "#27AE60",
  111. pointBorderColor: "#27AE60",
  112. pointBackgroundColor: "#27AE60",
  113. data: <?php echo json_encode($overview_open_rate) ?>
  114. },
  115. {
  116. label: "Click",
  117. fill: false,
  118. strokeColor: "#C0392B",
  119. backgroundColor: "#C0392B",
  120. borderColor: "#C0392B",
  121. pointBorderColor: "#C0392B",
  122. pointBackgroundColor: "#C0392B",
  123. data: <?php echo json_encode($overview_click_rate) ?>,
  124. yAxisID: "y-axis-2"
  125. }
  126. ]
  127. };
  128. var titles = <?php echo json_encode(array_reverse($overview_titles)) ?>;
  129. jQuery(document).ready(function ($) {
  130. ctxe = $('#tnp-events-chart-canvas').get(0).getContext("2d");
  131. eventsLineChart = new Chart(ctxe, {type: 'line', data: events_data,
  132. options: {
  133. scales: {
  134. xAxes: [{type: "category", "id": "x-axis-1", gridLines: {display: false}, ticks: {fontFamily: "Source Sans Pro"}}],
  135. yAxes: [
  136. {type: "linear", "id": "y-axis-1", gridLines: {display: false}, ticks: {fontColor: "#27AE60", fontFamily: "Source Sans Pro"}},
  137. {type: "linear", "id": "y-axis-2", position: "right", gridLines: {display: false}, ticks: {fontColor: "#C0392B", fontFamily: "Source Sans Pro"}}
  138. ]
  139. },
  140. tooltips: {
  141. callbacks: {
  142. afterTitle: function (data) {
  143. return titles[data[0].index];
  144. },
  145. label: function (tooltipItem, data) {
  146. return data.datasets[0].label + ": " + data.datasets[0].data[tooltipItem.index] + "% " +
  147. data.datasets[1].label + ": " + data.datasets[1].data[tooltipItem.index] + "%";
  148. }
  149. }
  150. }
  151. }
  152. });
  153. });
  154. </script>
  155. <div class="row">
  156. <div class="col-md-4">
  157. <div class="tnp-data">
  158. <div class="tnp-data-title"><?php _e('Total Sent Messages', 'newsletter') ?></div>
  159. <div class="tnp-data-value"><?php echo $total_sent; ?></div>
  160. </div>
  161. </div>
  162. <div class="col-md-4">
  163. <div class="tnp-data">
  164. <div class="tnp-data-title"><?php _e('Opened Newsletters', 'newsletter') ?></div>
  165. <div class="tnp-data-value"><?php echo $open_count_total; ?> (<?php echo $module->percent($open_count_total, $total_sent); ?>)</div>
  166. </div>
  167. </div>
  168. <div class="col-md-4">
  169. <div class="tnp-data">
  170. <div class="tnp-data-title"><?php _e('Clicked Newsletters', 'newsletter') ?></div>
  171. <div class="tnp-data-value"><?php echo $click_count_total; ?> (<?php echo $module->percent($click_count_total, $total_sent); ?>)</div>
  172. </div>
  173. </div>
  174. </div>
  175. </div>
  176. </div>
  177. </div>
  178. <!-- WORLD MAP -->
  179. <div class="col-md-6">
  180. <div class="tnp-widget">
  181. <h3><?php _e('Countries', 'newsletter') ?></h3>
  182. <div class="inside">
  183. <?php
  184. if (!has_action('newsletter_statistics_index_map')) {
  185. ?><a href="https://www.thenewsletterplugin.com/premium?utm_source=plugin&utm_medium=link&utm_content=worldmap&utm_campaign=newsletter-reports" target="_blank">
  186. <img src="<?php echo plugins_url('newsletter') ?>/statistics/images/map.gif" style="width: 100%">
  187. </a><?php
  188. } else {
  189. do_action('newsletter_statistics_index_map');
  190. }
  191. ?>
  192. </div>
  193. </div>
  194. </div>
  195. </div>
  196. <div class="row">
  197. <!-- LAST NEWSLETTERS -->
  198. <div class="col-md-12">
  199. <div class="tnp-widget">
  200. <h3><?php _e('Last newsletters', 'newsletter') ?> <a href="admin.php?page=newsletter_statistics_newsletters"><?php _e('Details', 'newsletter') ?></a></h3>
  201. <div class="inside">
  202. <?php
  203. $emails = $wpdb->get_results($wpdb->prepare("select send_on, id, subject, total, status, type, track, sent, subject from " . NEWSLETTER_EMAILS_TABLE . " where status in ('sent', 'sending') and send_on<%d order by send_on desc limit 5", time()));
  204. ?>
  205. <table class="widefat">
  206. <thead>
  207. <tr>
  208. <th>Id</th>
  209. <th><?php _e('Subject', 'newsletter') ?></th>
  210. <th>Type</th>
  211. <th><?php _e('Status', 'newsletter') ?></th>
  212. <th>&nbsp;</th>
  213. </tr>
  214. </thead>
  215. <tbody>
  216. <?php foreach ($emails as &$email) { ?>
  217. <tr>
  218. <td><?php echo $email->id; ?></td>
  219. <td><?php echo htmlspecialchars($email->subject); ?></td>
  220. <td><?php echo $module->get_email_type_label($email); ?></td>
  221. <td><?php echo $module->get_email_status_label($email); ?></td>
  222. <td>
  223. <a href="<?php echo NewsletterStatistics::instance()->get_statistics_url($email->id); ?>" class="button-primary">Statistics</a>
  224. </td>
  225. </tr>
  226. <?php } ?>
  227. </tbody>
  228. </table>
  229. </div>
  230. </div>
  231. </div>
  232. </div>
  233. <div class="row">
  234. <div class="col-md-6">
  235. <div class="tnp-statistics-info-box">
  236. <p class="tnp-legend">Check Statistics global<br>configurations.</p>
  237. <a class="button-primary" href="admin.php?page=newsletter_statistics_settings"><?php _e('Settings') ?></a>
  238. </div>
  239. </div>
  240. <div class="col-md-6">
  241. </div>
  242. </div>
  243. <?php } ?>
  244. </form>
  245. </div>
  246. <?php include NEWSLETTER_DIR . '/tnp-footer.php' ?>
  247. </div>