sync_service = $sync_service; } public function get_slug() { return self::SLUG; } public function get_label() { return __( 'Event Tickets', '404-emails' ); } /** Core Event Tickets active - checked independently of Event Tickets Plus, which is only required for the in-form consent checkbox (see consent_given_for_attendee()). */ public function is_available() { return class_exists( 'Tribe__Tickets__Main' ); } /** Event Tickets Plus active - required for the in-form consent checkbox mechanism; see the class doc comment on why core Event Tickets alone has no field-extensibility point in its registration form. */ public static function is_plus_available() { return class_exists( 'Tribe__Tickets_Plus__Main' ); } public function register_hooks() { add_action( 'event_tickets_rsvp_attendee_created', array( $this, 'handle_rsvp_attendee_created' ), 10, 4 ); // The newsletter tick-box on Event Tickets' own forms. Event Tickets // renders every part of its forms through its template system, which // fires `tribe_template_after_include:{template}` after each part (the // same hooks Event Tickets uses itself to add things to its forms) - // output there lands inside the form, right after the email field. if ( self::uses_own_checkbox() ) { add_action( 'tribe_template_after_include:tickets/v2/rsvp/form/fields/email', array( $this, 'render_rsvp_checkbox' ), 10, 3 ); add_action( 'tribe_template_after_include:tickets/v2/commerce/checkout/purchaser-info/email', array( $this, 'render_checkout_checkbox' ), 10, 3 ); add_action( 'wp_ajax_' . self::AJAX_ACTION, array( $this, 'remember_checkout_consent' ) ); add_action( 'wp_ajax_nopriv_' . self::AJAX_ACTION, array( $this, 'remember_checkout_consent' ) ); } // Status_Interface is an interface: class_exists() is false for it, so // this used to be skipped and ticket orders were never passed on. // Priority 20: after Event Tickets has created the order's attendees // on this same flag. if ( function_exists( 'tribe' ) && interface_exists( '\TEC\Tickets\Commerce\Status\Status_Interface' ) ) { add_action( 'tec_tickets_commerce_order_status_flag_attendee_generation', array( $this, 'handle_tickets_commerce_order_completed' ), 20, 3 ); } } /** * @param int $attendee_id * @param int $post_id Event ID. * @param string $order_id * @param int $product_id */ public function handle_rsvp_attendee_created( $attendee_id, $post_id, $order_id, $product_id ) { if ( ! Settings::is_event_tickets_integration_enabled() ) { return; } if ( ! $this->is_rsvp_going( $attendee_id ) ) { // An explicit "not going" response, or a status we can't // positively confirm as "going" - never synced, per Part I. return; } // The RSVP form posts all of its fields with the registration // (Event Tickets' rsvp-block.js serializes the whole form into its // tribe_tickets_rsvp_handle request, after checking its own nonce), // so this plugin's tick-box arrives in the same request. if ( self::uses_own_checkbox() && self::request_ticked( $_POST ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Event Tickets verified the RSVP request's nonce; only our own tick-box is read. update_post_meta( $attendee_id, self::CONSENT_META, '1' ); } $this->sync_attendee( $attendee_id, $post_id ); } /** * @param \TEC\Tickets\Commerce\Status\Status_Interface $new_status * @param \TEC\Tickets\Commerce\Status\Status_Interface|null $old_status * @param \WP_Post $post Tickets Commerce order post. */ public function handle_tickets_commerce_order_completed( $new_status, $old_status, $post ) { if ( ! Settings::is_event_tickets_integration_enabled() ) { return; } if ( ! $post instanceof \WP_Post ) { return; } $ticked = self::uses_own_checkbox() && self::take_cart_consent( (string) get_post_meta( $post->ID, self::order_hash_meta_key(), true ) ); foreach ( $this->get_order_attendee_ids( $post->ID ) as $attendee_id ) { if ( $ticked ) { update_post_meta( $attendee_id, self::CONSENT_META, '1' ); } $event_id = $this->get_attendee_event_id( $attendee_id ); if ( $event_id ) { $this->sync_attendee( $attendee_id, $event_id ); } } } /** * Only people who ticked the newsletter box (this plugin's own, or the * Event Tickets Plus field) are passed on to 404 Emails. Everyone else * is left alone - nothing about them leaves the site. */ private function sync_attendee( $attendee_id, $event_id ) { if ( ! $this->consent_given_for_attendee( $attendee_id ) ) { return; } $contact = $this->resolve_attendee_contact( $attendee_id ); if ( empty( $contact['email'] ) || ! is_email( $contact['email'] ) ) { // No usable email - nothing safe to send. Not logged as a // failure (there was never a valid attempt to make). return; } $dto = $this->build_dto( $attendee_id, $event_id, $contact ); $this->sync_service->send( $dto, $this->get_slug() ); } /** * @param int $attendee_id * @param int $event_id * @param array $contact {email, first_name, last_name} * @return Signup_Dto */ private function build_dto( $attendee_id, $event_id, array $contact ) { // Only reached for attendees who ticked the box (sync_attendee()). $destination_form = Settings::get_selected_form_public_id(); $idempotency_key = Idempotency::build_key( $this->get_slug(), $attendee_id, $destination_form ? $destination_form : 'contact' ); $source_reference = sprintf( 'wordpress-event-tickets:event-%d:attendee-%d', (int) $event_id, (int) $attendee_id ); return new Signup_Dto( $contact['email'], $contact['first_name'], $contact['last_name'], true, $source_reference, $destination_form, $idempotency_key, Settings::get_consent_label() ); } /** * This plugin adds its own tick-box when it's switched on ("Show * checkbox") and no Event Tickets Plus field is set up for it - with a * Plus field configured, that field is the tick-box instead. */ public static function uses_own_checkbox() { return Settings::is_event_tickets_integration_enabled() && Settings::is_consent_checkbox_visible() && ! ( self::is_plus_available() && '' !== Settings::get_consent_field_slug() ); } /** Whether a request's fields include our ticked box. Never assumed: absent or empty is "no". */ public static function request_ticked( array $fields ) { return isset( $fields[ self::CHECKBOX_FIELD ] ) && '1' === (string) $fields[ self::CHECKBOX_FIELD ]; } /** The tick-box's markup, in Event Tickets' own form styles. Never pre-ticked. */ public static function checkbox_html( $id ) { return sprintf( '
', esc_attr( $id ), esc_attr( self::CHECKBOX_FIELD ), esc_html( Settings::get_consent_label() ) ); } /** * After the RSVP form's email field. * * @param string $file The template file. * @param array $name The template name parts. * @param object $template Event Tickets' Tribe__Template. */ public function render_rsvp_checkbox( $file, $name, $template ) { $rsvp = is_object( $template ) && method_exists( $template, 'get' ) ? $template->get( 'rsvp' ) : null; $id = 'emails404-consent-rsvp-' . ( is_object( $rsvp ) && isset( $rsvp->ID ) ? (int) $rsvp->ID : 0 ); echo self::checkbox_html( $id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped in checkbox_html(). } /** * After the ticket checkout's email field. Event Tickets' checkout sends * only the buyer's name and email, so a small script tells this site * when the box changes; it's kept against the visitor's cart until the * order completes. */ public function render_checkout_checkbox( $file, $name, $template ) { echo self::checkbox_html( 'emails404-consent-checkout' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped in checkbox_html(). wp_enqueue_script( 'emails404-ticket-consent', EMAILS404_PLUGIN_URL . 'assets/ticket-consent.js', array(), EMAILS404_VERSION, true ); wp_localize_script( 'emails404-ticket-consent', 'emails404TicketConsent', array( 'url' => admin_url( 'admin-ajax.php' ), 'action' => self::AJAX_ACTION, 'nonce' => wp_create_nonce( self::AJAX_ACTION ), 'field' => self::CHECKBOX_FIELD, ) ); } /** admin-ajax: the checkout tick-box changed. */ public function remember_checkout_consent() { check_ajax_referer( self::AJAX_ACTION, 'nonce' ); $hash = self::current_cart_hash(); if ( '' === $hash ) { wp_send_json_error( null, 400 ); } if ( self::request_ticked( $_POST ) ) { set_transient( self::CART_TRANSIENT . md5( $hash ), 1, self::CART_TTL ); } else { delete_transient( self::CART_TRANSIENT . md5( $hash ) ); } wp_send_json_success(); } /** * Whether the cart this order came from had the box ticked. Kept until it * expires (CART_TTL) rather than read once: an order's status hook can * fire more than once, and each attendee keeps the answer anyway. */ public static function take_cart_consent( $hash ) { if ( '' === $hash ) { return false; } return (bool) get_transient( self::CART_TRANSIENT . md5( $hash ) ); } /** The visitor's Tickets Commerce cart (Event Tickets reads it from its own cookie). */ private static function current_cart_hash() { if ( ! function_exists( 'tribe' ) || ! class_exists( '\TEC\Tickets\Commerce\Cart' ) ) { return ''; } try { $hash = tribe( \TEC\Tickets\Commerce\Cart::class )->get_cart_hash(); } catch ( \Throwable $e ) { return ''; } return is_string( $hash ) ? $hash : ''; } /** Where an order keeps the cart it was made from (Order::$hash_meta_key). */ private static function order_hash_meta_key() { return class_exists( '\TEC\Tickets\Commerce\Order' ) && isset( \TEC\Tickets\Commerce\Order::$hash_meta_key ) ? \TEC\Tickets\Commerce\Order::$hash_meta_key : '_tec_tc_order_hash'; } /** * Whether the visitor explicitly checked the marketing-consent field - * never inferred from purchasing a ticket, registering, or supplying * an email (Part G). Requires Event Tickets Plus's own "Attendee * Information" custom-field system (the one documented, supported * mechanism for adding a field to the registration form - see the * class doc comment) and a configured field slug * (Settings::get_consent_field_slug()) matching a Checkbox-type field * the site admin added to the relevant ticket(s). Defaults to false * (no consent) whenever any of these preconditions aren't met - never * defaults to true. */ public function consent_given_for_attendee( $attendee_id ) { // This plugin's own tick-box (RSVP form or ticket checkout). if ( '1' === (string) get_post_meta( $attendee_id, self::CONSENT_META, true ) ) { return true; } if ( ! self::is_plus_available() ) { return false; } $slug = Settings::get_consent_field_slug(); if ( '' === $slug ) { return false; } if ( ! class_exists( 'Tribe__Tickets_Plus__Meta' ) || ! function_exists( 'tribe' ) ) { return false; } $meta = tribe( 'tickets-plus.meta' )->get_attendee_meta_values( $attendee_id ); if ( ! is_array( $meta ) ) { return false; } foreach ( $meta as $field_slug => $value ) { if ( sanitize_key( $field_slug ) !== $slug ) { continue; } return self::is_truthy_checkbox_value( $value ); } return false; } /** Event Tickets Plus checkbox fields commonly store a non-empty string/array when checked and an empty value when unchecked - normalized defensively rather than assuming one exact representation. */ private static function is_truthy_checkbox_value( $value ) { if ( is_array( $value ) ) { return ! empty( $value ); } $value = strtolower( trim( (string) $value ) ); return '' !== $value && ! in_array( $value, array( '0', 'no', 'false', 'off' ), true ); } private function is_rsvp_going( $attendee_id ) { if ( ! class_exists( 'Tribe__Tickets__RSVP' ) ) { return false; } // Tribe__Tickets__RSVP::ATTENDEE_RSVP_KEY is the class's own // documented meta-key constant for the going/not-going value on // an attendee post (referenced by the RSVP class's own internal // going/not-going dispatch - see the class doc comment's hook // citation) - preferred over a hardcoded literal so a future // Event Tickets release changing the underlying key string still // resolves correctly. Falls back to the long-stable literal only // if the constant isn't present on the installed version. $meta_key = defined( 'Tribe__Tickets__RSVP::ATTENDEE_RSVP_KEY' ) ? \Tribe__Tickets__RSVP::ATTENDEE_RSVP_KEY : '_tribe_rsvp_status'; $status = get_post_meta( $attendee_id, $meta_key, true ); return self::RSVP_GOING_STATUS === strtolower( (string) $status ); } /** * Resolves an attendee's email/name. * * RSVP: `tribe( 'tickets.rsvp' )->get_attendee( $attendee_id )`, a * documented public method (see * https://docs.theeventscalendar.com/reference/classes/tribe__tickets__rsvp/get_attendee/ ) * returning `holder_email`/`holder_name` (who actually gets emailed), * falling back to `purchaser_email`/`purchaser_name` if the holder * fields are blank (an RSVP taken on someone else's behalf). * * Tickets Commerce: no equivalent single documented "read one * attendee's contact fields" method was confirmed during this slice's * research (see docs/wordpress-integration.md#event-tickets-support * for the exact gap). Falls back to the WordPress-core-guaranteed * attendee post's `post_title` (confirmed by the Event Tickets REST * API's own documented attendee shape - `title` = full name - at * https://docs.nexcess.com/software/the-events-calendar/event-tickets-rest-api/ ) * for the name, and the REST-documented `email` postmeta key * (`_tec_tickets_commerce_email` where present) - **flagged in * docs/wordpress-integration.md's manual verification plan as the one * data-access point to confirm against the site's actual installed * Event Tickets version**, since this is a data-shape question, not a * hook name, and the public documentation available during this * slice's research did not give full certainty on it. * * @return array{email:?string,first_name:?string,last_name:?string} */ private function resolve_attendee_contact( $attendee_id ) { if ( function_exists( 'tribe' ) && class_exists( 'Tribe__Tickets__RSVP' ) ) { $rsvp = tribe( 'tickets.rsvp' ); if ( $rsvp && method_exists( $rsvp, 'get_attendee' ) ) { $attendee = $rsvp->get_attendee( $attendee_id ); if ( is_array( $attendee ) ) { $email = ! empty( $attendee['holder_email'] ) ? $attendee['holder_email'] : ( $attendee['purchaser_email'] ?? '' ); $name = ! empty( $attendee['holder_name'] ) ? $attendee['holder_name'] : ( $attendee['purchaser_name'] ?? '' ); if ( $email ) { return self::split_contact( $email, $name ); } } } } $post = get_post( $attendee_id ); $name = $post ? $post->post_title : ''; $email = get_post_meta( $attendee_id, '_tec_tickets_commerce_email', true ); if ( ! $email ) { $email = get_post_meta( $attendee_id, '_tribe_tickets_full_name_email', true ); } return self::split_contact( (string) $email, (string) $name ); } private static function split_contact( $email, $full_name ) { $full_name = trim( (string) $full_name ); $first = $full_name; $last = null; $space_pos = strrpos( $full_name, ' ' ); if ( false !== $space_pos ) { $first = trim( substr( $full_name, 0, $space_pos ) ); $last = trim( substr( $full_name, $space_pos + 1 ) ); } return array( 'email' => sanitize_email( $email ), 'first_name' => '' !== $first ? $first : null, 'last_name' => $last, ); } /** Attendee IDs generated for a Tickets Commerce order - via the documented Attendee ORM (tec_tc_attendees()), scoped to this one order. */ private function get_order_attendee_ids( $order_id ) { if ( ! function_exists( 'tec_tc_attendees' ) ) { return array(); } $attendees = tec_tc_attendees()->by( 'order', $order_id )->get_ids(); return is_array( $attendees ) ? $attendees : array(); } private function get_attendee_event_id( $attendee_id ) { $event_id = get_post_meta( $attendee_id, '_tribe_eddtickets_event', true ); if ( ! $event_id ) { $event_id = get_post_meta( $attendee_id, '_tribe_tickets_event', true ); } return $event_id ? (int) $event_id : 0; } }