Skip to content

Commit aeae0c3

Browse files
committed
feat: new show_past argument for list shortcode
1 parent 6238fc5 commit aeae0c3

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

includes/shortcodes/wp-zoom-list-shortcode.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
function wp_zoom_list_shortcode( $atts, $content = '' ) {
1616
$atts = shortcode_atts(
1717
array(
18-
'type' => 'webinars',
19-
'per_page' => 20,
18+
'type' => 'webinars',
19+
'per_page' => 20,
20+
'show_past' => 0,
2021
),
2122
$atts
2223
);
@@ -27,7 +28,7 @@ function wp_zoom_list_shortcode( $atts, $content = '' ) {
2728

2829
$page = get_query_var( 'paged', 1 );
2930
$per_page = intval( $atts['per_page'] );
30-
$data = wp_zoom_get_occurrences( $atts['type'] );
31+
$data = wp_zoom_get_occurrences( $atts['type'], (bool) $atts['show_past'] );
3132
$total = count( $data );
3233

3334
if ( $page < 2 ) {

includes/wp-zoom-ajax.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function wp_zoom_ajax_get_calendar_webinars() {
4848
$response[] = array(
4949
'id' => $webinar['id'],
5050
'start' => $webinar['start_time'],
51-
'end' => wp_zoom_format_get_end_date_time( $webinar['start_time'], $webinar['duration'] ),
51+
'end' => wp_zoom_format_end_date_time( $webinar['start_time'], $webinar['duration'] ),
5252
'title' => $webinar['topic'],
5353
'url' => $purchase_url,
5454
);
@@ -63,7 +63,7 @@ function wp_zoom_ajax_get_calendar_webinars() {
6363
$response[] = array(
6464
'id' => $occurrence['occurrence_id'],
6565
'start' => $occurrence['start_time'],
66-
'end' => wp_zoom_format_get_end_date_time( $occurrence['start_time'], $occurrence['duration'] ),
66+
'end' => wp_zoom_format_end_date_time( $occurrence['start_time'], $occurrence['duration'] ),
6767
'title' => $webinar['topic'],
6868
'url' => $purchase_url,
6969
);

includes/wp-zoom-helper-functions.php

+30-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77

88
use SeattleWebCo\WPZoom\Cache;
99

10+
/**
11+
* Return a DateTime object with the GMT end time
12+
*
13+
* @param string $datetime Date / time to get end.
14+
* @param integer $duration Duration in minutes.
15+
* @return DateTime
16+
*/
17+
function wp_zoom_end_date_time( string $datetime, int $duration ) {
18+
$gmt_timezone = new DateTimeZone( 'GMT' );
19+
$gmt_datetime = new DateTime( trim( $datetime, 'Z' ), $gmt_timezone );
20+
$gmt_datetime->add( DateInterval::createFromDateString( (string) $duration . 'minutes' ) );
21+
22+
return $gmt_datetime;
23+
}
24+
1025
/**
1126
* Format date / time string
1227
*
@@ -37,7 +52,7 @@ function wp_zoom_format_date_time( string $datetime, string $timezone = '', stri
3752
* @param integer $duration Duration in minutes.
3853
* @return string
3954
*/
40-
function wp_zoom_format_get_end_date_time( string $datetime, int $duration ) {
55+
function wp_zoom_format_end_date_time( string $datetime, int $duration ) {
4156
$gmt_timezone = new DateTimeZone( 'GMT' );
4257

4358
$gmt_datetime = new DateTime( trim( $datetime, 'Z' ), $gmt_timezone );
@@ -146,10 +161,11 @@ function( &$webinar ) use ( $wp_zoom ) {
146161
/**
147162
* Get all occurrences of meetings or webinars merged into an array
148163
*
149-
* @param string $type Either webinars or meetings.
164+
* @param string $type Either webinars or meetings.
165+
* @param boolean $show_past Whether to show occurrences that have ended.
150166
* @return array
151167
*/
152-
function wp_zoom_get_occurrences( $type = 'webinars' ) {
168+
function wp_zoom_get_occurrences( $type = 'webinars', $show_past = false ) {
153169
global $wp_zoom;
154170

155171
$occurrences = array();
@@ -180,6 +196,17 @@ function( $a, $b ) {
180196
}
181197
);
182198

199+
if ( ! $show_past ) {
200+
$occurrences = array_filter(
201+
$occurrences,
202+
function( $occurrence ) {
203+
$end = wp_zoom_end_date_time( (string) $occurrence['start_time'], (int) $occurrence['duration'] );
204+
205+
return $end->getTimestamp() > time();
206+
}
207+
);
208+
}
209+
183210
return $occurrences;
184211
}
185212

0 commit comments

Comments
 (0)