slug; } } if ( $slugs ) { $url = add_query_arg( 'evwr_cat', implode( ',', $slugs ), $url ); } } return $url; } /** * .ics download URL of one event. * * @param int $event_id Event. * @return string */ public static function event_url( $event_id ) { return add_query_arg( 'evwr_ics', (int) $event_id, home_url( '/' ) ); } /** * webcal:// version of a feed URL. * * @param string $url URL. * @return string */ public static function webcal_url( $url ) { return preg_replace( '#^https?://#', 'webcal://', $url ); } /** * Google Calendar "add by URL" link. * * @param string $feed Feed URL. * @return string */ public static function google_subscribe_url( $feed ) { return 'https://calendar.google.com/calendar/render?cid=' . rawurlencode( self::webcal_url( $feed ) ); } /** * Google Calendar "add this event" link for one occurrence. * * @param int $event_id Event. * @param array $occ Occurrence row. * @return string */ public static function google_event_url( $event_id, array $occ ) { if ( $occ['all_day'] ) { $dates = gmdate( 'Ymd', strtotime( $occ['start_local'] . ' UTC' ) ) . '/' . gmdate( 'Ymd', strtotime( substr( $occ['end_local'], 0, 10 ) . ' 00:00:00 UTC' ) + DAY_IN_SECONDS ); } else { $dates = gmdate( 'Ymd\THis\Z', strtotime( $occ['start_utc'] . ' UTC' ) ) . '/' . gmdate( 'Ymd\THis\Z', strtotime( $occ['end_utc'] . ' UTC' ) ); } $venue = eventwright_get_venue( (int) get_post_meta( $event_id, '_evwr_venue_id', true ) ); $args = array( 'action' => 'TEMPLATE', 'text' => html_entity_decode( get_the_title( $event_id ), ENT_QUOTES, 'UTF-8' ), 'dates' => $dates, // Line breaks would be stripped from the URL, so the link follows a space. 'details' => trim( wp_trim_words( Views::excerpt( get_post( $event_id ), 40 ), 40, '…' ) . ' ' . get_permalink( $event_id ) ), ); if ( $venue ) { $args['location'] = trim( $venue['name'] . ', ' . $venue['full_address'], ', ' ); } return add_query_arg( array_map( 'rawurlencode', $args ), 'https://calendar.google.com/calendar/render' ); } /** * Serve .ics when asked. */ public static function maybe_output() { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Public read-only feeds. $single = get_query_var( 'evwr_ics' ); if ( ! $single && isset( $_GET['evwr_ics'] ) ) { $single = absint( $_GET['evwr_ics'] ); } $feed = get_query_var( 'evwr_ical' ) || isset( $_GET['evwr_ical'] ); // TEC kept its feed at /events/?ical=1; calendars subscribed to it keep working. if ( ! $feed && isset( $_GET['ical'] ) && Settings::get( 'tec_urls' ) && ! Compat_TEC::tec_active() ) { $page_id = (int) Settings::get( 'events_page_id' ); if ( $page_id && is_page( $page_id ) ) { $feed = true; } elseif ( is_singular( Post_Types::EVENT ) ) { $single = get_queried_object_id(); } } $cat_param = isset( $_GET['evwr_cat'] ) ? sanitize_text_field( wp_unslash( $_GET['evwr_cat'] ) ) : ''; // phpcs:enable if ( $single ) { // "/event//ical/" sets evwr_ics=1 with the event as the queried object. if ( 1 === (int) $single && is_singular( Post_Types::EVENT ) ) { $single = get_queried_object_id(); } $post = get_post( (int) $single ); if ( ! $post || Post_Types::EVENT !== $post->post_type || ! self::can_view( $post ) ) { return; } self::send( self::build( array( $post->ID ) ), sanitize_title( $post->post_name ? $post->post_name : 'event' ) . '.ics', true ); } if ( $feed ) { $cats = $cat_param ? Views::term_ids( $cat_param ) : array(); self::send( self::build_feed( $cats ), 'events.ics', false ); } } /** * Whether the visitor can see an event. * * @param \WP_Post $post Post. * @return bool */ private static function can_view( $post ) { if ( post_password_required( $post ) ) { return false; } if ( 'publish' === $post->post_status ) { return true; } return current_user_can( 'read_post', $post->ID ); } /** * Send an .ics response and stop. * * @param string $body Body. * @param string $filename File name. * @param bool $attachment Download instead of inline. */ private static function send( $body, $filename, $attachment ) { nocache_headers(); header( 'Content-Type: text/calendar; charset=utf-8' ); header( 'Content-Disposition: ' . ( $attachment ? 'attachment' : 'inline' ) . '; filename="' . $filename . '"' ); echo $body; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- iCalendar text, escaped by self::text(). exit; } /** * Feed: events from 30 days ago to one year ahead. * * @param int[] $cats Categories. * @return string */ public static function build_feed( array $cats ) { $rows = Occurrences::query( array( 'from' => wp_date( 'Y-m-d', time() - 30 * DAY_IN_SECONDS ) . ' 00:00:00', 'to' => wp_date( 'Y-m-d', time() + 365 * DAY_IN_SECONDS ) . ' 23:59:59', 'categories' => $cats, 'hide_hidden' => true, 'limit' => (int) apply_filters( 'eventwright_ical_feed_limit', 1000 ), ) ); Views::prime( $rows ); return self::calendar( $rows ); } /** * Calendar for some events (all their occurrences). * * @param int[] $event_ids Events. * @return string */ public static function build( array $event_ids ) { $rows = array(); foreach ( $event_ids as $id ) { foreach ( Occurrences::for_event( $id ) as $row ) { $rows[] = $row; } } return self::calendar( $rows ); } /** * VCALENDAR text for occurrence rows. * * @param array $rows Rows. * @return string */ public static function calendar( array $rows ) { $host = wp_parse_url( home_url(), PHP_URL_HOST ); $lines = array( 'BEGIN:VCALENDAR', 'VERSION:2.0', 'PRODID:-//Eventwright//Eventwright ' . EVWR_VERSION . '//EN', 'CALSCALE:GREGORIAN', 'METHOD:PUBLISH', 'X-WR-CALNAME:' . self::text( html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES, 'UTF-8' ) ), 'X-ORIGINAL-URL:' . self::text( home_url( '/' ) ), 'REFRESH-INTERVAL;VALUE=DURATION:PT6H', 'X-PUBLISHED-TTL:PT6H', ); $stamp = gmdate( 'Ymd\THis\Z' ); foreach ( $rows as $row ) { $id = (int) $row['event_id']; $post = get_post( $id ); if ( ! $post ) { continue; } $lines[] = 'BEGIN:VEVENT'; $lines[] = 'UID:' . $id . '-' . gmdate( 'Ymd\THis', strtotime( $row['start_utc'] . ' UTC' ) ) . '@' . $host; $lines[] = 'DTSTAMP:' . $stamp; if ( $row['all_day'] ) { $lines[] = 'DTSTART;VALUE=DATE:' . gmdate( 'Ymd', strtotime( $row['start_local'] . ' UTC' ) ); $lines[] = 'DTEND;VALUE=DATE:' . gmdate( 'Ymd', strtotime( substr( $row['end_local'], 0, 10 ) . ' 00:00:00 UTC' ) + DAY_IN_SECONDS ); } else { $lines[] = 'DTSTART:' . gmdate( 'Ymd\THis\Z', strtotime( $row['start_utc'] . ' UTC' ) ); $lines[] = 'DTEND:' . gmdate( 'Ymd\THis\Z', strtotime( $row['end_utc'] . ' UTC' ) ); } $lines[] = 'SUMMARY:' . self::text( html_entity_decode( get_the_title( $post ), ENT_QUOTES, 'UTF-8' ) ); $desc = Views::excerpt( $post, 80 ); if ( $desc ) { $lines[] = 'DESCRIPTION:' . self::text( $desc . "\n\n" . get_permalink( $post ) ); } $lines[] = 'URL:' . self::text( eventwright_occurrence_url( $id, $row['start_local'] ) ); $venue = eventwright_get_venue( (int) get_post_meta( $id, '_evwr_venue_id', true ) ); if ( $venue ) { $lines[] = 'LOCATION:' . self::text( trim( $venue['name'] . ', ' . $venue['full_address'], ', ' ) ); if ( $venue['lat'] && $venue['lng'] ) { $lines[] = 'GEO:' . (float) $venue['lat'] . ';' . (float) $venue['lng']; } } elseif ( get_post_meta( $id, '_evwr_online_url', true ) ) { $lines[] = 'LOCATION:' . self::text( get_post_meta( $id, '_evwr_online_url', true ) ); } $terms = get_the_terms( $id, Post_Types::CATEGORY ); if ( $terms && ! is_wp_error( $terms ) ) { $lines[] = 'CATEGORIES:' . implode( ',', array_map( array( __CLASS__, 'text' ), wp_list_pluck( $terms, 'name' ) ) ); } if ( 'cancelled' === get_post_meta( $id, '_evwr_status', true ) ) { $lines[] = 'STATUS:CANCELLED'; } else { $lines[] = 'STATUS:CONFIRMED'; } $lines[] = 'LAST-MODIFIED:' . gmdate( 'Ymd\THis\Z', strtotime( $post->post_modified_gmt . ' UTC' ) ); $lines[] = 'END:VEVENT'; } $lines[] = 'END:VCALENDAR'; $out = ''; foreach ( $lines as $line ) { $out .= self::fold( $line ) . "\r\n"; } return $out; } /** * Escape text per RFC 5545. * * @param string $text Text. * @return string */ public static function text( $text ) { $text = str_replace( array( '\\', ';', ',', "\r\n", "\r", "\n" ), array( '\\\\', '\\;', '\\,', '\\n', '\\n', '\\n' ), (string) $text ); return $text; } /** * Fold long lines at 75 octets without splitting UTF-8 characters. * * @param string $line Line. * @return string */ public static function fold( $line ) { if ( strlen( $line ) <= 75 ) { return $line; } $out = ''; $limit = 75; while ( strlen( $line ) > $limit ) { $cut = $limit; // Step back to a character boundary. while ( $cut > 0 && ( ord( $line[ $cut ] ) & 0xC0 ) === 0x80 ) { --$cut; } $out .= substr( $line, 0, $cut ) . "\r\n "; $line = substr( $line, $cut ); $limit = 74; // Continuation lines start with a space. } return $out . $line; } }