Refactor schedule exports into feed builders

This commit is contained in:
2026-04-01 18:20:56 -05:00
parent 894109ae6c
commit bfc74fcab6
6 changed files with 1962 additions and 534 deletions

View File

@@ -10,256 +10,8 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* Register the SportsPress calendar CSV feed endpoint.
*
* @return void
* SportsPress event CSV importer tools.
*/
function tse_sp_register_calendar_csv_feed() {
add_feed( 'sp-csv', 'tse_sp_render_calendar_csv_feed' );
}
add_action( 'init', 'tse_sp_register_calendar_csv_feed', 20 );
/**
* Replace the stock SportsPress calendar feeds metabox with one that includes CSV.
*
* @return void
*/
function tse_sp_replace_calendar_feeds_metabox() {
remove_meta_box( 'sp_feedsdiv', 'sp_calendar', 'side' );
add_meta_box(
'sp_feedsdiv',
esc_attr__( 'Feeds', 'sportspress' ),
'tse_sp_render_calendar_feeds_metabox',
'sp_calendar',
'side',
'default'
);
}
add_action( 'add_meta_boxes_sp_calendar', 'tse_sp_replace_calendar_feeds_metabox', 40 );
/**
* Return the CSV feed format definition used in the metabox.
*
* @return array
*/
function tse_sp_get_calendar_csv_feed_formats() {
return array(
'download' => array(
'name' => __( 'CSV Download', 'tonys-sportspress-enhancements' ),
),
);
}
/**
* Render the calendar feeds metabox with CSV support.
*
* @param WP_Post $post Current calendar post.
* @return void
*/
function tse_sp_render_calendar_feeds_metabox( $post ) {
$feeds = new SP_Feeds();
$calendar_feeds = is_array( $feeds->calendar ) ? $feeds->calendar : array();
$calendar_feeds['csv'] = tse_sp_get_calendar_csv_feed_formats();
?>
<div>
<?php foreach ( $calendar_feeds as $slug => $formats ) : ?>
<?php
if ( 'csv' === $slug ) {
$link = tse_sp_get_calendar_csv_url( $post->ID );
} else {
$link = add_query_arg( 'feed', 'sp-' . $slug, untrailingslashit( get_post_permalink( $post ) ) );
}
?>
<?php foreach ( $formats as $format ) : ?>
<?php
if ( 'csv' === $slug ) {
$feed = $link;
} else {
$protocol = sp_array_value( $format, 'protocol' );
if ( $protocol ) {
$feed = str_replace( array( 'http:', 'https:' ), 'webcal:', $link );
} else {
$feed = $link;
}
$prefix = sp_array_value( $format, 'prefix' );
if ( $prefix ) {
$feed = $prefix . urlencode( $feed );
}
}
?>
<p>
<strong><?php echo esc_html( sp_array_value( $format, 'name' ) ); ?></strong>
<a class="sp-link" href="<?php echo esc_url( $feed ); ?>" target="_blank" title="<?php esc_attr_e( 'Link', 'sportspress' ); ?>"></a>
</p>
<p>
<input type="text" value="<?php echo esc_attr( $feed ); ?>" readonly="readonly" class="code widefat">
</p>
<?php if ( 'csv' === $slug ) : ?>
<p class="description">
<?php esc_html_e( 'Optional team filter: add &team_id=123 to only include games for that team.', 'tonys-sportspress-enhancements' ); ?>
</p>
<?php endif; ?>
<?php endforeach; ?>
<?php endforeach; ?>
</div>
<?php
}
/**
* Build the CSV feed URL for a SportsPress calendar.
*
* @param int $calendar_id SportsPress calendar post ID.
* @param int $team_id Optional team ID filter.
* @return string
*/
function tse_sp_get_calendar_csv_url( $calendar_id, $team_id = 0 ) {
$calendar_id = absint( $calendar_id );
$team_id = absint( $team_id );
if ( ! $calendar_id || 'sp_calendar' !== get_post_type( $calendar_id ) ) {
return '';
}
$url = add_query_arg( 'feed', 'sp-csv', get_post_permalink( $calendar_id ) );
if ( $team_id ) {
$url = add_query_arg( 'team_id', $team_id, $url );
}
return $url;
}
/**
* Get the queried SportsPress calendar post for the CSV feed.
*
* @return WP_Post|null
*/
function tse_sp_get_calendar_csv_post() {
$post = get_post();
if ( $post instanceof WP_Post && 'sp_calendar' === $post->post_type ) {
return $post;
}
$queried_object = get_queried_object();
if ( $queried_object instanceof WP_Post && 'sp_calendar' === $queried_object->post_type ) {
return $queried_object;
}
$calendar_id = get_queried_object_id();
if ( $calendar_id && 'sp_calendar' === get_post_type( $calendar_id ) ) {
return get_post( $calendar_id );
}
return null;
}
/**
* Return the home and away teams for an event in stored order.
*
* @param int $event_id SportsPress event ID.
* @return array
*/
function tse_sp_get_event_home_away_teams( $event_id ) {
$teams = array_values( array_filter( array_map( 'absint', get_post_meta( $event_id, 'sp_team', false ) ) ) );
return array(
'home' => isset( $teams[0] ) ? get_the_title( $teams[0] ) : '',
'away' => isset( $teams[1] ) ? get_the_title( $teams[1] ) : '',
);
}
/**
* Return the field name(s) for an event.
*
* @param int $event_id SportsPress event ID.
* @return string
*/
function tse_sp_get_event_field_name( $event_id ) {
$venues = get_the_terms( $event_id, 'sp_venue' );
if ( empty( $venues ) || is_wp_error( $venues ) ) {
return '';
}
return implode( ', ', wp_list_pluck( $venues, 'name' ) );
}
/**
* Render the SportsPress calendar CSV feed.
*
* @return void
*/
function tse_sp_render_calendar_csv_feed() {
if ( ! class_exists( 'SP_Calendar' ) ) {
wp_die( esc_html__( 'ERROR: SportsPress is required for this feed.', 'tonys-sportspress-enhancements' ), '', array( 'response' => 500 ) );
}
$calendar = tse_sp_get_calendar_csv_post();
if ( ! $calendar ) {
wp_die( esc_html__( 'ERROR: This is not a valid calendar feed.', 'tonys-sportspress-enhancements' ), '', array( 'response' => 404 ) );
}
$team_id = isset( $_GET['team_id'] ) ? absint( wp_unslash( $_GET['team_id'] ) ) : 0;
$calendar_data = new SP_Calendar( $calendar );
if ( $team_id ) {
$calendar_data->team = $team_id;
}
$events = (array) $calendar_data->data();
$filename = sanitize_title( $calendar->post_name ? $calendar->post_name : $calendar->post_title );
if ( '' === $filename ) {
$filename = 'schedule';
}
if ( $team_id ) {
$filename .= '-team-' . $team_id;
}
header( 'Content-Type: text/csv; charset=utf-8' );
header( 'Content-Disposition: inline; filename=' . $filename . '.csv' );
$output = fopen( 'php://output', 'w' );
if ( false === $output ) {
wp_die( esc_html__( 'ERROR: Unable to generate the CSV feed.', 'tonys-sportspress-enhancements' ), '', array( 'response' => 500 ) );
}
// Excel expects a BOM for UTF-8 CSV files.
fwrite( $output, "\xEF\xBB\xBF" );
fputcsv(
$output,
array(
'Date',
'Time',
'Away Team',
'Home Team',
'Field Name',
)
);
foreach ( $events as $event ) {
$teams = tse_sp_get_event_home_away_teams( $event->ID );
fputcsv(
$output,
array(
sp_get_date( $event ),
sp_get_time( $event ),
$teams['away'],
$teams['home'],
tse_sp_get_event_field_name( $event->ID ),
)
);
}
fclose( $output );
exit;
}
/**
* CSV headers recognized by this importer.