Advertisement
ofmarconi

Untitled

May 25th, 2024
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. // Função para adicionar o filtro de mídia baseado no nome ou e-mail do autor
  2. function marks_filter_media_by_author_name_or_email($query) {
  3.     global $pagenow;
  4.  
  5.     // Verifica se estamos na página de upload e se o parâmetro author_name_email está definido
  6.     if (($pagenow == 'upload.php' || $pagenow == 'post.php' || $pagenow == 'post-new.php') && isset($_GET['author_name_email'])) {
  7.         $author_name_email = sanitize_text_field($_GET['author_name_email']);
  8.        
  9.         // Realiza uma consulta para buscar usuários com base no nome ou e-mail
  10.         $user_query = new WP_User_Query(array(
  11.             'search'         => '*'.esc_attr($author_name_email).'*',
  12.             'search_columns' => array('user_nicename', 'user_email'),
  13.         ));
  14.  
  15.         $authors = $user_query->get_results();
  16.         if (!empty($authors)) {
  17.             $author_ids = wp_list_pluck($authors, 'ID');
  18.             // Define a consulta para buscar mídia com os IDs dos autores encontrados
  19.             $query->set('author__in', $author_ids);
  20.         } else {
  21.             // Se nenhum autor corresponder, não retorna nenhum resultado
  22.             $query->set('author__in', array(0));
  23.         }
  24.     }
  25. }
  26.  
  27. add_action('pre_get_posts', 'marks_filter_media_by_author_name_or_email');
  28.  
  29. // Adiciona um campo de filtro personalizado no topo da página de mídia e nos modais de mídia
  30. function marks_add_author_name_email_filter() {
  31.     $screen = get_current_screen();
  32.     if ($screen->id !== 'upload' && strpos($screen->id, 'media-modal') === false) {
  33.         return;
  34.     }
  35.  
  36.     $author_name_email = isset($_GET['author_name_email']) ? sanitize_text_field($_GET['author_name_email']) : '';
  37.     ?>
  38.     <div style="margin-top: 10px; padding: 10px; background-color: #f1f1f1;">
  39.         <form method="GET" action="" id="author-filter-form" style="display: flex; align-items: center;">
  40.             <?php
  41.             // Preserva os parâmetros existentes na URL
  42.             foreach ($_GET as $key => $value) {
  43.                 if ($key !== 'author_name_email') {
  44.                     echo '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_attr($value) . '" />';
  45.                 }
  46.             }
  47.             ?>
  48.             <input type="text" name="author_name_email" placeholder="Nome ou E-mail do Autor" value="<?php echo esc_attr($author_name_email); ?>" style="margin-right: 5px;" />
  49.             <button type="submit">Filtrar por Autor</button>
  50.         </form>
  51.     </div>
  52.     <?php
  53. }
  54.  
  55. add_action('all_admin_notices', 'marks_add_author_name_email_filter');
  56. add_action('print_media_templates', 'marks_add_author_name_email_filter');
  57.  
  58.  
  59.  
  60. // Adiciona um filtro para modificar as consultas AJAX de anexos na visualização em grade e modais
  61. function marks_ajax_query_attachments_args($query) {
  62.     if (isset($_REQUEST['query']['author_name_email'])) {
  63.         $author_name_email = sanitize_text_field($_REQUEST['query']['author_name_email']);
  64.  
  65.         $user_query = new WP_User_Query(array(
  66.             'search'         => '*'.esc_attr($author_name_email).'*',
  67.             'search_columns' => array('user_nicename', 'user_email'),
  68.         ));
  69.  
  70.         $authors = $user_query->get_results();
  71.         if (!empty($authors)) {
  72.             $author_ids = wp_list_pluck($authors, 'ID');
  73.             $query['author__in'] = $author_ids;
  74.         } else {
  75.             $query['author__in'] = [0];
  76.         }
  77.     }
  78.     return $query;
  79. }
  80.  
  81. add_filter('ajax_query_attachments_args', 'marks_ajax_query_attachments_args');
  82.  
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement