function prepare_items() { global $wpdb; $per_page = 10; $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array($columns, $hidden, $sortable); $this->process_bulk_action(); $orderby = !empty($_REQUEST['orderby']) ? sanitize_sql_orderby($_REQUEST['orderby']) : 'id'; //If no sort, default to title $order = !empty($_REQUEST['order']) ? sanitize_text_field($_REQUEST['order']) : 'desc'; //If no order, default to desc $current_page = $this->get_pagenum(); $post_table_name = $wpdb->prefix . 'posts'; $where = "1=1"; if (isset($_REQUEST['tour_id'])) { $where .= " AND schedule.tour_id = '" . esc_sql(trav_tour_org_id($_REQUEST['tour_id'])) . "'"; } if (isset($_REQUEST['st_id'])) { $where .= " AND schedule.st_id = '" . esc_sql($_REQUEST['st_id']) . "'"; } if (isset($_REQUEST['date'])) { $where .= " AND (schedule.tour_date = '" . esc_sql($_REQUEST['date']) . "' OR ( schedule.is_daily = 1 AND schedule.tour_date <= '" . esc_sql($_REQUEST['date']) . "' AND schedule.date_to >= '" . esc_sql($_REQUEST['date']) . "' ) )"; } if (!current_user_can('manage_options')) { $where .= " AND tour.post_author = '" . get_current_user_id() . "' "; } $sql = $wpdb->prepare('SELECT schedule.* , tour.ID as tour_id, tour.post_title as tour_name FROM %1$s as schedule INNER JOIN %2$s as tour ON schedule.tour_id=tour.ID WHERE ' . $where . ' ORDER BY %3$s %4$s LIMIT %5$s, %6$s', TRAV_TOUR_SCHEDULES_TABLE, $post_table_name, $orderby, $order, $per_page * ($current_page - 1), $per_page); $data = $wpdb->get_results($sql, ARRAY_A); $sql = sprintf('SELECT COUNT(*) FROM %1$s as schedule INNER JOIN %2$s as tour ON schedule.tour_id=tour.ID WHERE ' . $where, TRAV_TOUR_SCHEDULES_TABLE, $post_table_name); $total_items = $wpdb->get_var($sql); $this->items = $data; $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => ceil($total_items / $per_page))); }
function trav_tour_get_tour_duration($tour_id) { global $wpdb; $tour_id = esc_sql(trav_tour_org_id($tour_id)); if (empty($tour_id)) { return false; } $sql = "SELECT MIN(tour_date) AS start_date, MAX( IF((is_daily=1 AND date_to IS NOT NULL ),date_to,tour_date)) AS end_date FROM " . TRAV_TOUR_SCHEDULES_TABLE . " WHERE tour_id={$tour_id}"; $duration_data = $wpdb->get_row($sql, ARRAY_A); if (empty($duration_data)) { return false; } $duration = date('d M Y', strtotime($duration_data['start_date'])); if ($duration_data['start_date'] != $duration_data['end_date']) { $duration .= ' - ' . date('d M Y', strtotime($duration_data['end_date'])); } return $duration; }
function trav_ajax_tour_submit_booking() { global $wpdb, $trav_options; // validation $result_json = array('success' => 0, 'result' => ''); if (!isset($_POST['transaction_id']) || !isset($_SESSION['booking_data'][$_POST['transaction_id']])) { $result_json['success'] = 0; $result_json['result'] = __('Sorry, some error occurred on input data validation.', 'trav'); wp_send_json($result_json); } $raw_booking_data = $_SESSION['booking_data'][$_POST['transaction_id']]; $booking_fields = array('tour_id', 'st_id', 'tour_date', 'adults', 'kids', 'total_price', 'currency_code', 'exchange_rate', 'deposit_price'); $booking_data = array(); foreach ($booking_fields as $booking_field) { if (!empty($raw_booking_data[$booking_field])) { $booking_data[$booking_field] = $raw_booking_data[$booking_field]; } } $is_payment_enabled = trav_is_payment_enabled() && !empty($booking_data['deposit_price']); if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'post-' . $booking_data['tour_id'])) { $result_json['success'] = 0; $result_json['result'] = __('Sorry, your nonce did not verify.', 'trav'); wp_send_json($result_json); } if (isset($trav_options['vld_captcha']) && !empty($trav_options['vld_captcha'])) { if (!isset($_POST['security_code']) || $_POST['security_code'] != $_SESSION['security_code']) { $result_json['success'] = 0; $result_json['result'] = __('Captcha error. Please check your security code again.', 'trav'); wp_send_json($result_json); } } if (isset($trav_options['vld_credit_card']) && !empty($trav_options['vld_credit_card'])) { if (!isset($_POST['cc_type']) || !isset($_POST['cc_holder_name']) || !isset($_POST['cc_number']) || !isset($_POST['cc_exp_month']) || !isset($_POST['cc_exp_year']) || !trav_cc_validation($_POST['cc_type'], $_POST['cc_holder_name'], $_POST['cc_number'], $_POST['cc_exp_month'], $_POST['cc_exp_year'])) { $result_json['success'] = 0; $result_json['result'] = __('Vcc validation An error.', 'trav'); wp_send_json($result_json); } } // init variables $post_fields = array('first_name', 'last_name', 'email', 'country_code', 'phone', 'address', 'city', 'zip', 'country', 'special_requirements'); $customer_info = array(); foreach ($post_fields as $post_field) { if (!empty($_POST[$post_field])) { $customer_info[$post_field] = sanitize_text_field($_POST[$post_field]); } } $data = array_merge($customer_info, $booking_data); $data['tour_date'] = date('Y-m-d', trav_strtotime($data['tour_date'])); if (is_user_logged_in()) { $data['user_id'] = get_current_user_id(); } $latest_booking_id = $wpdb->get_var('SELECT id FROM ' . TRAV_TOUR_BOOKINGS_TABLE . ' ORDER BY id DESC LIMIT 1'); $booking_no = mt_rand(1000, 9999); $booking_no .= $latest_booking_id; $pin_code = mt_rand(1000, 9999); if (!isset($_SESSION['exchange_rate'])) { trav_init_currency(); } $default_booking_data = array('first_name' => '', 'last_name' => '', 'email' => '', 'country_code' => '', 'phone' => '', 'address' => '', 'city' => '', 'zip' => '', 'country' => '', 'special_requirements' => '', 'tour_id' => '', 'st_id' => 0, 'tour_date' => '', 'adults' => '', 'kids' => '', 'total_price' => '', 'currency_code' => 'usd', 'exchange_rate' => 1, 'deposit_price' => 0, 'deposit_paid' => $is_payment_enabled ? 0 : 1, 'created' => date('Y-m-d H:i:s'), 'booking_no' => $booking_no, 'pin_code' => $pin_code, 'status' => 1); $data = array_replace($default_booking_data, $data); // credit card offline charge if (!empty($trav_options['vld_credit_card']) && !empty($trav_options['cc_off_charge'])) { $cc_fields = array('cc_type', 'cc_holder_name', 'cc_number', 'cc_cid', 'cc_exp_year', 'cc_exp_month'); $cc_infos = array(); foreach ($cc_fields as $cc_field) { $cc_infos[$cc_field] = empty($_POST[$cc_field]) ? '' : $_POST[$cc_field]; } $data['other'] = serialize($cc_infos); } // recheck availability $room_price_data = trav_tour_get_price_data(array('tour_id' => $data['tour_id'], 'st_id' => $data['st_id'], 'tour_date' => $booking_data['tour_date'], 'adults' => $data['adults'], 'kids' => $data['kids'])); if (empty($room_price_data) || !is_array($room_price_data)) { $result_json['success'] = -1; $result_json['result'] = __('Sorry, The tour you are booking now is just taken by another customer. Please have another look.', 'trav'); wp_send_json($result_json); } do_action('trav_tour_add_booking_before', $data); // save default language tour and room type $data['tour_id'] = trav_tour_org_id($data['tour_id']); // add to db if ($wpdb->insert(TRAV_TOUR_BOOKINGS_TABLE, $data)) { $booking_id = $wpdb->insert_id; $data['booking_id'] = $booking_id; $_SESSION['booking_data'][$_POST['transaction_id']] = $data; $result_json['success'] = 1; $result_json['result']['booking_no'] = $booking_no; $result_json['result']['pin_code'] = $pin_code; $result_json['result']['transaction_id'] = $_POST['transaction_id']; if ($is_payment_enabled) { if (trav_is_woo_enabled()) { // woocommerce do_action('trav_woo_add_tour_booking', $data); $result_json['result']['payment'] = 'woocommerce'; } elseif (trav_is_paypal_enabled()) { // paypal direct $result_json['result']['payment'] = 'paypal'; } } else { $result_json['result']['payment'] = 'no'; } do_action('trav_tour_add_booking_after', $data); } else { $result_json['success'] = 0; $result_json['result'] = __('Sorry, An error occurred while add booking.', 'trav'); } wp_send_json($result_json); }