Add iCal feeds to calendars close #62

This commit is contained in:
Brian Miyaji
2014-10-22 18:53:10 +11:00
parent 86ebb587ae
commit 453e212b12
5 changed files with 204 additions and 1 deletions

105
feeds/ical.php Normal file
View File

@@ -0,0 +1,105 @@
<?php
/**
* iCal Feed
*
* @author ThemeBoy
* @category Feeds
* @package SportsPress/Feeds
* @version 1.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( 'sp_calendar' !== get_post_type( $post ) ) {
wp_die( __( 'ERROR: This is not a valid feed template.', 'sportspress' ), '', array( 'response' => 404 ) );
}
// Get events in calendar
$calendar = new SP_Calendar( $post );
$events = $calendar->data();
// Get blog locale
$locale = substr( get_locale(), 0, 2 );
// Initialize output. Max line length is 75 chars.
$output =
"BEGIN:VCALENDAR\n" .
"METHOD:PUBLISH\n" .
"VERSION:2.0\n" .
"URL:" . add_query_arg( 'feed', 'sp-calendar-ical', get_post_permalink( $post ) ) . "\n" .
"NAME:" . $post->post_title . "\n" .
"X-WR-CALNAME:" . $post->post_title . "\n" .
"DESCRIPTION:" . $post->post_title . "\n" .
"DESCRIPTION:" . $post->post_title . "\n" .
"X-WR-CALDESC:" . $post->post_title . "\n" .
"REFRESH-INTERVAL;VALUE=DURATION:PT1H\n" .
"X-PUBLISHED-TTL:PT1H\n" .
"PRODID:-//ThemeBoy//SportsPress//" . strtoupper( $locale ) . "\n";
// Loop through each event
foreach ( $events as $event):
// Define date format
$date_format = 'Ymd\THis\Z';
// Initialize end time
$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 );
// Add full time minutes to end time
$end->add( new DateInterval( 'PT' . $minutes . 'M' ) );
// Initialize location
$location = '';
// Get venue information
$venues = get_the_terms( $event->ID, 'sp_venue' );
if ( $venues ) {
$venue = reset( $venues );
$location .= $venue->name;
// Get venue term meta
$t_id = $venue->term_id;
$meta = get_option( "taxonomy_$t_id" );
// Add details to location
$address = sp_array_value( $meta, 'sp_address', false );
if ( false !== $address ) {
$location = $address;
}
// Generate geo tag
$latitude = sp_array_value( $meta, 'sp_latitude', false );
$longitude = sp_array_value( $meta, 'sp_longitude', false );
if ( false !== $latitude && false !== $longitude ) {
$geo = $latitude . ';' . $longitude;
} else {
$geo = false;
}
}
// Append to output string
$output .=
"BEGIN:VEVENT\n" .
"SUMMARY:$event->post_title\n" .
"UID:$event->ID\n" .
"STATUS:CONFIRMED\n" .
"DTSTART:" . mysql2date( $date_format, $event->post_date_gmt ) . "\n" .
"DTEND:" . $end->format( $date_format ) . "\n" .
"LAST-MODIFIED:" . mysql2date( $date_format, $event->post_modified_gmt ) . "\n" .
"LOCATION:" . $location . "\n";
if ( false !== $geo ) {
$output .= "GEO:" . $geo . "\n";
}
$output .= "END:VEVENT\n";
endforeach;
// End output
$output .= "END:VCALENDAR";
echo $output;

View File

@@ -131,6 +131,9 @@ class SP_Admin_Meta_Boxes {
// Calendars // Calendars
add_meta_box( 'sp_shortcodediv', __( 'Shortcode', 'sportspress' ), 'SP_Meta_Box_Calendar_Shortcode::output', 'sp_calendar', 'side', 'default' ); add_meta_box( 'sp_shortcodediv', __( 'Shortcode', 'sportspress' ), 'SP_Meta_Box_Calendar_Shortcode::output', 'sp_calendar', 'side', 'default' );
if ( isset( $post ) && 'publish' == $post->post_status ):
add_meta_box( 'sp_feedsdiv', __( 'Feeds', 'sportspress' ), 'SP_Meta_Box_Calendar_Feeds::output', 'sp_calendar', 'side', 'default' );
endif;
add_meta_box( 'sp_formatdiv', __( 'Layout', 'sportspress' ), 'SP_Meta_Box_Calendar_Format::output', 'sp_calendar', 'side', 'default' ); add_meta_box( 'sp_formatdiv', __( 'Layout', 'sportspress' ), 'SP_Meta_Box_Calendar_Format::output', 'sp_calendar', 'side', 'default' );
add_meta_box( 'sp_detailsdiv', __( 'Details', 'sportspress' ), 'SP_Meta_Box_Calendar_Details::output', 'sp_calendar', 'side', 'default' ); add_meta_box( 'sp_detailsdiv', __( 'Details', 'sportspress' ), 'SP_Meta_Box_Calendar_Details::output', 'sp_calendar', 'side', 'default' );
add_meta_box( 'sp_datadiv', __( 'Events', 'sportspress' ), 'SP_Meta_Box_Calendar_Data::output', 'sp_calendar', 'normal', 'high' ); add_meta_box( 'sp_datadiv', __( 'Events', 'sportspress' ), 'SP_Meta_Box_Calendar_Data::output', 'sp_calendar', 'normal', 'high' );

View File

@@ -0,0 +1,41 @@
<?php
/**
* Calendar Feeds
*
* Based on a tutorial by Steve Thomas.
*
* @author ThemeBoy
* @category Admin
* @package SportsPress/Admin/Meta_Boxes
* @version 1.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* SP_Meta_Box_Calendar_Feeds
*/
class SP_Meta_Box_Calendar_Feeds {
/**
* Output the metabox
*/
public static function output( $post ) {
$feeds = new SP_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 ) ); ?>
<p>
<strong><?php echo $name; ?></strong>
<a class="sp-link" href="<?php echo $link; ?>" target="_blank" title="<?php _e( 'Link', 'sportspress' ); ?>"></a>
</p>
<p>
<input type="text" value="<?php echo $link; ?>" readonly="readonly" class="code widefat">
</p>
<?php } ?>
</div>
<?php
}
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* SportsPress Feeds Class
*
* @class SP_Feeds
* @version 1.4
* @package SportsPress/Classes
* @category Class
* @author ThemeBoy
*/
class SP_Feeds {
/** @var array Array of feeds */
private $data;
/**
* Constructor for the feeds class - defines all preset feeds.
*
* @access public
* @return void
*/
public function __construct() {
$data = array(
'calendar' => array(
'ical' => __( 'iCal', 'sportspress' ),
),
);
$this->data = apply_filters( 'sportspress_feeds', $data );
foreach ( $data as $type => $feeds ) {
foreach ( $feeds as $slug => $name ) {
$this->feed = $slug;
add_feed( 'sp-' . $type . '-' . $slug, array( $this, 'load_' . $type . '_' . $slug . '_feed' ) );
}
}
}
public function __get( $key ) {
return ( array_key_exists( $key, $this->data ) ? $this->data[ $key ] : null );
}
public function __set( $key, $value ){
$this->data[ $key ] = $value;
}
public static function load_calendar_ical_feed() {
$feed_template = SP()->plugin_path() . '/feeds/ical.php';
load_template( $feed_template );
}
}

View File

@@ -214,6 +214,7 @@ final class SportsPress {
// Classes (used on all pages) // Classes (used on all pages)
include_once( 'includes/class-sp-countries.php' ); // Defines continents and countries include_once( 'includes/class-sp-countries.php' ); // Defines continents and countries
include_once( 'includes/class-sp-formats.php' ); // Defines custom post type formats include_once( 'includes/class-sp-formats.php' ); // Defines custom post type formats
include_once( 'includes/class-sp-feeds.php' ); // Adds feeds
// Include template functions making them pluggable by plugins and themes. // Include template functions making them pluggable by plugins and themes.
include_once( 'includes/sp-template-functions.php' ); include_once( 'includes/sp-template-functions.php' );
@@ -269,6 +270,7 @@ final class SportsPress {
// Load class instances // Load class instances
$this->countries = new SP_Countries(); // Countries class $this->countries = new SP_Countries(); // Countries class
$this->formats = new SP_Formats(); // Formats class $this->formats = new SP_Formats(); // Formats class
$this->feeds = new SP_Feeds(); // Feeds class
// Load string options // Load string options
$this->text = get_option( 'sportspress_text', array() ); $this->text = get_option( 'sportspress_text', array() );
@@ -297,7 +299,7 @@ final class SportsPress {
add_theme_support( 'post-thumbnails' ); add_theme_support( 'post-thumbnails' );
// Add image sizes // Add image sizes
add_image_size( 'sportspress-fit-thumbnail', 320, 320, false ); add_image_size( 'sportspress-fit-medium', 300, 300, false );
add_image_size( 'sportspress-fit-icon', 128, 128, false ); add_image_size( 'sportspress-fit-icon', 128, 128, false );
add_image_size( 'sportspress-fit-mini', 32, 32, false ); add_image_size( 'sportspress-fit-mini', 32, 32, false );
} }