Last active
May 18, 2023 16:09
-
-
Save dantetesta/a8d465a5476f12ad746f4e40c82a8f90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*MOTRA OS ANEXOS DOS TICKETS*/ | |
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 | |
$icone_nome = 'download-1.svg'; | |
if( $atts['tipo'] =='notas'){ | |
$table_name = $wpdb->prefix . 'jet_cct_notas_ticket'; | |
}elseif($atts['tipo'] =='cpt'){ | |
$ticket_id = get_the_ID(); | |
// Busca metadados do post ao invés de buscar do CCT | |
$anexos_str = get_post_meta($ticket_id, 'anexos', true); | |
$anexos = !empty($anexos_str) ? explode( ',', $anexos_str ) : []; | |
// Prepara a variável de saída | |
$output = ''; | |
// 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 | |
// Obtém o diretório base de upload | |
$upload_dir = wp_upload_dir(); | |
// Constrói a URL para o ícone | |
$icone_url = $upload_dir['baseurl'] . '/2023/05/' . $icone_nome; | |
$output .= '<a href="' . esc_url( $anexo ) . '" download><img src="' . esc_url( $icone_url ) . '" 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; | |
}else{ | |
$table_name = $wpdb->prefix . 'jet_cct_comentarios_de_tickets'; | |
} | |
// Se não for CPT, segue com a consulta SQL normalmente | |
if($atts['tipo'] !='cpt'){ | |
// 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 | |
// Obtém o diretório base de upload | |
$upload_dir = wp_upload_dir(); | |
// Constrói a URL para o ícone | |
$icone_url = $upload_dir['baseurl'] . '/2023/05/' . $icone_nome; | |
$output .= '<a href="' . esc_url( $anexo ) . '" download><img src="' . esc_url( $icone_url ) . '" 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