PluginProbe
Stream – Activity Log & Audit Trail / 4.0.2
Stream – Activity Log & Audit Trail v4.0.2
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
← All changes | classes/class-date-interval.php +112 -78 3.0.54.0.2 View file →
@@ -1,14 +1,16 @@
1 1 <?php
2 +/**
3 + * Calculate date intervals for a specific timezone.
4 + *
5 + * @package WP_Stream
6 + */
7 +
2 8 namespace WP_Stream;
3 9
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 -}
8 -
9 -use Carbon\Carbon;
10 -
10 +/**
11 + * Class - Date_Interval
12 + */
11 13 class Date_Interval {
12 14 /**
13 15 * Contains an array of all available intervals
14 16 *
@@ -19,14 +21,17 @@
19 21 /**
20 22 * Class constructor
21 23 */
22 24 public function __construct() {
23 - // Get all default intervals
25 + // Get all default intervals.
24 26 $this->intervals = $this->get_predefined_intervals();
25 27 }
26 28
27 29 /**
28 - * @return mixed|void
30 + * Returns date interval data based upon "Timezone" site setting.
31 + *
32 + * @todo add better inline comments
33 + * @return mixed
29 34 */
30 35 public function get_predefined_intervals() {
31 36 $timezone = get_option( 'timezone_string' );
32 37
@@ -31,81 +36,110 @@
31 36 $timezone = get_option( 'timezone_string' );
32 37
33 38 if ( empty( $timezone ) ) {
34 39 $gmt_offset = (int) get_option( 'gmt_offset' );
35 - $timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, true );
40 + $timezone = timezone_name_from_abbr( '', $gmt_offset * 3600, true );
36 41 if ( false === $timezone ) {
37 - $timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, false );
42 + $timezone = timezone_name_from_abbr( '', $gmt_offset * 3600, false );
38 43 }
39 - if ( false === $timezone ) {
40 - $timezone = null;
41 - }
42 44 }
43 45
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 - 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ),
59 - 'start' => Carbon::today( $timezone )->subDays( 7 ),
60 - 'end' => Carbon::today( $timezone ),
61 - ),
62 - 'last-14-days' => array(
63 - 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ),
64 - 'start' => Carbon::today( $timezone )->subDays( 14 ),
65 - 'end' => Carbon::today( $timezone ),
66 - ),
67 - 'last-30-days' => array(
68 - 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ),
69 - 'start' => Carbon::today( $timezone )->subDays( 30 ),
70 - 'end' => Carbon::today( $timezone ),
71 - ),
72 - 'this-month' => array(
73 - 'label' => esc_html__( 'This Month', 'stream' ),
74 - 'start' => Carbon::today( $timezone )->startOfMonth(),
75 - 'end' => Carbon::today( $timezone )->endOfMonth(),
76 - ),
77 - 'last-month' => array(
78 - 'label' => esc_html__( 'Last Month', 'stream' ),
79 - 'start' => Carbon::today( $timezone )->startOfMonth()->subMonth(),
80 - 'end' => Carbon::today( $timezone )->startOfMonth()->subSecond(),
81 - ),
82 - 'last-3-months' => array(
83 - 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ),
84 - 'start' => Carbon::today( $timezone )->subMonths( 3 ),
85 - 'end' => Carbon::today( $timezone ),
86 - ),
87 - 'last-6-months' => array(
88 - 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ),
89 - 'start' => Carbon::today( $timezone )->subMonths( 6 ),
90 - 'end' => Carbon::today( $timezone ),
91 - ),
92 - 'last-12-months' => array(
93 - 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ),
94 - 'start' => Carbon::today( $timezone )->subMonths( 12 ),
95 - 'end' => Carbon::today( $timezone ),
96 - ),
97 - 'this-year' => array(
98 - 'label' => esc_html__( 'This Year', 'stream' ),
99 - 'start' => Carbon::today( $timezone )->startOfYear(),
100 - 'end' => Carbon::today( $timezone )->endOfYear(),
101 - ),
102 - 'last-year' => array(
103 - 'label' => esc_html__( 'Last Year', 'stream' ),
104 - 'start' => Carbon::today( $timezone )->startOfYear()->subYear(),
105 - 'end' => Carbon::today( $timezone )->startOfYear()->subSecond(),
106 - ),
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' ),
107 81 ),
108 - $timezone
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 + ),
109 143 );
110 144 }
111 145 }