| 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(); |
| 27 |
} |
| 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' ); |
| 37 |
|
| 38 |
if ( empty( $timezone ) ) { |
| 39 |
$gmt_offset = (int) get_option( 'gmt_offset' ); |
| 40 |
$timezone = timezone_name_from_abbr( '', $gmt_offset * 3600, true ); |
| 41 |
if ( false === $timezone ) { |
| 42 |
$timezone = timezone_name_from_abbr( '', $gmt_offset * 3600, false ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 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' ), |
| 81 |
), |
| 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 |
), |
| 143 |
); |
| 144 |
} |
| 145 |
} |
| 146 |
|