← Back
Editing: RSVP.php
<?php /** * RSVP Attendee Repository. * * Handles ORM operations for RSVP attendees. * * @since 4.10.6 * * @package Tribe\Tickets\Repositories\Attendee */ // phpcs:disable StellarWP.Classes.ValidClassName.NotSnakeCase use TEC\Tickets\Repositories\Traits\Get_Field; use TEC\Tickets\RSVP\Contracts\Attendee_Repository_Interface; use Tribe__Utils__Array as Arr; /** * The ORM/Repository class for RSVP attendees. * * @since 4.10.6 * * @property Tribe__Tickets__RSVP $attendee_provider */ class Tribe__Tickets__Repositories__Attendee__RSVP extends Tribe__Tickets__Attendee_Repository implements Attendee_Repository_Interface { use Get_Field; /** * Key name to use when limiting lists of keys. * * @var string */ protected $key_name = 'rsvp'; /** * {@inheritdoc} */ public function __construct() { parent::__construct(); $this->attendee_provider = tribe( 'tickets.rsvp' ); $this->create_args['post_type'] = $this->attendee_provider->attendee_object; // Use a regular variable so we can get constants from it in a PHP <7.0 compatible way. $attendee_provider = $this->attendee_provider; // Add object specific aliases. $this->update_fields_aliases = array_merge( $this->update_fields_aliases, [ 'ticket_id' => $attendee_provider::ATTENDEE_PRODUCT_KEY, 'event_id' => $attendee_provider::ATTENDEE_EVENT_KEY, 'post_id' => $attendee_provider::ATTENDEE_EVENT_KEY, 'security_code' => $attendee_provider->security_code, 'order_id' => $attendee_provider->order_key, 'optout' => $attendee_provider::ATTENDEE_OPTOUT_KEY, 'user_id' => $attendee_provider->attendee_user_id, 'price_paid' => $attendee_provider->price_paid, 'full_name' => $attendee_provider->full_name, 'email' => $attendee_provider->email, 'attendee_status' => $attendee_provider::ATTENDEE_RSVP_KEY, 'rsvp_status' => $attendee_provider::ATTENDEE_RSVP_KEY, 'ticket_sent' => $attendee_provider->attendee_ticket_sent, 'deleted_product' => $attendee_provider->deleted_product, 'unique_id' => '_unique_id', 'subscribed' => $attendee_provider->attendee_subscribed, 'qr_status' => '_tribe_qr_status', 'check_in_details' => $attendee_provider->checkin_key . '_details', 'activity_log' => '_tribe_attendee_activity_log', ] ); } /** * {@inheritdoc} */ public function attendee_types() { return $this->limit_list( $this->key_name, parent::attendee_types() ); } /** * {@inheritdoc} */ public function attendee_to_event_keys() { return $this->limit_list( $this->key_name, parent::attendee_to_event_keys() ); } /** * {@inheritdoc} */ public function attendee_to_ticket_keys() { return $this->limit_list( $this->key_name, parent::attendee_to_ticket_keys() ); } /** * {@inheritdoc} */ public function attendee_to_order_keys() { return $this->limit_list( $this->key_name, parent::attendee_to_order_keys() ); } /** * {@inheritdoc} */ public function purchaser_name_keys() { return $this->limit_list( $this->key_name, parent::purchaser_name_keys() ); } /** * {@inheritdoc} */ public function purchaser_email_keys() { return $this->limit_list( $this->key_name, parent::purchaser_email_keys() ); } /** * {@inheritdoc} */ public function security_code_keys() { return $this->limit_list( $this->key_name, parent::security_code_keys() ); } /** * {@inheritdoc} */ public function attendee_optout_keys() { return $this->limit_list( $this->key_name, parent::attendee_optout_keys() ); } /** * {@inheritdoc} */ public function checked_in_keys() { return $this->limit_list( $this->key_name, parent::checked_in_keys() ); } /** * Set up the arguments to set for the attendee for this provider. * * @since 5.1.0 * * @param array $args List of arguments to set for the attendee. * @param array $attendee_data List of additional attendee data. * @param Tribe__Tickets__Ticket_Object $ticket The ticket object or null if not relying on it. * * @return array List of arguments to set for the attendee. */ public function setup_attendee_args( $args, $attendee_data, $ticket = null ) { // Set default order ID. if ( empty( $args['order_id'] ) ) { // Use a regular variable so we can call static methods from it in a PHP <7.0 compatible way. $attendee_provider = $this->attendee_provider; $args['order_id'] = $attendee_provider::generate_order_id(); } // Set default attendee status. if ( ! isset( $args['attendee_status'] ) ) { $args['attendee_status'] = 'yes'; } return $args; } /** * Handle backwards compatible creation actions for RSVPs. * * @since 5.1.0 * * @param WP_Post $attendee The attendee object. * @param array $attendee_data List of additional attendee data. * @param Tribe__Tickets__Ticket_Object $ticket The ticket object. */ public function trigger_create_actions( $attendee, $attendee_data, $ticket ) { $attendee_id = $attendee->ID; $post_id = Arr::get( $attendee_data, 'post_id' ); $order_id = $attendee_data['order_id']; $product_id = $ticket->ID; $order_attendee_id = Arr::get( $attendee_data, 'order_attendee_id' ); $attendee_status = Arr::get( $attendee_data, 'attendee_status', 'yes' ); /** * RSVP specific action fired when a RSVP-driven attendee ticket for an event is generated. * Used to assign a unique ID to the attendee. * * @param int $attendee_id ID of attendee ticket. * @param int $post_id ID of event. * @param string $order_id RSVP order ID (hash). * @param int $product_id RSVP product ID. */ do_action( 'event_tickets_rsvp_attendee_created', $attendee_id, $post_id, $order_id, $product_id ); /** * Action fired when an RSVP attendee ticket is created. * Used to store attendee meta. * * @param int $attendee_id ID of the attendee post. * @param int $post_id Event post ID. * @param int $product_id RSVP ticket post ID. * @param int $order_attendee_id Attendee # for order. */ do_action( 'event_tickets_rsvp_ticket_created', $attendee_id, $post_id, $product_id, $order_attendee_id ); if ( null === $order_attendee_id ) { /** * Action fired when an RSVP ticket has had attendee tickets generated for it. * * @param int $product_id RSVP ticket post ID. * @param string $order_id ID (hash) of the RSVP order. * @param int $qty Quantity ordered. */ do_action( 'event_tickets_rsvp_tickets_generated_for_product', $product_id, $order_id, 1 ); } // Update the ticket sales numbers. if ( $post_id && 'yes' === $attendee_status ) { $this->attendee_provider->increase_ticket_sales_by( $product_id, 1 ); } parent::trigger_create_actions( $attendee, $attendee_data, $ticket ); } /** * Handle backwards compatible update actions for RSVPs. * * @since 5.1.0 * * @param array $attendee_data List of attendee data to be saved. */ public function trigger_update_actions( $attendee_data ) { parent::trigger_update_actions( $attendee_data ); $attendee_id = (int) Arr::get( $attendee_data, 'attendee_id' ); $post_id = (int) Arr::get( $attendee_data, 'post_id' ); $attendee_status = Arr::get( $attendee_data, 'attendee_status' ); /** * An Action fired when an RSVP is updated. * * @since 4.11.0 * * @param int $attendee_id The attendee ID. * @param int $post_id The event/post ID. * @param string $attendee_status The status of the attendee, either yes or no. */ do_action( 'event_tickets_rsvp_after_attendee_update', $attendee_id, $post_id, $attendee_status ); } /** * Bulk update multiple attendees with the same field values. * * More efficient than calling update() in a loop. * * Note: This method does NOT use database transactions. Updates are * performed independently and may result in partial success if some * updates fail. * * @since 5.30.0 * * @param array $attendee_ids Attendee IDs. * @param array $updates Fields to update (e.g., ['attendee_status' => 'yes']). * * @return array Results indexed by attendee ID (true = success, false = failure). */ public function bulk_update( array $attendee_ids, array $updates ): array { $results = []; foreach ( $attendee_ids as $attendee_id ) { $result = $this->by( 'id', $attendee_id ) ->set_args( $updates ) ->save(); $results[ $attendee_id ] = false !== $result; } return $results; } /** * Get attendee counts grouped by RSVP status. * * @since 5.30.0 * * @param int $event_id Event ID. * * @return array Status counts (e.g., ['yes' => 10, 'no' => 5]). */ public function get_status_counts( int $event_id ): array { global $wpdb; $results = $wpdb->get_results( $wpdb->prepare( 'SELECT COALESCE(pm2.meta_value, %s) as status, COUNT(*) as count FROM %i p INNER JOIN %i pm1 ON p.ID = pm1.post_id AND pm1.meta_key = %s LEFT JOIN %i pm2 ON p.ID = pm2.post_id AND pm2.meta_key = %s WHERE p.post_type = %s AND pm1.meta_value = %d GROUP BY COALESCE(pm2.meta_value, %s)', 'unknown', $wpdb->posts, $wpdb->postmeta, '_tribe_rsvp_event', $wpdb->postmeta, '_tribe_rsvp_status', 'tribe_rsvp_attendees', $event_id, 'unknown' ) ); $counts = []; foreach ( $results as $row ) { $counts[ $row->status ] = (int) $row->count; } return $counts; } /** * Get attendees by email address for privacy operations. * * @since 5.30.0 * * @param string $email The email address to search for. * @param int $page The page number (1-indexed). * @param int $per_page Number of results per page. * * @return array{ * posts: WP_Post[], * has_more: bool * } The list of attendees and whether there are more results to fetch. */ public function get_attendees_by_email( string $email, int $page, int $per_page ): array { $posts = $this->by( 'purchaser_email', $email ) ->per_page( $per_page ) ->page( $page ) ->order_by( 'ID' ) ->order( 'ASC' ) ->all(); return [ 'posts' => $posts, 'has_more' => count( $posts ) >= $per_page, ]; } /** * Delete an attendee for privacy erasure. * * This method will immediately delete the Attendee skipping trash. * * @since 5.30.0 * * @param int $attendee_id The attendee post ID to delete. * * @return array{ * success: bool, * event_id: int|null * } The deletion result and the event ID where the attendee was found, if any. */ public function delete_attendee( int $attendee_id ): array { $event_id = get_post_meta( $attendee_id, $this->attendee_provider::ATTENDEE_EVENT_KEY, true ); $deleted = wp_delete_post( $attendee_id, true ); return [ 'success' => (bool) $deleted, 'event_id' => $event_id ? (int) $event_id : null, ]; } /** * Get the ticket/product ID for an attendee. * * @since 5.30.0 * * @param int $attendee_id The attendee post ID. * * @return int The ticket/product ID, or 0 if not found. */ public function get_ticket_id( int $attendee_id ): int { $ticket_id = get_post_meta( $attendee_id, $this->attendee_provider::ATTENDEE_PRODUCT_KEY, true ); return $ticket_id ? (int) $ticket_id : 0; } }
Save File
Cancel