label). Add-ons can add their own with the eventwright_attendee_statuses filter. * * @return array */ public static function statuses() { $statuses = array( 'going' => __( 'Going', 'eventwright' ), 'waitlist' => __( 'Waitlist', 'eventwright' ), 'not_going' => __( 'Not going', 'eventwright' ), 'cancelled' => __( 'Cancelled', 'eventwright' ), ); /** * Filters the attendee statuses. * * @param array $statuses Status key => label. Keep "going", "waitlist" and "cancelled". */ $statuses = apply_filters( 'eventwright_attendee_statuses', $statuses ); return is_array( $statuses ) ? $statuses : array(); } /** * Whether "Waitlist when full" is on for an event. * * @param int $event_id Event. * @return bool */ public static function waitlist_enabled( $event_id ) { return (bool) get_post_meta( $event_id, '_evwr_rsvp_waitlist', true ); } /** * Places for one date (0 = unlimited). * * @param int $event_id Event. * @param string $occ_key Occurrence key. * @return int */ public static function capacity( $event_id, $occ_key = '' ) { $cap = (int) get_post_meta( $event_id, '_evwr_rsvp_capacity', true ); /** * Filters the number of places for an event date. * * @param int $cap Places (0 = unlimited). * @param int $event_id Event ID. * @param string $occ_key Local start of the date for repeating events, '' for one-off events. */ return max( 0, (int) apply_filters( 'eventwright_rsvp_capacity', $cap, (int) $event_id, (string) $occ_key ) ); } /** * Whether RSVP is on for an event. * * @param int $event_id Event. * @return bool */ public static function enabled( $event_id ) { return (bool) get_post_meta( $event_id, '_evwr_rsvp_enabled', true ); } /** * Occurrence key used for capacity: the date for repeating events, '' for one-off events. * * @param int $event_id Event. * @param array|null $occ Occurrence row. * @return string */ public static function occurrence_key( $event_id, $occ ) { return $occ && Recurrence::is_recurring( $event_id ) ? $occ['start_local'] : ''; } /** * Number of people going. * * @param int $event_id Event. * @param string $occ_key Occurrence key. * @return int */ public static function going_count( $event_id, $occ_key = '' ) { return self::count_status( $event_id, $occ_key, 'going' ); } /** * Number of people with a status (going, waitlist, ...) for one date. * * @param int $event_id Event. * @param string $occ_key Occurrence key. * @param string $status Status. * @return int */ public static function count_status( $event_id, $occ_key, $status ) { global $wpdb; $sql = "SELECT COALESCE(SUM(CAST(g.meta_value AS UNSIGNED)),0) FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} e ON e.post_id = p.ID AND e.meta_key = '_evwr_event_id' AND e.meta_value = %s INNER JOIN {$wpdb->postmeta} s ON s.post_id = p.ID AND s.meta_key = '_evwr_status' AND s.meta_value = %s INNER JOIN {$wpdb->postmeta} g ON g.post_id = p.ID AND g.meta_key = '_evwr_guests' LEFT JOIN {$wpdb->postmeta} o ON o.post_id = p.ID AND o.meta_key = '_evwr_occurrence' WHERE p.post_type = %s AND p.post_status = 'publish' AND COALESCE(o.meta_value,'') = %s"; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Prepared here. return (int) $wpdb->get_var( $wpdb->prepare( $sql, (string) (int) $event_id, sanitize_key( $status ), Post_Types::ATTENDEE, (string) $occ_key ) ); } /** * Places left (null = unlimited). * * @param int $event_id Event. * @param string $occ_key Occurrence key. * @return int|null */ public static function remaining( $event_id, $occ_key = '' ) { $cap = self::capacity( $event_id, $occ_key ); if ( ! $cap ) { return null; } return max( 0, $cap - self::going_count( $event_id, $occ_key ) ); } /** * Whether an occurrence is full. * * @param int $event_id Event. * @param array|null $occ Occurrence. * @return bool */ public static function is_full( $event_id, $occ ) { if ( ! self::enabled( $event_id ) ) { return false; } $left = self::remaining( $event_id, self::occurrence_key( $event_id, $occ ) ); return null !== $left && $left <= 0; } /** * Whether sign-ups are closed for an occurrence. * * @param int $event_id Event. * @param array|null $occ Occurrence. * @return bool */ public static function is_closed( $event_id, $occ ) { if ( ! $occ ) { return true; } $status = get_post_meta( $event_id, '_evwr_status', true ); if ( in_array( $status, array( 'cancelled', 'postponed' ), true ) ) { return true; } $closes = (string) get_post_meta( $event_id, '_evwr_rsvp_closes', true ); $now = eventwright_now_utc(); if ( $closes && eventwright_local_to_utc( $closes, eventwright_event_timezone( $event_id ) ) <= $now ) { return true; } return $occ['start_utc'] <= $now; } /** * RSVP section for an event page. * * @param int $event_id Event. * @return string */ public static function render( $event_id ) { $html = self::render_default( $event_id ); /** * Filters (or replaces) the RSVP section HTML of an event page and of the RSVP block. * * @param string $html The HTML ('' when there is nothing to show). * @param int $event_id Event ID. * @param array|null $occ Occurrence row being shown. */ return (string) apply_filters( 'eventwright_rsvp_html', $html, (int) $event_id, Occurrences::pick( $event_id, Frontend::requested_date() ) ); } /** * The built-in RSVP section. * * @param int $event_id Event. * @return string */ private static function render_default( $event_id ) { if ( ! self::enabled( $event_id ) ) { return ''; } $occ = Occurrences::pick( $event_id, Frontend::requested_date() ); if ( ! $occ || $occ['end_utc'] < eventwright_now_utc() ) { return ''; // Nothing to sign up for once the date has passed. } Frontend::enqueue_assets(); $occ_key = self::occurrence_key( $event_id, $occ ); $left = self::remaining( $event_id, $occ_key ); $max_per = max( 1, (int) get_post_meta( $event_id, '_evwr_rsvp_max_per', true ) ); $closed = self::is_closed( $event_id, $occ ); // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only status messages and cancel token. $result = isset( $_GET['evwr_rsvp'] ) ? sanitize_key( wp_unslash( $_GET['evwr_rsvp'] ) ) : ''; $cancel = isset( $_GET['evwr_cancel'] ) ? sanitize_text_field( wp_unslash( $_GET['evwr_cancel'] ) ) : ''; // phpcs:enable $cancel_attendee = $cancel ? self::find_by_token( $cancel ) : null; if ( $cancel_attendee && (int) get_post_meta( $cancel_attendee->ID, '_evwr_event_id', true ) !== (int) $event_id ) { $cancel_attendee = null; } return eventwright_render_template( 'rsvp-form', array( 'event_id' => $event_id, 'occurrence' => $occ, 'occurrence_key' => $occ_key, 'remaining' => $left, 'max_per' => null === $left || ( 0 === $left && self::waitlist_enabled( $event_id ) ) ? $max_per : max( 0, min( $max_per, $left ) ), 'waitlist' => 0 === $left && self::waitlist_enabled( $event_id ), 'waiting' => self::count_status( $event_id, $occ_key, 'waitlist' ), 'going' => self::going_count( $event_id, $occ_key ), 'closed' => $closed, 'result' => $result, 'cancel_attendee' => $cancel_attendee, 'cancel_token' => $cancel, 'collect_phone' => (bool) Settings::get( 'rsvp_collect_phone' ), 'action' => admin_url( 'admin-post.php' ), ) ); } /** * Find an attendee by cancel token. * * @param string $token Token. * @return \WP_Post|null */ public static function find_by_token( $token ) { if ( ! preg_match( '/^[A-Za-z0-9]{32}$/', (string) $token ) ) { return null; } $posts = get_posts( array( 'post_type' => Post_Types::ATTENDEE, 'post_status' => 'publish', 'posts_per_page' => 1, 'meta_key' => '_evwr_token', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_value' => $token, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value 'no_found_rows' => true, ) ); return $posts ? $posts[0] : null; } /** * Create (or update) an attendee. * * @param array $data {event_id, occurrence, name, email, phone, guests, status, source, date}. * @return int|\WP_Error Attendee id. */ public static function create_attendee( array $data ) { $data = wp_parse_args( $data, array( 'event_id' => 0, 'occurrence' => '', 'name' => '', 'email' => '', 'phone' => '', 'guests' => 1, 'status' => 'going', 'source' => 'form', 'date' => '', ) ); $postarr = array( 'post_type' => Post_Types::ATTENDEE, 'post_status' => 'publish', 'post_title' => $data['name'] ? $data['name'] : $data['email'], ); if ( $data['date'] ) { $postarr['post_date'] = $data['date']; } $id = wp_insert_post( wp_slash( $postarr ), true ); if ( is_wp_error( $id ) ) { return $id; } update_post_meta( $id, '_evwr_event_id', (int) $data['event_id'] ); update_post_meta( $id, '_evwr_occurrence', (string) $data['occurrence'] ); update_post_meta( $id, '_evwr_email', sanitize_email( $data['email'] ) ); update_post_meta( $id, '_evwr_phone', sanitize_text_field( $data['phone'] ) ); update_post_meta( $id, '_evwr_guests', max( 1, (int) $data['guests'] ) ); $status = array_key_exists( $data['status'], self::statuses() ) ? $data['status'] : 'going'; update_post_meta( $id, '_evwr_status', $status ); update_post_meta( $id, '_evwr_token', wp_generate_password( 32, false ) ); update_post_meta( $id, '_evwr_source', sanitize_key( $data['source'] ) ); /** * Fires after a new RSVP (attendee record) was created, from the form, the admin or an import. * * @param int $attendee_id Attendee post ID. * @param int $event_id Event ID. * @param string $occ_key Local start of the date for repeating events, '' otherwise. * @param string $status Status (going, waitlist, ...). * @param string $source form, admin, tec_import, or an add-on's own key. */ do_action( 'eventwright_rsvp_created', $id, (int) $data['event_id'], (string) $data['occurrence'], $status, sanitize_key( $data['source'] ) ); return $id; } /** * Visitor IP (only used for rate limiting, stored hashed and briefly). * * @return string */ private static function ip_hash() { $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; return wp_hash( $ip, 'nonce' ); } /** * Redirect back to the event with a status code. * * @param int $event_id Event. * @param string $occ_key Occurrence key. * @param string $code Status code. */ private static function back( $event_id, $occ_key, $code ) { $url = $event_id ? eventwright_occurrence_url( $event_id, $occ_key ) : home_url( '/' ); $url = add_query_arg( 'evwr_rsvp', $code, $url ) . '#evw-rsvp'; wp_safe_redirect( $url ); exit; } /** * Public AJAX: hand out a fresh nonce for the RSVP form of a published event, or for the cancel form of a * valid cancel token. Nothing else is returned, and the answer is never cached. The nonce is still checked * in full when the form is submitted. */ public static function ajax_nonce() { nocache_headers(); // phpcs:disable WordPress.Security.NonceVerification.Missing -- Public, read-only: it only returns a nonce for a published event. $token = isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : ''; if ( '' !== $token ) { $attendee = self::find_by_token( $token ); if ( ! $attendee ) { wp_send_json_error( null, 404 ); } wp_send_json_success( array( 'nonce' => wp_create_nonce( 'evwr_rsvp_cancel' ) ) ); } $event_id = isset( $_POST['event'] ) ? absint( $_POST['event'] ) : 0; // phpcs:enable WordPress.Security.NonceVerification.Missing $post = $event_id ? get_post( $event_id ) : null; if ( ! $post || Post_Types::EVENT !== $post->post_type || 'publish' !== $post->post_status || ! self::enabled( $event_id ) ) { wp_send_json_error( null, 404 ); } wp_send_json_success( array( 'nonce' => wp_create_nonce( 'evwr_rsvp_' . $event_id ) ) ); } /** * Handle the RSVP form. */ public static function handle_submit() { // phpcs:disable WordPress.Security.NonceVerification.Missing -- The nonce is verified below, before anything is saved; the event ID is needed to know which nonce to check. $event_id = isset( $_POST['evwr_event'] ) ? absint( $_POST['evwr_event'] ) : 0; $occ_req = isset( $_POST['evwr_occurrence'] ) ? eventwright_sanitize_local_datetime( sanitize_text_field( wp_unslash( $_POST['evwr_occurrence'] ) ) ) : ''; $post = $event_id ? get_post( $event_id ) : null; if ( ! $post || Post_Types::EVENT !== $post->post_type || 'publish' !== $post->post_status || ! self::enabled( $event_id ) ) { self::back( 0, '', 'error' ); } // Nonce check for every visitor, logged in or not. if ( ! isset( $_POST['_evwr_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_evwr_nonce'] ) ), 'evwr_rsvp_' . $event_id ) ) { self::back( $event_id, $occ_req, 'expired' ); } // Honeypot: humans never fill the hidden field. if ( ! empty( $_POST['evwr_website'] ) ) { self::back( $event_id, $occ_req, 'done' ); } $name = isset( $_POST['evwr_name'] ) ? sanitize_text_field( wp_unslash( $_POST['evwr_name'] ) ) : ''; $email = isset( $_POST['evwr_email'] ) ? sanitize_email( wp_unslash( $_POST['evwr_email'] ) ) : ''; $phone = isset( $_POST['evwr_phone'] ) ? sanitize_text_field( wp_unslash( $_POST['evwr_phone'] ) ) : ''; $guests = isset( $_POST['evwr_guests'] ) ? max( 1, absint( $_POST['evwr_guests'] ) ) : 1; // phpcs:enable $occ = Occurrences::pick( $event_id, $occ_req ? substr( $occ_req, 0, 10 ) : '' ); if ( $occ_req && ( ! $occ || $occ['start_local'] !== $occ_req ) ) { self::back( $event_id, '', 'error' ); } $occ_key = self::occurrence_key( $event_id, $occ ); if ( '' === $name || mb_strlen( $name ) > 120 || ! is_email( $email ) ) { self::back( $event_id, $occ_key, 'invalid' ); } if ( self::is_closed( $event_id, $occ ) ) { self::back( $event_id, $occ_key, 'closed' ); } // Rate limit: 5 sign-ups per visitor per 10 minutes. $rl_key = 'evwr_rl_' . substr( self::ip_hash(), 0, 20 ); $hits = (int) get_transient( $rl_key ); if ( $hits >= (int) apply_filters( 'eventwright_rsvp_rate_limit', 5 ) ) { self::back( $event_id, $occ_key, 'slow' ); } set_transient( $rl_key, $hits + 1, 10 * MINUTE_IN_SECONDS ); $max_per = max( 1, (int) get_post_meta( $event_id, '_evwr_rsvp_max_per', true ) ); $guests = min( $guests, $max_per ); // Same email for the same date: update instead of adding a duplicate. $existing = get_posts( array( 'post_type' => Post_Types::ATTENDEE, 'post_status' => 'publish', 'posts_per_page' => 1, 'no_found_rows' => true, 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query array( 'key' => '_evwr_event_id', 'value' => (string) $event_id, ), array( 'key' => '_evwr_email', 'value' => $email, ), array( 'key' => '_evwr_occurrence', 'value' => $occ_key, ), ), ) ); $previous = 0; if ( $existing && 'going' === get_post_meta( $existing[0]->ID, '_evwr_status', true ) ) { $previous = (int) get_post_meta( $existing[0]->ID, '_evwr_guests', true ); } $left = self::remaining( $event_id, $occ_key ); $status = 'going'; if ( null !== $left && $guests > $left + $previous ) { if ( 0 === $left + $previous && self::waitlist_enabled( $event_id ) ) { $status = 'waitlist'; // Full: join the waitlist instead. } else { self::back( $event_id, $occ_key, $left + $previous > 0 ? 'toomany' : 'full' ); } } if ( $existing ) { $attendee_id = $existing[0]->ID; wp_update_post( array( 'ID' => $attendee_id, 'post_title' => $name, ) ); update_post_meta( $attendee_id, '_evwr_guests', $guests ); update_post_meta( $attendee_id, '_evwr_status', $status ); if ( $phone ) { update_post_meta( $attendee_id, '_evwr_phone', $phone ); } $code = 'updated'; } else { $attendee_id = self::create_attendee( array( 'event_id' => $event_id, 'occurrence' => $occ_key, 'name' => $name, 'email' => $email, 'phone' => $phone, 'guests' => $guests, 'status' => $status, ) ); if ( is_wp_error( $attendee_id ) ) { self::back( $event_id, $occ_key, 'error' ); } $code = 'done'; } if ( 'waitlist' === $status ) { $code = 'waitlisted'; } do_action( 'eventwright_rsvp_saved', $attendee_id, $event_id, $occ_key, $code ); self::send_confirmation( $attendee_id, $occ ); self::send_notification( $attendee_id, $occ ); self::back( $event_id, $occ_key, $code ); } /** * Old cancel links: /?evwr_cancel=TOKEN goes to the event page, which shows a confirm button. * (A plain link must never cancel by itself: email scanners open links.) */ public static function cancel_link_redirect() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Token is the credential; this only redirects. $token = isset( $_GET['evwr_cancel'] ) ? sanitize_text_field( wp_unslash( $_GET['evwr_cancel'] ) ) : ''; if ( ! $token || is_singular( Post_Types::EVENT ) ) { return; } $attendee = self::find_by_token( $token ); if ( ! $attendee ) { return; } $event_id = (int) get_post_meta( $attendee->ID, '_evwr_event_id', true ); $url = add_query_arg( 'evwr_cancel', $token, eventwright_occurrence_url( $event_id, get_post_meta( $attendee->ID, '_evwr_occurrence', true ) ) ) . '#evw-rsvp'; wp_safe_redirect( $url ); exit; } /** * Confirmed cancel (POST with token). */ public static function handle_cancel() { // phpcs:disable WordPress.Security.NonceVerification.Missing -- The nonce is verified below, before anything changes; the token is needed to send the visitor back to the right page. $token = isset( $_POST['evwr_token'] ) ? sanitize_text_field( wp_unslash( $_POST['evwr_token'] ) ) : ''; $attendee = self::find_by_token( $token ); if ( ! $attendee ) { self::back( 0, '', 'error' ); } $event_id = (int) get_post_meta( $attendee->ID, '_evwr_event_id', true ); $occ_key = (string) get_post_meta( $attendee->ID, '_evwr_occurrence', true ); if ( ! isset( $_POST['_evwr_cancel_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_evwr_cancel_nonce'] ) ), 'evwr_rsvp_cancel' ) ) { // Expired page: show the confirm button again with a fresh nonce. wp_safe_redirect( add_query_arg( 'evwr_cancel', $token, eventwright_occurrence_url( $event_id, $occ_key ) ) . '#evw-rsvp' ); exit; } // phpcs:enable WordPress.Security.NonceVerification.Missing // Only RSVPs can be cancelled with this link. An add-on's records (for example a paid ticket) have their own rules, // and cancelling one here would free the seat without a refund. $current = (string) get_post_meta( $attendee->ID, '_evwr_status', true ); if ( ! in_array( $current, array( 'going', 'waitlist', 'not_going', 'cancelled' ), true ) ) { self::back( $event_id, $occ_key, 'error' ); } update_post_meta( $attendee->ID, '_evwr_status', 'cancelled' ); /** * Fires after an attendee cancelled with their cancel link. * * @param int $attendee_id Attendee post ID. * @param int $event_id Event ID. * @param string $occ_key Local start of the date for repeating events, '' otherwise. */ do_action( 'eventwright_rsvp_cancelled', $attendee->ID, $event_id, $occ_key ); self::send_notification( $attendee->ID, Occurrences::pick( $event_id, substr( $occ_key, 0, 10 ) ), true ); self::promote_waitlist( $event_id, $occ_key ); self::back( $event_id, $occ_key, 'cancelled' ); } /** * Promote waitlisted guests, oldest first, while places allow. A party that does not fit in the places left * is skipped so that smaller parties behind it can still use them. * * @param int $event_id Event. * @param string|null $occ_key Date key; null = every date of the event that has a waitlist. * @return int[] Promoted attendee IDs. */ public static function promote_waitlist( $event_id, $occ_key = null ) { static $running = array(); $event_id = (int) $event_id; if ( ! $event_id || ! self::enabled( $event_id ) ) { return array(); } $keys = array(); if ( null === $occ_key ) { foreach ( self::waitlisted( $event_id ) as $p ) { $keys[ (string) get_post_meta( $p->ID, '_evwr_occurrence', true ) ] = true; } $keys = array_keys( $keys ); } else { $keys = array( (string) $occ_key ); } $promoted = array(); foreach ( $keys as $key ) { $guard = $event_id . '|' . $key; if ( isset( $running[ $guard ] ) ) { continue; } $running[ $guard ] = true; foreach ( self::waitlisted( $event_id, (string) $key ) as $p ) { $left = self::remaining( $event_id, (string) $key ); if ( null !== $left && $left <= 0 ) { break; } $guests = max( 1, (int) get_post_meta( $p->ID, '_evwr_guests', true ) ); if ( null !== $left && $guests > $left ) { continue; } update_post_meta( $p->ID, '_evwr_status', 'going' ); $promoted[] = $p->ID; /** * Fires after a waitlisted guest got a place. * * @param int $attendee_id Attendee post ID. * @param int $event_id Event ID. * @param string $occ_key Local start of the date for repeating events, '' otherwise. */ do_action( 'eventwright_rsvp_promoted', $p->ID, $event_id, (string) $key ); self::send_promotion( $p->ID, Occurrences::pick( $event_id, '' === $key ? '' : substr( $key, 0, 10 ) ) ); } unset( $running[ $guard ] ); } return $promoted; } /** * Waitlisted attendees of an event (and date), oldest first. * * @param int $event_id Event. * @param string|null $occ_key Date key; null = all dates. * @return \WP_Post[] */ public static function waitlisted( $event_id, $occ_key = null ) { $meta = array( array( 'key' => '_evwr_event_id', 'value' => (string) (int) $event_id, ), array( 'key' => '_evwr_status', 'value' => 'waitlist', ), ); if ( null !== $occ_key ) { $meta[] = array( 'key' => '_evwr_occurrence', 'value' => (string) $occ_key, ); } return get_posts( array( 'post_type' => Post_Types::ATTENDEE, 'post_status' => 'publish', 'posts_per_page' => 500, 'orderby' => array( 'date' => 'ASC', 'ID' => 'ASC', ), 'no_found_rows' => true, 'meta_query' => $meta, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query ) ); } /** * A raised (or newly added) limit frees places. * * @param int $meta_id Meta ID. * @param int $post_id Post. * @param string $meta_key Key. */ public static function on_capacity_meta( $meta_id, $post_id, $meta_key ) { if ( '_evwr_rsvp_capacity' === $meta_key && Post_Types::EVENT === get_post_type( $post_id ) ) { self::promote_waitlist( (int) $post_id ); } } /** * A trashed attendee frees their places. * * @param int $post_id Post. */ public static function on_attendee_trashed( $post_id ) { if ( Post_Types::ATTENDEE === get_post_type( $post_id ) ) { $event_id = (int) get_post_meta( $post_id, '_evwr_event_id', true ); if ( $event_id ) { self::promote_waitlist( $event_id, (string) get_post_meta( $post_id, '_evwr_occurrence', true ) ); } } } /** * Email: "a place opened up", with the cancel link. * * @param int $attendee_id Attendee. * @param array|null $occ Occurrence. */ public static function send_promotion( $attendee_id, $occ ) { $event_id = (int) get_post_meta( $attendee_id, '_evwr_event_id', true ); $email = get_post_meta( $attendee_id, '_evwr_email', true ); if ( ! is_email( $email ) || ! apply_filters( 'eventwright_send_rsvp_promotion', true, $attendee_id ) ) { return; } $title = html_entity_decode( get_the_title( $event_id ), ENT_QUOTES, 'UTF-8' ); /* translators: %s: event title */ $subject = sprintf( __( 'A place opened up: %s', 'eventwright' ), $title ); $body = sprintf( /* translators: 1: attendee name, 2: event summary, 3: number of people, 4: calendar file link, 5: cancel link, 6: site name */ __( "Hi %1\$s,\n\nGood news: a place opened up and you're now on the list for:\n\n%2\$s\n\nPeople: %3\$d\n\nAdd it to your calendar: %4\$s\n\nCan't come after all? Cancel here so someone else can have your place: %5\$s\n\n— %6\$s", 'eventwright' ), get_the_title( $attendee_id ), self::event_summary( $event_id, $occ ), (int) get_post_meta( $attendee_id, '_evwr_guests', true ), ICal::event_url( $event_id ), self::cancel_url( $attendee_id ), html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES, 'UTF-8' ) ); $headers = self::mail_headers(); $reply = self::organizer_email( $event_id ); if ( $reply ) { $headers[] = 'Reply-To: ' . $reply; } wp_mail( $email, $subject, apply_filters( 'eventwright_rsvp_promotion_body', $body, $attendee_id, $occ ), $headers ); } /** * Cancel link sent by email. * * @param int $attendee_id Attendee. * @return string */ public static function cancel_url( $attendee_id ) { return add_query_arg( 'evwr_cancel', get_post_meta( $attendee_id, '_evwr_token', true ), home_url( '/' ) ); } /** * From header for our emails. * * @return string[] */ private static function mail_headers() { $headers = array( 'Content-Type: text/plain; charset=UTF-8' ); $from = Settings::get( 'rsvp_from_name' ); if ( $from ) { $headers[] = 'From: ' . str_replace( array( "\r", "\n", '"' ), '', $from ) . ' <' . get_option( 'admin_email' ) . '>'; } return $headers; } /** * Plain text event summary for emails. * * @param int $event_id Event. * @param array|null $occ Occurrence. * @return string */ private static function event_summary( $event_id, $occ ) { $lines = array( html_entity_decode( get_the_title( $event_id ), ENT_QUOTES, 'UTF-8' ) ); if ( $occ ) { $lines[] = eventwright_format_when( $occ['start_local'], $occ['end_local'], (bool) $occ['all_day'], eventwright_event_timezone( $event_id ) ); } $venue = eventwright_get_venue( (int) get_post_meta( $event_id, '_evwr_venue_id', true ) ); if ( $venue ) { $lines[] = trim( $venue['name'] . ', ' . $venue['full_address'], ', ' ); } $online = get_post_meta( $event_id, '_evwr_online_url', true ); if ( $online ) { $lines[] = $online; } $lines[] = eventwright_occurrence_url( $event_id, $occ ? $occ['start_local'] : '' ); return implode( "\n", $lines ); } /** * Email to the attendee. * * @param int $attendee_id Attendee. * @param array|null $occ Occurrence. */ public static function send_confirmation( $attendee_id, $occ ) { $event_id = (int) get_post_meta( $attendee_id, '_evwr_event_id', true ); $email = get_post_meta( $attendee_id, '_evwr_email', true ); if ( ! is_email( $email ) || ! apply_filters( 'eventwright_send_rsvp_confirmation', true, $attendee_id ) ) { return; } $guests = (int) get_post_meta( $attendee_id, '_evwr_guests', true ); $title = html_entity_decode( get_the_title( $event_id ), ENT_QUOTES, 'UTF-8' ); if ( 'waitlist' === get_post_meta( $attendee_id, '_evwr_status', true ) ) { /* translators: %s: event title */ $subject = sprintf( __( "You're on the waitlist: %s", 'eventwright' ), $title ); $body = sprintf( /* translators: 1: attendee name, 2: event summary, 3: number of people, 4: cancel link, 5: site name */ __( "Hi %1\$s,\n\nThis event is full, so we put you on the waitlist for:\n\n%2\$s\n\nPeople: %3\$d\n\nIf a place opens up we will email you straight away. Don't want to wait any more? Leave the waitlist here: %4\$s\n\n— %5\$s", 'eventwright' ), get_the_title( $attendee_id ), self::event_summary( $event_id, $occ ), $guests, self::cancel_url( $attendee_id ), html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES, 'UTF-8' ) ); } else { /* translators: %s: event title */ $subject = sprintf( __( "You're going: %s", 'eventwright' ), $title ); $body = sprintf( /* translators: 1: attendee name, 2: event summary, 3: number of people, 4: calendar file link, 5: cancel link, 6: site name */ __( "Hi %1\$s,\n\nYou're on the list for:\n\n%2\$s\n\nPeople: %3\$d\n\nAdd it to your calendar: %4\$s\n\nCan't come? Cancel here: %5\$s\n\n— %6\$s", 'eventwright' ), get_the_title( $attendee_id ), self::event_summary( $event_id, $occ ), $guests, ICal::event_url( $event_id ), self::cancel_url( $attendee_id ), html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES, 'UTF-8' ) ); } $headers = self::mail_headers(); $reply = self::organizer_email( $event_id ); if ( $reply ) { $headers[] = 'Reply-To: ' . $reply; } wp_mail( $email, $subject, apply_filters( 'eventwright_rsvp_confirmation_body', $body, $attendee_id, $occ ), $headers ); } /** * First organizer email of an event. * * @param int $event_id Event. * @return string */ private static function organizer_email( $event_id ) { foreach ( (array) get_post_meta( $event_id, '_evwr_organizer_ids', true ) as $oid ) { $e = get_post_meta( (int) $oid, '_evwr_email', true ); if ( is_email( $e ) ) { return $e; } } return ''; } /** * Email to the site/organizer about a new RSVP or a cancellation. * * @param int $attendee_id Attendee. * @param array|null $occ Occurrence. * @param bool $cancelled Cancellation. */ public static function send_notification( $attendee_id, $occ, $cancelled = false ) { $to = Settings::get( 'rsvp_notify_email' ); if ( ! $to ) { $to = get_option( 'admin_email' ); } if ( ! is_email( $to ) || ! apply_filters( 'eventwright_send_rsvp_notification', true, $attendee_id ) ) { return; } $event_id = (int) get_post_meta( $attendee_id, '_evwr_event_id', true ); $occ_key = self::occurrence_key( $event_id, $occ ); $going = self::going_count( $event_id, $occ_key ); $cap = self::capacity( $event_id, $occ_key ); $waitlist = 'waitlist' === get_post_meta( $attendee_id, '_evwr_status', true ); $title = html_entity_decode( get_the_title( $event_id ), ENT_QUOTES, 'UTF-8' ); $name = html_entity_decode( get_the_title( $attendee_id ), ENT_QUOTES, 'UTF-8' ); $subject = $cancelled /* translators: 1: attendee name, 2: event title */ ? sprintf( __( 'RSVP cancelled: %1$s for %2$s', 'eventwright' ), $name, $title ) /* translators: 1: attendee name, 2: event title */ : ( $waitlist /* translators: 1: attendee name, 2: event title */ ? sprintf( __( 'New waitlist sign-up: %1$s for %2$s', 'eventwright' ), $name, $title ) /* translators: 1: attendee name, 2: event title */ : sprintf( __( 'New RSVP: %1$s for %2$s', 'eventwright' ), $name, $title ) ); $total = $cap /* translators: 1: people going, 2: capacity */ ? sprintf( __( '%1$d of %2$d places taken', 'eventwright' ), $going, $cap ) /* translators: %d: people going */ : sprintf( _n( '%d person going', '%d people going', $going, 'eventwright' ), $going ); $body = sprintf( "%s\n\n%s <%s>\n%s: %d\n\n%s\n\n%s\n%s", $subject, $name, get_post_meta( $attendee_id, '_evwr_email', true ), __( 'People', 'eventwright' ), (int) get_post_meta( $attendee_id, '_evwr_guests', true ), self::event_summary( $event_id, $occ ), $total, admin_url( 'edit.php?post_type=' . Post_Types::ATTENDEE . '&evwr_event=' . $event_id ) ); wp_mail( $to, $subject, $body, self::mail_headers() ); } }