/**
  * @return EED_Ticket_Selector_Caff
  */
 public static function instance()
 {
     return parent::get_instance(__CLASS__);
 }
 /**
  * 		process_reg_step
  *
  * 		@access 		public
  * 		@return 		string
  */
 public function process_reg_step()
 {
     $this->_set_reg_event();
     EE_Registry::instance()->REQ->set_espresso_page(TRUE);
     //what step are we on?
     $cart = EE_Registry::instance()->SSN->get_session_data('cart');
     $step = empty($cart) ? 'ticket' : 'questions';
     //if doing ajax then we need to verify the nonce
     if ('DOING_AJAX') {
         $nonce = isset($this->_req_data[$this->_req_nonce]) ? sanitize_text_field($this->_req_data[$this->_req_nonce]) : '';
         $this->_verify_nonce($nonce, $this->_req_nonce);
     }
     switch ($step) {
         case 'ticket':
             //process ticket selection
             $success = EED_Ticket_Selector::instance()->process_ticket_selections();
             if ($success) {
                 EE_Error::add_success(__('Tickets Selected. Now complete the registration.'), 'event_espresso');
             } else {
                 $query_args['step_error'] = $this->_req_data['step_error'] = TRUE;
             }
             if (defined('DOING_AJAX')) {
                 $this->new_registration();
                 //display next step
             } else {
                 $query_args['action'] = 'new_registration';
                 $query_args['processing_registration'] = true;
                 $query_args['event_id'] = $this->_reg_event->ID();
                 $this->_redirect_after_action(FALSE, '', '', $query_args, TRUE);
             }
             break;
         case 'questions':
             if (!isset($this->_req_data['txn_reg_status_change'], $this->_req_data['txn_reg_status_change']['send_notifications'])) {
                 add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_false', 15);
             }
             //process registration
             $transaction = EED_Single_Page_Checkout::instance()->process_registration_from_admin();
             if (!$transaction instanceof EE_Transaction) {
                 $query_args = array('action' => 'new_registration', 'processing_registration' => true, 'event_id' => $this->_reg_event->ID());
                 if (defined('DOING_AJAX')) {
                     //display registration form again because there are errors (maybe validation?)
                     $this->new_registration();
                     return;
                 } else {
                     $this->_redirect_after_action(FALSE, '', '', $query_args, TRUE);
                     return;
                 }
             }
             /** @type EE_Transaction_Payments $transaction_payments */
             $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
             // maybe update status, and make sure to save transaction if not done already
             if (!$transaction_payments->update_transaction_status_based_on_total_paid($transaction)) {
                 $transaction->save();
             }
             $query_args = array('action' => 'view_transaction', 'TXN_ID' => $transaction->ID(), 'page' => 'espresso_transactions');
             EE_Error::add_success(__('Registration Created.  Please review the transaction and add any payments as necessary', 'event_espresso'));
             $this->_redirect_after_action(FALSE, '', '', $query_args, TRUE);
             break;
     }
     //what are you looking here for?  Should be nothing to do at this point.
 }
 /**
  * 	process_ticket_selections
  *
  *	@access public
  * 	@access 		public
  * 	@return		array  or FALSE
  */
 public function process_ticket_selections()
 {
     do_action('EED_Ticket_Selector__process_ticket_selections__before');
     // check nonce
     if (!is_admin() && (!EE_Registry::instance()->REQ->is_set('process_ticket_selections_nonce') || !wp_verify_nonce(EE_Registry::instance()->REQ->get('process_ticket_selections_nonce'), 'process_ticket_selections'))) {
         EE_Error::add_error(sprintf(__('We\'re sorry but your request failed to pass a security check.%sPlease click the back button on your browser and try again.', 'event_espresso'), '<br/>'), __FILE__, __FUNCTION__, __LINE__);
         return FALSE;
     }
     //		d( EE_Registry::instance()->REQ );
     self::$_available_spaces = array('tickets' => array(), 'datetimes' => array());
     //we should really only have 1 registration in the works now (ie, no MER) so clear any previous items in the cart.
     // When MER happens this will probably need to be tweaked, possibly wrapped in a conditional checking for some constant defined in MER etc.
     EE_Registry::instance()->load_core('Session');
     // unless otherwise requested, clear the session
     if (apply_filters('FHEE__EE_Ticket_Selector__process_ticket_selections__clear_session', TRUE)) {
         EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__);
     }
     //d( EE_Registry::instance()->SSN );
     do_action('AHEE_log', __FILE__, __FUNCTION__, '');
     // do we have an event id?
     if (EE_Registry::instance()->REQ->is_set('tkt-slctr-event-id')) {
         // validate/sanitize data
         $valid = self::_validate_post_data();
         //EEH_Debug_Tools::printr( $_REQUEST, '$_REQUEST', __FILE__, __LINE__ );
         //EEH_Debug_Tools::printr( $valid, '$valid', __FILE__, __LINE__ );
         //EEH_Debug_Tools::printr( $valid[ 'total_tickets' ], 'total_tickets', __FILE__, __LINE__ );
         //EEH_Debug_Tools::printr( $valid[ 'max_atndz' ], 'max_atndz', __FILE__, __LINE__ );
         //check total tickets ordered vs max number of attendees that can register
         if ($valid['total_tickets'] > $valid['max_atndz']) {
             // ordering too many tickets !!!
             $total_tickets_string = _n('You have attempted to purchase %s ticket.', 'You have attempted to purchase %s tickets.', $valid['total_tickets'], 'event_espresso');
             $limit_error_1 = sprintf($total_tickets_string, $valid['total_tickets']);
             // dev only message
             $max_atndz_string = _n('The registration limit for this event is %s ticket per registration, therefore the total number of tickets you may purchase at a time can not exceed %s.', 'The registration limit for this event is %s tickets per registration, therefore the total number of tickets you may purchase at a time can not exceed %s.', $valid['max_atndz'], 'event_espresso');
             $limit_error_2 = sprintf($max_atndz_string, $valid['max_atndz'], $valid['max_atndz']);
             EE_Error::add_error($limit_error_1 . '<br/>' . $limit_error_2, __FILE__, __FUNCTION__, __LINE__);
         } else {
             // all data appears to be valid
             $tckts_slctd = FALSE;
             $success = TRUE;
             // load cart
             EE_Registry::instance()->load_core('Cart');
             // cycle thru the number of data rows sent from the event listing
             for ($x = 0; $x < $valid['rows']; $x++) {
                 // does this row actually contain a ticket quantity?
                 if (isset($valid['qty'][$x]) && $valid['qty'][$x] > 0) {
                     // YES we have a ticket quantity
                     $tckts_slctd = TRUE;
                     //						d( $valid['ticket_obj'][$x] );
                     if ($valid['ticket_obj'][$x] instanceof EE_Ticket) {
                         // then add ticket to cart
                         $ticket_added = self::_add_ticket_to_cart($valid['ticket_obj'][$x], $valid['qty'][$x]);
                         $success = !$ticket_added ? FALSE : $success;
                         if (EE_Error::has_error()) {
                             break;
                         }
                     } else {
                         // nothing added to cart retrieved
                         EE_Error::add_error(sprintf(__('A valid ticket could not be retrieved for the event.%sPlease click the back button on your browser and try again.', 'event_espresso'), '<br/>'), __FILE__, __FUNCTION__, __LINE__);
                     }
                 }
             }
             //d( EE_Registry::instance()->CART );
             //die(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< KILL REDIRECT HERE BEFORE CART UPDATE
             if ($tckts_slctd) {
                 if ($success) {
                     do_action('FHEE__EE_Ticket_Selector__process_ticket_selections__before_redirecting_to_checkout', EE_Registry::instance()->CART, $this);
                     EE_Registry::instance()->CART->recalculate_all_cart_totals();
                     EE_Registry::instance()->CART->save_cart(FALSE);
                     EE_Registry::instance()->SSN->update();
                     //d( EE_Registry::instance()->CART );
                     //die(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< OR HERE TO KILL REDIRECT AFTER CART UPDATE
                     // just return TRUE for registrations being made from admin
                     if (is_admin()) {
                         return TRUE;
                     }
                     wp_safe_redirect(apply_filters('FHEE__EE_Ticket_Selector__process_ticket_selections__success_redirect_url', EE_Registry::instance()->CFG->core->reg_page_url()));
                     exit;
                 } else {
                     if (!EE_Error::has_error()) {
                         // nothing added to cart
                         EE_Error::add_attention(__('No tickets were added for the event', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
                     }
                 }
             } else {
                 // no ticket quantities were selected
                 EE_Error::add_error(__('You need to select a ticket quantity before you can proceed.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
             }
         }
         //die(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< KILL BEFORE REDIRECT
         // at this point, just return if registration is being made from admin
         if (is_admin()) {
             return FALSE;
         }
         if ($valid['return_url']) {
             EE_Error::get_notices(FALSE, TRUE);
             wp_safe_redirect($valid['return_url']);
             exit;
         } elseif (isset($event_to_add['id'])) {
             EE_Error::get_notices(FALSE, TRUE);
             wp_safe_redirect(get_permalink($event_to_add['id']));
             exit;
         } else {
             echo EE_Error::get_notices();
         }
     } else {
         // $_POST['tkt-slctr-event-id'] was not set ?!?!?!?
         EE_Error::add_error(sprintf(__('An event id was not provided or was not received.%sPlease click the back button on your browser and try again.', 'event_espresso'), '<br/>'), __FILE__, __FUNCTION__, __LINE__);
     }
     return FALSE;
 }
 /**
  * espresso_view_details_btn
  * @param null $event
  */
 function espresso_view_details_btn($event = NULL)
 {
     if (!apply_filters('FHEE_disable_espresso_view_details_btn', FALSE)) {
         espresso_load_ticket_selector();
         echo EED_Ticket_Selector::display_ticket_selector($event, TRUE);
     }
 }
 /**
  * 		_process_registration_step
  *
  * 		@access 		public
  * 		@return 		string
  */
 public function _process_registration_step()
 {
     $this->_set_reg_event();
     EE_Registry::instance()->REQ->set_espresso_page(TRUE);
     //what step are we on?
     $cart = EE_Registry::instance()->SSN->get_session_data('cart');
     $step = empty($cart) ? 'ticket' : 'questions';
     //if doing ajax then we need to verify the nonce
     if ('DOING_AJAX') {
         $nonce = isset($this->_req_data[$this->_req_nonce]) ? sanitize_text_field($this->_req_data[$this->_req_nonce]) : '';
         $this->_verify_nonce($nonce, $this->_req_nonce);
     }
     switch ($step) {
         case 'ticket':
             //process ticket selection
             $success = EED_Ticket_Selector::instance()->process_ticket_selections();
             if ($success) {
                 EE_Error::add_success(__('Tickets Selected. Now complete the registration.'), 'event_espresso');
             } else {
                 $query_args['step_error'] = $this->_req_data['step_error'] = TRUE;
             }
             if (defined('DOING_AJAX')) {
                 $this->new_registration();
                 //display next step
             } else {
                 $query_args['action'] = 'new_registration';
                 $query_args['processing_registration'] = true;
                 $query_args['event_id'] = $this->_reg_event->ID();
                 $this->_redirect_after_action(FALSE, '', '', $query_args, TRUE);
             }
             break;
         case 'questions':
             //process registration
             $TXN_ID = EED_Single_Page_Checkout::instance()->process_registration_from_admin();
             if (!$TXN_ID) {
                 $query_args = array('action' => 'new_registration', 'processing_registration' => true, 'event_id' => $this->_reg_event->ID());
                 if (defined('DOING_AJAX')) {
                     $this->new_registration();
                     //display registration form again because there are errors (maybe validation?)
                 } else {
                     $this->_redirect_after_action(FALSE, '', '', $query_args, TRUE);
                 }
             }
             $query_args = array('action' => 'view_transaction', 'TXN_ID' => $TXN_ID, 'page' => 'espresso_transactions');
             EE_Error::add_success(__('Registration Created.  Please review the transaction and add any payments as necessary', 'event_espresso'));
             $this->_redirect_after_action(FALSE, '', '', $query_args, TRUE);
             break;
     }
     //what are you looking here for?  Should be nothing to do at this point.
 }
 /**
  *    process_ticket_selections
  *
  * @access 	public
  * @return 	void
  */
 public static function process_ticket_selections()
 {
     $response = array('tickets_added' => false);
     if (EED_Ticket_Selector::instance()->process_ticket_selections()) {
         $EVT_ID = absint(EE_Registry::instance()->REQ->get('tkt-slctr-event-id', 0));
         $tickets = EE_Registry::instance()->REQ->get('tkt-slctr-qty-' . $EVT_ID, array());
         $ticket_count = 0;
         // radio buttons send ticket info as a string like: "TKT_ID-QTY"
         if (!is_array($tickets)) {
             $tickets = explode('-', $tickets);
             array_shift($tickets);
         }
         foreach ($tickets as $quantity) {
             $ticket_count += $quantity;
         }
         $response = array('tickets_added' => true, 'ticket_count' => $ticket_count, 'btn_id' => "#ticket-selector-submit-{$EVT_ID}-btn", 'btn_txt' => EED_Multi_Event_Registration::filter_ticket_selector_submit_button('', null, true), 'form_html' => EED_Multi_Event_Registration::filter_ticket_selector_form_html('', null, true), 'mini_cart' => EED_Multi_Event_Registration::get_mini_cart(), 'cart_results' => EED_Multi_Event_Registration::get_cart_results($ticket_count));
     }
     // just send the ajax
     echo json_encode(array_merge(EE_Error::get_notices(false), $response));
     // to be... or...
     die;
 }