PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.3.1
Independent Analytics – WordPress Analytics Plugin v2.3.1
2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / Date_Range / Relative_Date_Range.php
independent-analytics / IAWP / Date_Range Last commit date
Date_Range.php 2 years ago Exact_Date_Range.php 2 years ago Relative_Date_Range.php 2 years ago
Relative_Date_Range.php
220 lines
1 <?php
2
3 namespace IAWP\Date_Range;
4
5 use DateTime;
6 use IAWP\Illuminate_Builder;
7 use IAWP\Query;
8 use IAWPSCOPED\Proper\Timezone;
9 /** @internal */
10 class Relative_Date_Range extends \IAWP\Date_Range\Date_Range
11 {
12 private const VALID_RELATIVE_RANGE_IDS = ['TODAY', 'YESTERDAY', 'THIS_WEEK', 'LAST_WEEK', 'LAST_SEVEN', 'LAST_THIRTY', 'LAST_SIXTY', 'LAST_NINETY', 'THIS_MONTH', 'LAST_MONTH', 'LAST_THREE_MONTHS', 'LAST_SIX_MONTHS', 'LAST_TWELVE_MONTHS', 'THIS_YEAR', 'LAST_YEAR', 'ALL_TIME'];
13 private static $beginning_of_time = null;
14 /**
15 * @var string
16 */
17 private $label;
18 /**
19 * @var string
20 */
21 private $relative_range_id = 'LAST_THIRTY';
22 /**
23 * @param string $relative_range_id A valid range id such as LAST_MONTH
24 * @param bool $convert_to_full_days
25 */
26 public function __construct(string $relative_range_id, bool $convert_to_full_days = \true)
27 {
28 if (\in_array($relative_range_id, self::VALID_RELATIVE_RANGE_IDS)) {
29 $this->relative_range_id = $relative_range_id;
30 }
31 // Call the method whose name matches the relative range id
32 $method_name = \strtolower($relative_range_id);
33 list($start, $end, $label) = $this->{$method_name}();
34 $this->set_range($start, $end, $convert_to_full_days);
35 $this->label = $label;
36 }
37 /**
38 * Get the id of the current range such as THIS_YEAR
39 *
40 * @return string
41 */
42 public function relative_range_id() : string
43 {
44 return $this->relative_range_id;
45 }
46 public function label() : string
47 {
48 return $this->label;
49 }
50 private function today() : array
51 {
52 $tz = Timezone::site_timezone();
53 $today = new DateTime('now', $tz);
54 return [$today, $today, \__('Today', 'independent-analytics')];
55 }
56 private function yesterday() : array
57 {
58 $tz = Timezone::site_timezone();
59 $yesterday = new DateTime('-1 day', $tz);
60 return [$yesterday, $yesterday, \__('Yesterday', 'independent-analytics')];
61 }
62 private function last_seven() : array
63 {
64 $tz = Timezone::site_timezone();
65 $today = new DateTime('now', $tz);
66 $seven_days_ago = new DateTime('-6 days', $tz);
67 return [$seven_days_ago, $today, \__('Last 7 Days', 'independent-analytics')];
68 }
69 private function last_thirty() : array
70 {
71 $tz = Timezone::site_timezone();
72 $today = new DateTime('now', $tz);
73 $thirty_days_ago = new DateTime('-29 days', $tz);
74 return [$thirty_days_ago, $today, \__('Last 30 Days', 'independent-analytics')];
75 }
76 private function last_sixty() : array
77 {
78 $tz = Timezone::site_timezone();
79 $today = new DateTime('now', $tz);
80 $sixty_days_ago = new DateTime('-59 days', $tz);
81 return [$sixty_days_ago, $today, \__('Last 60 Days', 'independent-analytics')];
82 }
83 private function last_ninety() : array
84 {
85 $tz = Timezone::site_timezone();
86 $today = new DateTime('now', $tz);
87 $ninety_days_ago = new DateTime('-89 days', $tz);
88 return [$ninety_days_ago, $today, \__('Last 90 Days', 'independent-analytics')];
89 }
90 private function this_week() : array
91 {
92 $tz = Timezone::site_timezone();
93 $today = new DateTime('now', $tz);
94 $firstDayOfWeek = \intval(\get_option('iawp_dow'));
95 $currentDayOfWeek = \intval($today->format('w'));
96 $startOfWeekDaysAgo = $currentDayOfWeek - $firstDayOfWeek;
97 if ($startOfWeekDaysAgo < 0) {
98 $startOfWeekDaysAgo += 7;
99 }
100 $startOfWeek = new DateTime("-{$startOfWeekDaysAgo} days", $tz);
101 $endOfWeek = (clone $startOfWeek)->modify('+6 days');
102 return [$startOfWeek, $endOfWeek, \__('This Week', 'independent-analytics')];
103 }
104 private function last_week() : array
105 {
106 list($start, $end) = $this->this_week();
107 $startOfWeek = $start->modify('-7 days');
108 $endOfWeek = $end->modify('-7 days');
109 return [$startOfWeek, $endOfWeek, \__('Last Week', 'independent-analytics')];
110 }
111 private function this_month() : array
112 {
113 $tz = Timezone::site_timezone();
114 $today = new DateTime('now', $tz);
115 $day_of_month = \intval($today->format('d')) - 1;
116 $days_in_month = \intval($today->format('t')) - 1;
117 $start_of_month = (clone $today)->modify("-{$day_of_month} days");
118 $end_of_month = (clone $start_of_month)->modify("+{$days_in_month} days");
119 return [$start_of_month, $end_of_month, \__('This Month', 'independent-analytics')];
120 }
121 private function last_month() : array
122 {
123 list($start, $end) = $this->this_month();
124 $start_of_last_month = (clone $start)->modify('-1 month');
125 $days_in_last_month = \intval($start_of_last_month->format('t')) - 1;
126 $end_of_last_month = (clone $start_of_last_month)->modify("+{$days_in_last_month} days");
127 return [$start_of_last_month, $end_of_last_month, \__('Last Month', 'independent-analytics')];
128 }
129 private function last_three_months() : array
130 {
131 $tz = Timezone::site_timezone();
132 $first_of_three_months_ago = new DateTime('first day of last month', $tz);
133 $first_of_three_months_ago = $first_of_three_months_ago->modify('-2 months');
134 $last_of_last_month = new DateTime('last day of last month', $tz);
135 return [$first_of_three_months_ago, $last_of_last_month, \__('Last 3 Months', 'independent-analytics')];
136 }
137 private function last_six_months() : array
138 {
139 $tz = Timezone::site_timezone();
140 $first_of_six_months_ago = new DateTime('first day of last month', $tz);
141 $first_of_six_months_ago = $first_of_six_months_ago->modify('-5 months');
142 $last_of_last_month = new DateTime('last day of last month', $tz);
143 return [$first_of_six_months_ago, $last_of_last_month, \__('Last 6 Months', 'independent-analytics')];
144 }
145 private function last_twelve_months() : array
146 {
147 $tz = Timezone::site_timezone();
148 $first_of_twelve_months_ago = new DateTime('first day of last month', $tz);
149 $first_of_twelve_months_ago = $first_of_twelve_months_ago->modify('-11 months');
150 $last_of_last_month = new DateTime('last day of last month', $tz);
151 return [$first_of_twelve_months_ago, $last_of_last_month, \__('Last 12 Months', 'independent-analytics')];
152 }
153 private function this_year() : array
154 {
155 $tz = Timezone::site_timezone();
156 $today = new DateTime('now', $tz);
157 $year = \intval($today->format('Y'));
158 $start_of_year = (clone $today)->setDate($year, 1, 1);
159 $end_of_year = clone $today;
160 return [$start_of_year, $end_of_year, \__('This Year', 'independent-analytics')];
161 }
162 private function last_year() : array
163 {
164 $tz = Timezone::site_timezone();
165 $today = new DateTime('now', $tz);
166 $last_year = \intval($today->format('Y')) - 1;
167 $start_of_last_year = (clone $today)->setDate($last_year, 1, 1);
168 $end_of_last_year = (clone $today)->setDate($last_year, 12, 31);
169 return [$start_of_last_year, $end_of_last_year, \__('Last Year', 'independent-analytics')];
170 }
171 private function all_time() : array
172 {
173 $tz = Timezone::site_timezone();
174 $today = new DateTime('now', $tz);
175 return [self::beginning_of_time(), $today, \__('All Time', 'independent-analytics')];
176 }
177 /**
178 * Returns an array of relative ranges representing all supported ranges
179 *
180 * @return Relative_Date_Range[]
181 */
182 public static function ranges() : array
183 {
184 return \array_map(function (string $range_id) {
185 return new self($range_id);
186 }, self::VALID_RELATIVE_RANGE_IDS);
187 }
188 /**
189 * @param string $relative_range_id
190 *
191 * @return bool
192 */
193 public static function is_valid_range(string $relative_range_id) : bool
194 {
195 if (\in_array($relative_range_id, self::VALID_RELATIVE_RANGE_IDS)) {
196 return \true;
197 }
198 return \false;
199 }
200 private static function beginning_of_time() : DateTime
201 {
202 if (!\is_null(self::$beginning_of_time)) {
203 return self::$beginning_of_time;
204 }
205 $tz = Timezone::site_timezone();
206 $views_table = Query::get_table_name(Query::VIEWS);
207 $first_view_at = Illuminate_Builder::get_builder()->select('viewed_at')->from($views_table, 'views')->orderBy('viewed_at')->value('viewed_at');
208 if (!\is_null($first_view_at)) {
209 try {
210 self::$beginning_of_time = new DateTime($first_view_at);
211 } catch (\Throwable $e) {
212 self::$beginning_of_time = new DateTime('now', $tz);
213 }
214 } else {
215 self::$beginning_of_time = new DateTime('now', $tz);
216 }
217 return self::$beginning_of_time;
218 }
219 }
220