Google_Calendar::generate_single_url( string|int|TribeEventsViewsV2iCalendarLinksWP_post $post = null )
Generate a link that will import a single event into Google Calendar.
Contents
Required link items: action=TEMPLATE text=[the title of the event] dates= in YYYYMMDDHHMMSS format. start datetime / end datetime
Optional link items: ctz=[time zone] details=[event details] location=[event location]
URL format: https://www.google.com/calendar/render?action=TEMPLATE&text=Title&dates=20190227/20190228
Parameters
- $post
-
(string|int|TribeEventsViewsV2iCalendarLinksWP_post) (Optional) The ID or post object the rui is for, defaults to the current post.
Default value: null
Return
(string) URL string. Empty string if post not found or post is not an event.
Source
File: src/Tribe/Views/V2/iCalendar/Links/Google_Calendar.php
public function generate_single_url( $post = null ) {
if ( empty( $post ) ) {
$post = get_the_ID();
}
$event = tribe_get_event( $post );
if ( empty( $event ) || ! tribe_is_event( $event ) ) {
return '';
}
$base_url = 'https://www.google.com/calendar/event';
/**
* Allow users to Filter our Google Calendar Link base URL before constructing the URL.
* After this filter, the list will be trimmed to remove any empty values and discarded if any required params are missing.
* Returning an empty/falsy value here will short-circuit the function to bail out now with an empty string.
*
* @since 5.14.0
*
* @var array $base_url The base url used in the add_query_arg.
* @var WP_Post $event The Event the link is for. As decorated by tribe_get_event().
*/
$base_url = apply_filters( 'tec_views_v2_single_event_gcal_link_base_url', $base_url, $event );
if ( empty( $base_url ) ) {
return '';
}
$event_details = empty( $event->description ) ? urlencode( $event->description ) : '';
if ( ! empty( $event_details ) ) {
//Truncate Event Description and add permalink if greater than 996 characters
$event_details = $this->format_event_details_for_url( $event_details, $event, 996 );
}
if ( Tribe__Timezones::is_mode( Tribe__Timezones::SITE_TIMEZONE ) ) {
$ctz = Tribe__Timezones::build_timezone_object()->getName();
} else {
$ctz = Tribe__Events__Timezones::get_event_timezone_string( $event->ID );
}
$pieces = [
'action' => 'TEMPLATE',
'dates' => $event->dates->start->format( 'Ymd\THis' ) . '/' . $event->dates->end->format( 'Ymd\THis' ),
'text' => rawurlencode( get_the_title( $event ) ),
'details' => $event_details,
'location' => self::generate_string_address( $event ),
'trp' => 'false',
'ctz' => $ctz,
'sprop' => 'website:' . home_url(),
];
/**
* Allow users to Filter our Google Calendar Link params
*
* @deprecated 5.14.0 Moved generic hook to something more specific and appropriate.
*
* @var array Params used in the add_query_arg
* @var int Event ID
*/
$pieces = apply_filters_deprecated(
'tribe_google_calendar_parameters',
[ $pieces, $event->ID ],
'5.14.0',
'tec_views_v2_single_event_gcal_link_parameters',
'Moved generic hook to something more specific and appropriate while moving function.'
);
/**
* Allow users to Filter our Google Calendar Link params before constructing the URL.
* After this filter, the list will be trimmed to remove any empty values and discarded if any required params are missing.
*
* @since 5.14.0
*
* @var array $pieces The params used in the add_query_arg.
* @var WP_Post $event The Event the link is for. As decorated by tribe_get_event().
*/
$pieces = apply_filters( 'tec_views_v2_single_event_gcal_link_parameters', $pieces, $event );
$pieces = array_filter( $pieces );
// Missing required info - bail.
if ( empty( $pieces[ 'action' ] ) || empty( $pieces[ 'dates' ] ) || empty( $pieces[ 'text' ] ) ) {
return '';
}
$url = add_query_arg( $pieces, $base_url );
/**
* Allow users to Filter our Google Calendar Link URL - after all params have been applied to the URL.
*
* @since 5.14.0
*
* @var array $url The url to use.
* @var WP_Post $event The Event the link is for. As decorated by tribe_get_event().
*/
return apply_filters( 'tec_views_v2_single_gcal_subscribe_link', $url, $event );
}
Changelog
| Version | Description |
|---|---|
| 5.14.0 | Introduced. |