PluginProbe
Stream – Activity Log & Audit Trail / 3.9.0
Stream – Activity Log & Audit Trail v3.9.0
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
stream / classes / class-date-interval.php

class-date-interval.php in Stream – Activity Log & Audit Trail 3.9.0, at classes/class-date-interval.php

129 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Calculate date intervals for a specific timezone.
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
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
16 /**
17 * Class - Date_Interval
18 */
19 class Date_Interval {
20 /**
21 * Contains an array of all available intervals
22 *
23 * @var array $intervals
24 */
25 public $intervals;
26
27 /**
28 * Class constructor
29 */
30 public function __construct() {
31 // Get all default intervals.
32 $this->intervals = $this->get_predefined_intervals();
33 }
34
35 /**
36 * Returns date interval data based upon "Timezone" site setting.
37 *
38 * @todo add better inline comments
39 * @return mixed
40 */
41 public function get_predefined_intervals() {
42 $timezone = get_option( 'timezone_string' );
43
44 if ( empty( $timezone ) ) {
45 $gmt_offset = (int) get_option( 'gmt_offset' );
46 $timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, true );
47 if ( false === $timezone ) {
48 $timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, false );
49 }
50 if ( false === $timezone ) {
51 $timezone = null;
52 }
53 }
54
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 ),
124 ),
125 $timezone
126 );
127 }
128 }
129