Skip to content

Instantly share code, notes, and snippets.

@dantetesta
Created May 16, 2023 02:59
Show Gist options
  • Save dantetesta/f3859a3ed394058efdd27fd07d215781 to your computer and use it in GitHub Desktop.
Save dantetesta/f3859a3ed394058efdd27fd07d215781 to your computer and use it in GitHub Desktop.
function mostrar_anexos_de_tickets( $atts ) {
global $wpdb;
// Atributos padrões
$atts = shortcode_atts(
array(
'id' => 0,
'tipo' => 'comentarios',
), $atts, 'mostrar_anexos'
);
// Obtém o ID do post a partir dos atributos
$ticket_id = intval( $atts['id'] ); // Garantir que o ID é um número
if( $atts['tipo'] =='notas'){
$table_name = $wpdb->prefix . 'jet_cct_notas_ticket';
}else{
$table_name = $wpdb->prefix . 'jet_cct_comentarios_de_tickets';
}
// Consulta SQL para buscar os anexos do ticket atual com consulta preparada
$results = $wpdb->get_results( $wpdb->prepare( "SELECT anexos FROM {$table_name} WHERE _ID = %d AND TRIM(anexos) <> ''", $ticket_id ) );
// Verifica se há resultados
if ( ! empty( $results ) ) {
// Prepara a variável de saída
$output = '';
// Percorre cada resultado
foreach ( $results as $row ) {
// Separa as strings de anexos por vírgula
$anexos = explode( ',', $row->anexos );
// Percorre cada anexo
foreach ( $anexos as $anexo ) {
// Obtém a extensão do arquivo
$extensao = pathinfo( $anexo, PATHINFO_EXTENSION );
$nome_arquivo = basename( $anexo );
// Verifica a extensão para determinar como exibir o anexo
if ( in_array( $extensao, array( 'jpeg', 'jpg', 'png' ) ) ) {
// Exibe uma miniatura do anexo com um link para a imagem completa
$output .= '<a href="' . esc_url( $anexo ) . '" style="margin: 2px"><img src="' . esc_url( $anexo ) . '" width="100" height="100" style="border-radius:4px;" /></a>';
} else {
// Exibe um ícone com um link para download do anexo
// Substitua 'url_do_icone' pelo URL do ícone que você deseja exibir
$output .= '<a href="' . esc_url( $anexo ) . '" download><img src="https://wortickets.dantetesta.com.br/wp-content/uploads/2023/05/download-1.svg" width="32" height="32" /> <span style="position: relative; top: -9px; left:5px;">' . esc_html( $nome_arquivo ) . '</span></a><br>';
}
}
}
// Retorna a saída
return $output;
}
}
// Adiciona o shortcode
add_shortcode( 'mostrar_anexos', 'mostrar_anexos_de_tickets' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment