// ============================ // LER RANGE (Viewer + Editor) // ============================ add_action('wp_ajax_ep_read_range', 'ep_read_range'); add_action('wp_ajax_nopriv_ep_read_range', 'ep_read_range'); // permite uso no front sem login function ep_read_range() { global $wpdb; // Tabela fixa, SEM prefixo e com espaço no nome $table_name = 'parkm_ranges 2'; $id = isset($_GET['id']) ? intval($_GET['id']) : 0; $spot_code = isset($_GET['spot_code']) ? sanitize_text_field( wp_unslash($_GET['spot_code']) ) : ''; $action_context = isset($_GET['action_context']) ? sanitize_text_field( wp_unslash($_GET['action_context']) ) : ''; if ($id > 0) { // Busca por ID $sql = $wpdb->prepare( "SELECT * FROM `{$table_name}` WHERE id = %d", $id ); $row = $wpdb->get_row($sql, ARRAY_A); } elseif ($spot_code && $action_context) { // Busca pelo par (spot_code + action_context), último inserido $sql = $wpdb->prepare( "SELECT * FROM `{$table_name}` WHERE spot_code = %s AND action_context = %s ORDER BY id DESC LIMIT 1", $spot_code, $action_context ); $row = $wpdb->get_row($sql, ARRAY_A); } else { wp_send_json_error('Parâmetros insuficientes.'); } if (!$row) { wp_send_json_error('Range não encontrado.'); } // Decodifica o JSON de frequências $freq = []; if (!empty($row['frequencies_json'])) { $decoded = json_decode($row['frequencies_json'], true); if (is_array($decoded)) { $freq = $decoded; } } // Permitimos que o JSON venha tanto como {hands:{...}} quanto direto {...} $hands = isset($freq['hands']) && is_array($freq['hands']) ? $freq['hands'] : $freq; wp_send_json_success([ 'id' => (int) $row['id'], 'spot_code' => $row['spot_code'], 'action_context' => $row['action_context'], 'action_posflop' => $row['action_posflop'], 'tag1' => $row['tag1'], 'tag2' => $row['tag2'], 'hands' => $hands, ]); }