Last active
May 15, 2018 21:53
-
-
Save andrasguseo/b8e2739b11fdbd2d5ebacce23d862046 to your computer and use it in GitHub Desktop.
TEC - iCal export of this year's events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Description: This snippet will enable you to have an iCal export of all this year's events | |
* when using this url: | |
* http://example.com/events/?ical=1&tribe_display=custom | |
* | |
* Formats you can use: | |
* Simple: exports events of this year, i.e. Jan 1st to Dec 31 of current year | |
* http://example.com/events/?ical=1&tribe_display=custom | |
* | |
* If no end date is defined then events until the end of the current year will be exported | |
* I.e. from Feb 2, 2017 until end of current year | |
* http://example.com/events/?ical=1&tribe_display=custom&start_date=2017-02-10 | |
* | |
* If no start date is defined, then events starting from Jan 1 of current year will be exported | |
* I.e. from Jan 1 of current year until July 31, 2018 | |
* http://example.com/events/?ical=1&tribe_display=custom&end_date=2018-07-31 | |
* | |
* Define start and end date | |
* I.e. from July 1, 2017 until June 30, 2018 | |
* http://example.com/events/?ical=1&tribe_display=custom&start_date=2017-07-01&end_date=2018-06-30 | |
* | |
* Define start year: events from January 1 of that year will be exported | |
* I.e. from Jan 1, current year until Dec 31, 2019 | |
* http://example.com/events/?ical=1&tribe_display=custom&start_date=2019 | |
* | |
* Define end year: events until December 31 of that year will be exported | |
* I.e. from Jan 1, current year until Dec 31, 2019 | |
* http://example.com/events/?ical=1&tribe_display=custom&end_date=2019 | |
* | |
* Paste the code in your (child) theme's functions.php file | |
* | |
* Required plugins: The Events Calendar | |
* Last updated: 2017-11-28 | |
* Author: Brook Harding, Andras Guseo | |
*/ | |
add_action( 'pre_get_posts', 'filter_ical_query' ); | |
function filter_ical_query( $query ) { | |
if ( ! isset( $_GET['ical'] ) || ! isset( $query->tribe_is_event_query ) || ! $query->tribe_is_event_query ) return; | |
$tribe_display = $_GET[ 'tribe_display' ]; | |
$start_date = isset( $_GET[ 'start_date' ] ) ? $_GET[ 'start_date' ] : "" ; | |
$end_date = isset( $_GET[ 'end_date' ] ) ? $_GET[ 'end_date' ] : ""; | |
if( $tribe_display === 'custom' ) { | |
// Check if there is a start_date set | |
if( isset( $start_date ) && !empty( $start_date ) ) { | |
// Full date | |
if ( validateDate( $start_date, 'Y-m-d' ) ) { | |
$start_of_year = $start_date; | |
} | |
// Only year, then from beggining of that year | |
elseif ( validateDate( $start_date, 'Y' ) ) { | |
$start_of_year = $start_date . '-01-01'; | |
} | |
// If set to anything else then fall back to this year's beginning | |
else { | |
$start_of_year = date( 'Y' ) . '-01-01'; | |
} | |
} | |
// If not, fall back to this year's beginning | |
else { | |
$start_of_year = date( 'Y' ) . '-01-01'; | |
} | |
// Check if there is an end_date set | |
if( isset( $end_date ) && !empty( $end_date ) ) { | |
// Full date | |
if( validateDate( $end_date, 'Y-m-d' ) ) { | |
$end_of_year = $end_date; | |
} | |
// only year, then end of that year (Max. 3 years ahead) | |
elseif( validateDate( $end_date, 'Y' ) && date( 'Y' ) <= $end_date && $end_date <= date('Y') + 3 ) { | |
$end_of_year = $end_date . '-12-31'; | |
} | |
} | |
// If there is no end date but there was a start year defined, then till the end of that year | |
elseif( validateDate( $start_date, 'Y' ) ) { | |
$end_of_year = $start_date . '-12-31'; | |
} | |
// If no end date defined, fall back to this year's end | |
else { | |
$end_of_year = date( 'Y' ) . '-12-31'; | |
} | |
$query->set( 'eventDisplay', 'custom' ); | |
$query->set( 'start_date', $start_of_year ); | |
$query->set( 'end_date', $end_of_year ); | |
$query->set( 'posts_per_page', - 1 ); | |
} | |
} | |
function validateDate($date, $format = 'Y-m-d H:i:s') | |
{ | |
$d = DateTime::createFromFormat($format, $date); | |
return $d && $d->format($format) == $date; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment