File: /home/globfdxw/www/wp-content/plugins/wpforms-surveys-polls/src/Reporting/Datepicker.php
<?php
namespace WPFormsSurveys\Reporting;
/**
* Handles datepicker-related logic for addon admin area.
*
* Provides helpers to determine currently selected dates/ranges, active
* timespan keys, and available date filter choices for UI controls.
*
* @since 1.18.0
*/
class Datepicker {
/**
* Date range delimiter used when returning concatenated ranges.
*
* @since 1.18.0
*/
public const RANGE_DELIMITER = ' - ';
/**
* Selected timespan key (e.g., 'today', 'week_ago', 'custom').
*
* @since 1.18.0
*
* @var string
*/
private $timespan;
/**
* Selected date or date range string.
* Examples: "2025-09-12" or "2025-08-15 - 2025-09-12".
*
* @since 1.18.0
*
* @var string
*/
private $date;
/**
* Datepicker constructor.
*
* @since 1.18.0
*
* @param array $filters Filters array that may contain 'timespan' and 'date' keys.
*/
public function __construct( array $filters ) {
$this->timespan = $filters['timespan'] ?? '';
$this->date = $filters['date'] ?? '';
}
/**
* Get a currently active date or date range string for filtering.
*
* Returns an empty string for "all", a single date for shortcuts like
* "today" or "yesterday", a concatenated range for presets, or the
* custom date string provided by the user.
*
* @since 1.18.0
*
* @return string
*/
public function get_current_date(): string {
$ranges = $this->get_ranges();
$active_timespan = $this->get_active_timespan();
if ( $active_timespan !== 'custom' && isset( $ranges[ $active_timespan ] ) ) {
// active timespan range.
return $ranges[ $active_timespan ];
}
return (string) $this->date;
}
/**
* Build an array of predefined date values keyed by timespan.
*
* Keys include: all, today, yesterday, week_ago, month_ago, quarter_ago, year_ago.
* Values are empty string, a single date (Y-m-d), or a concatenated range
* of two dates using RANGE_DELIMITER.
*
* @since 1.18.0
*
* @return array
*/
public function get_ranges(): array {
$timezone = wp_timezone(); // Retrieve the timezone string for the site.
$current_date = date_create_immutable( 'now', $timezone )->setTime( 23, 59, 59 );
$today = $current_date->format( 'Y-m-d' );
return [
'all' => '',
'today' => $today,
'yesterday' => $current_date->modify( '-1 day' )->format( 'Y-m-d' ),
'week_ago' => $current_date->modify( '-7 days' )->format( 'Y-m-d' ) . self::RANGE_DELIMITER . $today,
'month_ago' => $current_date->modify( '-30 days' )->format( 'Y-m-d' ) . self::RANGE_DELIMITER . $today,
'quarter_ago' => $current_date->modify( '-90 days' )->format( 'Y-m-d' ) . self::RANGE_DELIMITER . $today,
'year_ago' => $current_date->modify( '-365 days' )->format( 'Y-m-d' ) . self::RANGE_DELIMITER . $today,
'custom' => '',
];
}
/**
* Determine the active timespan key based on the provided filters.
*
* @since 1.18.0
*
* @return string Timespan key such as 'all', 'today', 'week_ago', or 'custom'.
*/
public function get_active_timespan(): string {
if ( $this->timespan ) {
return (string) $this->timespan;
}
if ( empty( $this->date ) ) {
return 'all';
}
foreach ( $this->get_ranges() as $key => $range ) {
if ( ( isset( $range ) && $this->date === $range ) ) {
return $key;
}
}
return 'custom';
}
/**
* Get date choices.
*
* @since 1.18.0
*
* @return array
*/
public function get_date_choices(): array {
return [
'all' => __( 'All Time', 'wpforms-surveys-polls' ),
'today' => __( 'Today', 'wpforms-surveys-polls' ),
'yesterday' => __( 'Yesterday', 'wpforms-surveys-polls' ),
'week_ago' => __( 'Last 7 days', 'wpforms-surveys-polls' ),
'month_ago' => __( 'Last 30 days', 'wpforms-surveys-polls' ),
'quarter_ago' => __( 'Last 90 days', 'wpforms-surveys-polls' ),
'year_ago' => __( 'Last 1 year', 'wpforms-surveys-polls' ),
'custom' => __( 'Custom', 'wpforms-surveys-polls' ),
];
}
}