route_price($post_infos); ?>
'; print_r($ticket_infos); echo '';
//echo ''; print_r($price_infos); echo '
';
?>
__('Invalid security token or Insufficient permissions.', 'abp-transport-booking'), 'type' => 'warn'], 403);
}
$post_val = fn($key, $default = '') => isset($_POST[$key]) ? sanitize_text_field(wp_unslash($_POST[$key])) : $default;
$post_int = fn($key, $default = 0) => isset($_POST[$key]) ? absint($_POST[$key]) : $default;
$post_json = function ($key) {
if (!check_ajax_referer('abptb_admin_ajax_nonce', 'nonce', false) || !current_user_can('manage_options')) {
wp_send_json_error(['msg' => __('Invalid security token or Insufficient permissions.', 'abp-transport-booking'), 'type' => 'warn'], 403);
}
if (isset($_POST[$key])) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized below via map_deep.
$raw_input = wp_unslash($_POST[$key]);
$raw_data = json_decode($raw_input, true);
if (!is_array($raw_data)) {
return array();
}
return map_deep($raw_data, 'sanitize_text_field');
}
return [];
};
ob_start();
$route = $post_json('routing_infos');
$route_info = [];
if (!empty($route)) {
foreach ($route as $value) {
$stop = $value['stop'] ?? '';
$type = $value['type'] ?? '';
if (!empty($stop) && !empty($type)) {
$route_info[$stop]['type'] = $type;
}
}
}
if (!empty($route_info)) {
$route_info[array_key_first($route_info)]['type'] = 'bp';
if (sizeof($route_info) > 1) {
$route_info[array_key_last($route_info)]['type'] = 'dp';
}
}
$display_return = $post_val('display_return');
$price_infos = $post_json('price_infos');
$ticket_infos = $post_json('all_ticket_type');
$post_infos['post_id'] = $post_int('post_id');
$post_infos['seat_type'] = $post_val('seat_type');
$post_infos['display_ticket_type'] = $post_val('display_ticket_type');
$post_infos['routing_infos'] = $route_info;
$post_infos['all_ticket_type'] = $ticket_infos;
$post_infos['price_infos'] = $this->get_route_array($route_info, $price_infos, $ticket_infos);
$post_infos['display_return'] = $display_return;
if ($display_return == 'on') {
$return_price_infos = $post_json('return_price_infos');
$return_route = $post_json('return_routing_infos');
$return_routing_infos = [];
if (!empty($return_route)) {
foreach ($return_route as $value) {
$stop = $value['stop'] ?? '';
$type = $value['type'] ?? '';
if (!empty($stop) && !empty($type)) {
$return_routing_infos[$stop]['type'] = $type;
}
}
}
if (!empty($return_routing_infos)) {
$return_routing_infos[array_key_first($return_routing_infos)]['type'] = 'bp';
if (sizeof($return_routing_infos) > 1) {
$return_routing_infos[array_key_last($return_routing_infos)]['type'] = 'dp';
}
}
$post_infos['return_price_infos'] = $this->get_route_array($return_routing_infos, $return_price_infos, $ticket_infos);
}
//echo ''; print_r($post_infos); echo '
';
$this->route_price($post_infos);
$html = ob_get_clean();
wp_send_json_success(['html' => $html, 'msg' => __('Price Configuration Updated....!', 'abp-transport-booking'), 'type' => 'success']);
}
public function get_route_array(array $routing_infos = [], $price_infos = [], $ticket_infos = []): array {
$route_array = [];
if (!empty($routing_infos)) {
foreach ($routing_infos as $key => $routing_info) {
if ($routing_info['type'] == 'bp' || $routing_info['type'] == 'both') {
$bp = $key;
$keys = array_keys($routing_infos);
$index = array_search($key, $keys);
if ($index !== false) {
$next_infos = array_slice($routing_infos, $index + 1, null, true);
if (!empty($next_infos) > 0) {
foreach ($next_infos as $dp => $next_info) {
if ($next_info['type'] == 'dp' || $next_info['type'] == 'both') {
$path_price = [];
$path_price['bp'] = $bp;
$path_price['dp'] = $dp;
$bp_dp = $bp . '_' . $dp;
if (!empty($price_infos) && is_array($price_infos) && sizeof($price_infos) > 0) {
foreach ($price_infos as $key => $price_info) {
if ($bp_dp == $key) {
if (!empty($ticket_infos) && is_array($ticket_infos) && sizeof($ticket_infos) > 0) {
foreach ($ticket_infos as $ticket_type) {
$path_price[$ticket_type] = $price_info[$ticket_type] ?? '';
}
}
}
}
}
$route_array[$bp_dp] = $path_price;
}
}
}
}
}
}
}
return $route_array;
}
}
new ABPTB_Price();
}