• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xwp / stream / #6024

16 Jul 2024 11:07AM UTC coverage: 45.942%. First build
#6024

Pull #1494

travis-ci

Pull Request #1494: Fix more PHP 8.1 compatibility issues

37 of 49 new or added lines in 5 files covered. (75.51%)

3923 of 8539 relevant lines covered (45.94%)

4.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.2
/classes/class-date-interval.php
1
<?php
2
/**
3
 * Calculate date intervals for a specific timezone.
4
 *
5
 * @package WP_Stream
6
 */
7

8
namespace WP_Stream;
9

10
/**
11
 * Class - Date_Interval
12
 */
13
class Date_Interval {
14
        /**
15
         * Contains an array of all available intervals
16
         *
17
         * @var array $intervals
18
         */
19
        public $intervals;
20

21
        /**
22
         * Class constructor
23
         */
24
        public function __construct() {
25
                // Get all default intervals.
26
                $this->intervals = $this->get_predefined_intervals();
3✔
27
        }
3✔
28

29
        /**
30
         * Returns date interval data based upon "Timezone" site setting.
31
         *
32
         * @todo add better inline comments
33
         * @return mixed
34
         */
35
        public function get_predefined_intervals() {
36
                $timezone = get_option( 'timezone_string' );
3✔
37

38
                if ( empty( $timezone ) ) {
3✔
39
                        $gmt_offset = (int) get_option( 'gmt_offset' );
3✔
40
                        $timezone   = timezone_name_from_abbr( '', $gmt_offset * 3600, true );
3✔
41
                        if ( false === $timezone ) {
3✔
NEW
42
                                $timezone = timezone_name_from_abbr( '', $gmt_offset * 3600, false );
×
43
                        }
44
                }
45

46
                try {
47
                        $timezone_object = $timezone ? new \DateTimeZone( $timezone ) : null;
3✔
NEW
48
                } catch ( \Exception $e ) {
×
NEW
49
                        $timezone_object = null;
×
50
                }
51

52
                try {
53
                        $today          = new \DateTimeImmutable( 'today', $timezone_object );
3✔
54
                        $date_intervals = $this->generate_date_intervals( $today );
55
                } catch ( \Exception $e ) {
56
                        $date_intervals = array();
3✔
57
                }
3✔
58

3✔
59
                /**
60
                 * Allow other plugins to filter the predefined date intervals.
61
                 *
3✔
62
                 * @param array  $date_intervals Date intervals array.
3✔
63
                 * @param string $timezone       Timezone.
3✔
64
                 */
65
                return apply_filters( 'wp_stream_predefined_date_intervals', $date_intervals, $timezone );
66
        }
67

3✔
68
        /**
3✔
69
         * Generate date intervals relative to date object provided.
3✔
70
         *
71
         * @param \DateTimeImmutable $date Date object.
72
         *
73
         * @return array[]
3✔
74
         */
3✔
75
        public function generate_date_intervals( \DateTimeImmutable $date ) {
3✔
76
                return array(
77
                        'today'          => array(
78
                                'label' => esc_html__( 'Today', 'stream' ),
79
                                'start' => $date,
3✔
80
                                'end'   => $date->modify( '+1 day -1 microsecond' ),
3✔
81
                        ),
3✔
82
                        'yesterday'      => array(
83
                                'label' => esc_html__( 'Yesterday', 'stream' ),
84
                                'start' => $date->modify( '-1 day' ),
3✔
85
                                'end'   => $date->modify( '-1 microsecond' ),
3✔
86
                        ),
3✔
87
                        'last-7-days'    => array(
88
                                /* translators: %d: number of days (e.g. "7") */
89
                                'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ),
3✔
90
                                'start' => $date->modify( '-7 days' ),
3✔
91
                                'end'   => $date,
3✔
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 ),
3✔
96
                                'start' => $date->modify( '-14 days' ),
3✔
97
                                'end'   => $date,
3✔
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 ),
3✔
102
                                'start' => $date->modify( '-30 days' ),
3✔
103
                                'end'   => $date,
3✔
104
                        ),
105
                        'this-month'     => array(
106
                                'label' => esc_html__( 'This Month', 'stream' ),
107
                                'start' => $date->modify( 'first day of this month' ),
3✔
108
                                'end'   => $date->modify( 'last day of this month' )->modify( '+1 day -1 microsecond' ),
3✔
109
                        ),
3✔
110
                        'last-month'     => array(
111
                                'label' => esc_html__( 'Last Month', 'stream' ),
112
                                'start' => $date->modify( 'first day of last month' ),
3✔
113
                                'end'   => $date->modify( 'last day of last month' )->modify( '+1 day -1 microsecond' ),
3✔
114
                        ),
3✔
115
                        'last-3-months'  => array(
116
                                /* translators: %d: number of months (e.g. "3") */
117
                                'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ),
3✔
118
                                'start' => $date->modify( '-3 months' ),
3✔
119
                                'end'   => $date,
3✔
120
                        ),
121
                        'last-6-months'  => array(
NEW
122
                                /* translators: %d: number of months (e.g. "3") */
×
NEW
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
                        ),
3✔
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
                        ),
143
                );
144
        }
145
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc