models
4 years ago
queries
4 years ago
utils
4 years ago
chart.php
4 years ago
db.php
4 years ago
delete_data_ajax.php
4 years ago
filters.php
4 years ago
filters_ajax.php
4 years ago
rest_api.php
4 years ago
settings.php
4 years ago
settings_ajax.php
4 years ago
super_secret_content_generator.php
4 years ago
table.php
4 years ago
table_referrers.php
4 years ago
table_views.php
4 years ago
track_resource_changes.php
4 years ago
chart.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | class Chart |
| 6 | { |
| 7 | public function output_chart(Views $views, string $title = null) |
| 8 | { |
| 9 | if (is_null($title)) { |
| 10 | $title = esc_html__('Last 30 Days', 'iawp'); |
| 11 | } |
| 12 | $labels = array_map(function ($data_point) { |
| 13 | return $data_point[0]->format('M j - l'); |
| 14 | }, $views->daily_views()); |
| 15 | |
| 16 | $views_data = array_map(function ($data_point) { |
| 17 | return $data_point[1]; |
| 18 | }, $views->daily_views()); |
| 19 | |
| 20 | $visitors_data = array_map(function ($data_point) { |
| 21 | return $data_point[1]; |
| 22 | }, $views->daily_visitors()); |
| 23 | |
| 24 | return $this->chart_html($labels, $views_data, $visitors_data, $title); |
| 25 | } |
| 26 | |
| 27 | public function chart_html(array $labels, array $views, array $visitors, $title) |
| 28 | { |
| 29 | // Todo - Bring tick color into the stimulus controller below |
| 30 | $tick_color = get_option('iawp_dark_mode') ? '#ffffff' : '#555555'; |
| 31 | ob_start(); ?> |
| 32 | <div class="chart-container"> |
| 33 | <div class="chart-inner"> |
| 34 | <div class="legend-container"> |
| 35 | <h2 class="legend-title"><?php echo $title ?></h2> |
| 36 | <div id="legend-container"></div> |
| 37 | </div> |
| 38 | <canvas id="myChart" |
| 39 | width="800" |
| 40 | height="200" |
| 41 | data-controller="chart" |
| 42 | data-chart-mode-value='<?php echo get_option('iawp_dark_mode') ? 'dark' : 'light' ?>' |
| 43 | data-chart-labels-value='<?php echo Security::json_encode($labels) ?>' |
| 44 | data-chart-views-value='<?php echo Security::json_encode($views) ?>' |
| 45 | data-chart-visitors-value='<?php echo Security::json_encode($visitors) ?>' |
| 46 | > |
| 47 | </canvas> |
| 48 | </div> |
| 49 | </div><?php |
| 50 | |
| 51 | $html = ob_get_contents(); |
| 52 | ob_end_clean(); |
| 53 | |
| 54 | return $html; |
| 55 | } |
| 56 | |
| 57 | public function quick_stats(Views $views, Views $unfiltered_views = null) |
| 58 | { |
| 59 | $filtered = !is_null($unfiltered_views) ? true : false; |
| 60 | |
| 61 | $stats = [[ |
| 62 | 'title' => __('Views', 'iawp'), |
| 63 | 'class' => 'views', |
| 64 | 'count' => $views->views(), |
| 65 | 'growth' => $views->views_percentage_growth(), |
| 66 | ], [ |
| 67 | 'title' => __('Visitors', 'iawp'), |
| 68 | 'class' => 'visitors', |
| 69 | 'count' => $views->visitors(), |
| 70 | 'growth' => $views->visitors_percentage_growth(), |
| 71 | ]]; |
| 72 | |
| 73 | if ($filtered) { |
| 74 | $stats[] = [ |
| 75 | 'title' => __('Total Views', 'iawp'), |
| 76 | 'class' => 'views unfiltered', |
| 77 | 'count' => $unfiltered_views->views(), |
| 78 | 'growth' => $unfiltered_views->views_percentage_growth(), |
| 79 | ]; |
| 80 | $stats[] = [ |
| 81 | 'title' => __('Total Visitors', 'iawp'), |
| 82 | 'class' => 'visitors unfiltered', |
| 83 | 'count' => $unfiltered_views->visitors(), |
| 84 | 'growth' => $unfiltered_views->visitors_percentage_growth(), |
| 85 | ]; |
| 86 | } |
| 87 | |
| 88 | return IAWP()->templates()->render('stats/index', [ |
| 89 | 'is_filtered' => $filtered, |
| 90 | 'stats' => $stats, |
| 91 | ]); |
| 92 | } |
| 93 | } |
| 94 |