| @@ -6,8 +6,14 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace WP_Stream; |
| 9 | 9 | |
| 10 | +// Load Carbon to Handle dates much easier. | |
| 11 | +if ( ! class_exists( 'Carbon\Carbon' ) ) { | |
| 12 | + require_once wp_stream_get_instance()->locations['inc_dir'] . 'lib/Carbon.php'; | |
| 13 | +} | |
| 14 | +use Carbon\Carbon; | |
| 15 | + | |
| 10 | 16 | /** |
| 11 | 17 | * Class - Date_Interval |
| 12 | 18 | */ |
| 13 | 19 | class Date_Interval { |
| @@ -36,110 +42,87 @@ | ||
| 36 | 42 | $timezone = get_option( 'timezone_string' ); |
| 37 | 43 | |
| 38 | 44 | if ( empty( $timezone ) ) { |
| 39 | 45 | $gmt_offset = (int) get_option( 'gmt_offset' ); |
| 40 | - $timezone = timezone_name_from_abbr( '', $gmt_offset * 3600, true ); | |
| 46 | + $timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, true ); | |
| 41 | 47 | if ( false === $timezone ) { |
| 42 | - $timezone = timezone_name_from_abbr( '', $gmt_offset * 3600, false ); | |
| 48 | + $timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, false ); | |
| 43 | 49 | } |
| 50 | + if ( false === $timezone ) { | |
| 51 | + $timezone = null; | |
| 52 | + } | |
| 44 | 53 | } |
| 45 | 54 | |
| 46 | - try { | |
| 47 | - $timezone_object = $timezone ? new \DateTimeZone( $timezone ) : null; | |
| 48 | - } catch ( \Exception $e ) { | |
| 49 | - $timezone_object = null; | |
| 50 | - } | |
| 51 | - | |
| 52 | - try { | |
| 53 | - $today = new \DateTimeImmutable( 'today', $timezone_object ); | |
| 54 | - $date_intervals = $this->generate_date_intervals( $today ); | |
| 55 | - } catch ( \Exception $e ) { | |
| 56 | - $date_intervals = array(); | |
| 57 | - } | |
| 58 | - | |
| 59 | - /** | |
| 60 | - * Allow other plugins to filter the predefined date intervals. | |
| 61 | - * | |
| 62 | - * @param array $date_intervals Date intervals array. | |
| 63 | - * @param string $timezone Timezone. | |
| 64 | - */ | |
| 65 | - return apply_filters( 'wp_stream_predefined_date_intervals', $date_intervals, $timezone ); | |
| 66 | - } | |
| 67 | - | |
| 68 | - /** | |
| 69 | - * Generate date intervals relative to date object provided. | |
| 70 | - * | |
| 71 | - * @param \DateTimeImmutable $date Date object. | |
| 72 | - * | |
| 73 | - * @return array[] | |
| 74 | - */ | |
| 75 | - public function generate_date_intervals( \DateTimeImmutable $date ) { | |
| 76 | - return array( | |
| 77 | - 'today' => array( | |
| 78 | - 'label' => esc_html__( 'Today', 'stream' ), | |
| 79 | - 'start' => $date, | |
| 80 | - 'end' => $date->modify( '+1 day -1 microsecond' ), | |
| 55 | + return apply_filters( | |
| 56 | + 'wp_stream_predefined_date_intervals', | |
| 57 | + array( | |
| 58 | + 'today' => array( | |
| 59 | + 'label' => esc_html__( 'Today', 'stream' ), | |
| 60 | + 'start' => Carbon::today( $timezone )->startOfDay(), | |
| 61 | + 'end' => Carbon::today( $timezone )->endOfDay(), | |
| 62 | + ), | |
| 63 | + 'yesterday' => array( | |
| 64 | + 'label' => esc_html__( 'Yesterday', 'stream' ), | |
| 65 | + 'start' => Carbon::today( $timezone )->startOfDay()->subDay(), | |
| 66 | + 'end' => Carbon::today( $timezone )->startOfDay()->subSecond(), | |
| 67 | + ), | |
| 68 | + 'last-7-days' => array( | |
| 69 | + /* translators: %d: number of days (e.g. "7") */ | |
| 70 | + 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ), | |
| 71 | + 'start' => Carbon::today( $timezone )->subDays( 7 ), | |
| 72 | + 'end' => Carbon::today( $timezone ), | |
| 73 | + ), | |
| 74 | + 'last-14-days' => array( | |
| 75 | + /* translators: %d: number of days (e.g. "7") */ | |
| 76 | + 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ), | |
| 77 | + 'start' => Carbon::today( $timezone )->subDays( 14 ), | |
| 78 | + 'end' => Carbon::today( $timezone ), | |
| 79 | + ), | |
| 80 | + 'last-30-days' => array( | |
| 81 | + /* translators: %d: number of days (e.g. "7") */ | |
| 82 | + 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ), | |
| 83 | + 'start' => Carbon::today( $timezone )->subDays( 30 ), | |
| 84 | + 'end' => Carbon::today( $timezone ), | |
| 85 | + ), | |
| 86 | + 'this-month' => array( | |
| 87 | + 'label' => esc_html__( 'This Month', 'stream' ), | |
| 88 | + 'start' => Carbon::today( $timezone )->startOfMonth(), | |
| 89 | + 'end' => Carbon::today( $timezone )->endOfMonth(), | |
| 90 | + ), | |
| 91 | + 'last-month' => array( | |
| 92 | + 'label' => esc_html__( 'Last Month', 'stream' ), | |
| 93 | + 'start' => Carbon::today( $timezone )->startOfMonth()->subMonth(), | |
| 94 | + 'end' => Carbon::today( $timezone )->startOfMonth()->subSecond(), | |
| 95 | + ), | |
| 96 | + 'last-3-months' => array( | |
| 97 | + /* translators: %d: number of months (e.g. "3") */ | |
| 98 | + 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ), | |
| 99 | + 'start' => Carbon::today( $timezone )->subMonths( 3 ), | |
| 100 | + 'end' => Carbon::today( $timezone ), | |
| 101 | + ), | |
| 102 | + 'last-6-months' => array( | |
| 103 | + /* translators: %d: number of months (e.g. "3") */ | |
| 104 | + 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ), | |
| 105 | + 'start' => Carbon::today( $timezone )->subMonths( 6 ), | |
| 106 | + 'end' => Carbon::today( $timezone ), | |
| 107 | + ), | |
| 108 | + 'last-12-months' => array( | |
| 109 | + /* translators: %d: number of months (e.g. "3") */ | |
| 110 | + 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ), | |
| 111 | + 'start' => Carbon::today( $timezone )->subMonths( 12 ), | |
| 112 | + 'end' => Carbon::today( $timezone ), | |
| 113 | + ), | |
| 114 | + 'this-year' => array( | |
| 115 | + 'label' => esc_html__( 'This Year', 'stream' ), | |
| 116 | + 'start' => Carbon::today( $timezone )->startOfYear(), | |
| 117 | + 'end' => Carbon::today( $timezone )->endOfYear(), | |
| 118 | + ), | |
| 119 | + 'last-year' => array( | |
| 120 | + 'label' => esc_html__( 'Last Year', 'stream' ), | |
| 121 | + 'start' => Carbon::today( $timezone )->startOfYear()->subYear(), | |
| 122 | + 'end' => Carbon::today( $timezone )->startOfYear()->subSecond(), | |
| 123 | + ), | |
| 81 | 124 | ), |
| 82 | - 'yesterday' => array( | |
| 83 | - 'label' => esc_html__( 'Yesterday', 'stream' ), | |
| 84 | - 'start' => $date->modify( '-1 day' ), | |
| 85 | - 'end' => $date->modify( '-1 microsecond' ), | |
| 86 | - ), | |
| 87 | - 'last-7-days' => array( | |
| 88 | - /* translators: %d: number of days (e.g. "7") */ | |
| 89 | - 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ), | |
| 90 | - 'start' => $date->modify( '-7 days' ), | |
| 91 | - 'end' => $date, | |
| 92 | - ), | |
| 93 | - 'last-14-days' => array( | |
| 94 | - /* translators: %d: number of days (e.g. "7") */ | |
| 95 | - 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ), | |
| 96 | - 'start' => $date->modify( '-14 days' ), | |
| 97 | - 'end' => $date, | |
| 98 | - ), | |
| 99 | - 'last-30-days' => array( | |
| 100 | - /* translators: %d: number of days (e.g. "7") */ | |
| 101 | - 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ), | |
| 102 | - 'start' => $date->modify( '-30 days' ), | |
| 103 | - 'end' => $date, | |
| 104 | - ), | |
| 105 | - 'this-month' => array( | |
| 106 | - 'label' => esc_html__( 'This Month', 'stream' ), | |
| 107 | - 'start' => $date->modify( 'first day of this month' ), | |
| 108 | - 'end' => $date->modify( 'last day of this month' )->modify( '+1 day -1 microsecond' ), | |
| 109 | - ), | |
| 110 | - 'last-month' => array( | |
| 111 | - 'label' => esc_html__( 'Last Month', 'stream' ), | |
| 112 | - 'start' => $date->modify( 'first day of last month' ), | |
| 113 | - 'end' => $date->modify( 'last day of last month' )->modify( '+1 day -1 microsecond' ), | |
| 114 | - ), | |
| 115 | - 'last-3-months' => array( | |
| 116 | - /* translators: %d: number of months (e.g. "3") */ | |
| 117 | - 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ), | |
| 118 | - 'start' => $date->modify( '-3 months' ), | |
| 119 | - 'end' => $date, | |
| 120 | - ), | |
| 121 | - 'last-6-months' => array( | |
| 122 | - /* translators: %d: number of months (e.g. "3") */ | |
| 123 | - 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ), | |
| 124 | - 'start' => $date->modify( '-6 months' ), | |
| 125 | - 'end' => $date, | |
| 126 | - ), | |
| 127 | - 'last-12-months' => array( | |
| 128 | - /* translators: %d: number of months (e.g. "3") */ | |
| 129 | - 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ), | |
| 130 | - 'start' => $date->modify( '-12 months' ), | |
| 131 | - 'end' => $date, | |
| 132 | - ), | |
| 133 | - 'this-year' => array( | |
| 134 | - 'label' => esc_html__( 'This Year', 'stream' ), | |
| 135 | - 'start' => $date->modify( 'first day of January' ), | |
| 136 | - 'end' => $date->modify( 'last day of December' )->modify( '+1 day -1 microsecond' ), | |
| 137 | - ), | |
| 138 | - 'last-year' => array( | |
| 139 | - 'label' => esc_html__( 'Last Year', 'stream' ), | |
| 140 | - 'start' => $date->modify( 'first day of January' )->modify( '-1 year' ), | |
| 141 | - 'end' => $date->modify( 'first day of January' )->modify( '-1 microsecond' ), | |
| 142 | - ), | |
| 125 | + $timezone | |
| 143 | 126 | ); |
| 144 | 127 | } |
| 145 | 128 | } |