← Back
Editing: Rsvp.php
<?php /** * RSVP Block. * * @since 4.9 */ // phpcs:disable StellarWP.Classes.ValidClassName.NotSnakeCase /** * Class Tribe__Tickets__Editor__Blocks__Rsvp * * @since 4.9 */ class Tribe__Tickets__Editor__Blocks__Rsvp extends Tribe__Editor__Blocks__Abstract { /** * Deprecated hook method. * * @deprecated 5.30.0 Hooks are now registered by TEC\Tickets\RSVP\V1\Controller. * * @since 5.30.0 * * @return void */ public function hook() { _deprecated_function( __METHOD__, '5.30.0', 'TEC\\Tickets\\RSVP\\V1\\Controller' ); } /** * Which is the name/slug of this block * * @since 4.9 * * @return string */ public function slug() { return 'rsvp'; } /** * Set the default attributes of this block * * @since 4.9 * * @return array */ public function default_attributes() { return []; } /** * Since we are dealing with a Dynamic type of Block we need a PHP method to render it * * @since 4.9 * * @param array $attributes Block attributes. * * @return string */ public function render( $attributes = [] ) { /** @var Tribe__Tickets__Editor__Template $template */ $template = tribe( 'tickets.editor.template' ); $post_id = $template->get( 'post_id', null, false ); $tickets_view = Tribe__Tickets__Tickets_View::instance(); return $tickets_view->get_rsvp_block( $post_id, false ); } /** * Method to get all RSVP tickets. * * @since 4.9 * * @param int $post_id The post ID. * * @return array */ public function get_tickets( $post_id ) { $tickets = []; // Bail if there's no event id. if ( ! $post_id ) { return $tickets; } if ( did_action( 'tec_tickets_rsvp_v2_registered' ) ) { return $this->get_v2_tickets( $post_id ); } /** @var Tribe__Tickets__RSVP $rsvp */ $rsvp = tribe( 'tickets.rsvp' ); // Get the tickets IDs for this event. $ticket_ids = $rsvp->get_tickets_ids( $post_id ); // Bail if we don't have tickets. if ( ! $ticket_ids ) { return $tickets; } // We only want RSVP tickets. foreach ( $ticket_ids as $post ) { // Get the ticket. $ticket = $rsvp->get_ticket( $post_id, $post ); if ( ! $ticket instanceof Tribe__Tickets__Ticket_Object || $rsvp->class_name !== $ticket->provider_class ) { continue; } $tickets[] = $ticket; } return $tickets; } /** * Get TC-RSVP tickets for the RSVP block when V2 is enabled. * * @since 5.30.0 * * @param int $post_id The post ID. * * @return Tribe__Tickets__Ticket_Object[] */ protected function get_v2_tickets( $post_id ) { $tickets = []; /** @var Tribe__Tickets__RSVP $rsvp */ $rsvp = tribe( 'tickets.rsvp' ); foreach ( tribe( 'tickets.ticket-repository.rsvp' )->by( 'event', $post_id )->all() as $ticket_post ) { if ( ! $ticket_post instanceof WP_Post ) { continue; } $ticket = $rsvp->get_ticket( $post_id, $ticket_post->ID ); if ( $ticket instanceof Tribe__Tickets__Ticket_Object ) { $tickets[] = $ticket; } } return $tickets; } /** * Method to get the active RSVP tickets. * * @since 4.9 * * @param array $tickets The tickets array. * * @return array */ public function get_active_tickets( $tickets ) { $active_tickets = []; foreach ( $tickets as $ticket ) { // Continue if it's not in date range. if ( ! $ticket->date_in_range() ) { continue; } $active_tickets[] = $ticket; } return $active_tickets; } /** * Method to get the all RSVPs past flag. * All RSVPs past flag is true if all RSVPs end date is earlier than current date. * If there are no RSVPs, false is returned. * * @since 4.9 * * @param array $tickets The tickets array. * * @return bool */ public function get_all_tickets_past( $tickets ) { if ( empty( $tickets ) ) { return false; } $all_past = true; foreach ( $tickets as $ticket ) { $all_past = $all_past && $ticket->date_is_later(); } return $all_past; } /** * Get the threshold. * * @since 4.12.3 * * @param int $post_id The post ID. * * @return int */ public function get_threshold( $post_id = 0 ) { /** @var Tribe__Settings_Manager $settings_manager */ $settings_manager = tribe( 'settings.manager' ); $threshold = $settings_manager::get_option( 'ticket-display-tickets-left-threshold', 0 ); /** * Overwrites the threshold to display "# tickets left". * * @since 4.11.1 * * @param int $threshold Stock threshold to trigger display of "# tickets left" text. * @param int $post_id Event ID. */ $threshold = absint( apply_filters( 'tribe_display_rsvp_block_tickets_left_threshold', $threshold, $post_id ) ); return $threshold; } /** * Show unlimited? * * @since 4.12.3 * * @param bool $is_unlimited Whether the ticket is unlimited. * * @return bool */ public function show_unlimited( $is_unlimited ) { /** * Allows hiding of "unlimited" to be toggled on/off conditionally. * * @since 4.11.1 * * @param int $show_unlimited allow showing of "unlimited". */ return (bool) apply_filters( 'tribe_rsvp_block_show_unlimited_availability', false, $is_unlimited ); } /** * Register block assets * * @since 4.9 * * @return void */ public function assets() { $plugin = Tribe__Tickets__Main::instance(); tec_asset( $plugin, 'tribe-tickets-gutenberg-rsvp', 'rsvp-block.js', [ 'jquery' ], null, [ 'localize' => [ 'name' => 'TribeRsvp', 'data' => fn() => [ 'ajaxurl' => admin_url( 'admin-ajax.php', ( is_ssl() ? 'https' : 'http' ) ), 'nonces' => [ 'rsvpHandle' => wp_create_nonce( 'tribe_tickets_rsvp_handle' ), ], ], ], ] ); tec_asset( $plugin, 'tribe-tickets-gutenberg-block-rsvp-style', 'rsvp/frontend.css' ); tec_asset( $plugin, 'tribe-tickets-rsvp-ari', 'v2/rsvp-ari.js', [ 'jquery', 'wp-util', 'tribe-common' ], null, [ 'groups' => 'tribe-tickets-rsvp', 'conditionals' => [ $this, 'should_enqueue_ari' ], 'localize' => [ 'name' => 'TribeRsvp', 'data' => fn() => [ 'ajaxurl' => admin_url( 'admin-ajax.php', ( is_ssl() ? 'https' : 'http' ) ), 'nonces' => [ 'rsvpHandle' => wp_create_nonce( 'tribe_tickets_rsvp_handle' ), ], ], ], ] ); tec_asset( $plugin, 'tribe-tickets-rsvp-manager', 'v2/rsvp-manager.js', [ 'jquery', 'tribe-common', 'tribe-tickets-rsvp-block', 'tribe-tickets-rsvp-tooltip', 'tribe-tickets-rsvp-ari', ], null, [ 'localize' => [ 'name' => 'TribeRsvp', 'data' => static function () { return [ 'ajaxurl' => admin_url( 'admin-ajax.php', ( is_ssl() ? 'https' : 'http' ) ), 'cancelText' => __( 'Are you sure you want to cancel?', 'event-tickets' ), ]; }, ], 'groups' => 'tribe-tickets-rsvp', ] ); tec_asset( $plugin, 'tribe-tickets-rsvp-block', 'v2/rsvp-block.js', [ 'jquery' ], null, [ 'groups' => 'tribe-tickets-rsvp' ] ); tec_asset( $plugin, 'tribe-tickets-rsvp-tooltip', 'v2/rsvp-tooltip.js', [ 'jquery', 'tribe-common', 'tribe-tooltipster', ], null, [ 'groups' => 'tribe-tickets-rsvp', ] ); tec_asset( $plugin, 'tribe-tickets-rsvp-style', 'rsvp.css', [ 'tribe-common-skeleton-style', 'tribe-common-responsive' ], null ); $stylesheet = Tribe__Templates::locate_stylesheet( 'tribe-events/tickets/rsvp.css' ); if ( $stylesheet ) { tec_asset( $plugin, 'tribe-tickets-rsvp-style-override', $stylesheet, [], null ); } } /** * Determine whether we should enqueue the ARI assets. * * @since 5.0.0 * * @return bool Whether we should enqueue the ARI assets. */ public function should_enqueue_ari() { return class_exists( 'Tribe__Tickets_Plus__Main' ); } /** * Function that returns the RSVP form from an AJAX call * * @since 4.9 * * @return void */ public function rsvp_form() { $response = [ 'html' => '', 'view' => 'rsvp-form', ]; $ticket_id = absint( tribe_get_request_var( 'ticket_id', 0 ) ); $going = tribe_get_request_var( 'going', 'yes' ); if ( 0 === $ticket_id ) { wp_send_json_error( $response ); } /** @var Tribe__Tickets__RSVP $rsvp */ $rsvp = tribe( 'tickets.rsvp' ); $ticket = $rsvp->get_ticket( get_the_id(), $ticket_id ); if ( ! $ticket instanceof Tribe__Tickets__Ticket_Object ) { wp_send_json_error( $response ); } $args = [ 'post_id' => $ticket->get_event_id(), 'ticket' => $ticket, 'going' => $going, ]; /** @var Tribe__Tickets__Editor__Template $template */ $template = tribe( 'tickets.editor.template' ); $html = $template->template( 'blocks/rsvp/form/form', $args, false ); $response['html'] = $html; wp_send_json_success( $response ); } /** * Function that process the RSVP * * @since 4.9 * @since 5.29.1 Reject the request unless the requesting user can access the ticket's event. * @since 5.29.3.1 Reject requests without a valid nonce and tickets outside their sale window. * @since 5.31.1.1 Reply with the error when no attendee could be generated, such as for a logged-out visitor when login is required. * @since 5.31.2 Reports an RSVP that reserved no place as unavailable, not as received, and never redirects mid-request. * * @return void */ public function rsvp_process() { $response = [ 'html' => '', 'view' => 'rsvp-process', ]; // Reject requests without a valid nonce; the RSVP form renders one. if ( ! wp_verify_nonce( tribe_get_request_var( 'nonce', '' ), 'tribe_tickets_rsvp_handle' ) ) { wp_send_json_error( $response ); } $ticket_id = absint( tribe_get_request_var( 'ticket_id', 0 ) ); if ( 0 === $ticket_id ) { wp_send_json_error( $response ); } /** @var Tribe__Tickets__RSVP $rsvp */ $rsvp = tribe( 'tickets.rsvp' ); $has_tickets = false; $event = $rsvp->get_event_for_ticket( $ticket_id ); if ( ! $event instanceof WP_Post ) { wp_send_json_error( $response ); } $post_id = $event->ID; // Block processing RSVPs for events the requesting user cannot currently see. if ( ! $rsvp->is_event_accessible( $post_id ) ) { wp_send_json_error( $response ); } $ticket = $rsvp->get_ticket( $post_id, $ticket_id ); // Reject requests for tickets outside their sale window or that cannot be resolved. if ( ! $ticket instanceof Tribe__Tickets__Ticket_Object || ! $ticket->date_in_range() ) { wp_send_json_error( $response ); } /** * RSVP specific action fired just before a RSVP-driven attendee tickets for an order are generated * * @param array $data $_POST Parameters comes from RSVP Form */ do_action( 'tribe_tickets_rsvp_before_order_processing' ); $attendee_details = $rsvp->parse_attendee_details(); if ( false === $attendee_details ) { wp_send_json_error( $response ); } $products = []; // phpcs:disable WordPress.Security.NonceVerification.Missing if ( isset( $_POST['tribe_tickets'] ) ) { $products = wp_list_pluck( array_map( 'tribe_sanitize_deep', (array) $_POST['tribe_tickets'] ), 'ticket_id' ); } elseif ( isset( $_POST['product_id'] ) ) { $products = array_map( 'tribe_sanitize_deep', (array) $_POST['product_id'] ); } // phpcs:enable WordPress.Security.NonceVerification.Missing $products = array_map( 'absint', $products ); $products = array_filter( $products ); $error = null; // Iterate over each product. foreach ( $products as $product_id ) { $ticket_qty = $rsvp->parse_ticket_quantity( $product_id ); if ( ! $ticket_qty ) { // If there were no RSVP tickets for the product added to the cart, continue. continue; } // No redirect: this is an AJAX request, and a refusal must reach the visitor as a message. $generated = $rsvp->generate_tickets_for( $product_id, $ticket_qty, $attendee_details, false ); if ( is_wp_error( $generated ) ) { $error ??= $generated; continue; } $has_tickets = true; } /* * Nothing was created, for example a logged-out visitor when login is required, or an RSVP that is full: * say why instead of confirming. */ if ( ! $has_tickets ) { $message = $error instanceof WP_Error ? $error->get_error_message() : __( 'Requested amount of Tickets are not available!', 'event-tickets' ); $response['remaining_html'] = tribe( 'tickets.editor.template' )->template( 'blocks/rsvp/details/availability', [ 'ticket' => $ticket ], false ); $response['html'] = sprintf( '<div class="tribe-block__rsvp__message__error" role="alert">%s</div>', esc_html( $message ) ); wp_send_json_error( $response ); } $order_id = $attendee_details['order_id']; $attendee_order_status = $attendee_details['order_status']; /** * Fires when an RSVP attendee tickets have been generated. * * @param int $order_id ID of the RSVP order * @param int $post_id ID of the post the order was placed for * @param string $attendee_order_status 'yes' if the user indicated they will attend */ do_action( 'event_tickets_rsvp_tickets_generated', $order_id, $post_id, $attendee_order_status ); $send_mail_stati = [ 'yes' ]; /** * Filters whether a confirmation email should be sent or not for RSVP tickets. * * This applies to attendance and non attendance emails. * * @param bool $send_mail Defaults to `true`. */ $send_mail = apply_filters( 'tribe_tickets_rsvp_send_mail', true ); if ( $send_mail && $has_tickets ) { /** * Filters the attendee order stati that should trigger an attendance confirmation. * * Any attendee order status not listed here will trigger a non attendance email. * * @param array $send_mail_stati An array of default stati triggering an attendance email. * @param int $order_id ID of the RSVP order * @param int $post_id ID of the post the order was placed for * @param string $attendee_order_status 'yes' if the user indicated they will attend */ $send_mail_stati = apply_filters( 'tribe_tickets_rsvp_send_mail_stati', $send_mail_stati, $order_id, $post_id, $attendee_order_status ); // No point sending tickets if their current intention is not to attend. if ( in_array( $attendee_order_status, $send_mail_stati, true ) ) { $rsvp->send_tickets_email( $order_id, $post_id ); } else { $rsvp->send_non_attendance_confirmation( $order_id, $post_id ); } } $args = [ 'ticket' => $ticket, ]; $remaining = $ticket->remaining(); /** @var Tribe__Tickets__Editor__Template $template */ $template = tribe( 'tickets.editor.template' ); if ( ! $remaining ) { $response['status_html'] = $template->template( 'blocks/rsvp/status', $args, false ); } $response['remaining'] = $ticket->remaining(); $response['remaining_html'] = $template->template( 'blocks/rsvp/details/availability', $args, false ); $response['html'] = $template->template( 'blocks/rsvp/messages/success', $args, false ); wp_send_json_success( $response ); } }
Save File
Cancel