Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Função para adicionar o filtro de mídia baseado no nome ou e-mail do autor
- function marks_filter_media_by_author_name_or_email($query) {
- global $pagenow;
- // Verifica se estamos na página de upload e se o parâmetro author_name_email está definido
- if (($pagenow == 'upload.php' || $pagenow == 'post.php' || $pagenow == 'post-new.php') && isset($_GET['author_name_email'])) {
- $author_name_email = sanitize_text_field($_GET['author_name_email']);
- // Realiza uma consulta para buscar usuários com base no nome ou e-mail
- $user_query = new WP_User_Query(array(
- 'search' => '*'.esc_attr($author_name_email).'*',
- 'search_columns' => array('user_nicename', 'user_email'),
- ));
- $authors = $user_query->get_results();
- if (!empty($authors)) {
- $author_ids = wp_list_pluck($authors, 'ID');
- // Define a consulta para buscar mídia com os IDs dos autores encontrados
- $query->set('author__in', $author_ids);
- } else {
- // Se nenhum autor corresponder, não retorna nenhum resultado
- $query->set('author__in', array(0));
- }
- }
- }
- add_action('pre_get_posts', 'marks_filter_media_by_author_name_or_email');
- // Adiciona um campo de filtro personalizado no topo da página de mídia e nos modais de mídia
- function marks_add_author_name_email_filter() {
- $screen = get_current_screen();
- if ($screen->id !== 'upload' && strpos($screen->id, 'media-modal') === false) {
- return;
- }
- $author_name_email = isset($_GET['author_name_email']) ? sanitize_text_field($_GET['author_name_email']) : '';
- ?>
- <div style="margin-top: 10px; padding: 10px; background-color: #f1f1f1;">
- <form method="GET" action="" id="author-filter-form" style="display: flex; align-items: center;">
- <?php
- // Preserva os parâmetros existentes na URL
- foreach ($_GET as $key => $value) {
- if ($key !== 'author_name_email') {
- echo '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_attr($value) . '" />';
- }
- }
- ?>
- <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;" />
- <button type="submit">Filtrar por Autor</button>
- </form>
- </div>
- <?php
- }
- add_action('all_admin_notices', 'marks_add_author_name_email_filter');
- add_action('print_media_templates', 'marks_add_author_name_email_filter');
- // Adiciona um filtro para modificar as consultas AJAX de anexos na visualização em grade e modais
- function marks_ajax_query_attachments_args($query) {
- if (isset($_REQUEST['query']['author_name_email'])) {
- $author_name_email = sanitize_text_field($_REQUEST['query']['author_name_email']);
- $user_query = new WP_User_Query(array(
- 'search' => '*'.esc_attr($author_name_email).'*',
- 'search_columns' => array('user_nicename', 'user_email'),
- ));
- $authors = $user_query->get_results();
- if (!empty($authors)) {
- $author_ids = wp_list_pluck($authors, 'ID');
- $query['author__in'] = $author_ids;
- } else {
- $query['author__in'] = [0];
- }
- }
- return $query;
- }
- add_filter('ajax_query_attachments_args', 'marks_ajax_query_attachments_args');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement