AJAX
1 year ago
Admin_Page
1 year ago
Custom_WordPress_Columns
2 years ago
Data_Pruning
1 year ago
Date_Picker
1 year ago
Date_Range
1 year ago
Ecommerce
1 year ago
Email_Reports
1 year ago
Filter_Lists
2 years ago
Form_Submissions
1 year ago
Interval
2 years ago
Menu_Bar_Stats
1 year ago
Migrations
1 year ago
Models
1 year ago
Public_API
2 years ago
Rows
1 year ago
Statistics
1 year ago
Tables
1 year ago
Utils
1 year ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
1 year ago
Chart.php
1 year ago
Chart_Geo.php
1 year ago
Cron_Job.php
1 year ago
Cron_Job_Autoloader.php
1 year ago
Cron_Manager.php
2 years ago
Current_Traffic_Finder.php
2 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
2 years ago
Database.php
2 years ago
Database_Manager.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
1 year ago
Filters.php
1 year ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Manager.php
1 year ago
Geoposition.php
2 years ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
1 year ago
Independent_Analytics.php
1 year ago
Interrupt.php
1 year ago
Known_Referrers.php
2 years ago
Patch.php
1 year ago
Plugin_Conflict_Detector.php
1 year ago
Plugin_Group.php
1 year ago
Plugin_Group_Option.php
2 years ago
Query.php
1 year ago
Query_Taps.php
1 year ago
Quick_Stats.php
2 years ago
REST_API.php
1 year ago
Real_Time.php
1 year ago
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
1 year ago
Resource_Identifier.php
1 year ago
Settings.php
1 year ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
1 year ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
Chart_Geo.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWP\Models\Geo; |
| 6 | /** @internal */ |
| 7 | class Chart_Geo |
| 8 | { |
| 9 | private $countries; |
| 10 | private $title; |
| 11 | /** |
| 12 | * @param Geo[] $geos |
| 13 | * @param $title |
| 14 | */ |
| 15 | public function __construct(array $geos, $title = null) |
| 16 | { |
| 17 | $this->countries = $this->parse($geos); |
| 18 | $this->title = $title; |
| 19 | } |
| 20 | public function get_html() |
| 21 | { |
| 22 | $chart_data = \array_map(function ($country) { |
| 23 | return [$country['country_code'], $country['views'], $this->get_tooltip($country)]; |
| 24 | }, $this->countries); |
| 25 | $dark_mode = \IAWPSCOPED\iawp()->get_option('iawp_dark_mode', '0'); |
| 26 | \ob_start(); |
| 27 | ?> |
| 28 | <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> |
| 29 | <div class="chart-container"> |
| 30 | <div class="chart-inner"> |
| 31 | <div class="legend-container"> |
| 32 | <h2 class="legend-title"><?php |
| 33 | echo $this->title; |
| 34 | ?></h2> |
| 35 | </div> |
| 36 | <div id="independent-analytics-chart" |
| 37 | data-controller="chart-geo" |
| 38 | data-chart-geo-data-value="<?php |
| 39 | echo \esc_attr(\json_encode($chart_data)); |
| 40 | ?>" |
| 41 | data-chart-geo-dark-mode-value="<?php |
| 42 | echo \esc_attr($dark_mode); |
| 43 | ?>"> |
| 44 | <div data-chart-geo-target="chart"></div> |
| 45 | </div> |
| 46 | </div> |
| 47 | </div><?php |
| 48 | $html = \ob_get_contents(); |
| 49 | \ob_end_clean(); |
| 50 | return $html; |
| 51 | } |
| 52 | private function parse($geos) : array |
| 53 | { |
| 54 | $countries = []; |
| 55 | foreach ($geos as $geo) { |
| 56 | $existing_country_index = null; |
| 57 | foreach ($countries as $index => $country) { |
| 58 | if ($geo->country_code() === $country['country_code']) { |
| 59 | $existing_country_index = $index; |
| 60 | } |
| 61 | } |
| 62 | if (\is_numeric($existing_country_index)) { |
| 63 | $countries[$existing_country_index]['views'] += $geo->views(); |
| 64 | $countries[$existing_country_index]['visitors'] += $geo->visitors(); |
| 65 | $countries[$existing_country_index]['sessions'] += $geo->sessions(); |
| 66 | } else { |
| 67 | $countries[] = ['country_code' => $geo->country_code(), 'country' => $geo->country(), 'views' => $geo->views(), 'visitors' => $geo->visitors(), 'sessions' => $geo->sessions()]; |
| 68 | } |
| 69 | } |
| 70 | return $countries; |
| 71 | } |
| 72 | private function get_tooltip($country) : string |
| 73 | { |
| 74 | \ob_start(); |
| 75 | ?> |
| 76 | <div class="iawp-geo-chart-tooltip"> |
| 77 | <?php |
| 78 | echo \IAWP\Icon_Directory_Factory::flags()->find($country['country_code']); |
| 79 | ?> |
| 80 | <h1><?php |
| 81 | echo $country['country']; |
| 82 | ?></h1> |
| 83 | <p><span><?php |
| 84 | \_e('Views'); |
| 85 | ?>: </span> <?php |
| 86 | echo \number_format_i18n($country['views']); |
| 87 | ?></p> |
| 88 | <p><span><?php |
| 89 | \_e('Visitors'); |
| 90 | ?>: </span><?php |
| 91 | echo \number_format_i18n($country['visitors']); |
| 92 | ?> </p> |
| 93 | <p><span><?php |
| 94 | \_e('Sessions'); |
| 95 | ?>: </span><?php |
| 96 | echo \number_format_i18n($country['sessions']); |
| 97 | ?> </p> |
| 98 | </div> |
| 99 | <?php |
| 100 | $html = \ob_get_contents(); |
| 101 | \ob_end_clean(); |
| 102 | return $html; |
| 103 | } |
| 104 | } |
| 105 |