| @@ -1,16 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Calculate date intervals for a specific timezone. | |
| 4 | - * | |
| 5 | - * @package WP_Stream | |
| 6 | - */ | |
| 2 | +namespace WP_Stream; | |
| 7 | 3 | |
| 8 | -namespace WP_Stream; | |
| 4 | +// Load Carbon to Handle dates much easier | |
| 5 | +if ( ! class_exists( 'Carbon\Carbon' ) ) { | |
| 6 | + require_once wp_stream_get_instance()->locations['inc_dir'] . 'lib/Carbon.php'; | |
| 7 | +} | |
| 9 | 8 | |
| 10 | -/** | |
| 11 | - * Class - Date_Interval | |
| 12 | - */ | |
| 9 | +use Carbon\Carbon; | |
| 10 | + | |
| 13 | 11 | class Date_Interval { |
| 14 | 12 | /** |
| 15 | 13 | * Contains an array of all available intervals |
| 16 | 14 | * |
| @@ -21,16 +19,13 @@ | ||
| 21 | 19 | /** |
| 22 | 20 | * Class constructor |
| 23 | 21 | */ |
| 24 | 22 | public function __construct() { |
| 25 | - // Get all default intervals. | |
| 23 | + // Get all default intervals | |
| 26 | 24 | $this->intervals = $this->get_predefined_intervals(); |
| 27 | 25 | } |
| 28 | 26 | |
| 29 | 27 | /** |
| 30 | - * Returns date interval data based upon "Timezone" site setting. | |
| 31 | - * | |
| 32 | - * @todo add better inline comments | |
| 33 | 28 | * @return mixed |
| 34 | 29 | */ |
| 35 | 30 | public function get_predefined_intervals() { |
| 36 | 31 | $timezone = get_option( 'timezone_string' ); |
| @@ -36,110 +31,87 @@ | ||
| 36 | 31 | $timezone = get_option( 'timezone_string' ); |
| 37 | 32 | |
| 38 | 33 | if ( empty( $timezone ) ) { |
| 39 | 34 | $gmt_offset = (int) get_option( 'gmt_offset' ); |
| 40 | - $timezone = timezone_name_from_abbr( '', $gmt_offset * 3600, true ); | |
| 35 | + $timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, true ); | |
| 41 | 36 | if ( false === $timezone ) { |
| 42 | - $timezone = timezone_name_from_abbr( '', $gmt_offset * 3600, false ); | |
| 37 | + $timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, false ); | |
| 43 | 38 | } |
| 39 | + if ( false === $timezone ) { | |
| 40 | + $timezone = null; | |
| 41 | + } | |
| 44 | 42 | } |
| 45 | 43 | |
| 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' ), | |
| 44 | + return apply_filters( | |
| 45 | + 'wp_stream_predefined_date_intervals', | |
| 46 | + array( | |
| 47 | + 'today' => array( | |
| 48 | + 'label' => esc_html__( 'Today', 'stream' ), | |
| 49 | + 'start' => Carbon::today( $timezone )->startOfDay(), | |
| 50 | + 'end' => Carbon::today( $timezone )->endOfDay(), | |
| 51 | + ), | |
| 52 | + 'yesterday' => array( | |
| 53 | + 'label' => esc_html__( 'Yesterday', 'stream' ), | |
| 54 | + 'start' => Carbon::today( $timezone )->startOfDay()->subDay(), | |
| 55 | + 'end' => Carbon::today( $timezone )->startOfDay()->subSecond(), | |
| 56 | + ), | |
| 57 | + 'last-7-days' => array( | |
| 58 | + // translators: Placeholder refers to a number of days (e.g. "7") | |
| 59 | + 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ), | |
| 60 | + 'start' => Carbon::today( $timezone )->subDays( 7 ), | |
| 61 | + 'end' => Carbon::today( $timezone ), | |
| 62 | + ), | |
| 63 | + 'last-14-days' => array( | |
| 64 | + // translators: Placeholder refers to a number of days (e.g. "7") | |
| 65 | + 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ), | |
| 66 | + 'start' => Carbon::today( $timezone )->subDays( 14 ), | |
| 67 | + 'end' => Carbon::today( $timezone ), | |
| 68 | + ), | |
| 69 | + 'last-30-days' => array( | |
| 70 | + // translators: Placeholder refers to a number of days (e.g. "7") | |
| 71 | + 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ), | |
| 72 | + 'start' => Carbon::today( $timezone )->subDays( 30 ), | |
| 73 | + 'end' => Carbon::today( $timezone ), | |
| 74 | + ), | |
| 75 | + 'this-month' => array( | |
| 76 | + 'label' => esc_html__( 'This Month', 'stream' ), | |
| 77 | + 'start' => Carbon::today( $timezone )->startOfMonth(), | |
| 78 | + 'end' => Carbon::today( $timezone )->endOfMonth(), | |
| 79 | + ), | |
| 80 | + 'last-month' => array( | |
| 81 | + 'label' => esc_html__( 'Last Month', 'stream' ), | |
| 82 | + 'start' => Carbon::today( $timezone )->startOfMonth()->subMonth(), | |
| 83 | + 'end' => Carbon::today( $timezone )->startOfMonth()->subSecond(), | |
| 84 | + ), | |
| 85 | + 'last-3-months' => array( | |
| 86 | + // translators: Placeholder refers to a number of months (e.g. "3") | |
| 87 | + 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ), | |
| 88 | + 'start' => Carbon::today( $timezone )->subMonths( 3 ), | |
| 89 | + 'end' => Carbon::today( $timezone ), | |
| 90 | + ), | |
| 91 | + 'last-6-months' => array( | |
| 92 | + // translators: Placeholder refers to a number of months (e.g. "3") | |
| 93 | + 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ), | |
| 94 | + 'start' => Carbon::today( $timezone )->subMonths( 6 ), | |
| 95 | + 'end' => Carbon::today( $timezone ), | |
| 96 | + ), | |
| 97 | + 'last-12-months' => array( | |
| 98 | + // translators: Placeholder refers to a number of months (e.g. "3") | |
| 99 | + 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ), | |
| 100 | + 'start' => Carbon::today( $timezone )->subMonths( 12 ), | |
| 101 | + 'end' => Carbon::today( $timezone ), | |
| 102 | + ), | |
| 103 | + 'this-year' => array( | |
| 104 | + 'label' => esc_html__( 'This Year', 'stream' ), | |
| 105 | + 'start' => Carbon::today( $timezone )->startOfYear(), | |
| 106 | + 'end' => Carbon::today( $timezone )->endOfYear(), | |
| 107 | + ), | |
| 108 | + 'last-year' => array( | |
| 109 | + 'label' => esc_html__( 'Last Year', 'stream' ), | |
| 110 | + 'start' => Carbon::today( $timezone )->startOfYear()->subYear(), | |
| 111 | + 'end' => Carbon::today( $timezone )->startOfYear()->subSecond(), | |
| 112 | + ), | |
| 81 | 113 | ), |
| 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 | - ), | |
| 114 | + $timezone | |
| 143 | 115 | ); |
| 144 | 116 | } |
| 145 | 117 | } |