cliff
9/13/2016 - 7:26 PM

The Events Calendar: Add Year's Events iCal export button to archive views (i.e. not Event Category or Single Event views)

The Events Calendar: Add Year's Events iCal export button to archive views (i.e. not Event Category or Single Event views)

<?php

/**
 * The Events Calendar: Add Year's Events iCal export button to archive views (i.e. not Event Category or Single Event views)
 *
 * From https://gist.github.com/cliffordp/f7a9ef262017cae04e6fd9645e478c0d This snippet.
 *
 * Updated on 2018-12-06 to work with TEC 4.4+.
 * Optionally, use in conjunction with https://theeventscalendar.com/content/uploads/2016/07/tribe-snippet-ical-export.zip
 * to generate the full year export links:
 * example.com/events/?ical=1&tribe_display=recently-published
 * example.com/events/?ical=1&tribe_display=year
 *
 * For reference: the default export button's code is in /wp-content/plugins/the-events-calendar/src/Tribe/iCal.php
 */
function cliff_tec_year_ical_export_button() {
	if (
		! class_exists( 'Tribe__Events__Main' )
		|| ! class_exists( 'Tribe__Events__iCal' )
	) {
		return false;
	}

	$tecmain = Tribe__Events__Main::instance();

	$tecical = new Tribe__Events__iCal;

	// https://developer.wordpress.org/reference/functions/is_post_type_archive/
	if ( ! is_post_type_archive( $tecmain::POSTTYPE ) ) {
		return false;
	}

	$year_link_title = esc_html__( 'Use this to share calendar data with Google Calendar, Apple iCal and other compatible apps', 'the-events-calendar' );

	// If desired, change 'year' to 'recently-published'
	$year_link = add_query_arg( 'tribe_display', 'year', $tecical->get_ical_link() );

	// display specific year to avoid confusion when user navigates forward into next year (e.g. 2017) and then clicks the Export Year's Events button
	// should probably change this text if you use 'recently-published' iCal link instead
	$button_text = sprintf( "+ Export %d's Events", date( 'Y' ) );

	// output button below other iCal Export button
	// do not add .tribe-events-ical to avoid JavaScript overwriting the href
	// which is why inline styling is added here. Also, looks bad on mobile as-is: https://cl.ly/0d2K3y3f0M2m -- suggested to add your own styling along the lines of .tribe-events-ical until that bug is fixed
	printf(
		'<a style="clear: right; float: right; margin-top: 21px;" class="tribe-events-ical-year tribe-events-button" title="%s" href="%s">%s</a>',
		$year_link_title,
		esc_url( $year_link ),
		$button_text
	);
}

add_action( 'tribe_events_after_footer', 'cliff_tec_year_ical_export_button', 20 ); // priority higher than 10 to make it appear below the default export buttons