PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.8.4
Independent Analytics – WordPress Analytics Plugin v2.8.4
2.15.5 2.15.4 2.15.3 2.15.2 2.15.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 1 year ago Relative_Date_Range.php 1 year ago
Relative_Date_Range.php
230 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 /**
14 * @var string
15 */
16 private $label;
17 /**
18 * @var string
19 */
20 private $relative_range_id = 'LAST_THIRTY';
21 /**
22 * @param string $relative_range_id A valid range id such as LAST_MONTH
23 * @param bool $convert_to_full_days
24 */
25 public function __construct(string $relative_range_id, bool $convert_to_full_days = \true)
26 {
27 if (\in_array($relative_range_id, self::VALID_RELATIVE_RANGE_IDS)) {
28 $this->relative_range_id = $relative_range_id;
29 }
30 // Call the method whose name matches the relative range id
31 $method_name = \strtolower($relative_range_id);
32 list($start, $end, $label) = $this->{$method_name}();
33 $this->set_range($start, $end, $convert_to_full_days);
34 $this->label = $label;
35 }
36 /**
37 * Get the id of the current range such as THIS_YEAR
38 *
39 * @return string
40 */
41 public function relative_range_id() : string
42 {
43 return $this->relative_range_id;
44 }
45 public function label() : string
46 {
47 return $this->label;
48 }
49 private function today() : array
50 {
51 $tz = Timezone::site_timezone();
52 $today = new DateTime('now', $tz);
53 return [$today, $today, \__('Today', 'independent-analytics')];
54 }
55 private function yesterday() : array
56 {
57 $tz = Timezone::site_timezone();
58 $yesterday = new DateTime('-1 day', $tz);
59 return [$yesterday, $yesterday, \__('Yesterday', 'independent-analytics')];
60 }
61 private function last_seven() : array
62 {
63 $tz = Timezone::site_timezone();
64 $today = new DateTime('now', $tz);
65 $seven_days_ago = new DateTime('-6 days', $tz);
66 return [$seven_days_ago, $today, \__('Last 7 Days', 'independent-analytics')];
67 }
68 private function last_thirty() : array
69 {
70 $tz = Timezone::site_timezone();
71 $today = new DateTime('now', $tz);
72 $thirty_days_ago = new DateTime('-29 days', $tz);
73 return [$thirty_days_ago, $today, \__('Last 30 Days', 'independent-analytics')];
74 }
75 private function last_sixty() : array
76 {
77 $tz = Timezone::site_timezone();
78 $today = new DateTime('now', $tz);
79 $sixty_days_ago = new DateTime('-59 days', $tz);
80 return [$sixty_days_ago, $today, \__('Last 60 Days', 'independent-analytics')];
81 }
82 private function last_ninety() : array
83 {
84 $tz = Timezone::site_timezone();
85 $today = new DateTime('now', $tz);
86 $ninety_days_ago = new DateTime('-89 days', $tz);
87 return [$ninety_days_ago, $today, \__('Last 90 Days', 'independent-analytics')];
88 }
89 private function this_week() : array
90 {
91 $tz = Timezone::site_timezone();
92 $today = new DateTime('now', $tz);
93 $firstDayOfWeek = \intval(\get_option('iawp_dow'));
94 $currentDayOfWeek = \intval($today->format('w'));
95 $startOfWeekDaysAgo = $currentDayOfWeek - $firstDayOfWeek;
96 if ($startOfWeekDaysAgo < 0) {
97 $startOfWeekDaysAgo += 7;
98 }
99 $startOfWeek = new DateTime("-{$startOfWeekDaysAgo} days", $tz);
100 $endOfWeek = (clone $startOfWeek)->modify('+6 days');
101 return [$startOfWeek, $endOfWeek, \__('This Week', 'independent-analytics')];
102 }
103 private function last_week() : array
104 {
105 list($start, $end) = $this->this_week();
106 $startOfWeek = $start->modify('-7 days');
107 $endOfWeek = $end->modify('-7 days');
108 return [$startOfWeek, $endOfWeek, \__('Last Week', 'independent-analytics')];
109 }
110 private function this_month() : array
111 {
112 $tz = Timezone::site_timezone();
113 $today = new DateTime('now', $tz);
114 $day_of_month = \intval($today->format('d')) - 1;
115 $days_in_month = \intval($today->format('t')) - 1;
116 $start_of_month = (clone $today)->modify("-{$day_of_month} days");
117 $end_of_month = (clone $start_of_month)->modify("+{$days_in_month} days");
118 return [$start_of_month, $end_of_month, \__('This Month', 'independent-analytics')];
119 }
120 private function last_month() : array
121 {
122 list($start, $end) = $this->this_month();
123 $start_of_last_month = (clone $start)->modify('-1 month');
124 $days_in_last_month = \intval($start_of_last_month->format('t')) - 1;
125 $end_of_last_month = (clone $start_of_last_month)->modify("+{$days_in_last_month} days");
126 return [$start_of_last_month, $end_of_last_month, \__('Last Month', 'independent-analytics')];
127 }
128 private function last_three_months() : array
129 {
130 $tz = Timezone::site_timezone();
131 $first_of_three_months_ago = new DateTime('first day of last month', $tz);
132 $first_of_three_months_ago = $first_of_three_months_ago->modify('-2 months');
133 $last_of_last_month = new DateTime('last day of last month', $tz);
134 return [$first_of_three_months_ago, $last_of_last_month, \__('Last 3 Months', 'independent-analytics')];
135 }
136 private function last_six_months() : array
137 {
138 $tz = Timezone::site_timezone();
139 $first_of_six_months_ago = new DateTime('first day of last month', $tz);
140 $first_of_six_months_ago = $first_of_six_months_ago->modify('-5 months');
141 $last_of_last_month = new DateTime('last day of last month', $tz);
142 return [$first_of_six_months_ago, $last_of_last_month, \__('Last 6 Months', 'independent-analytics')];
143 }
144 private function last_twelve_months() : array
145 {
146 $tz = Timezone::site_timezone();
147 $first_of_twelve_months_ago = new DateTime('first day of last month', $tz);
148 $first_of_twelve_months_ago = $first_of_twelve_months_ago->modify('-11 months');
149 $last_of_last_month = new DateTime('last day of last month', $tz);
150 return [$first_of_twelve_months_ago, $last_of_last_month, \__('Last 12 Months', 'independent-analytics')];
151 }
152 private function this_year() : array
153 {
154 $tz = Timezone::site_timezone();
155 $today = new DateTime('now', $tz);
156 $year = \intval($today->format('Y'));
157 $start_of_year = (clone $today)->setDate($year, 1, 1);
158 $end_of_year = clone $today;
159 return [$start_of_year, $end_of_year, \__('This Year', 'independent-analytics')];
160 }
161 private function last_year() : array
162 {
163 $tz = Timezone::site_timezone();
164 $today = new DateTime('now', $tz);
165 $last_year = \intval($today->format('Y')) - 1;
166 $start_of_last_year = (clone $today)->setDate($last_year, 1, 1);
167 $end_of_last_year = (clone $today)->setDate($last_year, 12, 31);
168 return [$start_of_last_year, $end_of_last_year, \__('Last Year', 'independent-analytics')];
169 }
170 private function all_time() : array
171 {
172 $tz = Timezone::site_timezone();
173 $today = new DateTime('now', $tz);
174 return [self::beginning_of_time(), $today, \__('All Time', 'independent-analytics')];
175 }
176 /**
177 * Returns an array of relative ranges representing all supported ranges
178 *
179 * @return Relative_Date_Range[]
180 */
181 public static function ranges() : array
182 {
183 return \array_map(function (string $range_id) {
184 return new self($range_id);
185 }, self::VALID_RELATIVE_RANGE_IDS);
186 }
187 /**
188 * @return string[]
189 */
190 public static function range_ids() : array
191 {
192 return self::VALID_RELATIVE_RANGE_IDS;
193 }
194 /**
195 * @param string $relative_range_id
196 *
197 * @return bool
198 */
199 public static function is_valid_range(string $relative_range_id) : bool
200 {
201 if (\in_array($relative_range_id, self::VALID_RELATIVE_RANGE_IDS)) {
202 return \true;
203 }
204 return \false;
205 }
206 public static function beginning_of_time() : DateTime
207 {
208 $option_value = \get_option('iawp_beginning_of_time');
209 if ($option_value !== \false && $option_value !== '') {
210 try {
211 return new DateTime($option_value, Timezone::site_timezone());
212 } catch (\Throwable $e) {
213 return new DateTime('now', Timezone::site_timezone());
214 }
215 }
216 $views_table = Query::get_table_name(Query::VIEWS);
217 $first_view_at = Illuminate_Builder::get_builder()->select('viewed_at')->from($views_table, 'views')->orderBy('viewed_at')->value('viewed_at');
218 if (\is_null($first_view_at)) {
219 return new DateTime('now', Timezone::site_timezone());
220 }
221 try {
222 $date = new DateTime($first_view_at, Timezone::site_timezone());
223 \update_option('iawp_beginning_of_time', $first_view_at);
224 return $date;
225 } catch (\Throwable $e) {
226 return new DateTime('now', Timezone::site_timezone());
227 }
228 }
229 }
230