<?php

namespace Blazemedia\Blackhole\CustomPostTypes;

class Blackhole {

    static function init() {

        // Add the custom columns to the posts post type:
        add_filter( 'init',    [ get_called_class(), 'add_post_type'] , 20);
    }

    /**
     * registra il post-type per le tabelle
     *
     * @return void
     */
    static function add_post_type() {

        $taxonomy_objects = get_taxonomies();

        register_post_type('blackhole', [

            'public'              => false,
            'show_ui'             => true,
            'show_in_menu'        => false,
            'publicly_queryable'  => false,
            'capability_type'     => 'post',
            'exclude_from_search' => true,
            'can_export'          => false,
            'hierarchical'        => false,
            'rewrite'             => false,
            'has_archive'         => false,
            'supports'            => [ 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'revisions', 'custom-fields' ],
            //'show_in_nav_menus' => false,
            'taxonomies'          => array_keys($taxonomy_objects),


            'menu_position' => 81,

            'labels' => [
                'name' => 'Blackhole',
                'singular_name' => 'Blackhole',
                'add_new' => 'Aggiungi nuovo',
                'add_new_item' => 'Aggiungi nuovo in Blackhole',
                'edit' => 'Modifica',
                'edit_item' => 'Modifica in Blackhole',
                'new_item' => 'Nuovo in Blackhole',
                'view' => 'Vedi',
                'view_item' => 'Vedi Blackhole',
                'search_items' => 'Cerca Blackhole',
                'not_found' => 'Nessun Blackhole trovato',
                'not_found_in_trash' => 'Nessuna Blackhole trovato nel cestino'
            ]
        ]);

        register_taxonomy_for_object_type('post_tag', 'blackhole'); 
       
        add_action('restrict_manage_posts', [ get_called_class(),'add_tag_filter']);
        add_action('restrict_manage_posts', [ get_called_class(),'filter_dropdown_by_author']);
        add_action('parse_query', [ get_called_class(),'handle_author_filter']);
    }

    static function add_tag_filter($query) {
        global $typenow;
     
        // Verifica che tu sia nella pagina 'edit.php' e che sia un tipo di post che supporta i tag
        if ($typenow == 'blackhole') {
            $args = array(
                'show_option_all' => 'Mostra tutti i tag',
                'taxonomy'        => 'post_tag', // Assicurati di utilizzare il nome corretto della tassonomia dei tag
                'name'            => 'post_tag',
                'orderby'         => 'name',
                'selected'        => isset($_GET['tag']) ? $_GET['tag'] : '',
                'hierarchical'    => false,
                'depth'           => 1,
                'show_count'      => false,
                'hide_empty'      => true,
            );

            wp_dropdown_categories($args);
        }
    }

    static function filter_dropdown_by_author() {
        global $typenow;

        // Verifica se il post_type corrente supporta l'ordinamento per autore
        if ("blackhole" == $typenow) {
            $authors = get_users();
    
            if (!empty($authors)) {
                // Stampa il menu a discesa
                echo '<select name="filter_by_author">';
                echo '<option value="">Filtra per autore</option>';
                foreach ($authors as $author) {
                    $selected = (isset($_GET['filter_by_author']) && $_GET['filter_by_author'] == $author->ID) ? 'selected' : '';
                    echo '<option value="' . $author->ID . '" ' . $selected . '>' . $author->display_name . '</option>';
                }
                echo '</select>';
            }
        }
    }

    static function handle_author_filter($query) {
        if (!is_admin() || !$query->is_main_query())
            return;

        global $typenow;
        global $pagenow;
        if ('edit.php' == $pagenow && "blackhole" == $typenow ) {
            if (isset($_GET['filter_by_author']) && !empty($_GET['filter_by_author'])) {
                $query->set('author', $_GET['filter_by_author']);
            }
        }
    }
}
