range_query.php
4 years ago
referrers.php
4 years ago
resources.php
4 years ago
views.php
4 years ago
views.php
140 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | class Views extends Range_Query |
| 6 | { |
| 7 | const REFERRERS = 'REFERRERS'; |
| 8 | const RESOURCES = 'RESOURCES'; |
| 9 | private $views; |
| 10 | private $prev_period_views; |
| 11 | private $daily_views; |
| 12 | |
| 13 | public function __construct($type, $ids = null, $start = null, $end = null) |
| 14 | { |
| 15 | parent::__construct($start, $end); |
| 16 | if ($type == self::RESOURCES || $type == self::REFERRERS) { |
| 17 | $this->query($type, $ids); |
| 18 | } else { |
| 19 | throw new \Exception('IAWP_Views: Unsupported type'); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | public function views() |
| 24 | { |
| 25 | return $this->views; |
| 26 | } |
| 27 | |
| 28 | public function prev_period_views() |
| 29 | { |
| 30 | return $this->prev_period_views; |
| 31 | } |
| 32 | |
| 33 | public function daily_views() |
| 34 | { |
| 35 | return $this->daily_views; |
| 36 | } |
| 37 | |
| 38 | public function views_percentage_growth() |
| 39 | { |
| 40 | if ($this->views() == 0 || $this->prev_period_views() == 0) { |
| 41 | return 0; |
| 42 | } else { |
| 43 | return round((($this->views() / $this->prev_period_views()) - 1) * 100); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | private function query($type, $ids) |
| 48 | { |
| 49 | global $wpdb; |
| 50 | $views_table = DB::views_table(); |
| 51 | |
| 52 | if (is_null($ids)) { |
| 53 | $skip_in = 1; |
| 54 | $ids = [0]; |
| 55 | $in = '%s'; |
| 56 | } elseif (count($ids) == 0) { |
| 57 | $skip_in = 0; |
| 58 | $ids = [0]; |
| 59 | $in = '%s'; |
| 60 | } else { |
| 61 | $skip_in = 0; |
| 62 | $in = implode(',', array_fill(0, count($ids), '%s')); |
| 63 | } |
| 64 | |
| 65 | $column = $type == self::RESOURCES ? 'resource_id' : 'referrer_id'; |
| 66 | $has_null_id = false; |
| 67 | foreach ($ids as $id) { |
| 68 | if (is_null($id)) { |
| 69 | $has_null_id = true; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | $null_check = ''; |
| 74 | |
| 75 | if ($type == self::REFERRERS && $has_null_id) { |
| 76 | $null_check = "OR $column IS NULL"; |
| 77 | } |
| 78 | |
| 79 | $utc = Timezone::utc_offset(); |
| 80 | $local_offset = Timezone::local_offset(); |
| 81 | $query = $wpdb->prepare( |
| 82 | "SELECT DATE(CONVERT_TZ(viewed_at, '$utc', '$local_offset')) as date, count(*) as views FROM $views_table WHERE viewed_at BETWEEN %s AND %s AND (%d = 1 OR $column IN ($in) $null_check) GROUP BY DATE(CONVERT_TZ(viewed_at, '$utc', '$local_offset'))", |
| 83 | $this->formatted_start(), |
| 84 | $this->formatted_end(), |
| 85 | $skip_in, |
| 86 | ...$ids |
| 87 | ); |
| 88 | $rows = $wpdb->get_results($query); |
| 89 | |
| 90 | $prev_period_query = $wpdb->prepare( |
| 91 | "SELECT count(*) as views FROM $views_table WHERE viewed_at BETWEEN %s AND %s AND (%d = 1 OR $column IN ($in))", |
| 92 | $this->prev_period_formatted_start(), |
| 93 | $this->prev_period_formatted_end(), |
| 94 | $skip_in, |
| 95 | ...$ids |
| 96 | ); |
| 97 | $prev_period_views = intval($wpdb->get_var($prev_period_query)); |
| 98 | |
| 99 | $views = 0; |
| 100 | $daily_views = []; |
| 101 | |
| 102 | foreach ($rows as $row) { |
| 103 | $views += $row->views; |
| 104 | $daily_views[] = [$row->date, intval($row->views)]; |
| 105 | } |
| 106 | |
| 107 | $this->views = $views; |
| 108 | $this->prev_period_views = $prev_period_views; |
| 109 | $this->daily_views = $this->fill_in_data_range($daily_views); |
| 110 | } |
| 111 | |
| 112 | private function fill_in_data_range($partial_data) |
| 113 | { |
| 114 | $interval = new \DateInterval('P1D'); |
| 115 | $start = clone $this->start(); |
| 116 | $end = clone $this->end(); |
| 117 | $user_timezone = new \DateTimeZone(Timezone::local_offset()); |
| 118 | $start->setTimezone($user_timezone); |
| 119 | $end->setTimezone($user_timezone); |
| 120 | |
| 121 | $date_range = new \DatePeriod($start, $interval, $end); |
| 122 | $new_data = []; |
| 123 | |
| 124 | foreach ($date_range as $date) { |
| 125 | $views = 0; |
| 126 | |
| 127 | foreach ($partial_data as $data_point) { |
| 128 | if ($date == (new \DateTime($data_point[0], $user_timezone))) { |
| 129 | $views = intval($data_point[1]); |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | $new_data[] = [$date, $views]; |
| 135 | } |
| 136 | |
| 137 | return $new_data; |
| 138 | } |
| 139 | } |
| 140 |