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 |