/**
  * Processes the btb_checkout_overview shortcode.
  *
  * Based on the set style it calls a sub function to render the output.
  *
  * @param array $atts The shortcode attributes. See class description for explanation.
  */
 public static function btb_checkout_overview_func($atts)
 {
     if (isset($_GET['booking']) && isset($_GET['btbnonce']) && !isset($_POST['btb_checkout_nonce'])) {
         if (!wp_verify_nonce($_GET['btbnonce'], 'btb_direct_booking_nonce')) {
             return;
         }
         $master_instance = get_option('btb_instance_type', 'master') == 'master';
         if ($master_instance) {
             $booking = btb_get_booking($_GET['booking'], OBJECT, 'display');
         } else {
             $booking = btb_get_booking_from_api($_GET['booking'], OBJECT, 'display');
         }
         if (!$booking) {
             return;
         }
         if ($booking->post_type !== "btb_booking") {
             return;
         }
         if ($master_instance) {
             $time = btb_get_time($booking->booked_time, OBJECT, 'display');
         } else {
             $time = btb_get_time_from_api($booking->booked_time, OBJECT, 'display');
         }
         if (!$time) {
             return;
         }
         if ($time->post_type !== "btb_time") {
             return;
         }
         if ($master_instance) {
             $event = btb_get_event($time->event, OBJECT, 'display');
         } else {
             $event = btb_get_event_from_api($time->event, OBJECT, 'display');
         }
         if (!$event) {
             return;
         }
         if ($event->post_type !== "btb_event") {
             return;
         }
         $a = shortcode_atts(array('headline' => ''), $atts);
         switch (get_option('btb_style', 'custom')) {
             case 'avada':
                 return self::btb_checkout_overview_avada($a, $booking, $time, $event);
             case 'bootstrap3':
                 return self::btb_checkout_overview_bs3($a, $booking, $time, $event);
             default:
                 return self::btb_checkout_overview_default($a, $booking, $time, $event);
         }
     }
 }
    /**
     * Processes the @c btb_checkout shortcode.
     *
     * This handles the POST data if avaialble and applies the @c btb_create_checkout_form filter generate
     * the display content. This filter is chosen based on the selected style.
     *
     * @param array $atts The shortcode attributes. See class description for explanation.
     */
    public static function btb_checkout_func($atts)
    {
        $master_instance = get_option('btb_instance_type', 'master') == 'master';
        if (isset($_GET['booking']) && isset($_GET['btbnonce']) && !isset($_POST['btb_checkout_nonce'])) {
            // This part is executed before the customer has entered the data. It shows the form and enqueues
            // the necessary scripts.
            if (!wp_verify_nonce($_GET['btbnonce'], 'btb_direct_booking_nonce')) {
                return '<h4>' . esc_html__('Sorry, there has been an error', 'bt-booking') . '</h4><p>' . esc_html__('Security check failed.', 'bt-booking') . '</p>';
            }
            if ($master_instance) {
                $booking = btb_get_booking(intval($_GET['booking']));
            } else {
                $booking = btb_get_booking_from_api(intval($_GET['booking']), OBJECT, 'display');
            }
            if (!$booking) {
                return '<h4>' . esc_html__('Sorry, there has been an error', 'bt-booking') . '</h4><p>' . esc_html__('Booking not found. Maybe your booking session expired.', 'bt-booking') . '</p>';
            }
            if ($booking->post_type !== "btb_booking") {
                return '<h4>' . esc_html__('Sorry, there has been an error', 'bt-booking') . '</h4><p>' . esc_html__('Booking not found. Maybe your booking session expired.', 'bt-booking') . '</p>';
            }
            wp_localize_script('btb-checkout-script', 'BTBooking', array('require_terms' => get_option('btb_checkout_require_terms', 0), 'strings' => array('first_name' => __('First name', 'bt-booking'), 'last_name' => __('Last name', 'bt-booking'), 'address' => __('Address', 'bt-booking'), 'zip' => __('Postal code', 'bt-booking'), 'city' => __('City', 'bt-booking'), 'email' => __('E-mail address', 'bt-booking'), 'phone' => __('Phone number', 'bt-booking'), 'country' => __('Country', 'bt-booking'), 'email_confirmation_failed' => __('The confirmation of your E-mail address failed. Please check your input.', 'bt-booking'), 'email_check_failed' => __('The entered E-mail address seems not to be valid. Please check your input.', 'bt-booking'), 'missing_input' => __('The following required fields are missing. Please check your input.', 'bt-booking'), 'tems_not_accepted' => __('You have to accept our terms and conditions before you can proceed with your booking.', 'bt-booking'))));
            wp_enqueue_script('btb-checkout-script');
            $a = shortcode_atts(array('headline' => __('Booking data', 'bt-booking')), $atts);
            return apply_filters('btb_create_checkout_form', '', $booking->ID, $a);
        } else {
            if (isset($_POST['btb_checkout_bookingid']) && isset($_POST['btb_checkout_nonce'])) {
                if (!wp_verify_nonce($_POST['btb_checkout_nonce'], 'btb_checkout_data')) {
                    return '<h4>' . esc_html__('Sorry, there has been an error', 'bt-booking') . '</h4><p>' . esc_html__('Security check failed.', 'bt-booking') . '</p>';
                }
                $booking_id = $_POST['btb_checkout_bookingid'];
                if ($master_instance) {
                    $booking = btb_get_booking($booking_id);
                } else {
                    $booking = btb_get_booking_from_api($booking_id, OBJECT, 'display');
                }
                if ($_POST['btb_checkout_cancel'] == "true") {
                    if ($booking && $booking->post_type == "btb_booking") {
                        if ($master_instance) {
                            $desc_page = btb_get_description_page($booking, true);
                        }
                        if ($master_instance) {
                            btb_delete_booking($booking->ID, true);
                        } else {
                            btb_delete_booking_via_api($booking->ID, true);
                        }
                        $ret = '<h4>' . esc_html__('Booking canceled', 'bt-booking') . '</h4>';
                        $ret .= '<p>' . esc_html__('Your booking has been canceled.', 'bt-booking');
                        if ($master_instance) {
                            $ret .= ' <a href="' . $desc_page . '">' . esc_html__('Back to the offer.', 'bt-booking') . '</a></p>';
                        }
                        return $ret;
                    }
                }
                if (!$booking) {
                    return '<h4>' . esc_html__('Sorry, there has been an error', 'bt-booking') . '</h4><p>' . esc_html__('Booking not found. Maybe your booking session expired.', 'bt-booking') . '</p>';
                }
                if ($booking->post_type !== "btb_booking") {
                    return '<h4>' . esc_html__('Sorry, there has been an error', 'bt-booking') . '</h4><p>' . esc_html__('Booking not found. Maybe your booking session expired.', 'bt-booking') . '</p>';
                }
                $booking->title = $_POST['btb_checkout_title'];
                $booking->first_name = sanitize_text_field($_POST['btb_checkout_first_name']);
                $booking->last_name = sanitize_text_field($_POST['btb_checkout_last_name']);
                $booking->company = isset($_POST['btb_checkout_company']) ? sanitize_text_field($_POST['btb_checkout_company']) : null;
                $booking->address = sanitize_text_field($_POST['btb_checkout_address']);
                $booking->address2 = isset($_POST['btb_checkout_address2']) ? sanitize_text_field($_POST['btb_checkout_address2']) : null;
                $booking->zip = sanitize_text_field($_POST['btb_checkout_zip']);
                $booking->city = sanitize_text_field($_POST['btb_checkout_city']);
                $booking->country = $_POST['btb_checkout_country'];
                $booking->email = sanitize_email($_POST['btb_checkout_mail']);
                $booking->phone = sanitize_text_field($_POST['btb_checkout_phone']);
                $booking->notes = isset($_POST['btb_checkout_notes']) ? sanitize_text_field($_POST['btb_checkout_notes']) : null;
                $booking->booking_time = time();
                $booking->booking_status = 'btb_booked';
                if (btb_update_booking($booking) == 0) {
                    if ($master_instance) {
                        $desc_page = btb_get_description_page($booking, true);
                        btb_delete_booking($booking->ID, true);
                    } else {
                        btb_delete_booking_via_api($booking->ID, true);
                    }
                    $ret = '<h4>' . esc_html__('Sorry, but we failed to process your booking.', 'bt-booking') . '</h4>';
                    $ret .= '<p>' . esc_html__('When updating your data an error has occured.', 'bt-booking');
                    if ($master_instance) {
                        $ret .= ' <a href="' . $desc_page . '">' . esc_html__('Please try it again.', 'bt-booking') . '</a></p>';
                    }
                    $ret .= '<p>' . esc_html__('If this error still occures:', 'bt-booking') . ' <a href="' . get_permalink(get_option('btb_general_contact_page')) . '">' . esc_html__('Please contact us.', 'bt-booking') . '</a></p>';
                    return $ret;
                }
                $mail_success = self::send_mails($booking);
                $ret = '<h4>' . esc_html__('Thank you four your booking.', 'bt-booking') . '</h4>';
                // everything went fine
                if ($mail_success == 1) {
                    $ret .= '<p>' . esc_html__('We have successfully received your booking. You will soon receive a confirmation to your e-mail address.', 'bt-booking') . '</p>';
                }
                // mail to operator failed
                if ($mail_success == 0) {
                    $ret .= '<p>' . esc_html__('We have successfully received your booking. You will soon receive a confirmation to your e-mail address.', 'bt-booking') . '</p>';
                    $ret .= '<p>' . esc_html__('Unfortunately, no notification e-mail could be sent to the site operator. Neverthelss your booking has been added properly to our system. If you want to inform us of this error:', 'bt-booking') . ' <a href="' . get_permalink(get_option('btb_general_contact_page')) . '">' . esc_html__('Please contact us.', 'bt-booking') . '</a></p>';
                }
                // mail to customer failed
                if ($mail_success == -1) {
                    $ret .= '<p>' . esc_html__('We have successfully received your booking, but unfortunately there was a problem with delivery of the confirmation to your e-mail address. Therefore, please write down your booking code. Neverthelss your booking has been added properly to our system. In order to still send you a confirmation:', 'bt-booking') . ' <a href="' . get_permalink(get_option('btb_general_contact_page')) . '">' . esc_html__('Please contact us.', 'bt-booking') . '</a></p>';
                }
                // mail to operator and customer failed
                if ($mail_success == -2) {
                    $ret .= '<p>' . esc_html__('We have successfully received your booking, but unfortunately there was a problem with delivery of the notification to the operator and with sending the confirmation to your e-mail address. Therefore, please write down your booking code. Neverthelss your booking has been added properly to our system. In order to still send you a confirmation:', 'bt-booking') . ' <a href="' . get_permalink(get_option('btb_general_contact_page')) . '">' . esc_html__('Please contact us.', 'bt-booking') . '</a></p>';
                }
                $ret .= '<p>' . esc_html__('Your booking code:', 'bt-booking') . ' ' . $booking->code . '</p>';
                return $ret;
            }
        }
        ?>

        <?php 
    }
 /**
  * Provides the meta box showing the customers identity data.
  *
  * @param object $post
  */
 public static function btb_booking_address_box($post)
 {
     $booking = btb_get_booking($post->ID);
     wp_nonce_field('btb_save_booking_address_box_data', 'btb_booking_addres_box_nonce');
     $companyrow = new BTCTableRow();
     $companyrow->add_content(BTCWPAdminInputSelect::create('btb_title', __('Form of address', 'bt-booking'), $booking->title, array('mr' => __('Mr.', 'bt-booking'), 'mrs' => __('Mrs.', 'bt-booking')), true));
     $companyrow->add_content(BTCWPAdminInputText::create('btb_company', __('Company', 'bt-booking'), $booking->company, true));
     $namerow = new BTCTableRow();
     $namerow->add_content(BTCWPAdminInputText::create('btb_first_name', __('First name', 'bt-booking'), $booking->first_name, true));
     $namerow->add_content(BTCWPAdminInputText::create('btb_last_name', __('Last name', 'bt-booking'), $booking->last_name, true));
     $address = get_post_meta($post->ID, 'btb_address');
     $a = $address[0];
     $addressrow = new BTCTableRow();
     $addressrow->add_content(BTCWPAdminInputText::create('btb_address', __('Address', 'bt-booking'), $booking->address, true));
     $addressrow->add_content(BTCWPAdminInputText::create('btb_address2', __('Additional address', 'bt-booking'), $booking->address2, true));
     $cityrow = new BTCTableRow();
     $cityrow->add_content(BTCWPAdminInputText::create('btb_city', __('City', 'bt-booking'), $booking->city, true));
     $cityrow->add_content(BTCWPAdminInputText::create('btb_zip', __('Postal code', 'bt-booking'), $booking->zip, true));
     $countryrow = new BTCTableRow();
     $countryrow->add_content(BTCWPAdminInputSelect::create('btb_country', __('Country', 'bt-booking'), $booking->country, BTBookingCountries::get_countries(), true));
     $emailrow = new BTCTableRow();
     $emailrow->add_content(BTCWPAdminInputText::create('btb_mail', __('E-mail address', 'bt-booking'), $booking->email, true));
     $emailrow->add_content(BTCWPAdminInputText::create('btb_phone', __('Phone number', 'bt-booking'), $booking->phone, true));
     $table = new BTCTable(array('htmlClasses' => 'form-table'), new BTCTableBody(array(), array($companyrow, $namerow, $addressrow, $cityrow, $countryrow, $emailrow)));
     $table->render();
 }
/**
 * @brief Retrieves booking data from master API given a booking ID.
 *
 * @see sanitize_booking() for optional $filter values. Also, the parameter $booking
 * must be given as a variable, since it is passed by reference.
 *
 * @param int				$booking Booking ID or BTB_Booking object.
 * @param string 			$output Optional, default is Object. Accepts OBJECT, ARRAY_A or ARRAY_N.
 * @param string			$filter Optional. Type fo filter to apply. Accepts 'raw', 'edit', 'db', 'display',
 *									'attribute' or 'js'. Default 'raw'.
 * @return BTB_Booking|array|null	Type corresponding to $output on success or null on failure.
 *						      		When $output is OBJECT, a `BTB_Booking` instance is returned.
 */
function btb_get_booking_from_api($booking, $output = OBJECT, $filter = 'raw')
{
    $r_url = get_option('btb_master_url', '');
    $r_url .= '/wp-json/wp/v2/btb-bookings-api/' . $booking;
    $headers = array('Authorization' => 'Basic ' . base64_encode(get_option(btb_app_user) . ':' . get_option(btb_app_secret)));
    $response = wp_remote_get($r_url, array('headers' => $headers));
    $b = json_decode($response['body']);
    if (empty($b)) {
        return null;
    }
    $_booking = new BTB_Booking();
    $_booking->from_api_response($b);
    return btb_get_booking($_booking, $output, $filter);
}