Files
tonys-sportspress-enhancements/includes/sp-schedule-exporter.php
Anthony Correa 5fbd902c6c Support multi-team printable schedule titles
Allow the printable calendar URL builders to select multiple teams and carry the selected title_format parameter through CSV, iCal, and print URLs.

Add selected-first and matchup title modes so single-team schedules can show only the opponent while multi-team schedules consistently place a chosen team first, using vs for selected home games and at for away or selected-vs-selected games.

Keep team-layout exports available for multiple selected teams, including title, separator, and opponent columns derived from the selected-team perspective.

Adjust printable calendar matchup day styling so date numbers stay visible while event content can use the full day cell behind the badge.

Extend schedule exporter tests for multi-team team layout, selected-first titles, matchup titles, and printable URL title_format propagation.
2026-05-18 18:06:58 -05:00

1048 lines
37 KiB
PHP

<?php
/**
* SportsPress schedule exporter frontend and shared helpers.
*
* @package Tonys_Sportspress_Enhancements
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_shortcode( 'tse_schedule_exporter', 'tse_sp_schedule_exporter_render_shortcode' );
add_action( 'init', 'tse_sp_schedule_exporter_register_block' );
/**
* Handle schedule export downloads.
*
* @return void
*/
function tse_sp_schedule_exporter_handle_download() {
if ( is_user_logged_in() && ! current_user_can( 'manage_sportspress' ) ) {
wp_die( esc_html__( 'You do not have permission to export schedules.', 'tonys-sportspress-enhancements' ), '', array( 'response' => 403 ) );
}
check_admin_referer( 'tse_schedule_export' );
$filters = tse_sp_event_export_normalize_request_args();
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 ) );
}
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' );
/**
* Register the schedule exporter block.
*
* @return void
*/
function tse_sp_schedule_exporter_register_block() {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
wp_register_script(
'tse-schedule-exporter-block',
TONY_SPORTSPRESS_ENHANCEMENTS_URL . 'assets/schedule-exporter-block.js',
array( 'wp-blocks', 'wp-block-editor', 'wp-element', 'wp-i18n' ),
TONY_SPORTSPRESS_ENHANCEMENTS_VERSION,
true
);
register_block_type(
'tse/schedule-exporter',
array(
'api_version' => 3,
'title' => __( 'Schedule Exporter', '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',
'render_callback' => 'tse_sp_schedule_exporter_render_block',
'supports' => array(
'html' => false,
),
)
);
}
/**
* Render the public shortcode.
*
* @return string
*/
function tse_sp_schedule_exporter_render_shortcode() {
$leagues = tse_sp_schedule_exporter_get_leagues();
$league_id = tse_sp_schedule_exporter_resolve_league_id( $leagues );
$seasons = tse_sp_schedule_exporter_get_seasons();
$season_id = tse_sp_schedule_exporter_resolve_season_id( $seasons );
$teams = tse_sp_schedule_exporter_get_teams( $league_id, $season_id );
$team_ids = tse_sp_schedule_exporter_resolve_team_ids( $teams );
$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();
$title_format = tse_sp_schedule_exporter_resolve_title_format();
if ( empty( $teams ) ) {
return '<p>' . esc_html__( 'No SportsPress teams match the selected league and season.', 'tonys-sportspress-enhancements' ) . '</p>';
}
ob_start();
?>
<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( '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="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-title-format-wrap="1" style="margin-bottom:16px;">
<label for="tse-public-title-format"><strong><?php esc_html_e( 'Title Format', 'tonys-sportspress-enhancements' ); ?></strong></label><br />
<select id="tse-public-title-format" name="title_format">
<option value="selected_first" <?php selected( $title_format, 'selected_first' ); ?>><?php esc_html_e( 'Selected Teams First', 'tonys-sportspress-enhancements' ); ?></option>
<option value="matchup" <?php selected( $title_format, 'matchup' ); ?>><?php esc_html_e( 'Matchup', 'tonys-sportspress-enhancements' ); ?></option>
</select>
<p class="description"><?php esc_html_e( 'Selected Teams First shows only the opponent when one team is selected; Matchup always shows away at home.', '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 puts a selected team first with vs or at as the separator.', '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 ) : ?>
<option value="<?php echo esc_attr( (string) $league->term_id ); ?>" <?php selected( $league_id, (int) $league->term_id ); ?>>
<?php echo esc_html( $league->name ); ?>
</option>
<?php endforeach; ?>
</select>
</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>
<?php foreach ( $seasons as $season ) : ?>
<option value="<?php echo esc_attr( (string) $season->term_id ); ?>" <?php selected( $season_id, (int) $season->term_id ); ?>>
<?php echo esc_html( $season->name ); ?>
</option>
<?php endforeach; ?>
</select>
</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[]" multiple="multiple" size="<?php echo esc_attr( (string) min( 8, max( 3, count( $teams ) ) ) ); ?>">
<?php foreach ( $teams as $team ) : ?>
<option value="<?php echo esc_attr( (string) $team->ID ); ?>" <?php selected( in_array( (int) $team->ID, $team_ids, true ) ); ?>>
<?php echo esc_html( $team->post_title ); ?>
</option>
<?php endforeach; ?>
</select>
<p class="description"><?php esc_html_e( 'Select one or more teams. Team layout keeps one selected team first for each game.', 'tonys-sportspress-enhancements' ); ?></p>
</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>
<div style="margin-bottom:16px;">
<label for="tse-public-month-pages" style="display:inline-flex;align-items:center;gap:6px;">
<input id="tse-public-month-pages" type="checkbox" name="month_pages" value="1" <?php checked( isset( $_GET['month_pages'] ) && '1' === (string) wp_unslash( $_GET['month_pages'] ) ); ?> />
<strong><?php esc_html_e( 'Print each month on its own page', 'tonys-sportspress-enhancements' ); ?></strong>
</label>
</div>
</form>
<?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_ids, 'season_id' => $season_id, 'league_id' => $league_id, 'field_id' => $field_id, 'format' => $subformat, 'title_format' => $title_format ), 'csv' );
$ics_url = tse_sp_event_export_get_feed_url( array( 'team_id' => $team_ids, 'season_id' => $season_id, 'league_id' => $league_id, 'field_id' => $field_id, 'title_format' => $title_format ), 'ics' );
$print_url = tse_sp_schedule_exporter_get_printable_url( $team_ids, $season_id, 'letter', $league_id, false, $field_id, false, 'name', 'name', $title_format );
$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>
<button type="button" class="button button-primary tse-ics-ios-link" data-ics-url="<?php echo esc_url( $ics_url ); ?>" title="<?php esc_attr_e( 'Subscribe on iPhone or iPad', 'tonys-sportspress-enhancements' ); ?>" style="display:none;"><?php esc_html_e( 'Subscribe on iPhone/iPad', 'tonys-sportspress-enhancements' ); ?></button>
<button type="button" class="button tse-ics-android-link" data-ics-url="<?php echo esc_url( $ics_url ); ?>" title="<?php esc_attr_e( 'Subscribe on Android', 'tonys-sportspress-enhancements' ); ?>" style="display:none;"><?php esc_html_e( 'Subscribe on Android', 'tonys-sportspress-enhancements' ); ?></button>
</div>
<p class="description tse-output-note"><?php esc_html_e( 'Use the buttons to copy the generated URL or open the right destination for this export type.', 'tonys-sportspress-enhancements' ); ?></p>
</div>
<?php
$output = (string) ob_get_clean();
return $output . tse_sp_schedule_exporter_render_link_sync_script();
}
/**
* Render the schedule exporter block.
*
* @return string
*/
function tse_sp_schedule_exporter_render_block() {
if ( is_admin() || ( function_exists( 'wp_is_json_request' ) && wp_is_json_request() ) ) {
return '<div class="tse-schedule-exporter-block-placeholder"><strong>' . esc_html__( 'Schedule Exporter', 'tonys-sportspress-enhancements' ) . '</strong><p>' . esc_html__( 'The schedule exporter renders on the frontend.', 'tonys-sportspress-enhancements' ) . '</p></div>';
}
return tse_sp_schedule_exporter_render_shortcode();
}
/**
* Get teams for the exporter.
*
* @return WP_Post[]
*/
function tse_sp_schedule_exporter_get_leagues() {
$leagues = get_terms(
array(
'taxonomy' => 'sp_league',
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
)
);
if ( is_wp_error( $leagues ) || ! is_array( $leagues ) ) {
return array();
}
return $leagues;
}
/**
* Get seasons for the exporter.
*
* @return WP_Term[]
*/
function tse_sp_schedule_exporter_get_seasons() {
$seasons = get_terms(
array(
'taxonomy' => 'sp_season',
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
)
);
if ( is_wp_error( $seasons ) || ! is_array( $seasons ) ) {
return array();
}
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.
*
* @param int $league_id League ID.
* @param int $season_id Season ID.
* @return WP_Post[]
*/
function tse_sp_schedule_exporter_get_teams( $league_id = 0, $season_id = 0 ) {
$tax_query = array();
if ( $league_id > 0 ) {
$tax_query[] = array(
'taxonomy' => 'sp_league',
'field' => 'term_id',
'terms' => array( $league_id ),
);
}
if ( $season_id > 0 ) {
$tax_query[] = array(
'taxonomy' => 'sp_season',
'field' => 'term_id',
'terms' => array( $season_id ),
);
}
$args = array(
'post_type' => 'sp_team',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'cache_results' => false,
);
if ( ! empty( $tax_query ) ) {
if ( count( $tax_query ) > 1 ) {
$tax_query['relation'] = 'AND';
}
$args['tax_query'] = $tax_query;
}
$teams = get_posts( $args );
return is_array( $teams ) ? $teams : array();
}
/**
* Resolve selected team IDs.
*
* @param WP_Post[] $teams Team posts.
* @return int[]
*/
function tse_sp_schedule_exporter_resolve_team_ids( $teams ) {
$available = array();
foreach ( $teams as $team ) {
if ( $team instanceof WP_Post ) {
$available[] = (int) $team->ID;
}
}
$requested = array();
if ( isset( $_GET['team_id'] ) ) {
$requested = tse_sp_event_export_parse_id_list( wp_unslash( $_GET['team_id'] ) );
}
$selected = array_values( array_intersect( $requested, $available ) );
if ( ! empty( $selected ) ) {
return $selected;
}
if ( isset( $available[0] ) ) {
return array( (int) $available[0] );
}
return array();
}
/**
* 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.
*
* @param WP_Term[] $leagues League terms.
* @return int
*/
function tse_sp_schedule_exporter_resolve_league_id( $leagues ) {
$requested = isset( $_GET['league_id'] ) ? absint( wp_unslash( $_GET['league_id'] ) ) : 0;
if ( $requested > 0 ) {
return $requested;
}
foreach ( $leagues as $league ) {
if ( ! $league instanceof WP_Term ) {
continue;
}
$slug = isset( $league->slug ) ? strtolower( (string) $league->slug ) : '';
$name = isset( $league->name ) ? strtolower( trim( (string) $league->name ) ) : '';
if ( 'cmba' === $slug || 'cmba' === $name ) {
return (int) $league->term_id;
}
}
if ( isset( $leagues[0] ) && $leagues[0] instanceof WP_Term ) {
return (int) $leagues[0]->term_id;
}
return 0;
}
/**
* Resolve selected season ID.
*
* @param WP_Term[] $seasons Season terms.
* @return int
*/
function tse_sp_schedule_exporter_resolve_season_id( $seasons ) {
$requested = isset( $_GET['season_id'] ) ? absint( wp_unslash( $_GET['season_id'] ) ) : 0;
if ( $requested > 0 ) {
return $requested;
}
$current = absint( (string) get_option( 'sportspress_season', '0' ) );
if ( $current > 0 ) {
return $current;
}
if ( isset( $seasons[0] ) && is_object( $seasons[0] ) && isset( $seasons[0]->term_id ) ) {
return (int) $seasons[0]->term_id;
}
return 0;
}
/**
* Get supported paper sizes.
*
* @return array
*/
function tse_sp_schedule_exporter_get_paper_sizes() {
return array(
'letter' => __( 'Letter', 'tonys-sportspress-enhancements' ),
'ledger' => __( '11x17 / Ledger', 'tonys-sportspress-enhancements' ),
);
}
/**
* Resolve selected paper size.
*
* @return string
*/
function tse_sp_schedule_exporter_resolve_paper_size() {
$paper = isset( $_GET['paper'] ) ? sanitize_key( wp_unslash( $_GET['paper'] ) ) : 'letter';
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 );
}
/**
* Resolve selected title format.
*
* @return string
*/
function tse_sp_schedule_exporter_resolve_title_format() {
$requested = isset( $_GET['title_format'] ) ? sanitize_key( wp_unslash( $_GET['title_format'] ) ) : 'selected_first';
return tse_sp_schedule_exporter_resolve_title_format_value( $requested );
}
/**
* Normalize a title format value.
*
* @param string $value Raw value.
* @return string
*/
function tse_sp_schedule_exporter_resolve_title_format_value( $value ) {
$value = sanitize_key( (string) $value );
return in_array( $value, array( 'selected_first', 'matchup' ), true ) ? $value : 'selected_first';
}
/**
* 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;
}
/**
* Collect team schedule events for export.
*
* @param int $team_id Team ID.
* @param int $season_id Optional season ID.
* @param int $league_id Optional league ID.
* @return array
*/
function tse_sp_schedule_exporter_get_events( $team_id, $season_id = 0, $league_id = 0 ) {
$team_id = absint( $team_id );
if ( $team_id <= 0 || 'sp_team' !== get_post_type( $team_id ) ) {
return array();
}
$args = array(
'post_type' => 'sp_event',
'post_status' => array( 'publish', 'future' ),
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'ASC',
'no_found_rows' => true,
'meta_query' => array(
array(
'key' => 'sp_team',
'value' => array( (string) $team_id ),
'compare' => 'IN',
),
),
);
$tax_query = array();
if ( $season_id > 0 ) {
$tax_query[] = array(
'taxonomy' => 'sp_season',
'field' => 'term_id',
'terms' => array( $season_id ),
);
}
if ( $league_id > 0 ) {
$tax_query[] = array(
'taxonomy' => 'sp_league',
'field' => 'term_id',
'terms' => array( $league_id ),
);
}
if ( ! empty( $tax_query ) ) {
if ( count( $tax_query ) > 1 ) {
$tax_query['relation'] = 'AND';
}
$args['tax_query'] = $tax_query;
}
$query = new WP_Query( $args );
$events = array();
$team_name = get_the_title( $team_id );
foreach ( $query->posts as $event ) {
$event_id = $event instanceof WP_Post ? (int) $event->ID : 0;
if ( $event_id <= 0 ) {
continue;
}
$teams = array_values( array_unique( array_map( 'intval', get_post_meta( $event_id, 'sp_team', false ) ) ) );
if ( ! in_array( $team_id, $teams, true ) ) {
continue;
}
$home_id = isset( $teams[0] ) ? (int) $teams[0] : 0;
$away_id = isset( $teams[1] ) ? (int) $teams[1] : 0;
$location_flag = $home_id === $team_id ? 'Home' : 'Away';
$opponent_id = $home_id === $team_id ? $away_id : $home_id;
$venue = tse_sp_schedule_exporter_get_primary_venue( $event_id );
$events[] = array(
'label' => '',
'event_id' => $event_id,
'date' => get_post_time( 'm/d/Y', false, $event_id, true ),
'time' => strtoupper( (string) ( function_exists( 'sp_get_time' ) ? sp_get_time( $event_id ) : get_post_time( get_option( 'time_format' ), false, $event_id, true ) ) ),
'team_name' => is_string( $team_name ) ? $team_name : '',
'opponent_name' => $opponent_id > 0 ? get_the_title( $opponent_id ) : __( 'TBD', 'tonys-sportspress-enhancements' ),
'location_flag' => $location_flag,
'home_team' => $home_id > 0 ? get_the_title( $home_id ) : '',
'away_team' => $away_id > 0 ? get_the_title( $away_id ) : '',
'venue_name' => isset( $venue['name'] ) ? $venue['name'] : '',
'venue_abbreviation' => isset( $venue['abbreviation'] ) ? $venue['abbreviation'] : '',
'venue_short_name' => isset( $venue['short_name'] ) ? $venue['short_name'] : '',
);
}
foreach ( $events as $index => $event ) {
$events[ $index ]['label'] = sprintf( 'G#%02d', $index + 1 );
}
wp_reset_postdata();
return $events;
}
/**
* Get the primary venue details for an event.
*
* @param int $event_id Event ID.
* @return array
*/
function tse_sp_schedule_exporter_get_primary_venue( $event_id ) {
$venues = get_the_terms( $event_id, 'sp_venue' );
if ( ! is_array( $venues ) || ! isset( $venues[0] ) || ! $venues[0] instanceof WP_Term ) {
return array(
'name' => '',
'abbreviation' => '',
'short_name' => '',
);
}
$venue = $venues[0];
return array(
'name' => isset( $venue->name ) ? (string) $venue->name : '',
'abbreviation' => trim( (string) get_term_meta( $venue->term_id, 'tse_abbreviation', true ) ),
'short_name' => trim( (string) get_term_meta( $venue->term_id, 'tse_short_name', true ) ),
);
}
/**
* Build the printable page URL.
*
* @param int $team_id Team ID.
* @param int $season_id Season ID.
* @param string $paper Paper size.
* @param int $league_id League ID.
* @param string $team_label_mode Team label mode.
* @param string $field_label_mode Field label mode.
* @param string $title_format Title format.
* @return string
*/
function tse_sp_schedule_exporter_get_printable_url( $team_id, $season_id, $paper, $league_id = 0, $autoprint = false, $field_id = 0, $month_pages = false, $team_label_mode = 'name', $field_label_mode = 'name', $title_format = 'selected_first' ) {
$team_ids = is_array( $team_id ) ? array_values( array_filter( array_map( 'absint', $team_id ) ) ) : array( absint( $team_id ) );
return add_query_arg(
array(
Tony_Sportspress_Printable_Calendars::QUERY_FLAG => '1',
'sp_team' => implode( ',', $team_ids ),
'sp_season' => $season_id > 0 ? (string) absint( $season_id ) : '',
'sp_league' => $league_id > 0 ? (string) absint( $league_id ) : '',
'sp_field' => $field_id > 0 ? (string) absint( $field_id ) : '',
'team_label' => sanitize_key( (string) $team_label_mode ),
'field_label' => sanitize_key( (string) $field_label_mode ),
'title_format' => tse_sp_schedule_exporter_resolve_title_format_value( $title_format ),
'paper' => $paper,
'autoprint' => $autoprint ? '1' : '',
'month_pages' => $month_pages ? '1' : '',
),
home_url( '/' )
);
}
/**
* Render a small script that keeps export links in sync with current selections.
*
* @param bool $echo Whether to echo immediately.
* @return string
*/
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) {
return;
}
var league = form.querySelector('[name="league_id"]');
var season = form.querySelector('[name="season_id"]');
var team = form.querySelector('[name="team_id[]"], [name="team_id"]');
var exportType = form.querySelector('[name="export_type"]');
var subformat = form.querySelector('[name="subformat"]');
var titleFormat = form.querySelector('[name="title_format"]');
var field = form.querySelector('[name="field_id"]');
var monthPages = form.querySelector('[name="month_pages"]');
var outputUrl = scope.querySelector('.tse-output-url');
var openButton = scope.querySelector('.tse-open-link');
var iosButton = scope.querySelector('.tse-ics-ios-link');
var androidButton = scope.querySelector('.tse-ics-android-link');
var outputNote = scope.querySelector('.tse-output-note');
var copyButton = scope.querySelector('.tse-copy-link');
var teamValues = team ? Array.prototype.slice.call(team.selectedOptions || []).map(function(option){
return option.value;
}).filter(function(value){
return value && value !== '0';
}) : [];
if (!teamValues.length && team && team.value && team.value !== '0') {
teamValues = [team.value];
}
var teamValue = teamValues.length ? teamValues.join(',') : '0';
var activeSubformat = subformat ? (subformat.value || 'matchup') : 'matchup';
var selectedExportType = exportType ? (exportType.value || 'csv') : 'csv';
scope.querySelectorAll('[data-column-group]').forEach(function(group){
var visible = selectedExportType === 'csv' && group.getAttribute('data-column-group') === activeSubformat;
group.style.display = visible ? 'block' : 'none';
});
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');
if (titleFormat) csvUrl.searchParams.set('title_format', titleFormat.value || 'selected_first');
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');
if (titleFormat) icsUrl.searchParams.set('title_format', titleFormat.value || 'selected_first');
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);
if (field) printUrl.searchParams.set('sp_field', field.value || '0');
if (titleFormat) printUrl.searchParams.set('title_format', titleFormat.value || 'selected_first');
if (monthPages && monthPages.checked) {
printUrl.searchParams.set('month_pages', '1');
} else {
printUrl.searchParams.delete('month_pages');
}
printUrl.searchParams.set('paper', 'letter');
}
var resolvedUrl = csvUrl ? csvUrl.toString() : '';
var label = 'Open URL in New Tab';
var disabled = false;
var note = 'Use the buttons to copy the generated URL or open the right destination for this export type.';
var iosUrl = icsUrl ? icsUrl.toString().replace(/^https?:\/\//, 'webcal://') : '';
var androidUrl = icsUrl ? 'https://calendar.google.com/calendar/render?cid=' + encodeURIComponent(icsUrl.toString()) : '';
if (selectedExportType === 'ics' && icsUrl) {
resolvedUrl = icsUrl.toString();
note = 'Use the iPhone/iPad or Android button to subscribe, or copy the feed URL.';
} else if (selectedExportType === 'printable' && printUrl) {
resolvedUrl = printUrl.toString();
if (teamValue === '0') {
disabled = true;
note = 'Printable requires at least one selected team.';
}
} else if (selectedExportType === 'csv' && activeSubformat === 'team' && teamValue === '0') {
disabled = true;
note = 'CSV team layout requires at least one selected team.';
}
if (outputUrl) {
outputUrl.value = resolvedUrl;
}
if (openButton) {
openButton.dataset.currentUrl = resolvedUrl;
openButton.textContent = label;
openButton.style.display = selectedExportType === 'ics' ? 'none' : 'inline-flex';
openButton.disabled = disabled;
openButton.setAttribute('aria-disabled', disabled ? 'true' : 'false');
openButton.style.opacity = disabled ? '0.55' : '1';
}
if (iosButton) {
iosButton.dataset.currentUrl = iosUrl;
iosButton.style.display = selectedExportType === 'ics' ? 'inline-flex' : 'none';
iosButton.disabled = !iosUrl;
iosButton.setAttribute('aria-disabled', !iosUrl ? 'true' : 'false');
iosButton.style.opacity = !iosUrl ? '0.55' : '1';
}
if (androidButton) {
androidButton.dataset.currentUrl = androidUrl;
androidButton.style.display = selectedExportType === 'ics' ? 'inline-flex' : 'none';
androidButton.disabled = !androidUrl;
androidButton.setAttribute('aria-disabled', !androidUrl ? 'true' : 'false');
androidButton.style.opacity = !androidUrl ? '0.55' : '1';
}
if (outputNote) {
outputNote.textContent = note;
}
if (copyButton) {
copyButton.disabled = disabled;
}
}
document.querySelectorAll('.tse-schedule-exporter, .wrap').forEach(function(scope){
if (!scope.querySelector('.tse-schedule-exporter-form')) {
return;
}
syncLinks(scope);
scope.querySelectorAll('.tse-schedule-exporter-form select, .tse-schedule-exporter-form input[type="checkbox"]').forEach(function(input){
input.addEventListener('change', function(){
if (input.dataset.autoSubmit === '1') {
input.form.submit();
return;
}
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 iosButton = scope.querySelector('.tse-ics-ios-link');
var androidButton = scope.querySelector('.tse-ics-android-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');
});
}
if (iosButton) {
iosButton.addEventListener('click', function(){
var targetUrl = iosButton.dataset.currentUrl || '';
if (iosButton.disabled || !targetUrl) {
return;
}
window.location.href = targetUrl;
});
}
if (androidButton) {
androidButton.addEventListener('click', function(){
var targetUrl = androidButton.dataset.currentUrl || '';
if (androidButton.disabled || !targetUrl) {
return;
}
window.open(targetUrl, '_blank', 'noopener,noreferrer');
});
}
});
})();
</script>
HTML;
if ( $echo ) {
echo $script; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return '';
}
return $script;
}