-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathclass-date-interval.php
More file actions
145 lines (136 loc) · 4.39 KB
/
class-date-interval.php
File metadata and controls
145 lines (136 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Calculate date intervals for a specific timezone.
*
* @package WP_Stream
*/
namespace WP_Stream;
/**
* Class - Date_Interval
*/
class Date_Interval {
/**
* Contains an array of all available intervals
*
* @var array $intervals
*/
public $intervals;
/**
* Class constructor
*/
public function __construct() {
// Get all default intervals.
$this->intervals = $this->get_predefined_intervals();
}
/**
* Returns date interval data based upon "Timezone" site setting.
*
* @todo add better inline comments
* @return mixed
*/
public function get_predefined_intervals() {
$timezone = get_option( 'timezone_string' );
if ( empty( $timezone ) ) {
$gmt_offset = (int) get_option( 'gmt_offset' );
$timezone = timezone_name_from_abbr( '', $gmt_offset * 3600, true );
if ( false === $timezone ) {
$timezone = timezone_name_from_abbr( '', $gmt_offset * 3600, false );
}
}
try {
$timezone_object = $timezone ? new \DateTimeZone( $timezone ) : null;
} catch ( \Exception $e ) {
$timezone_object = null;
}
try {
$today = new \DateTimeImmutable( 'today', $timezone_object );
$date_intervals = $this->generate_date_intervals( $today );
} catch ( \Exception $e ) {
$date_intervals = array();
}
/**
* Allow other plugins to filter the predefined date intervals.
*
* @param array $date_intervals Date intervals array.
* @param string $timezone Timezone.
*/
return apply_filters( 'wp_stream_predefined_date_intervals', $date_intervals, $timezone );
}
/**
* Generate date intervals relative to date object provided.
*
* @param \DateTimeImmutable $date Date object.
*
* @return array[]
*/
public function generate_date_intervals( \DateTimeImmutable $date ) {
return array(
'today' => array(
'label' => esc_html__( 'Today', 'stream' ),
'start' => $date,
'end' => $date->modify( '+1 day -1 microsecond' ),
),
'yesterday' => array(
'label' => esc_html__( 'Yesterday', 'stream' ),
'start' => $date->modify( '-1 day' ),
'end' => $date->modify( '-1 microsecond' ),
),
'last-7-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ),
'start' => $date->modify( '-7 days' ),
'end' => $date,
),
'last-14-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ),
'start' => $date->modify( '-14 days' ),
'end' => $date,
),
'last-30-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ),
'start' => $date->modify( '-30 days' ),
'end' => $date,
),
'this-month' => array(
'label' => esc_html__( 'This Month', 'stream' ),
'start' => $date->modify( 'first day of this month' ),
'end' => $date->modify( 'last day of this month' )->modify( '+1 day -1 microsecond' ),
),
'last-month' => array(
'label' => esc_html__( 'Last Month', 'stream' ),
'start' => $date->modify( 'first day of last month' ),
'end' => $date->modify( 'last day of last month' )->modify( '+1 day -1 microsecond' ),
),
'last-3-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ),
'start' => $date->modify( '-3 months' ),
'end' => $date,
),
'last-6-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ),
'start' => $date->modify( '-6 months' ),
'end' => $date,
),
'last-12-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ),
'start' => $date->modify( '-12 months' ),
'end' => $date,
),
'this-year' => array(
'label' => esc_html__( 'This Year', 'stream' ),
'start' => $date->modify( 'first day of January' ),
'end' => $date->modify( 'last day of December' )->modify( '+1 day -1 microsecond' ),
),
'last-year' => array(
'label' => esc_html__( 'Last Year', 'stream' ),
'start' => $date->modify( 'first day of January' )->modify( '-1 year' ),
'end' => $date->modify( 'first day of January' )->modify( '-1 microsecond' ),
),
);
}
}