Display event results in iCal feed

This commit is contained in:
Brian Miyaji
2014-10-22 21:24:21 +11:00
parent 6e3f511597
commit 42876f326f
3 changed files with 53 additions and 9 deletions

View File

@@ -21,6 +21,9 @@ $events = $calendar->data();
// Get blog locale
$locale = substr( get_locale(), 0, 2 );
// Get main result setting
$main_result = get_option( 'sportspress_primary_result', null );
// Initialize output. Max line length is 75 chars.
$output =
"BEGIN:VCALENDAR\n" .
@@ -46,8 +49,8 @@ foreach ( $events as $event):
$end = new DateTime( $event->post_date_gmt );
// Get full time minutes
$minutes = get_post_meta( $event->post_id, 'sp_minutes', true );
if ( false === $minutes ) $minutes = get_option( 'sportspress_event_minutes', 90 );
$minutes = get_post_meta( $event->ID, 'sp_minutes', true );
if ( '' === $minutes ) $minutes = get_option( 'sportspress_event_minutes', 90 );
// Add full time minutes to end time
$end->add( new DateInterval( 'PT' . $minutes . 'M' ) );
@@ -81,10 +84,47 @@ foreach ( $events as $event):
}
}
// Get title or write summary based on scores
$results = array();
$teams = (array)get_post_meta( $event->ID, 'sp_team', false );
$teams = array_filter( $teams );
$teams = array_unique( $teams );
if ( ! empty( $teams ) ) {
$event_results = get_post_meta( $event->ID, 'sp_results', true );
foreach( $teams as $team_id ) {
if ( ! $team_id ) continue;
$team = get_post( $team_id );
if ( $team ) {
$team_results = sportspress_array_value( $event_results, $team_id, null );
if ( $main_result ) {
$team_result = sportspress_array_value( $team_results, $main_result, null );
} else {
if ( is_array( $team_results ) ) {
end( $team_results );
$team_result = prev( $team_results );
} else {
$team_result = null;
}
}
if ( $team_result != null ) {
$results[] = get_the_title( $team_id ) . ' ' . $team_result;
}
}
}
}
if ( sizeof( $results ) ) {
$summary = implode( ' ', $results );
} else {
$summary = $event->post_title;
}
// Append to output string
$output .=
"BEGIN:VEVENT\n" .
"SUMMARY:$event->post_title\n" .
"SUMMARY:" . $summary . "\n" .
"UID:$event->ID\n" .
"STATUS:CONFIRMED\n" .
"DTSTART:" . mysql2date( $date_format, $event->post_date_gmt ) . "\n" .
@@ -102,4 +142,9 @@ endforeach;
// End output
$output .= "END:VCALENDAR";
// Print headers
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename=' . $post->post_name . '.ics');
// Print content
echo $output;

View File

@@ -25,11 +25,11 @@ class SP_Meta_Box_Calendar_Feeds {
$calendar_feeds = $feeds->calendar;
?>
<div>
<?php foreach ( $calendar_feeds as $slug => $name ) { ?>
<?php $link = add_query_arg( 'feed', 'sp-calendar-' . $slug, get_post_permalink( $post ) ); ?>
<?php foreach ( $calendar_feeds as $feed => $name ) { ?>
<?php $link = str_replace( array( 'http:', 'https:' ), 'webcal:', add_query_arg( 'feed', 'sp-' . $feed, untrailingslashit( get_post_permalink( $post ) ) ) ); ?>
<p>
<strong><?php echo $name; ?></strong>
<a class="sp-link" href="<?php echo $link; ?>" target="_blank" title="<?php _e( 'Link', 'sportspress' ); ?>"></a>
<a class="sp-link" href="<?php echo $link; ?>" title="<?php _e( 'Link', 'sportspress' ); ?>"></a>
</p>
<p>
<input type="text" value="<?php echo $link; ?>" readonly="readonly" class="code widefat">

View File

@@ -31,7 +31,7 @@ class SP_Feeds {
foreach ( $data as $type => $feeds ) {
foreach ( $feeds as $slug => $name ) {
$this->feed = $slug;
add_feed( 'sp-' . $type . '-' . $slug, array( $this, 'load_' . $type . '_' . $slug . '_feed' ) );
add_feed( 'sp-' . $slug, array( $this, $slug . '_feed' ) );
}
}
}
@@ -44,9 +44,8 @@ class SP_Feeds {
$this->data[ $key ] = $value;
}
public static function load_calendar_ical_feed() {
public static function ical_feed() {
$feed_template = SP()->plugin_path() . '/feeds/ical.php';
load_template( $feed_template );
}
}