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

@@ -40,104 +40,18 @@ function tse_sp_schedule_exporter_handle_download() {
check_admin_referer( 'tse_schedule_export' );
$team_id = isset( $_GET['team_id'] ) ? absint( wp_unslash( $_GET['team_id'] ) ) : 0;
$season_id = isset( $_GET['season_id'] ) ? absint( wp_unslash( $_GET['season_id'] ) ) : 0;
$league_id = isset( $_GET['league_id'] ) ? absint( wp_unslash( $_GET['league_id'] ) ) : 0;
$format = isset( $_GET['format'] ) ? sanitize_key( wp_unslash( $_GET['format'] ) ) : '';
$filters = tse_sp_event_export_normalize_request_args();
if ( $team_id <= 0 || 'sp_team' !== get_post_type( $team_id ) ) {
if ( $filters['team_id'] <= 0 || 'sp_team' !== get_post_type( $filters['team_id'] ) ) {
wp_die( esc_html__( 'Choose a valid team before exporting.', 'tonys-sportspress-enhancements' ), '', array( 'response' => 400 ) );
}
if ( ! in_array( $format, array( 'matchup', 'team' ), true ) ) {
wp_die( esc_html__( 'Choose a valid export format.', 'tonys-sportspress-enhancements' ), '', array( 'response' => 400 ) );
}
$events = tse_sp_schedule_exporter_get_events( $team_id, $season_id, $league_id );
$team = get_post( $team_id );
if ( ! $team instanceof WP_Post ) {
wp_die( esc_html__( 'The selected team could not be loaded.', 'tonys-sportspress-enhancements' ), '', array( 'response' => 404 ) );
}
$filename = sanitize_title( $team->post_name ? $team->post_name : $team->post_title );
if ( '' === $filename ) {
$filename = 'schedule';
}
if ( $season_id > 0 ) {
$season = get_term( $season_id, 'sp_season' );
if ( $season && ! is_wp_error( $season ) && ! empty( $season->slug ) ) {
$filename .= '-' . sanitize_title( $season->slug );
}
}
$filename .= '-' . $format . '.csv';
header( 'Content-Type: text/csv; charset=utf-8' );
header( 'Content-Disposition: attachment; filename=' . $filename );
$output = fopen( 'php://output', 'w' );
if ( false === $output ) {
wp_die( esc_html__( 'Unable to start the CSV export.', 'tonys-sportspress-enhancements' ), '', array( 'response' => 500 ) );
}
fwrite( $output, "\xEF\xBB\xBF" );
if ( 'matchup' === $format ) {
fputcsv(
$output,
array(
'Date',
'Time',
'Away Team',
'Home Team',
'Field Name',
)
);
foreach ( $events as $event ) {
fputcsv(
$output,
array(
$event['date'],
$event['time'],
$event['away_team'],
$event['home_team'],
$event['venue_name'],
)
);
}
} else {
fputcsv(
$output,
array(
'Extra Label',
'Date',
'Time',
'Opponent',
'Home/Away',
'Venue',
)
);
foreach ( $events as $event ) {
fputcsv(
$output,
array(
$event['label'],
$event['date'],
$event['time'],
$event['opponent_name'],
$event['location_flag'],
$event['venue_name'],
)
);
}
}
fclose( $output );
exit;
tse_sp_event_export_stream_csv(
$filters,
array(
'disposition' => 'attachment',
)
);
}
add_action( 'admin_post_tse_schedule_export', 'tse_sp_schedule_exporter_handle_download' );
add_action( 'admin_post_nopriv_tse_schedule_export', 'tse_sp_schedule_exporter_handle_download' );
@@ -165,7 +79,7 @@ function tse_sp_schedule_exporter_register_block() {
array(
'api_version' => 3,
'title' => __( 'Schedule Exporter', 'tonys-sportspress-enhancements' ),
'description' => __( 'Shows the public schedule exporter with CSV and printable PDF options.', 'tonys-sportspress-enhancements' ),
'description' => __( 'Shows the public schedule exporter with CSV, iCal, and printable page options.', 'tonys-sportspress-enhancements' ),
'category' => 'widgets',
'icon' => 'calendar-alt',
'editor_script' => 'tse-schedule-exporter-block',
@@ -193,19 +107,52 @@ function tse_sp_schedule_exporter_render_admin_page() {
$season_id = tse_sp_schedule_exporter_resolve_season_id( $seasons );
$teams = tse_sp_schedule_exporter_get_teams( $league_id, $season_id );
$team_id = tse_sp_schedule_exporter_resolve_team_id( $teams );
$paper = tse_sp_schedule_exporter_resolve_paper_size();
$fields = tse_sp_schedule_exporter_get_fields();
$field_id = tse_sp_schedule_exporter_resolve_field_id( $fields );
$export_type = tse_sp_schedule_exporter_resolve_export_type();
$subformat = tse_sp_schedule_exporter_resolve_subformat();
echo '<div class="wrap">';
echo '<h1>' . esc_html__( 'Schedule Exporter', 'tonys-sportspress-enhancements' ) . '</h1>';
echo '<p>' . esc_html__( 'Choose a team and season, then export the schedule as CSV or open the printable schedule in a PDF-ready print view.', 'tonys-sportspress-enhancements' ) . '</p>';
echo '<p>' . esc_html__( 'Choose filters once, then generate the CSV feed, iCal link, or printable page URL from the same controls.', 'tonys-sportspress-enhancements' ) . '</p>';
echo '<form method="get" action="' . esc_url( admin_url( 'admin.php' ) ) . '" class="tse-schedule-exporter-form" style="max-width:960px;margin:20px 0 28px;">';
echo '<form method="get" action="' . esc_url( admin_url( 'admin.php' ) ) . '" class="tse-schedule-exporter-form" style="max-width:720px;margin:20px 0 28px;">';
echo '<input type="hidden" name="page" value="tse-schedule-exporter" />';
echo '<table class="form-table" role="presentation"><tbody>';
echo '<div>';
echo '<tr>';
echo '<th scope="row"><label for="tse-schedule-exporter-league">' . esc_html__( 'League', 'tonys-sportspress-enhancements' ) . '</label></th>';
echo '<td><select id="tse-schedule-exporter-league" name="league_id" data-auto-submit="1">';
echo '<div style="margin-bottom:16px;">';
echo '<label for="tse-schedule-exporter-export-type"><strong>' . esc_html__( 'Format', 'tonys-sportspress-enhancements' ) . '</strong></label><br />';
echo '<select id="tse-schedule-exporter-export-type" name="export_type">';
foreach ( tse_sp_schedule_exporter_get_export_types() as $type_key => $type_label ) {
printf(
'<option value="%1$s" %2$s>%3$s</option>',
esc_attr( $type_key ),
selected( $export_type, $type_key, false ),
esc_html( $type_label )
);
}
echo '</select>';
echo '<p class="description">' . esc_html__( 'CSV builds a feed URL, iCal Link builds a subscription URL, and Printable opens the printable page.', 'tonys-sportspress-enhancements' ) . '</p>';
echo '</div>';
echo '<div data-subformat-wrap="1" style="margin-bottom:16px;">';
echo '<label for="tse-schedule-exporter-subformat"><strong>' . esc_html__( 'CSV Layout', 'tonys-sportspress-enhancements' ) . '</strong></label><br />';
echo '<select id="tse-schedule-exporter-subformat" name="subformat">';
foreach ( tse_sp_event_export_get_formats() as $format_key => $format_definition ) {
printf(
'<option value="%1$s" %2$s>%3$s</option>',
esc_attr( $format_key ),
selected( $subformat, $format_key, false ),
esc_html( $format_definition['label'] )
);
}
echo '</select>';
echo '<p class="description">' . esc_html__( 'Matchup is away vs home. Team is opponent-based and requires one specific team.', 'tonys-sportspress-enhancements' ) . '</p>';
echo '</div>';
echo '<div style="margin-bottom:16px;">';
echo '<label for="tse-schedule-exporter-league"><strong>' . esc_html__( 'League', 'tonys-sportspress-enhancements' ) . '</strong></label><br />';
echo '<select id="tse-schedule-exporter-league" name="league_id" data-auto-submit="1">';
foreach ( $leagues as $league ) {
printf(
'<option value="%1$s" %2$s>%3$s</option>',
@@ -214,12 +161,12 @@ function tse_sp_schedule_exporter_render_admin_page() {
esc_html( $league->name )
);
}
echo '</select></td>';
echo '</tr>';
echo '</select>';
echo '</div>';
echo '<tr>';
echo '<th scope="row"><label for="tse-schedule-exporter-season">' . esc_html__( 'Season', 'tonys-sportspress-enhancements' ) . '</label></th>';
echo '<td><select id="tse-schedule-exporter-season" name="season_id" data-auto-submit="1">';
echo '<div style="margin-bottom:16px;">';
echo '<label for="tse-schedule-exporter-season"><strong>' . esc_html__( 'Season', 'tonys-sportspress-enhancements' ) . '</strong></label><br />';
echo '<select id="tse-schedule-exporter-season" name="season_id" data-auto-submit="1">';
echo '<option value="0">' . esc_html__( 'Current / All matching events', 'tonys-sportspress-enhancements' ) . '</option>';
foreach ( $seasons as $season ) {
printf(
@@ -229,28 +176,13 @@ function tse_sp_schedule_exporter_render_admin_page() {
esc_html( $season->name )
);
}
echo '</select></td>';
echo '</tr>';
echo '<tr>';
echo '<th scope="row"><label for="tse-schedule-exporter-paper">' . esc_html__( 'Paper Size', 'tonys-sportspress-enhancements' ) . '</label></th>';
echo '<td><select id="tse-schedule-exporter-paper" name="paper">';
foreach ( tse_sp_schedule_exporter_get_paper_sizes() as $paper_value => $paper_label ) {
printf(
'<option value="%1$s" %2$s>%3$s</option>',
esc_attr( $paper_value ),
selected( $paper, $paper_value, false ),
esc_html( $paper_label )
);
}
echo '</select>';
echo '<p class="description">' . esc_html__( 'The PDF option opens the existing printable schedule and triggers the browser print dialog so you can save it as a PDF.', 'tonys-sportspress-enhancements' ) . '</p>';
echo '</td>';
echo '</tr>';
echo '</div>';
echo '<tr>';
echo '<th scope="row"><label for="tse-schedule-exporter-team">' . esc_html__( 'Team', 'tonys-sportspress-enhancements' ) . '</label></th>';
echo '<td><select id="tse-schedule-exporter-team" name="team_id">';
echo '<div style="margin-bottom:16px;">';
echo '<label for="tse-schedule-exporter-team"><strong>' . esc_html__( 'Team', 'tonys-sportspress-enhancements' ) . '</strong></label><br />';
echo '<select id="tse-schedule-exporter-team" name="team_id">';
echo '<option value="0">' . esc_html__( 'All teams', 'tonys-sportspress-enhancements' ) . '</option>';
foreach ( $teams as $team ) {
printf(
'<option value="%1$s" %2$s>%3$s</option>',
@@ -261,10 +193,25 @@ function tse_sp_schedule_exporter_render_admin_page() {
}
echo '</select>';
echo '<p class="description">' . esc_html__( 'Teams are filtered by the selected league and season.', 'tonys-sportspress-enhancements' ) . '</p>';
echo '</td>';
echo '</tr>';
echo '</div>';
echo '</tbody></table>';
echo '<div style="margin-bottom:16px;">';
echo '<label for="tse-schedule-exporter-field"><strong>' . esc_html__( 'Field', 'tonys-sportspress-enhancements' ) . '</strong></label><br />';
echo '<select id="tse-schedule-exporter-field" name="field_id">';
echo '<option value="0">' . esc_html__( 'All fields', 'tonys-sportspress-enhancements' ) . '</option>';
foreach ( $fields as $field ) {
printf(
'<option value="%1$s" %2$s>%3$s</option>',
esc_attr( (string) $field->term_id ),
selected( $field_id, (int) $field->term_id, false ),
esc_html( $field->name )
);
}
echo '</select>';
echo '<p class="description">' . esc_html__( 'Use the field filter to narrow the feed to a specific venue.', 'tonys-sportspress-enhancements' ) . '</p>';
echo '</div>';
echo '</div>';
echo '</form>';
if ( empty( $teams ) ) {
@@ -274,47 +221,38 @@ function tse_sp_schedule_exporter_render_admin_page() {
}
echo '<div style="max-width:960px;padding:20px 24px;border:1px solid #dcdcde;background:#fff;">';
echo '<h2 style="margin-top:0;">' . esc_html__( 'Exports', 'tonys-sportspress-enhancements' ) . '</h2>';
echo '<h2 style="margin-top:0;">' . esc_html__( 'Output URL', 'tonys-sportspress-enhancements' ) . '</h2>';
echo '<p>' . esc_html__( 'The generated URL below updates from the shared controls above.', 'tonys-sportspress-enhancements' ) . '</p>';
echo '<table class="widefat striped" style="max-width:100%;margin-top:16px;"><tbody>';
foreach ( array(
tse_sp_schedule_exporter_render_column_picker( 'matchup', 'admin', $subformat );
tse_sp_schedule_exporter_render_column_picker( 'team', 'admin', $subformat );
$base_args = array(
'league_id' => $league_id,
'team_id' => $team_id,
'season_id' => $season_id,
'field_id' => $field_id,
'format' => $subformat,
);
$csv_url = tse_sp_event_export_get_feed_url( $base_args, 'csv' );
$ics_url = tse_sp_event_export_get_feed_url(
array(
'format' => 'matchup',
'label' => __( 'Download Matchup CSV', 'tonys-sportspress-enhancements' ),
'description' => __( 'Date, time, away team, home team, and field name.', 'tonys-sportspress-enhancements' ),
'league_id' => $league_id,
'team_id' => $team_id,
'season_id' => $season_id,
'field_id' => $field_id,
),
array(
'format' => 'team',
'label' => __( 'Download Team CSV', 'tonys-sportspress-enhancements' ),
'description' => __( 'TeamSnap-compatible layout with game label, opponent, home/away flag, and venue.', 'tonys-sportspress-enhancements' ),
),
) as $export_option ) {
$url = wp_nonce_url(
add_query_arg(
array(
'action' => 'tse_schedule_export',
'league_id' => $league_id,
'team_id' => $team_id,
'season_id' => $season_id,
'format' => $export_option['format'],
),
admin_url( 'admin-post.php' )
),
'tse_schedule_export'
);
'ics'
);
$print_url = tse_sp_schedule_exporter_get_printable_url( $team_id, $season_id, 'letter', $league_id );
$current_url = tse_sp_schedule_exporter_get_output_url( $export_type, $csv_url, $ics_url, $print_url );
echo '<tr>';
echo '<td style="width:240px;"><a class="button button-primary tse-export-link" data-format="' . esc_attr( $export_option['format'] ) . '" href="' . esc_url( $url ) . '">' . esc_html( $export_option['label'] ) . '</a></td>';
echo '<td>' . esc_html( $export_option['description'] ) . '</td>';
echo '</tr>';
}
$pdf_url = tse_sp_schedule_exporter_get_pdf_url( $team_id, $season_id, $paper, $league_id );
echo '<tr>';
echo '<td style="width:240px;"><a class="button tse-pdf-link" href="' . esc_url( $pdf_url ) . '" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Open Printable PDF View', 'tonys-sportspress-enhancements' ) . '</a></td>';
echo '<td>' . esc_html__( 'Opens the printable schedule and launches the browser print dialog so you can save a PDF.', 'tonys-sportspress-enhancements' ) . '</td>';
echo '</tr>';
echo '</tbody></table>';
echo '<div style="display:flex;align-items:center;gap:8px;max-width:100%;margin-top:16px;">';
echo '<input type="text" class="large-text code tse-output-url" readonly="readonly" value="' . esc_attr( $current_url ) . '" />';
echo '<button type="button" class="button tse-copy-link" title="' . esc_attr__( 'Copy URL', 'tonys-sportspress-enhancements' ) . '">' . esc_html__( 'Copy URL', 'tonys-sportspress-enhancements' ) . '</button>';
echo '<button type="button" class="button button-primary tse-open-link" data-csv-url="' . esc_url( $csv_url ) . '" data-ics-url="' . esc_url( $ics_url ) . '" data-print-url="' . esc_url( $print_url ) . '" title="' . esc_attr__( 'Open URL in new tab', 'tonys-sportspress-enhancements' ) . '">' . esc_html__( 'Open URL in New Tab', 'tonys-sportspress-enhancements' ) . '</button>';
echo '</div>';
echo '<p class="description tse-output-note">' . esc_html__( 'Use the buttons to copy or open the generated URL.', 'tonys-sportspress-enhancements' ) . '</p>';
tse_sp_schedule_exporter_render_link_sync_script( true );
echo '</div>';
echo '</div>';
@@ -332,7 +270,10 @@ function tse_sp_schedule_exporter_render_shortcode() {
$season_id = tse_sp_schedule_exporter_resolve_season_id( $seasons );
$teams = tse_sp_schedule_exporter_get_teams( $league_id, $season_id );
$team_id = tse_sp_schedule_exporter_resolve_team_id( $teams );
$paper = tse_sp_schedule_exporter_resolve_paper_size();
$fields = tse_sp_schedule_exporter_get_fields();
$field_id = tse_sp_schedule_exporter_resolve_field_id( $fields );
$export_type = tse_sp_schedule_exporter_resolve_export_type();
$subformat = tse_sp_schedule_exporter_resolve_subformat();
if ( empty( $teams ) ) {
return '<p>' . esc_html__( 'No SportsPress teams match the selected league and season.', 'tonys-sportspress-enhancements' ) . '</p>';
@@ -342,10 +283,34 @@ function tse_sp_schedule_exporter_render_shortcode() {
?>
<div class="tse-schedule-exporter" style="max-width:960px;margin:0 auto;padding:24px;border:1px solid #d7d7db;background:#fff;">
<h2 style="margin-top:0;"><?php esc_html_e( 'Schedule Exporter', 'tonys-sportspress-enhancements' ); ?></h2>
<p><?php esc_html_e( 'Export schedules as CSV or open the printable version and save it as a PDF.', 'tonys-sportspress-enhancements' ); ?></p>
<p><?php esc_html_e( 'Choose filters once, then generate the CSV feed, iCal link, or printable page URL from the same controls.', 'tonys-sportspress-enhancements' ); ?></p>
<form method="get" action="<?php echo esc_url( get_permalink() ); ?>" class="tse-schedule-exporter-form" style="display:grid;gap:16px;margin:24px 0;">
<div>
<form method="get" action="<?php echo esc_url( get_permalink() ); ?>" class="tse-schedule-exporter-form" style="max-width:720px;margin:24px 0;">
<div style="margin-bottom:16px;">
<label for="tse-public-export-type"><strong><?php esc_html_e( 'Format', 'tonys-sportspress-enhancements' ); ?></strong></label><br />
<select id="tse-public-export-type" name="export_type">
<?php foreach ( tse_sp_schedule_exporter_get_export_types() as $type_key => $type_label ) : ?>
<option value="<?php echo esc_attr( $type_key ); ?>" <?php selected( $export_type, $type_key ); ?>>
<?php echo esc_html( $type_label ); ?>
</option>
<?php endforeach; ?>
</select>
<p class="description"><?php esc_html_e( 'CSV builds a feed URL, iCal Link builds a subscription URL, and Printable opens the printable page.', 'tonys-sportspress-enhancements' ); ?></p>
</div>
<div data-subformat-wrap="1" style="margin-bottom:16px;">
<label for="tse-public-subformat"><strong><?php esc_html_e( 'CSV Layout', 'tonys-sportspress-enhancements' ); ?></strong></label><br />
<select id="tse-public-subformat" name="subformat">
<?php foreach ( tse_sp_event_export_get_formats() as $format_key => $format_definition ) : ?>
<option value="<?php echo esc_attr( $format_key ); ?>" <?php selected( $subformat, $format_key ); ?>>
<?php echo esc_html( $format_definition['label'] ); ?>
</option>
<?php endforeach; ?>
</select>
<p class="description"><?php esc_html_e( 'Matchup is away vs home. Team is opponent-based and requires one specific team.', 'tonys-sportspress-enhancements' ); ?></p>
</div>
<div style="margin-bottom:16px;">
<label for="tse-public-league"><strong><?php esc_html_e( 'League', 'tonys-sportspress-enhancements' ); ?></strong></label><br />
<select id="tse-public-league" name="league_id" data-auto-submit="1">
<?php foreach ( $leagues as $league ) : ?>
@@ -356,7 +321,7 @@ function tse_sp_schedule_exporter_render_shortcode() {
</select>
</div>
<div>
<div style="margin-bottom:16px;">
<label for="tse-public-season"><strong><?php esc_html_e( 'Season', 'tonys-sportspress-enhancements' ); ?></strong></label><br />
<select id="tse-public-season" name="season_id" data-auto-submit="1">
<option value="0"><?php esc_html_e( 'Current / All matching events', 'tonys-sportspress-enhancements' ); ?></option>
@@ -368,20 +333,10 @@ function tse_sp_schedule_exporter_render_shortcode() {
</select>
</div>
<div>
<label for="tse-public-paper"><strong><?php esc_html_e( 'Paper Size', 'tonys-sportspress-enhancements' ); ?></strong></label><br />
<select id="tse-public-paper" name="paper">
<?php foreach ( tse_sp_schedule_exporter_get_paper_sizes() as $paper_value => $paper_label ) : ?>
<option value="<?php echo esc_attr( $paper_value ); ?>" <?php selected( $paper, $paper_value ); ?>>
<?php echo esc_html( $paper_label ); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div>
<div style="margin-bottom:16px;">
<label for="tse-public-team"><strong><?php esc_html_e( 'Team', 'tonys-sportspress-enhancements' ); ?></strong></label><br />
<select id="tse-public-team" name="team_id">
<option value="0"><?php esc_html_e( 'All teams', 'tonys-sportspress-enhancements' ); ?></option>
<?php foreach ( $teams as $team ) : ?>
<option value="<?php echo esc_attr( (string) $team->ID ); ?>" <?php selected( $team_id, (int) $team->ID ); ?>>
<?php echo esc_html( $team->post_title ); ?>
@@ -390,30 +345,35 @@ function tse_sp_schedule_exporter_render_shortcode() {
</select>
</div>
<div style="margin-bottom:16px;">
<label for="tse-public-field"><strong><?php esc_html_e( 'Field', 'tonys-sportspress-enhancements' ); ?></strong></label><br />
<select id="tse-public-field" name="field_id">
<option value="0"><?php esc_html_e( 'All fields', 'tonys-sportspress-enhancements' ); ?></option>
<?php foreach ( $fields as $field ) : ?>
<option value="<?php echo esc_attr( (string) $field->term_id ); ?>" <?php selected( $field_id, (int) $field->term_id ); ?>>
<?php echo esc_html( $field->name ); ?>
</option>
<?php endforeach; ?>
</select>
</div>
</form>
<table style="width:100%;border-collapse:collapse;margin-top:16px;">
<tbody>
<tr>
<td style="width:240px;padding:10px 12px;border:1px solid #d7d7db;">
<a class="button button-primary tse-export-link" data-format="matchup" href="<?php echo esc_url( tse_sp_schedule_exporter_get_export_url( $team_id, $season_id, 'matchup', $league_id ) ); ?>"><?php esc_html_e( 'Download Matchup CSV', 'tonys-sportspress-enhancements' ); ?></a>
</td>
<td style="padding:10px 12px;border:1px solid #d7d7db;"><?php esc_html_e( 'Date, time, away team, home team, and field name.', 'tonys-sportspress-enhancements' ); ?></td>
</tr>
<tr>
<td style="width:240px;padding:10px 12px;border:1px solid #d7d7db;">
<a class="button button-primary tse-export-link" data-format="team" href="<?php echo esc_url( tse_sp_schedule_exporter_get_export_url( $team_id, $season_id, 'team', $league_id ) ); ?>"><?php esc_html_e( 'Download Team CSV', 'tonys-sportspress-enhancements' ); ?></a>
</td>
<td style="padding:10px 12px;border:1px solid #d7d7db;"><?php esc_html_e( 'TeamSnap-compatible layout with game label, opponent, home/away flag, and venue.', 'tonys-sportspress-enhancements' ); ?></td>
</tr>
<tr>
<td style="width:240px;padding:10px 12px;border:1px solid #d7d7db;">
<a class="button tse-pdf-link" href="<?php echo esc_url( tse_sp_schedule_exporter_get_pdf_url( $team_id, $season_id, $paper, $league_id ) ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Open Printable PDF View', 'tonys-sportspress-enhancements' ); ?></a>
</td>
<td style="padding:10px 12px;border:1px solid #d7d7db;"><?php esc_html_e( 'Opens the printable schedule and starts the print dialog so visitors can save a PDF.', 'tonys-sportspress-enhancements' ); ?></td>
</tr>
</tbody>
</table>
<?php tse_sp_schedule_exporter_render_column_picker( 'matchup', 'public', $subformat ); ?>
<?php tse_sp_schedule_exporter_render_column_picker( 'team', 'public', $subformat ); ?>
<?php
$csv_url = tse_sp_event_export_get_feed_url( array( 'team_id' => $team_id, 'season_id' => $season_id, 'league_id' => $league_id, 'field_id' => $field_id, 'format' => $subformat ), 'csv' );
$ics_url = tse_sp_event_export_get_feed_url( array( 'team_id' => $team_id, 'season_id' => $season_id, 'league_id' => $league_id, 'field_id' => $field_id ), 'ics' );
$print_url = tse_sp_schedule_exporter_get_printable_url( $team_id, $season_id, 'letter', $league_id );
$current_url = tse_sp_schedule_exporter_get_output_url( $export_type, $csv_url, $ics_url, $print_url );
?>
<div style="display:flex;align-items:center;gap:8px;max-width:100%;margin-top:16px;">
<input type="text" class="large-text code tse-output-url" readonly="readonly" value="<?php echo esc_attr( $current_url ); ?>" />
<button type="button" class="button tse-copy-link" title="<?php esc_attr_e( 'Copy URL', 'tonys-sportspress-enhancements' ); ?>"><?php esc_html_e( 'Copy URL', 'tonys-sportspress-enhancements' ); ?></button>
<button type="button" class="button button-primary tse-open-link" data-csv-url="<?php echo esc_url( $csv_url ); ?>" data-ics-url="<?php echo esc_url( $ics_url ); ?>" data-print-url="<?php echo esc_url( $print_url ); ?>" title="<?php esc_attr_e( 'Open URL in new tab', 'tonys-sportspress-enhancements' ); ?>"><?php esc_html_e( 'Open URL in New Tab', 'tonys-sportspress-enhancements' ); ?></button>
</div>
<p class="description tse-output-note"><?php esc_html_e( 'Use the buttons to copy or open the generated URL.', 'tonys-sportspress-enhancements' ); ?></p>
</div>
<?php
$output = (string) ob_get_clean();
@@ -478,6 +438,28 @@ function tse_sp_schedule_exporter_get_seasons() {
return $seasons;
}
/**
* Get fields for the exporter.
*
* @return WP_Term[]
*/
function tse_sp_schedule_exporter_get_fields() {
$fields = get_terms(
array(
'taxonomy' => 'sp_venue',
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
)
);
if ( is_wp_error( $fields ) || ! is_array( $fields ) ) {
return array();
}
return $fields;
}
/**
* Get teams for the exporter.
*
@@ -552,6 +534,70 @@ function tse_sp_schedule_exporter_resolve_team_id( $teams ) {
return 0;
}
/**
* Resolve selected field ID.
*
* @param WP_Term[] $fields Field terms.
* @return int
*/
function tse_sp_schedule_exporter_resolve_field_id( $fields ) {
$requested = isset( $_GET['field_id'] ) ? absint( wp_unslash( $_GET['field_id'] ) ) : 0;
if ( 0 === $requested ) {
return 0;
}
foreach ( $fields as $field ) {
if ( $field instanceof WP_Term && (int) $field->term_id === $requested ) {
return $requested;
}
}
return 0;
}
/**
* Render selectable columns for a format.
*
* @param string $format Export format.
* @param string $context Render context suffix.
* @return void
*/
function tse_sp_schedule_exporter_render_column_picker( $format, $context, $active_format = '' ) {
$format = tse_sp_event_export_sanitize_format( $format );
$definitions = tse_sp_event_export_get_column_definitions();
$columns = isset( $definitions[ $format ] ) ? $definitions[ $format ] : array();
$selected = tse_sp_event_export_get_default_columns( $format );
$formats = tse_sp_event_export_get_formats();
$legend = isset( $formats[ $format ]['label'] ) ? $formats[ $format ]['label'] : ucfirst( $format );
if ( empty( $columns ) ) {
return;
}
$style = 'margin:18px 0;padding:16px;border:1px solid #d7d7db;';
if ( $active_format && $active_format !== $format ) {
$style .= 'display:none;';
}
echo '<fieldset data-column-group="' . esc_attr( $format ) . '" style="' . esc_attr( $style ) . '">';
echo '<legend><strong>' . esc_html( sprintf( __( '%s Columns', 'tonys-sportspress-enhancements' ), $legend ) ) . '</strong></legend>';
echo '<div style="display:flex;flex-wrap:wrap;gap:12px 18px;">';
foreach ( $columns as $column_key => $column_label ) {
$input_id = sprintf( 'tse-columns-%1$s-%2$s-%3$s', sanitize_html_class( $context ), sanitize_html_class( $format ), sanitize_html_class( $column_key ) );
echo '<label for="' . esc_attr( $input_id ) . '" style="display:inline-flex;align-items:center;gap:6px;">';
echo '<input id="' . esc_attr( $input_id ) . '" type="checkbox" data-columns-format="' . esc_attr( $format ) . '" value="' . esc_attr( $column_key ) . '" ' . checked( in_array( $column_key, $selected, true ), true, false ) . ' />';
echo esc_html( $column_label );
echo '</label>';
}
echo '</div>';
echo '<p class="description" style="margin:10px 0 0;">' . esc_html__( 'These checkboxes only change the CSV feed link. iCal and printable links use the same shared filters but ignore columns.', 'tonys-sportspress-enhancements' ) . '</p>';
echo '</fieldset>';
}
/**
* Resolve selected league ID.
*
@@ -631,6 +677,92 @@ function tse_sp_schedule_exporter_resolve_paper_size() {
return array_key_exists( $paper, tse_sp_schedule_exporter_get_paper_sizes() ) ? $paper : 'letter';
}
/**
* Resolve selected export format.
*
* @return string
*/
function tse_sp_schedule_exporter_resolve_format() {
$requested = isset( $_GET['format'] ) ? sanitize_key( wp_unslash( $_GET['format'] ) ) : 'matchup';
return tse_sp_event_export_sanitize_format( $requested );
}
/**
* Get supported exporter output types.
*
* @return array
*/
function tse_sp_schedule_exporter_get_export_types() {
return array(
'csv' => __( 'CSV', 'tonys-sportspress-enhancements' ),
'ics' => __( 'iCal Link', 'tonys-sportspress-enhancements' ),
'printable' => __( 'Printable', 'tonys-sportspress-enhancements' ),
);
}
/**
* Resolve selected exporter output type.
*
* @return string
*/
function tse_sp_schedule_exporter_resolve_export_type() {
$requested = isset( $_GET['export_type'] ) ? sanitize_key( wp_unslash( $_GET['export_type'] ) ) : 'csv';
$types = tse_sp_schedule_exporter_get_export_types();
return isset( $types[ $requested ] ) ? $requested : 'csv';
}
/**
* Resolve selected CSV subformat.
*
* @return string
*/
function tse_sp_schedule_exporter_resolve_subformat() {
$requested = isset( $_GET['subformat'] ) ? sanitize_key( wp_unslash( $_GET['subformat'] ) ) : 'matchup';
return tse_sp_event_export_sanitize_format( $requested );
}
/**
* Get current output URL for the selected export type.
*
* @param string $export_type Export type.
* @param string $csv_url CSV URL.
* @param string $ics_url ICS URL.
* @param string $print_url Printable URL.
* @return string
*/
function tse_sp_schedule_exporter_get_output_url( $export_type, $csv_url, $ics_url, $print_url ) {
if ( 'ics' === $export_type ) {
return $ics_url;
}
if ( 'printable' === $export_type ) {
return $print_url;
}
return $csv_url;
}
/**
* Get current output button label for the selected export type.
*
* @param string $export_type Export type.
* @return string
*/
function tse_sp_schedule_exporter_get_output_label( $export_type ) {
if ( 'ics' === $export_type ) {
return __( 'Subscribe to iCal / ICS', 'tonys-sportspress-enhancements' );
}
if ( 'printable' === $export_type ) {
return __( 'Open Printable Page', 'tonys-sportspress-enhancements' );
}
return __( 'Open CSV Feed', 'tonys-sportspress-enhancements' );
}
/**
* Collect team schedule events for export.
*
@@ -761,32 +893,7 @@ function tse_sp_schedule_exporter_get_primary_venue( $event_id ) {
}
/**
* Build an export download URL.
*
* @param int $team_id Team ID.
* @param int $season_id Season ID.
* @param string $format Export format.
* @param int $league_id League ID.
* @return string
*/
function tse_sp_schedule_exporter_get_export_url( $team_id, $season_id, $format, $league_id = 0 ) {
return wp_nonce_url(
add_query_arg(
array(
'action' => 'tse_schedule_export',
'league_id' => absint( $league_id ),
'team_id' => absint( $team_id ),
'season_id' => absint( $season_id ),
'format' => sanitize_key( $format ),
),
admin_url( 'admin-post.php' )
),
'tse_schedule_export'
);
}
/**
* Build the printable PDF URL.
* Build the printable page URL.
*
* @param int $team_id Team ID.
* @param int $season_id Season ID.
@@ -794,7 +901,7 @@ function tse_sp_schedule_exporter_get_export_url( $team_id, $season_id, $format,
* @param int $league_id League ID.
* @return string
*/
function tse_sp_schedule_exporter_get_pdf_url( $team_id, $season_id, $paper, $league_id = 0 ) {
function tse_sp_schedule_exporter_get_printable_url( $team_id, $season_id, $paper, $league_id = 0, $autoprint = false ) {
return add_query_arg(
array(
Tony_Sportspress_Printable_Calendars::QUERY_FLAG => '1',
@@ -802,7 +909,7 @@ function tse_sp_schedule_exporter_get_pdf_url( $team_id, $season_id, $paper, $le
'sp_season' => $season_id > 0 ? (string) absint( $season_id ) : '',
'sp_league' => $league_id > 0 ? (string) absint( $league_id ) : '',
'paper' => $paper,
'autoprint' => '1',
'autoprint' => $autoprint ? '1' : '',
),
home_url( '/' )
);
@@ -818,6 +925,30 @@ function tse_sp_schedule_exporter_render_link_sync_script( $echo = false ) {
$script = <<<HTML
<script>
(function(){
function copyText(text, done){
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(done).catch(function(){
var input = document.createElement('input');
input.value = text;
document.body.appendChild(input);
input.focus();
input.select();
document.execCommand('copy');
document.body.removeChild(input);
done();
});
return;
}
var input = document.createElement('input');
input.value = text;
document.body.appendChild(input);
input.focus();
input.select();
document.execCommand('copy');
document.body.removeChild(input);
done();
}
function syncLinks(scope){
var form = scope.querySelector('.tse-schedule-exporter-form');
if (!form) {
@@ -827,25 +958,103 @@ function tse_sp_schedule_exporter_render_link_sync_script( $echo = false ) {
var league = form.querySelector('[name="league_id"]');
var season = form.querySelector('[name="season_id"]');
var team = form.querySelector('[name="team_id"]');
var paper = form.querySelector('[name="paper"]');
var exportType = form.querySelector('[name="export_type"]');
var subformat = form.querySelector('[name="subformat"]');
var field = form.querySelector('[name="field_id"]');
var outputUrl = scope.querySelector('.tse-output-url');
var openButton = scope.querySelector('.tse-open-link');
var outputNote = scope.querySelector('.tse-output-note');
var copyButton = scope.querySelector('.tse-copy-link');
var teamValue = team ? (team.value || '0') : '0';
var activeSubformat = subformat ? (subformat.value || 'matchup') : 'matchup';
var selectedExportType = exportType ? (exportType.value || 'csv') : 'csv';
scope.querySelectorAll('.tse-export-link').forEach(function(link){
var url = new URL(link.href, window.location.origin);
if (league) url.searchParams.set('league_id', league.value || '0');
if (season) url.searchParams.set('season_id', season.value || '0');
if (team) url.searchParams.set('team_id', team.value || '0');
if (link.dataset.format) url.searchParams.set('format', link.dataset.format);
link.href = url.toString();
scope.querySelectorAll('[data-column-group]').forEach(function(group){
var visible = selectedExportType === 'csv' && group.getAttribute('data-column-group') === activeSubformat;
group.style.display = visible ? 'block' : 'none';
});
scope.querySelectorAll('.tse-pdf-link').forEach(function(link){
var url = new URL(link.href, window.location.origin);
if (league) url.searchParams.set('sp_league', league.value || '0');
if (season) url.searchParams.set('sp_season', season.value || '0');
if (team) url.searchParams.set('sp_team', team.value || '0');
if (paper) url.searchParams.set('paper', paper.value || 'letter');
link.href = url.toString();
});
if (scope.querySelector('[data-subformat-wrap]')) {
scope.querySelectorAll('[data-subformat-wrap]').forEach(function(wrap){
wrap.style.display = selectedExportType === 'csv' ? 'block' : 'none';
});
}
var csvUrl = openButton ? new URL(openButton.dataset.csvUrl, window.location.origin) : null;
var icsUrl = openButton ? new URL(openButton.dataset.icsUrl, window.location.origin) : null;
var printUrl = openButton ? new URL(openButton.dataset.printUrl, window.location.origin) : null;
if (csvUrl) {
if (league) csvUrl.searchParams.set('league_id', league.value || '0');
if (season) csvUrl.searchParams.set('season_id', season.value || '0');
if (team) csvUrl.searchParams.set('team_id', teamValue);
if (field) csvUrl.searchParams.set('field_id', field.value || '0');
csvUrl.searchParams.set('format', activeSubformat);
var columns = Array.prototype.slice.call(scope.querySelectorAll('[data-columns-format="' + activeSubformat + '"]:checked')).map(function(input){
return input.value;
}).filter(Boolean);
if (columns.length) {
csvUrl.searchParams.set('columns', columns.join(','));
} else {
csvUrl.searchParams.delete('columns');
}
}
if (icsUrl) {
if (league) icsUrl.searchParams.set('league_id', league.value || '0');
if (season) icsUrl.searchParams.set('season_id', season.value || '0');
if (team) icsUrl.searchParams.set('team_id', teamValue);
if (field) icsUrl.searchParams.set('field_id', field.value || '0');
icsUrl.searchParams.delete('format');
icsUrl.searchParams.delete('columns');
}
if (printUrl) {
if (league) printUrl.searchParams.set('sp_league', league.value || '0');
if (season) printUrl.searchParams.set('sp_season', season.value || '0');
if (team) printUrl.searchParams.set('sp_team', teamValue);
printUrl.searchParams.set('paper', 'letter');
}
var resolvedUrl = csvUrl ? csvUrl.toString() : '';
var label = 'Open CSV Feed';
var disabled = false;
var note = 'Use the buttons to copy or open the generated URL.';
if (selectedExportType === 'ics' && icsUrl) {
resolvedUrl = icsUrl.toString();
label = 'Subscribe to iCal / ICS';
} else if (selectedExportType === 'printable' && printUrl) {
resolvedUrl = printUrl.toString();
label = 'Open Printable Page';
if (teamValue === '0') {
disabled = true;
note = 'Printable requires a specific team. All teams is not supported.';
}
} else if (selectedExportType === 'csv' && activeSubformat === 'team' && teamValue === '0') {
disabled = true;
note = 'CSV team layout requires a specific team. All teams is not supported.';
}
if (outputUrl) {
outputUrl.value = resolvedUrl;
}
if (openButton) {
openButton.dataset.currentUrl = resolvedUrl;
openButton.textContent = label;
openButton.disabled = disabled;
openButton.setAttribute('aria-disabled', disabled ? 'true' : 'false');
openButton.style.opacity = disabled ? '0.55' : '1';
}
if (outputNote) {
outputNote.textContent = note;
}
if (copyButton) {
copyButton.disabled = disabled;
}
}
document.querySelectorAll('.tse-schedule-exporter, .wrap').forEach(function(scope){
@@ -865,6 +1074,43 @@ function tse_sp_schedule_exporter_render_link_sync_script( $echo = false ) {
syncLinks(scope);
});
});
scope.querySelectorAll('[data-columns-format]').forEach(function(input){
input.addEventListener('change', function(){
syncLinks(scope);
});
});
var copyButton = scope.querySelector('.tse-copy-link');
var openButton = scope.querySelector('.tse-open-link');
var outputUrl = scope.querySelector('.tse-output-url');
if (copyButton && outputUrl) {
copyButton.addEventListener('click', function(){
if (copyButton.disabled || !outputUrl.value) {
return;
}
var defaultTitle = copyButton.getAttribute('data-default-title') || copyButton.title || 'Copy URL';
copyButton.setAttribute('data-default-title', defaultTitle);
copyText(outputUrl.value, function(){
copyButton.title = 'Copied';
window.setTimeout(function(){
copyButton.title = defaultTitle;
}, 1200);
});
});
}
if (openButton && outputUrl) {
openButton.addEventListener('click', function(){
if (openButton.disabled || !outputUrl.value) {
return;
}
window.open(outputUrl.value, '_blank', 'noopener,noreferrer');
});
}
});
})();
</script>