setTimezone( new DateTimeZone( 'UTC' ) ); return $dt->format( 'Y-m-d H:i:s' ); } catch ( Exception $e ) { return $local; } } /** * Timestamp for a local date-time in a timezone. * * @param string $local Local date-time. * @param string $tz Timezone. * @return int */ function eventwright_local_to_timestamp( $local, $tz ) { try { $dt = new DateTime( $local, new DateTimeZone( $tz ) ); return $dt->getTimestamp(); } catch ( Exception $e ) { return (int) strtotime( $local . ' UTC' ); } } /** * ISO 8601 with offset for a local date-time in a timezone. * * @param string $local Local date-time. * @param string $tz Timezone. * @param bool $date_only Only the date part. * @return string */ function eventwright_iso8601( $local, $tz, $date_only = false ) { if ( $date_only ) { return substr( $local, 0, 10 ); } try { $dt = new DateTime( $local, new DateTimeZone( $tz ) ); return $dt->format( 'c' ); } catch ( Exception $e ) { return $local; } } /** * Current time in UTC 'Y-m-d H:i:s'. * * @return string */ function eventwright_now_utc() { return gmdate( 'Y-m-d H:i:s' ); } /** * Today's date in the site timezone. * * @return string Y-m-d */ function eventwright_today_local() { return wp_date( 'Y-m-d' ); } /** * Format a local date-time with a WordPress date format, without shifting timezones. * * @param string $format PHP date format. * @param string $local Local 'Y-m-d H:i:s'. * @return string */ function eventwright_format_local( $format, $local ) { $ts = strtotime( $local . ' UTC' ); if ( false === $ts ) { return ''; } return wp_date( $format, $ts, new DateTimeZone( 'UTC' ) ); } /** * Site date format. * * @return string */ function eventwright_date_format() { $f = get_option( 'date_format' ); return $f ? $f : 'F j, Y'; } /** * Site time format. * * @return string */ function eventwright_time_format() { $f = get_option( 'time_format' ); return $f ? $f : 'g:i a'; } /** * Human text for when an occurrence happens, e.g. "December 1, 2026, 6:00 pm – 8:00 pm". * * @param string $start Local start. * @param string $end Local end. * @param bool $all_day All day. * @param string $tz Timezone shown when it differs from the site timezone ('' to hide). * @return string */ function eventwright_format_when( $start, $end, $all_day, $tz = '' ) { $df = eventwright_date_format(); $tf = eventwright_time_format(); $start_date = substr( $start, 0, 10 ); $end_date = $end ? substr( $end, 0, 10 ) : $start_date; if ( $all_day ) { if ( $start_date === $end_date ) { $out = eventwright_format_local( $df, $start ); } else { /* translators: 1: start date, 2: end date */ $out = sprintf( __( '%1$s – %2$s', 'eventwright' ), eventwright_format_local( $df, $start ), eventwright_format_local( $df, $end ) ); } return $out; } if ( ! $end || $end === $start ) { /* translators: 1: date, 2: time */ $out = sprintf( __( '%1$s, %2$s', 'eventwright' ), eventwright_format_local( $df, $start ), eventwright_format_local( $tf, $start ) ); } elseif ( $start_date === $end_date ) { /* translators: 1: date, 2: start time, 3: end time */ $out = sprintf( __( '%1$s, %2$s – %3$s', 'eventwright' ), eventwright_format_local( $df, $start ), eventwright_format_local( $tf, $start ), eventwright_format_local( $tf, $end ) ); } else { /* translators: 1: start date, 2: start time, 3: end date, 4: end time */ $out = sprintf( __( '%1$s, %2$s – %3$s, %4$s', 'eventwright' ), eventwright_format_local( $df, $start ), eventwright_format_local( $tf, $start ), eventwright_format_local( $df, $end ), eventwright_format_local( $tf, $end ) ); } if ( $tz && $tz !== eventwright_site_timezone_string() ) { $out .= ' (' . eventwright_timezone_label( $tz, $start ) . ')'; } return $out; } /** * Short label for a timezone at a moment, e.g. "EST" or "UTC+2". * * @param string $tz Timezone. * @param string $local Local time used to pick DST. * @return string */ function eventwright_timezone_label( $tz, $local ) { try { $dt = new DateTime( $local, new DateTimeZone( $tz ) ); $abbr = $dt->format( 'T' ); if ( preg_match( '/^[+-]\d/', $abbr ) ) { $abbr = 'UTC' . $dt->format( 'P' ); } return $abbr; } catch ( Exception $e ) { return $tz; } } /** * Event data for display. * * @param int|WP_Post $post Event. * @return array|null */ function eventwright_get_event( $post ) { $post = get_post( $post ); if ( ! $post || Eventwright\Post_Types::EVENT !== $post->post_type ) { return null; } $id = $post->ID; $start = (string) get_post_meta( $id, '_evwr_start', true ); $end = (string) get_post_meta( $id, '_evwr_end', true ); return array( 'id' => $id, 'title' => get_the_title( $post ), 'url' => get_permalink( $post ), 'start' => $start, 'end' => $end ? $end : $start, 'all_day' => (bool) get_post_meta( $id, '_evwr_all_day', true ), 'timezone' => eventwright_event_timezone( $id ), 'venue_id' => (int) get_post_meta( $id, '_evwr_venue_id', true ), 'organizer_ids' => array_map( 'intval', (array) get_post_meta( $id, '_evwr_organizer_ids', true ) ), 'cost' => (string) get_post_meta( $id, '_evwr_cost', true ), 'website' => (string) get_post_meta( $id, '_evwr_website', true ), 'online_url' => (string) get_post_meta( $id, '_evwr_online_url', true ), 'status' => (string) get_post_meta( $id, '_evwr_status', true ), 'recurrence' => Eventwright\Recurrence::get_rule( $id ), 'rsvp' => (bool) get_post_meta( $id, '_evwr_rsvp_enabled', true ), ); } /** * Venue data. * * @param int $venue_id Venue. * @return array|null */ function eventwright_get_venue( $venue_id ) { $post = $venue_id ? get_post( $venue_id ) : null; if ( ! $post || Eventwright\Post_Types::VENUE !== $post->post_type || 'trash' === $post->post_status ) { return null; } $v = array( 'id' => $post->ID, 'name' => get_the_title( $post ), 'url' => 'publish' === $post->post_status ? get_permalink( $post ) : '', ); foreach ( array( 'address', 'city', 'state', 'zip', 'country', 'phone', 'website', 'lat', 'lng' ) as $k ) { $v[ $k ] = get_post_meta( $post->ID, '_evwr_' . $k, true ); } $v['full_address'] = eventwright_venue_address_line( $v ); return $v; } /** * One-line address. * * @param array $v Venue data. * @return string */ function eventwright_venue_address_line( $v ) { $city_line = trim( implode( ' ', array_filter( array( $v['city'] ? $v['city'] . ( $v['state'] || $v['zip'] ? ',' : '' ) : '', $v['state'], $v['zip'] ) ) ) ); return implode( ', ', array_filter( array( $v['address'], $city_line, $v['country'] ) ) ); } /** * Map link for a venue (or '' when there is nothing to map or maps are off). * * @param array $v Venue data. * @return string */ function eventwright_venue_map_url( $v ) { $provider = Eventwright\Settings::get( 'map_provider' ); if ( 'none' === $provider ) { return ''; } $has_geo = ! empty( $v['lat'] ) && ! empty( $v['lng'] ); $query = $v['full_address'] ? $v['name'] . ', ' . $v['full_address'] : ''; if ( ! $has_geo && ! $query ) { return ''; } if ( 'osm' === $provider ) { if ( $has_geo ) { return sprintf( 'https://www.openstreetmap.org/?mlat=%1$s&mlon=%2$s#map=16/%1$s/%2$s', rawurlencode( (string) $v['lat'] ), rawurlencode( (string) $v['lng'] ) ); } return 'https://www.openstreetmap.org/search?query=' . rawurlencode( $query ); } $q = $has_geo ? $v['lat'] . ',' . $v['lng'] : $query; return 'https://www.google.com/maps/search/?api=1&query=' . rawurlencode( (string) $q ); } /** * Organizer data. * * @param int $organizer_id Organizer. * @return array|null */ function eventwright_get_organizer( $organizer_id ) { $post = $organizer_id ? get_post( $organizer_id ) : null; if ( ! $post || Eventwright\Post_Types::ORGANIZER !== $post->post_type || 'trash' === $post->post_status ) { return null; } return array( 'id' => $post->ID, 'name' => get_the_title( $post ), 'url' => 'publish' === $post->post_status ? get_permalink( $post ) : '', 'phone' => get_post_meta( $post->ID, '_evwr_phone', true ), 'website' => get_post_meta( $post->ID, '_evwr_website', true ), 'email' => get_post_meta( $post->ID, '_evwr_email', true ), ); } /** * URL of the page that shows the calendar. * * @return string */ function eventwright_events_page_url() { $page_id = (int) Eventwright\Settings::get( 'events_page_id' ); if ( $page_id && 'publish' === get_post_status( $page_id ) ) { return get_permalink( $page_id ); } return home_url( '/' ); } /** * Link to one occurrence of an event. * * @param int $event_id Event. * @param string $start Local start of the occurrence ('' for the event itself). * @return string */ function eventwright_occurrence_url( $event_id, $start = '' ) { $url = get_permalink( $event_id ); if ( $start && Eventwright\Recurrence::is_recurring( $event_id ) ) { $url = add_query_arg( 'evwr_date', substr( $start, 0, 10 ), $url ); } return $url; } /** * Format a cost for display: numbers get the currency symbol, words stay as written. * * @param string $cost Raw cost. * @return string */ function eventwright_format_cost( $cost ) { $cost = trim( (string) $cost ); if ( '' === $cost ) { return ''; } if ( '0' === $cost || 0.0 === (float) $cost && is_numeric( $cost ) ) { return __( 'Free', 'eventwright' ); } if ( is_numeric( $cost ) ) { return Eventwright\Settings::get( 'currency_symbol' ) . number_format_i18n( (float) $cost, floor( (float) $cost ) == (float) $cost ? 0 : 2 ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual } return $cost; } /** * Lowest number found in a cost string (for schema.org offers). * * @param string $cost Cost. * @return float|null */ function eventwright_cost_number( $cost ) { if ( preg_match_all( '/\d+(?:[.,]\d{1,2})?/', (string) $cost, $m ) && $m[0] ) { $nums = array_map( function ( $n ) { return (float) str_replace( ',', '.', $n ); }, $m[0] ); return min( $nums ); } if ( false !== stripos( (string) $cost, 'free' ) ) { return 0.0; } return null; } /** * Status label (empty for normal events). * * @param string $status Status key. * @return string */ function eventwright_status_label( $status ) { switch ( $status ) { case 'cancelled': return __( 'Cancelled', 'eventwright' ); case 'postponed': return __( 'Postponed', 'eventwright' ); case 'moved_online': return __( 'Moved online', 'eventwright' ); } return ''; } /** * Load a template part from the theme (eventwright/.php) or the plugin. * * @param string $name Template name without extension. * @param array $args Variables for the template. * @return string HTML. */ function eventwright_render_template( $name, array $args = array() ) { $name = sanitize_file_name( $name ); $template = locate_template( array( 'eventwright/' . $name . '.php' ) ); if ( ! $template ) { $template = EVWR_DIR . 'templates/' . $name . '.php'; } $template = apply_filters( 'eventwright_template', $template, $name, $args ); if ( ! file_exists( $template ) ) { return ''; } ob_start(); include $template; // Templates read their data from $args. return (string) ob_get_clean(); }