Campaigns.php
3 years ago
City_Statistics.php
3 years ago
Country_Statistics.php
3 years ago
Current_Traffic_Finder.php
3 years ago
Interval.php
3 years ago
Minute_Interval.php
3 years ago
Range_Query.php
3 years ago
Referrers.php
3 years ago
Reset_Database.php
3 years ago
Resources.php
3 years ago
Ten_Second_Interval.php
3 years ago
View.php
3 years ago
Views.php
3 years ago
Visitors_Over_Time_Finder.php
3 years ago
City_Statistics.php
54 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Queries; |
| 4 | |
| 5 | use IAWP\Models\Geo; |
| 6 | use IAWP\Query; |
| 7 | |
| 8 | class City_Statistics extends Range_Query |
| 9 | { |
| 10 | private $results; |
| 11 | |
| 12 | public function __construct($options = []) |
| 13 | { |
| 14 | parent::__construct($options); |
| 15 | } |
| 16 | |
| 17 | public function fetch() |
| 18 | { |
| 19 | if (is_null($this->results)) { |
| 20 | $this->results = $this->query(); |
| 21 | } |
| 22 | |
| 23 | return $this->results; |
| 24 | } |
| 25 | |
| 26 | private function query() |
| 27 | { |
| 28 | $is_using_woocommerce = iawp_using_woocommerce(); |
| 29 | $query = $is_using_woocommerce ? 'city_statistics_with_woocommerce' : 'city_statistics'; |
| 30 | |
| 31 | $rows = Query::query($query, [ |
| 32 | 'start' => $this->formatted_start(), |
| 33 | 'end' => $this->formatted_end(), |
| 34 | 'prev_start' => $this->prev_period_formatted_start(), |
| 35 | 'prev_end' => $this->prev_period_formatted_end(), |
| 36 | ])->rows(); |
| 37 | |
| 38 | return self::rows_to_geos($rows); |
| 39 | } |
| 40 | |
| 41 | private function rows_to_geos($rows) |
| 42 | { |
| 43 | return array_map(function ($row) { |
| 44 | $row->views = intval($row->views); |
| 45 | $row->visitors = intval($row->visitors); |
| 46 | $row->visitor_ids = array_map(function ($id) { |
| 47 | return $id; |
| 48 | }, explode(',', $row->visitor_ids)); |
| 49 | |
| 50 | return new Geo($row); |
| 51 | }, $rows); |
| 52 | } |
| 53 | } |
| 54 |