' . '' . '
' . '' . '' . '' . '
' . ''; $types = array( Post_Types::EVENT => __( 'Single Event', 'eventwright' ), Post_Types::VENUE => __( 'Single Venue', 'eventwright' ), Post_Types::ORGANIZER => __( 'Single Organizer', 'eventwright' ), ); foreach ( $types as $type => $title ) { register_block_template( 'eventwright//single-' . $type, array( 'title' => $title, 'description' => __( 'Added by Eventwright. Shows the details box, RSVP and other dates around the content.', 'eventwright' ), 'post_types' => array( $type ), 'content' => $content, ) ); } } /** * Register CSS and JS. */ public static function register_assets() { wp_register_style( 'eventwright', EVWR_URL . 'assets/css/eventwright.css', array(), EVWR_VERSION ); wp_register_script( 'eventwright', EVWR_URL . 'assets/js/eventwright.js', array(), EVWR_VERSION, array( 'in_footer' => true, 'strategy' => 'defer' ) ); wp_localize_script( 'eventwright', 'eventwrightData', array( 'rest' => esc_url_raw( rest_url( 'eventwright/v1/calendar' ) ), 'ajax' => esc_url_raw( admin_url( 'admin-ajax.php' ) ), 'error' => __( 'Could not load events. Please reload the page.', 'eventwright' ), ) ); } /** * Enqueue on pages that clearly need it (others enqueue at render time). */ public static function maybe_enqueue() { if ( is_singular( array( Post_Types::EVENT, Post_Types::VENUE, Post_Types::ORGANIZER ) ) || is_tax( Post_Types::CATEGORY ) ) { self::enqueue_assets(); return; } $page_id = (int) Settings::get( 'events_page_id' ); if ( $page_id && is_page( $page_id ) ) { self::enqueue_assets(); return; } if ( is_singular() ) { $post = get_queried_object(); if ( $post instanceof \WP_Post && ( has_shortcode( $post->post_content, 'eventwright_calendar' ) || has_shortcode( $post->post_content, 'eventwright_list' ) || has_shortcode( $post->post_content, 'tribe_events' ) ) ) { self::enqueue_assets(); } } } /** * Enqueue our CSS and JS. */ public static function enqueue_assets() { if ( ! wp_style_is( 'eventwright', 'registered' ) ) { self::register_assets(); } if ( Settings::get( 'load_css' ) ) { wp_enqueue_style( 'eventwright' ); } wp_enqueue_script( 'eventwright' ); } /** * Add event details, RSVP and other dates to single event, venue and organizer pages. * * @param string $content Content. * @return string */ public static function content( $content ) { if ( self::$in_content || ! in_the_loop() || ! is_main_query() || post_password_required() ) { return $content; } $post = get_post(); if ( ! $post || (int) $post->ID !== (int) get_queried_object_id() || ! is_singular() ) { return $content; } self::$in_content = true; if ( Post_Types::EVENT === $post->post_type ) { $content = self::event_content( $post, $content ); } elseif ( Post_Types::VENUE === $post->post_type ) { $content = self::venue_content( $post, $content ); } elseif ( Post_Types::ORGANIZER === $post->post_type ) { $content = self::organizer_content( $post, $content ); } self::$in_content = false; return $content; } /** * The date requested for a recurring event (?evwr_date= or /event/name/2026-12-01/). * * @return string */ public static function requested_date() { $date = get_query_var( 'evwr_date' ); if ( ! $date ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only. $date = isset( $_GET['evwr_date'] ) ? sanitize_text_field( wp_unslash( $_GET['evwr_date'] ) ) : ''; } $date = eventwright_sanitize_local_datetime( $date ); return $date ? substr( $date, 0, 10 ) : ''; } /** * Single event content. * * @param \WP_Post $post Event. * @param string $content Content. * @return string */ private static function event_content( $post, $content ) { $raw = $post->post_content; $before = ''; $after = ''; if ( ! has_block( 'eventwright/event-details', $raw ) && ! has_shortcode( $raw, 'eventwright_event_details' ) ) { $before = self::event_details( $post->ID ); } if ( ! has_block( 'eventwright/rsvp', $raw ) && ! has_shortcode( $raw, 'eventwright_rsvp' ) ) { $after .= RSVP::render( $post->ID ); } $after .= self::other_dates( $post->ID ); return $before . $content . $after; } /** * The event details box. * * @param int $event_id Event. * @param string $date Occurrence date (default: requested or next). * @return string */ public static function event_details( $event_id, $date = null ) { $event = eventwright_get_event( $event_id ); if ( ! $event ) { return ''; } self::enqueue_assets(); $date = null === $date ? self::requested_date() : $date; $occ = Occurrences::pick( $event_id, $date ); $organizers = array(); foreach ( $event['organizer_ids'] as $oid ) { $o = eventwright_get_organizer( $oid ); if ( $o ) { $organizers[] = $o; } } return eventwright_render_template( 'event-details', array( 'event' => $event, 'occurrence' => $occ, 'venue' => eventwright_get_venue( $event['venue_id'] ), 'organizers' => $organizers, 'categories' => get_the_terms( $event_id, Post_Types::CATEGORY ), 'is_past' => $occ && $occ['end_utc'] < eventwright_now_utc(), 'recurring' => Recurrence::is_recurring( $event_id ), 'rule_text' => Recurrence::describe( $event['recurrence'], $event['start'] ), ) ); } /** * Upcoming dates of a recurring event. * * @param int $event_id Event. * @return string */ public static function other_dates( $event_id ) { if ( ! Recurrence::is_recurring( $event_id ) ) { return ''; } $current = Occurrences::pick( $event_id, self::requested_date() ); $rows = Occurrences::upcoming_for_event( $event_id, 13 ); $rows = array_values( array_filter( $rows, function ( $r ) use ( $current ) { return ! $current || $r['start_local'] !== $current['start_local']; } ) ); if ( ! $rows ) { return ''; } $tz = eventwright_event_timezone( $event_id ); $html = '

' . esc_html__( 'Other dates', 'eventwright' ) . '

'; return $html; } /** * Venue page: address, map link and upcoming events there. * * @param \WP_Post $post Venue. * @param string $content Content. * @return string */ private static function venue_content( $post, $content ) { $venue = eventwright_get_venue( $post->ID ); $html = '
'; if ( $venue['full_address'] ) { $html .= '

' . esc_html( $venue['full_address'] ); $map = eventwright_venue_map_url( $venue ); if ( $map ) { $html .= ' ' . esc_html__( 'Map', 'eventwright' ) . ''; } $html .= '

'; } $html .= Maps::button( $venue ); $bits = array(); if ( $venue['phone'] ) { $bits[] = '' . esc_html( $venue['phone'] ) . ''; } if ( $venue['website'] ) { $bits[] = '' . esc_html__( 'Website', 'eventwright' ) . ''; } if ( $bits ) { $html .= '

' . implode( ' ', $bits ) . '

'; } $html .= '
'; return $html . $content . self::related_events( array( 'venue' => $post->ID ) ); } /** * Organizer page: contact and upcoming events. * * @param \WP_Post $post Organizer. * @param string $content Content. * @return string */ private static function organizer_content( $post, $content ) { $o = eventwright_get_organizer( $post->ID ); $bits = array(); if ( $o['phone'] ) { $bits[] = '' . esc_html( $o['phone'] ) . ''; } if ( $o['email'] ) { $bits[] = '' . esc_html( antispambot( $o['email'] ) ) . ''; } if ( $o['website'] ) { $bits[] = '' . esc_html__( 'Website', 'eventwright' ) . ''; } $html = $bits ? '

' . implode( ' ', $bits ) . '

' : ''; return $html . $content . self::related_events( array( 'organizer' => $post->ID ) ); } /** * Upcoming events for a venue or organizer. * * @param array $filter Query filter. * @return string */ private static function related_events( array $filter ) { $rows = Occurrences::query( array_merge( $filter, array( 'upcoming' => true, 'limit' => 20, ) ) ); Views::prime( $rows ); $html = ''; return $html; } /** * Show the date in front of event excerpts in theme archives and search. * * @param string $excerpt Excerpt. * @param \WP_Post $post Post. * @return string */ public static function excerpt_prefix( $excerpt, $post = null ) { if ( is_admin() || ! $post || Post_Types::EVENT !== $post->post_type || is_singular() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { return $excerpt; } $occ = Occurrences::pick( $post->ID ); if ( ! $occ ) { return $excerpt; } $when = eventwright_format_when( $occ['start_local'], $occ['end_local'], (bool) $occ['all_day'], eventwright_event_timezone( $post->ID ) ); return $when . ' — ' . $excerpt; } /** * Theme archives of event tags and searches: soonest events first is not possible with WP_Query alone, so order by start meta. * * @param \WP_Query $q Query. */ public static function order_event_archives( $q ) { if ( is_admin() || ! $q->is_main_query() ) { return; } if ( $q->is_tax( Post_Types::CATEGORY ) || ( $q->is_tag() && Post_Types::EVENT === $q->get( 'post_type' ) ) ) { $q->set( 'meta_key', '_evwr_start' ); $q->set( 'orderby', 'meta_value' ); $q->set( 'order', 'DESC' ); } } /** * Canonical URL of the events page when it shows one category. * * @param string $url URL. * @param \WP_Post $post Post. * @return string */ public static function canonical( $url, $post ) { $page_id = (int) Settings::get( 'events_page_id' ); if ( $page_id && (int) $post->ID === $page_id ) { $state = Views::request_state(); if ( $state['cat'] ) { $term = get_term_by( 'slug', $state['cat'], Post_Types::CATEGORY ); if ( $term ) { $link = get_term_link( $term ); return is_wp_error( $link ) ? $url : $link; } } } return $url; } /** * Page title "Music – Events" for category views of the events page. * * @param array $parts Title parts. * @return array */ public static function title_parts( $parts ) { $page_id = (int) Settings::get( 'events_page_id' ); if ( $page_id && is_page( $page_id ) ) { $state = Views::request_state(); if ( $state['cat'] ) { $term = get_term_by( 'slug', $state['cat'], Post_Types::CATEGORY ); if ( $term ) { $parts['title'] = $term->name . ' – ' . $parts['title']; } } } return $parts; } /** * Status classes on event posts. * * @param string[] $classes Classes. * @param string[] $extra Extra. * @param int $post_id Post. * @return string[] */ public static function post_class( $classes, $extra, $post_id ) { if ( get_post_type( $post_id ) === Post_Types::EVENT ) { $status = get_post_meta( $post_id, '_evwr_status', true ); if ( $status && 'scheduled' !== $status ) { $classes[] = 'evw-status-' . sanitize_html_class( $status ); } } return $classes; } /** * schema.org Event data on single event pages. */ public static function json_ld() { if ( ! is_singular( Post_Types::EVENT ) || post_password_required() ) { return; } $data = self::schema( get_queried_object_id(), self::requested_date() ); if ( ! $data ) { return; } echo '' . "\n"; } /** * Build schema.org Event data. * * @param int $event_id Event. * @param string $date Occurrence date. * @return array|null */ public static function schema( $event_id, $date = '' ) { $event = eventwright_get_event( $event_id ); $occ = $event ? Occurrences::pick( $event_id, $date ) : null; if ( ! $occ ) { return null; } $tz = $event['timezone']; $status_map = array( 'scheduled' => 'https://schema.org/EventScheduled', 'cancelled' => 'https://schema.org/EventCancelled', 'postponed' => 'https://schema.org/EventPostponed', 'moved_online' => 'https://schema.org/EventMovedOnline', ); $data = array( '@context' => 'https://schema.org', '@type' => 'Event', 'name' => html_entity_decode( get_the_title( $event_id ), ENT_QUOTES, 'UTF-8' ), 'url' => eventwright_occurrence_url( $event_id, $occ['start_local'] ), 'startDate' => eventwright_iso8601( $occ['start_local'], $tz, (bool) $occ['all_day'] ), 'endDate' => eventwright_iso8601( $occ['end_local'], $tz, (bool) $occ['all_day'] ), 'eventStatus' => isset( $status_map[ $event['status'] ] ) ? $status_map[ $event['status'] ] : $status_map['scheduled'], ); $desc = Views::excerpt( get_post( $event_id ), 60 ); if ( $desc ) { $data['description'] = $desc; } $img = get_the_post_thumbnail_url( $event_id, 'full' ); if ( $img ) { $data['image'] = array( $img ); } $venue = eventwright_get_venue( $event['venue_id'] ); $locations = array(); if ( $venue ) { $place = array( '@type' => 'Place', 'name' => $venue['name'], ); $address = array_filter( array( '@type' => 'PostalAddress', 'streetAddress' => $venue['address'], 'addressLocality' => $venue['city'], 'addressRegion' => $venue['state'], 'postalCode' => $venue['zip'], 'addressCountry' => $venue['country'], ) ); if ( count( $address ) > 1 ) { $place['address'] = $address; } if ( $venue['lat'] && $venue['lng'] ) { $place['geo'] = array( '@type' => 'GeoCoordinates', 'latitude' => (float) $venue['lat'], 'longitude' => (float) $venue['lng'], ); } $locations[] = $place; } if ( $event['online_url'] ) { $locations[] = array( '@type' => 'VirtualLocation', 'url' => $event['online_url'], ); } if ( $locations ) { $data['location'] = 1 === count( $locations ) ? $locations[0] : $locations; $data['eventAttendanceMode'] = $venue && $event['online_url'] ? 'https://schema.org/MixedEventAttendanceMode' : ( $venue ? 'https://schema.org/OfflineEventAttendanceMode' : 'https://schema.org/OnlineEventAttendanceMode' ); } $orgs = array(); foreach ( $event['organizer_ids'] as $oid ) { $o = eventwright_get_organizer( $oid ); if ( $o ) { $orgs[] = array_filter( array( '@type' => 'Organization', 'name' => $o['name'], 'url' => $o['website'] ? $o['website'] : $o['url'], ) ); } } if ( $orgs ) { $data['organizer'] = 1 === count( $orgs ) ? $orgs[0] : $orgs; } $price = eventwright_cost_number( $event['cost'] ); if ( null !== $price ) { $data['offers'] = array( '@type' => 'Offer', 'price' => $price, 'priceCurrency' => Settings::get( 'currency' ), 'url' => $event['website'] ? $event['website'] : $data['url'], 'availability' => RSVP::is_full( $event_id, $occ ) ? 'https://schema.org/SoldOut' : 'https://schema.org/InStock', ); } return apply_filters( 'eventwright_schema', $data, $event_id, $occ ); } }