Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #320 - Date Interval Functionality #325

Merged
merged 4 commits into from
Mar 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions includes/date-interval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

// Load Carbon to Handle dates much easier
if ( ! class_exists( 'Carbon\Carbon' ) ) {
require_once WP_STREAM_INC_DIR . 'vendor/Carbon.php';
}

use Carbon\Carbon;

class WP_Stream_Date_Interval {

/**
* Contains an array of all available intervals
*
* @var array $intervals
*/
public $intervals;

/**
* Class constructor
*/
public function __construct() {

// Filter the Predefined list of intervals to make it work
add_filter( 'wp_stream_predefined_date_intervals', array( $this, 'filter_predefined_intervals' ), 20 );

// Get all default intervals
$this->intervals = $this->get_predefined_intervals();

}

/**
* @return mixed|void
*/
public function get_predefined_intervals() {
return apply_filters(
'wp_stream_predefined_date_intervals',
array(
'today' => array(
'label' => esc_html__( 'Today', 'stream' ),
'start' => Carbon::today(),
),
'yesterday' => array(
'label' => esc_html__( 'Yesterday', 'stream' ),
'start' => Carbon::today()->subDay(),
'end' => Carbon::today()->subSecond(),
),
'last-7-days' => array(
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ),
'start' => Carbon::today()->subDays( 7 ),
'end' => Carbon::today(),
),
'last-14-days' => array(
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ),
'start' => Carbon::today()->subDays( 14 ),
'end' => Carbon::today(),
),
'last-30-days' => array(
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ),
'start' => Carbon::today()->subDays( 30 ),
'end' => Carbon::today(),
),
'this-month' => array(
'label' => esc_html__( 'This Month', 'stream' ),
'start' => Carbon::today()->day( 1 ),
),
'last-month' => array(
'label' => esc_html__( 'Last Month', 'stream' ),
'start' => Carbon::today()->day( 1 )->subMonth(),
'end' => Carbon::today()->day( 1 )->subSecond(),
),
'last-3-months' => array(
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ),
'start' => Carbon::today()->subMonths( 3 ),
'end' => Carbon::today(),
),
'last-6-months' => array(
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ),
'start' => Carbon::today()->subMonths( 6 ),
'end' => Carbon::today(),
),
'last-12-months' => array(
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ),
'start' => Carbon::today()->subMonths( 12 ),
'end' => Carbon::today(),
),
'this-year' => array(
'label' => esc_html__( 'This Year', 'stream' ),
'start' => Carbon::today()->day( 1 )->month( 1 ),
),
'last-year' => array(
'label' => esc_html__( 'Last Year', 'stream' ),
'start' => Carbon::today()->day( 1 )->month( 1 )->subYear(),
'end' => Carbon::today()->day( 1 )->month( 1 )->subSecond(),
)
)
);
}

/**
* Filter the predefined intervals to reflect db oldest value
* @param $intervals
*
* @return array
*/
public function filter_predefined_intervals( $intervals ) {
$query = stream_query(
array(
'order' => 'ASC',
'orderby' => 'created',
'records_per_page' => 1,
'ignore_context' => true,
)
);

$first_stream_item = reset( $query );

if ( $first_stream_item === false ){
return false;
}

$first_stream_date = \Carbon\Carbon::parse( $first_stream_item->created );

foreach ( $intervals as $key => $interval ){
if ( ! isset( $interval['start'] ) || $interval['start'] === false ){
$intervals[$key]['start'] = $interval['start'] = $first_stream_date;
}
if ( ! isset( $interval['end'] ) || $interval['end'] === false ){
$intervals[$key]['end'] = $interval['end'] = \Carbon\Carbon::now();
}

if ( ! is_a( $interval['start'], '\Carbon\Carbon' ) || ! is_a( $interval['end'], '\Carbon\Carbon' ) ) {
unset( $intervals[$key] );
continue;
}
}

return $intervals;
}

}
64 changes: 51 additions & 13 deletions includes/list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,25 +528,63 @@ function filter_search() {
}

function filter_date() {

require_once WP_STREAM_INC_DIR . 'date-interval.php';

wp_enqueue_style( 'jquery-ui' );
wp_enqueue_style( 'wp-stream-datepicker' );

wp_enqueue_script( 'jquery-ui-datepicker' );

$out = sprintf(
'<div id="filter-date-range">
<label class="screen-reader-text" for="date_from">%1$s:</label>
<input type="text" name="date_from" id="date_from" class="date-picker" placeholder="%1$s" size="14" value="%2$s" />
<label class="screen-reader-text" for="date_to">%3$s:</label>
<input type="text" name="date_to" id="date_to" class="date-picker" placeholder="%3$s" size="14" value="%4$s" />
</div>',
esc_attr__( 'Start date', 'stream' ),
isset( $_GET['date_from'] ) ? esc_attr( $_GET['date_from'] ) : null,
esc_attr__( 'End date', 'stream' ),
isset( $_GET['date_to'] ) ? esc_attr( $_GET['date_to'] ) : null
);
$date_interval = new WP_Stream_Date_Interval();

return $out;
$date_predefined = wp_stream_filter_input( INPUT_GET, 'date_predefined' );
$date_from = wp_stream_filter_input( INPUT_GET, 'date_from' );
$date_to = wp_stream_filter_input( INPUT_GET, 'date_to' );

ob_start();
?>
<div class="date-interval">

<select class="field-predefined hide-if-no-js" name="date_predefined" data-placeholder="<?php _e( 'All Time', 'stream' ); ?>">
<option></option>
<option value="custom" <?php selected( 'custom' === $date_predefined ); ?>><?php esc_attr_e( 'Custom', 'stream' ) ?></option>
<?php foreach ( $date_interval->intervals as $key => $interval ) {
echo sprintf(
'<option value="%s" data-from="%s" data-to="%s" %s>%s</option>',
esc_attr( $key ),
esc_attr( $interval['start']->format( 'Y/m/d' ) ),
esc_attr( $interval['end']->format( 'Y/m/d' ) ),
selected( $key === $date_predefined ),
esc_html( $interval['label'] )
); // xss ok
} ?>
</select>

<div class="date-inputs">
<div class="box">
<i class="date-remove dashicons"></i>
<input type="text"
name="date_from"
class="date-picker field-from"
placeholder="<?php esc_attr_e( 'Start date', 'stream' ) ?>"
value="<?php echo esc_attr( $date_from ) ?>">
</div>
<span class="connector dashicons"></span>
<div class="box">
<i class="date-remove dashicons"></i>
<input type="text"
name="date_to"
class="date-picker field-to"
placeholder="<?php esc_attr_e( 'End date', 'stream' ) ?>"
value="<?php echo esc_attr( $date_to ) ?>">
</div>
</div>

</div>
<?php

return ob_get_clean();
}

function display() {
Expand Down
Loading