geos.php
3 years ago
range_query.php
3 years ago
referrers.php
3 years ago
resources.php
3 years ago
views.php
3 years ago
geos.php
126 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | class Geos extends Range_Query |
| 6 | { |
| 7 | private $results; |
| 8 | private $include_sub_and_city; |
| 9 | private $ids; |
| 10 | |
| 11 | public function __construct($start = null, $end = null, $include_sub_and_city = true, $ids = null) |
| 12 | { |
| 13 | parent::__construct($start, $end); |
| 14 | $this->include_sub_and_city = $include_sub_and_city; |
| 15 | $this->ids = $ids; |
| 16 | } |
| 17 | |
| 18 | public function get_geos() |
| 19 | { |
| 20 | if (is_null($this->results)) { |
| 21 | $this->results = $this->query(); |
| 22 | } |
| 23 | |
| 24 | return $this->results; |
| 25 | } |
| 26 | |
| 27 | public function get_geo_as_csv() |
| 28 | { |
| 29 | $referrers = $this->get_geos(); |
| 30 | $arr = [['Continent', 'Country', 'Subdivision', 'City', 'Views', 'Visitors']]; |
| 31 | |
| 32 | foreach ($referrers as $referrer) { |
| 33 | $arr[] = [ |
| 34 | $referrer->continent(), |
| 35 | $referrer->country(), |
| 36 | $referrer->subdivision(), |
| 37 | $referrer->city(), |
| 38 | $referrer->views(), |
| 39 | $referrer->visitors(), |
| 40 | ]; |
| 41 | } |
| 42 | |
| 43 | return Array_To_CSV::array2csv($arr); |
| 44 | } |
| 45 | |
| 46 | private function query() |
| 47 | { |
| 48 | global $wpdb; |
| 49 | $views_table = DB::views_table(); |
| 50 | $visitors_table = DB::visitors_table(); |
| 51 | $include_sub_and_city = $this->include_sub_and_city ? 'true' : 'false'; |
| 52 | $ids = $this->ids; |
| 53 | |
| 54 | if (is_null($ids)) { |
| 55 | $skip_in = 1; |
| 56 | $ids = [0]; |
| 57 | $in = '%s'; |
| 58 | } elseif (count($ids) == 0) { |
| 59 | $skip_in = 0; |
| 60 | $ids = [0]; |
| 61 | $in = '%s'; |
| 62 | } else { |
| 63 | $skip_in = 0; |
| 64 | $in = implode(',', array_fill(0, count($ids), '%s')); |
| 65 | } |
| 66 | |
| 67 | $geo_traffic_query = $wpdb->prepare( |
| 68 | " |
| 69 | SELECT |
| 70 | country_code, |
| 71 | IF( $include_sub_and_city = true, city, null) as city, |
| 72 | IF( $include_sub_and_city = true, subdivision, null) as subdivision, |
| 73 | country, |
| 74 | continent, |
| 75 | COUNT(*) AS views, |
| 76 | COUNT(DISTINCT visitor_id) AS visitors, |
| 77 | GROUP_CONCAT(DISTINCT $views_table.visitor_id) as visitor_ids |
| 78 | FROM |
| 79 | $views_table |
| 80 | LEFT JOIN $visitors_table ON $views_table.visitor_id = $visitors_table.id |
| 81 | WHERE |
| 82 | country_code IS NOT NULL |
| 83 | AND viewed_at BETWEEN %s AND %s |
| 84 | AND (%d = 1 OR $views_table.visitor_id IN ($in)) |
| 85 | GROUP BY |
| 86 | continent, |
| 87 | country, |
| 88 | country_code, |
| 89 | IF( $include_sub_and_city = true, subdivision, null), |
| 90 | IF( $include_sub_and_city = true, city, null) |
| 91 | ORDER BY |
| 92 | views DESC, |
| 93 | city |
| 94 | ", |
| 95 | $this->formatted_start(), |
| 96 | $this->formatted_end(), |
| 97 | $skip_in, |
| 98 | ...$ids |
| 99 | ); |
| 100 | |
| 101 | $geo_traffic = $wpdb->get_results($geo_traffic_query); |
| 102 | |
| 103 | return self::rows_to_geos($geo_traffic); |
| 104 | } |
| 105 | |
| 106 | private function rows_to_geos($rows) |
| 107 | { |
| 108 | return array_map(function ($row) { |
| 109 | $visitor_ids = array_map(function ($id) { |
| 110 | return intval($id); |
| 111 | }, explode(',', $row->visitor_ids)); |
| 112 | |
| 113 | return new Geo( |
| 114 | $row->continent, |
| 115 | $row->country, |
| 116 | $row->country_code, |
| 117 | $row->subdivision, |
| 118 | $row->city, |
| 119 | intval($row->views), |
| 120 | intval($row->visitors), |
| 121 | $visitor_ids |
| 122 | ); |
| 123 | }, $rows); |
| 124 | } |
| 125 | } |
| 126 |